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" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
| 28 | |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 29 | static bool isDeclWithinFunction(const Decl *D) { |
| 30 | const DeclContext *DC = D->getDeclContext(); |
| 31 | if (DC->isFunctionOrMethod()) |
| 32 | return true; |
| 33 | |
| 34 | if (DC->isRecord()) |
| 35 | return cast<CXXRecordDecl>(DC)->isLocalClass(); |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 40 | template<typename DeclT> |
| 41 | static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, |
| 42 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 43 | if (!OldDecl->getQualifierLoc()) |
| 44 | return false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 45 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 46 | assert((NewDecl->getFriendObjectKind() || |
| 47 | !OldDecl->getLexicalDeclContext()->isDependentContext()) && |
| 48 | "non-friend with qualified name defined in dependent context"); |
| 49 | Sema::ContextRAII SavedContext( |
| 50 | SemaRef, |
| 51 | const_cast<DeclContext *>(NewDecl->getFriendObjectKind() |
| 52 | ? NewDecl->getLexicalDeclContext() |
| 53 | : OldDecl->getLexicalDeclContext())); |
| 54 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 55 | NestedNameSpecifierLoc NewQualifierLoc |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 56 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 57 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 59 | if (!NewQualifierLoc) |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 60 | return true; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 62 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 66 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 67 | DeclaratorDecl *NewDecl) { |
| 68 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 69 | } |
| 70 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 71 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 72 | TagDecl *NewDecl) { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 73 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 74 | } |
| 75 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 76 | // Include attribute instantiation code. |
| 77 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 78 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 79 | static void instantiateDependentAlignedAttr( |
| 80 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 81 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 82 | if (Aligned->isAlignmentExpr()) { |
| 83 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 84 | EnterExpressionEvaluationContext Unevaluated( |
| 85 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 86 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 87 | if (!Result.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 88 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 89 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 90 | } else { |
| 91 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 92 | TemplateArgs, Aligned->getLocation(), |
| 93 | DeclarationName()); |
| 94 | if (Result) |
| 95 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 96 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static void instantiateDependentAlignedAttr( |
| 101 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 102 | const AlignedAttr *Aligned, Decl *New) { |
| 103 | if (!Aligned->isPackExpansion()) { |
| 104 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 109 | if (Aligned->isAlignmentExpr()) |
| 110 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 111 | Unexpanded); |
| 112 | else |
| 113 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 114 | Unexpanded); |
| 115 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 116 | |
| 117 | // Determine whether we can expand this attribute pack yet. |
| 118 | bool Expand = true, RetainExpansion = false; |
| 119 | Optional<unsigned> NumExpansions; |
| 120 | // FIXME: Use the actual location of the ellipsis. |
| 121 | SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 122 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 123 | Unexpanded, TemplateArgs, Expand, |
| 124 | RetainExpansion, NumExpansions)) |
| 125 | return; |
| 126 | |
| 127 | if (!Expand) { |
| 128 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 129 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 130 | } else { |
| 131 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 132 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 133 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 138 | static void instantiateDependentAssumeAlignedAttr( |
| 139 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 140 | const AssumeAlignedAttr *Aligned, Decl *New) { |
| 141 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 142 | EnterExpressionEvaluationContext Unevaluated( |
| 143 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 144 | |
| 145 | Expr *E, *OE = nullptr; |
| 146 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 147 | if (Result.isInvalid()) |
| 148 | return; |
| 149 | E = Result.getAs<Expr>(); |
| 150 | |
| 151 | if (Aligned->getOffset()) { |
| 152 | Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); |
| 153 | if (Result.isInvalid()) |
| 154 | return; |
| 155 | OE = Result.getAs<Expr>(); |
| 156 | } |
| 157 | |
| 158 | S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE, |
| 159 | Aligned->getSpellingListIndex()); |
| 160 | } |
| 161 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 162 | static void instantiateDependentAlignValueAttr( |
| 163 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 164 | const AlignValueAttr *Aligned, Decl *New) { |
| 165 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 166 | EnterExpressionEvaluationContext Unevaluated( |
| 167 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 168 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 169 | if (!Result.isInvalid()) |
| 170 | S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
| 171 | Aligned->getSpellingListIndex()); |
| 172 | } |
| 173 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 174 | static void instantiateDependentAllocAlignAttr( |
| 175 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 176 | const AllocAlignAttr *Align, Decl *New) { |
| 177 | Expr *Param = IntegerLiteral::Create( |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 178 | S.getASTContext(), |
| 179 | llvm::APInt(64, Align->getParamIndex().getSourceIndex()), |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 180 | S.getASTContext().UnsignedLongLongTy, Align->getLocation()); |
| 181 | S.AddAllocAlignAttr(Align->getLocation(), New, Param, |
| 182 | Align->getSpellingListIndex()); |
| 183 | } |
| 184 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 185 | static Expr *instantiateDependentFunctionAttrCondition( |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 186 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 187 | const Attr *A, Expr *OldCond, const Decl *Tmpl, FunctionDecl *New) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 188 | Expr *Cond = nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 189 | { |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 190 | Sema::ContextRAII SwitchContext(S, New); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 191 | EnterExpressionEvaluationContext Unevaluated( |
| 192 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 193 | ExprResult Result = S.SubstExpr(OldCond, TemplateArgs); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 194 | if (Result.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 195 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 196 | Cond = Result.getAs<Expr>(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 197 | } |
George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 198 | if (!Cond->isTypeDependent()) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 199 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 200 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 201 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 202 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | SmallVector<PartialDiagnosticAt, 8> Diags; |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 206 | if (OldCond->isValueDependent() && !Cond->isValueDependent() && |
| 207 | !Expr::isPotentialConstantExprUnevaluated(Cond, New, Diags)) { |
| 208 | S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A; |
| 209 | for (const auto &P : Diags) |
| 210 | S.Diag(P.first, P.second); |
| 211 | return nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 212 | } |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 213 | return Cond; |
| 214 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 215 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 216 | static void instantiateDependentEnableIfAttr( |
| 217 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 218 | const EnableIfAttr *EIA, const Decl *Tmpl, FunctionDecl *New) { |
| 219 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 220 | S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); |
| 221 | |
| 222 | if (Cond) |
| 223 | New->addAttr(new (S.getASTContext()) EnableIfAttr( |
| 224 | EIA->getLocation(), S.getASTContext(), Cond, EIA->getMessage(), |
| 225 | EIA->getSpellingListIndex())); |
| 226 | } |
| 227 | |
| 228 | static void instantiateDependentDiagnoseIfAttr( |
| 229 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 230 | const DiagnoseIfAttr *DIA, const Decl *Tmpl, FunctionDecl *New) { |
| 231 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 232 | S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New); |
| 233 | |
| 234 | if (Cond) |
| 235 | New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( |
| 236 | DIA->getLocation(), S.getASTContext(), Cond, DIA->getMessage(), |
| 237 | DIA->getDiagnosticType(), DIA->getArgDependent(), New, |
| 238 | DIA->getSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 241 | // Constructs and adds to New a new instance of CUDALaunchBoundsAttr using |
| 242 | // template A as the base and arguments from TemplateArgs. |
| 243 | static void instantiateDependentCUDALaunchBoundsAttr( |
| 244 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 245 | const CUDALaunchBoundsAttr &Attr, Decl *New) { |
| 246 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 247 | EnterExpressionEvaluationContext Unevaluated( |
| 248 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 249 | |
| 250 | ExprResult Result = S.SubstExpr(Attr.getMaxThreads(), TemplateArgs); |
| 251 | if (Result.isInvalid()) |
| 252 | return; |
| 253 | Expr *MaxThreads = Result.getAs<Expr>(); |
| 254 | |
| 255 | Expr *MinBlocks = nullptr; |
| 256 | if (Attr.getMinBlocks()) { |
| 257 | Result = S.SubstExpr(Attr.getMinBlocks(), TemplateArgs); |
| 258 | if (Result.isInvalid()) |
| 259 | return; |
| 260 | MinBlocks = Result.getAs<Expr>(); |
| 261 | } |
| 262 | |
| 263 | S.AddLaunchBoundsAttr(Attr.getLocation(), New, MaxThreads, MinBlocks, |
| 264 | Attr.getSpellingListIndex()); |
| 265 | } |
| 266 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 267 | static void |
| 268 | instantiateDependentModeAttr(Sema &S, |
| 269 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 270 | const ModeAttr &Attr, Decl *New) { |
| 271 | S.AddModeAttr(Attr.getRange(), New, Attr.getMode(), |
| 272 | Attr.getSpellingListIndex(), /*InInstantiation=*/true); |
| 273 | } |
| 274 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 275 | /// Instantiation of 'declare simd' attribute and its arguments. |
| 276 | static void instantiateOMPDeclareSimdDeclAttr( |
| 277 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 278 | const OMPDeclareSimdDeclAttr &Attr, Decl *New) { |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 279 | // Allow 'this' in clauses with varlists. |
| 280 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) |
| 281 | New = FTD->getTemplatedDecl(); |
| 282 | auto *FD = cast<FunctionDecl>(New); |
| 283 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 284 | SmallVector<Expr *, 4> Uniforms, Aligneds, Alignments, Linears, Steps; |
| 285 | SmallVector<unsigned, 4> LinModifiers; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 286 | |
| 287 | auto &&Subst = [&](Expr *E) -> ExprResult { |
| 288 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 289 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 290 | Sema::ContextRAII SavedContext(S, FD); |
| 291 | LocalInstantiationScope Local(S); |
| 292 | if (FD->getNumParams() > PVD->getFunctionScopeIndex()) |
| 293 | Local.InstantiatedLocal( |
| 294 | PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); |
| 295 | return S.SubstExpr(E, TemplateArgs); |
| 296 | } |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 297 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 298 | FD->isCXXInstanceMember()); |
| 299 | return S.SubstExpr(E, TemplateArgs); |
| 300 | }; |
| 301 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 302 | ExprResult Simdlen; |
| 303 | if (auto *E = Attr.getSimdlen()) |
| 304 | Simdlen = Subst(E); |
| 305 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 306 | if (Attr.uniforms_size() > 0) { |
| 307 | for(auto *E : Attr.uniforms()) { |
| 308 | ExprResult Inst = Subst(E); |
| 309 | if (Inst.isInvalid()) |
| 310 | continue; |
| 311 | Uniforms.push_back(Inst.get()); |
| 312 | } |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 315 | auto AI = Attr.alignments_begin(); |
| 316 | for (auto *E : Attr.aligneds()) { |
| 317 | ExprResult Inst = Subst(E); |
| 318 | if (Inst.isInvalid()) |
| 319 | continue; |
| 320 | Aligneds.push_back(Inst.get()); |
| 321 | Inst = ExprEmpty(); |
| 322 | if (*AI) |
| 323 | Inst = S.SubstExpr(*AI, TemplateArgs); |
| 324 | Alignments.push_back(Inst.get()); |
| 325 | ++AI; |
| 326 | } |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 327 | |
| 328 | auto SI = Attr.steps_begin(); |
| 329 | for (auto *E : Attr.linears()) { |
| 330 | ExprResult Inst = Subst(E); |
| 331 | if (Inst.isInvalid()) |
| 332 | continue; |
| 333 | Linears.push_back(Inst.get()); |
| 334 | Inst = ExprEmpty(); |
| 335 | if (*SI) |
| 336 | Inst = S.SubstExpr(*SI, TemplateArgs); |
| 337 | Steps.push_back(Inst.get()); |
| 338 | ++SI; |
| 339 | } |
| 340 | LinModifiers.append(Attr.modifiers_begin(), Attr.modifiers_end()); |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 341 | (void)S.ActOnOpenMPDeclareSimdDirective( |
| 342 | S.ConvertDeclToDeclGroup(New), Attr.getBranchState(), Simdlen.get(), |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 343 | Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps, |
| 344 | Attr.getRange()); |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame^] | 347 | static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 348 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 349 | const AMDGPUFlatWorkGroupSizeAttr &Attr, Decl *New) { |
| 350 | // Both min and max expression are constant expressions. |
| 351 | EnterExpressionEvaluationContext Unevaluated( |
| 352 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 353 | |
| 354 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 355 | if (Result.isInvalid()) |
| 356 | return; |
| 357 | Expr *MinExpr = Result.getAs<Expr>(); |
| 358 | |
| 359 | Result = S.SubstExpr(Attr.getMax(), TemplateArgs); |
| 360 | if (Result.isInvalid()) |
| 361 | return; |
| 362 | Expr *MaxExpr = Result.getAs<Expr>(); |
| 363 | |
| 364 | S.addAMDGPUFlatWorkGroupSizeAttr(Attr.getLocation(), New, MinExpr, MaxExpr, |
| 365 | Attr.getSpellingListIndex()); |
| 366 | } |
| 367 | |
| 368 | static void instantiateDependentAMDGPUWavesPerEUAttr( |
| 369 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 370 | const AMDGPUWavesPerEUAttr &Attr, Decl *New) { |
| 371 | // Both min and max expression are constant expressions. |
| 372 | EnterExpressionEvaluationContext Unevaluated( |
| 373 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 374 | |
| 375 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 376 | if (Result.isInvalid()) |
| 377 | return; |
| 378 | Expr *MinExpr = Result.getAs<Expr>(); |
| 379 | |
| 380 | Expr *MaxExpr = nullptr; |
| 381 | if (auto Max = Attr.getMax()) { |
| 382 | Result = S.SubstExpr(Max, TemplateArgs); |
| 383 | if (Result.isInvalid()) |
| 384 | return; |
| 385 | MaxExpr = Result.getAs<Expr>(); |
| 386 | } |
| 387 | |
| 388 | S.addAMDGPUWavesPerEUAttr(Attr.getLocation(), New, MinExpr, MaxExpr, |
| 389 | Attr.getSpellingListIndex()); |
| 390 | } |
| 391 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 392 | void Sema::InstantiateAttrsForDecl( |
| 393 | const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, |
| 394 | Decl *New, LateInstantiatedAttrVec *LateAttrs, |
| 395 | LocalInstantiationScope *OuterMostScope) { |
| 396 | if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { |
| 397 | for (const auto *TmplAttr : Tmpl->attrs()) { |
| 398 | // FIXME: If any of the special case versions from InstantiateAttrs become |
| 399 | // applicable to template declaration, we'll need to add them here. |
| 400 | CXXThisScopeRAII ThisScope( |
| 401 | *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 402 | Qualifiers(), ND->isCXXInstanceMember()); |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 403 | |
| 404 | Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( |
| 405 | TmplAttr, Context, *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 406 | if (NewAttr) |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 407 | New->addAttr(NewAttr); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 412 | static Sema::RetainOwnershipKind |
| 413 | attrToRetainOwnershipKind(const Attr *A) { |
| 414 | switch (A->getKind()) { |
| 415 | case clang::attr::CFConsumed: |
| 416 | return Sema::RetainOwnershipKind::CF; |
| 417 | case clang::attr::OSConsumed: |
| 418 | return Sema::RetainOwnershipKind::OS; |
| 419 | case clang::attr::NSConsumed: |
| 420 | return Sema::RetainOwnershipKind::NS; |
| 421 | default: |
| 422 | llvm_unreachable("Wrong argument supplied"); |
| 423 | } |
| 424 | } |
| 425 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 426 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 427 | const Decl *Tmpl, Decl *New, |
| 428 | LateInstantiatedAttrVec *LateAttrs, |
| 429 | LocalInstantiationScope *OuterMostScope) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 430 | for (const auto *TmplAttr : Tmpl->attrs()) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 431 | // FIXME: This should be generalized to more than just the AlignedAttr. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 432 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 433 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 434 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 435 | continue; |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 438 | const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr); |
| 439 | if (AssumeAligned) { |
| 440 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 441 | continue; |
| 442 | } |
| 443 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 444 | const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr); |
| 445 | if (AlignValue) { |
| 446 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 447 | continue; |
| 448 | } |
| 449 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 450 | if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { |
| 451 | instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | |
George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 456 | if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 457 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 458 | cast<FunctionDecl>(New)); |
| 459 | continue; |
| 460 | } |
| 461 | |
| 462 | if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { |
| 463 | instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, |
| 464 | cast<FunctionDecl>(New)); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 468 | if (const CUDALaunchBoundsAttr *CUDALaunchBounds = |
| 469 | dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { |
| 470 | instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, |
| 471 | *CUDALaunchBounds, New); |
| 472 | continue; |
| 473 | } |
| 474 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 475 | if (const ModeAttr *Mode = dyn_cast<ModeAttr>(TmplAttr)) { |
| 476 | instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); |
| 477 | continue; |
| 478 | } |
| 479 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 480 | if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { |
| 481 | instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); |
| 482 | continue; |
| 483 | } |
| 484 | |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame^] | 485 | if (const AMDGPUFlatWorkGroupSizeAttr *AMDGPUFlatWorkGroupSize = |
| 486 | dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) { |
| 487 | instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 488 | *this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); |
| 489 | } |
| 490 | |
| 491 | if (const AMDGPUWavesPerEUAttr *AMDGPUFlatWorkGroupSize = |
| 492 | dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) { |
| 493 | instantiateDependentAMDGPUWavesPerEUAttr(*this, TemplateArgs, |
| 494 | *AMDGPUFlatWorkGroupSize, New); |
| 495 | } |
| 496 | |
Hans Wennborg | c2b7f7a | 2014-08-24 00:12:36 +0000 | [diff] [blame] | 497 | // Existing DLL attribute on the instantiation takes precedence. |
| 498 | if (TmplAttr->getKind() == attr::DLLExport || |
| 499 | TmplAttr->getKind() == attr::DLLImport) { |
| 500 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 501 | continue; |
| 502 | } |
| 503 | } |
| 504 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 505 | if (auto ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { |
| 506 | AddParameterABIAttr(ABIAttr->getRange(), New, ABIAttr->getABI(), |
| 507 | ABIAttr->getSpellingListIndex()); |
| 508 | continue; |
| 509 | } |
| 510 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 511 | if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || |
| 512 | isa<CFConsumedAttr>(TmplAttr)) { |
| 513 | AddXConsumedAttr(New, TmplAttr->getRange(), |
| 514 | TmplAttr->getSpellingListIndex(), |
| 515 | attrToRetainOwnershipKind(TmplAttr), |
| 516 | /*template instantiation=*/true); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 520 | assert(!TmplAttr->isPackExpansion()); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 521 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 522 | // Late parsed attributes must be instantiated and attached after the |
| 523 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 524 | LocalInstantiationScope *Saved = nullptr; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 525 | if (CurrentInstantiationScope) |
| 526 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 527 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 528 | } else { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 529 | // Allow 'this' within late-parsed attributes. |
| 530 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 531 | CXXRecordDecl *ThisContext = |
| 532 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 533 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 534 | ND && ND->isCXXInstanceMember()); |
| 535 | |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 536 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 537 | *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 538 | if (NewAttr) |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 539 | New->addAttr(NewAttr); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 540 | } |
Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 544 | /// Get the previous declaration of a declaration for the purposes of template |
| 545 | /// instantiation. If this finds a previous declaration, then the previous |
| 546 | /// declaration of the instantiation of D should be an instantiation of the |
| 547 | /// result of this function. |
| 548 | template<typename DeclT> |
| 549 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 550 | DeclT *Result = D->getPreviousDecl(); |
| 551 | |
| 552 | // If the declaration is within a class, and the previous declaration was |
| 553 | // merged from a different definition of that class, then we don't have a |
| 554 | // previous declaration for the purpose of template instantiation. |
| 555 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 556 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 557 | return nullptr; |
| 558 | |
| 559 | return Result; |
| 560 | } |
| 561 | |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 562 | Decl * |
| 563 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 564 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | Decl * |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 568 | TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { |
| 569 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 570 | } |
| 571 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 572 | Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( |
| 573 | PragmaDetectMismatchDecl *D) { |
| 574 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 575 | } |
| 576 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 577 | Decl * |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 578 | TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { |
| 579 | llvm_unreachable("extern \"C\" context cannot be instantiated"); |
| 580 | } |
| 581 | |
| 582 | Decl * |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 583 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 584 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 585 | D->getIdentifier()); |
| 586 | Owner->addDecl(Inst); |
| 587 | return Inst; |
| 588 | } |
| 589 | |
| 590 | Decl * |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 591 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 592 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 593 | } |
| 594 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 595 | Decl * |
| 596 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 597 | NamespaceAliasDecl *Inst |
| 598 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 599 | D->getNamespaceLoc(), |
| 600 | D->getAliasLoc(), |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 601 | D->getIdentifier(), |
| 602 | D->getQualifierLoc(), |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 603 | D->getTargetNameLoc(), |
| 604 | D->getNamespace()); |
| 605 | Owner->addDecl(Inst); |
| 606 | return Inst; |
| 607 | } |
| 608 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 609 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 610 | bool IsTypeAlias) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 611 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 612 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 613 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 614 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 615 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 616 | D->getLocation(), D->getDeclName()); |
| 617 | if (!DI) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 618 | Invalid = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 619 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 620 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 621 | } else { |
| 622 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 625 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 626 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 627 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 628 | // semantics. See LWG issue 2141 for more information on the bug. |
| 629 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 630 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 631 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 632 | DT->isReferenceType() && |
| 633 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 634 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 635 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 636 | SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 637 | // Fold it to the (non-reference) type which g++ would have produced. |
| 638 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 639 | DI->getType().getNonReferenceType()); |
| 640 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 641 | // Create the new typedef |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 642 | TypedefNameDecl *Typedef; |
| 643 | if (IsTypeAlias) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 644 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 645 | D->getLocation(), D->getIdentifier(), DI); |
| 646 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 647 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 648 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 649 | if (Invalid) |
| 650 | Typedef->setInvalidDecl(); |
| 651 | |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 652 | // If the old typedef was the name for linkage purposes of an anonymous |
| 653 | // tag decl, re-establish that relationship for the new typedef. |
| 654 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 655 | TagDecl *oldTag = oldTagType->getDecl(); |
Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 656 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 657 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 658 | assert(!newTag->hasNameForLinkage()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 659 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 660 | } |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 661 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 662 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 663 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 664 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 665 | TemplateArgs); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 666 | if (!InstPrev) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 667 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 668 | |
Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 669 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 670 | |
| 671 | // If the typedef types are not identical, reject them. |
| 672 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 673 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 674 | Typedef->setPreviousDecl(InstPrevTypedef); |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 675 | } |
| 676 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 677 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 678 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 679 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 681 | return Typedef; |
| 682 | } |
| 683 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 684 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 685 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 686 | if (Typedef) |
| 687 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 688 | return Typedef; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 692 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 693 | if (Typedef) |
| 694 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 695 | return Typedef; |
| 696 | } |
| 697 | |
| 698 | Decl * |
| 699 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 700 | // Create a local instantiation scope for this type alias template, which |
| 701 | // will contain the instantiations of the template parameters. |
| 702 | LocalInstantiationScope Scope(SemaRef); |
| 703 | |
| 704 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 705 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 706 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 707 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 708 | |
| 709 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 710 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 711 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 712 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 713 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 714 | if (!Found.empty()) { |
| 715 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
| 719 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 720 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 721 | if (!AliasInst) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 722 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 723 | |
| 724 | TypeAliasTemplateDecl *Inst |
| 725 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 726 | D->getDeclName(), InstParams, AliasInst); |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 727 | AliasInst->setDescribedAliasTemplate(Inst); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 728 | if (PrevAliasTemplate) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 729 | Inst->setPreviousDecl(PrevAliasTemplate); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 730 | |
| 731 | Inst->setAccess(D->getAccess()); |
| 732 | |
| 733 | if (!PrevAliasTemplate) |
| 734 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 735 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 736 | Owner->addDecl(Inst); |
| 737 | |
| 738 | return Inst; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 741 | Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 742 | auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 743 | D->getIdentifier()); |
Richard Smith | 81df9eb | 2017-10-02 22:43:36 +0000 | [diff] [blame] | 744 | NewBD->setReferenced(D->isReferenced()); |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 745 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); |
| 746 | return NewBD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 750 | // Transform the bindings first. |
| 751 | SmallVector<BindingDecl*, 16> NewBindings; |
| 752 | for (auto *OldBD : D->bindings()) |
| 753 | NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); |
| 754 | ArrayRef<BindingDecl*> NewBindingArray = NewBindings; |
| 755 | |
| 756 | auto *NewDD = cast_or_null<DecompositionDecl>( |
| 757 | VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); |
| 758 | |
| 759 | if (!NewDD || NewDD->isInvalidDecl()) |
| 760 | for (auto *NewBD : NewBindings) |
| 761 | NewBD->setInvalidDecl(); |
| 762 | |
| 763 | return NewDD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 766 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 767 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 770 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 771 | bool InstantiatingVarTemplate, |
| 772 | ArrayRef<BindingDecl*> *Bindings) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 773 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 774 | // Do substitution on the type of the declaration |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 775 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 776 | D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), |
| 777 | D->getDeclName(), /*AllowDeducedTST*/true); |
John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 778 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 779 | return nullptr; |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 781 | if (DI->getType()->isFunctionType()) { |
| 782 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 783 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 784 | return nullptr; |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 785 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 786 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 787 | DeclContext *DC = Owner; |
| 788 | if (D->isLocalExternDecl()) |
| 789 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 790 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 791 | // Build the instantiated declaration. |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 792 | VarDecl *Var; |
| 793 | if (Bindings) |
| 794 | Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 795 | D->getLocation(), DI->getType(), DI, |
| 796 | D->getStorageClass(), *Bindings); |
| 797 | else |
| 798 | Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 799 | D->getLocation(), D->getIdentifier(), DI->getType(), |
| 800 | DI, D->getStorageClass()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 802 | // In ARC, infer 'retaining' for variables of retainable type. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 803 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 804 | SemaRef.inferObjCARCLifetime(Var)) |
| 805 | Var->setInvalidDecl(); |
| 806 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 807 | // Substitute the nested name specifier, if any. |
| 808 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 809 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 811 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 812 | StartingScope, InstantiatingVarTemplate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 813 | |
| 814 | if (D->isNRVOVariable()) { |
| 815 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
Richard Trieu | 09c163b | 2018-03-15 03:00:55 +0000 | [diff] [blame] | 816 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, Sema::CES_Strict)) |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 817 | Var->setNRVOVariable(true); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 818 | } |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 819 | |
Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 820 | Var->setImplicit(D->isImplicit()); |
| 821 | |
Hans Wennborg | 262baa4 | 2018-10-31 10:34:46 +0000 | [diff] [blame] | 822 | if (Var->isStaticLocal()) |
| 823 | SemaRef.CheckStaticLocalForDllExport(Var); |
| 824 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 825 | return Var; |
| 826 | } |
| 827 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 828 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 829 | AccessSpecDecl* AD |
| 830 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 831 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 832 | Owner->addHiddenDecl(AD); |
| 833 | return AD; |
| 834 | } |
| 835 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 836 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 837 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 838 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 839 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 840 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 841 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 842 | D->getLocation(), D->getDeclName()); |
| 843 | if (!DI) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 844 | DI = D->getTypeSourceInfo(); |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 845 | Invalid = true; |
| 846 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 847 | // C++ [temp.arg.type]p3: |
| 848 | // If a declaration acquires a function type through a type |
| 849 | // dependent on a template-parameter and this causes a |
| 850 | // declaration that does not use the syntactic form of a |
| 851 | // function declarator to have function type, the program is |
| 852 | // ill-formed. |
| 853 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 854 | << DI->getType(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 855 | Invalid = true; |
| 856 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 857 | } else { |
| 858 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | Expr *BitWidth = D->getBitWidth(); |
| 862 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 863 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 864 | else if (BitWidth) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 865 | // The bit-width expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 866 | EnterExpressionEvaluationContext Unevaluated( |
| 867 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 869 | ExprResult InstantiatedBitWidth |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 870 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 871 | if (InstantiatedBitWidth.isInvalid()) { |
| 872 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 873 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 874 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 875 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 876 | } |
| 877 | |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 878 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 879 | DI->getType(), DI, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | cast<RecordDecl>(Owner), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 881 | D->getLocation(), |
| 882 | D->isMutable(), |
| 883 | BitWidth, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 884 | D->getInClassInitStyle(), |
Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 885 | D->getInnerLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 886 | D->getAccess(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 887 | nullptr); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 888 | if (!Field) { |
| 889 | cast<Decl>(Owner)->setInvalidDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 890 | return nullptr; |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 891 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 893 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 894 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 895 | if (Field->hasAttrs()) |
| 896 | SemaRef.CheckAlignasUnderalignment(Field); |
| 897 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 898 | if (Invalid) |
| 899 | Field->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 901 | if (!Field->getDeclName()) { |
| 902 | // Keep track of where this decl came from. |
| 903 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 904 | } |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 905 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 906 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 907 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 908 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 909 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 911 | Field->setImplicit(D->isImplicit()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 912 | Field->setAccess(D->getAccess()); |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 913 | Owner->addDecl(Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 914 | |
| 915 | return Field; |
| 916 | } |
| 917 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 918 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 919 | bool Invalid = false; |
| 920 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 921 | |
| 922 | if (DI->getType()->isVariablyModifiedType()) { |
| 923 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 924 | << D; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 925 | Invalid = true; |
| 926 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 927 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 928 | D->getLocation(), D->getDeclName()); |
| 929 | if (!DI) { |
| 930 | DI = D->getTypeSourceInfo(); |
| 931 | Invalid = true; |
| 932 | } else if (DI->getType()->isFunctionType()) { |
| 933 | // C++ [temp.arg.type]p3: |
| 934 | // If a declaration acquires a function type through a type |
| 935 | // dependent on a template-parameter and this causes a |
| 936 | // declaration that does not use the syntactic form of a |
| 937 | // function declarator to have function type, the program is |
| 938 | // ill-formed. |
| 939 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 940 | << DI->getType(); |
| 941 | Invalid = true; |
| 942 | } |
| 943 | } else { |
| 944 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 945 | } |
| 946 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 947 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 948 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 949 | DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 950 | |
| 951 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 952 | StartingScope); |
| 953 | |
| 954 | if (Invalid) |
| 955 | Property->setInvalidDecl(); |
| 956 | |
| 957 | Property->setAccess(D->getAccess()); |
| 958 | Owner->addDecl(Property); |
| 959 | |
| 960 | return Property; |
| 961 | } |
| 962 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 963 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 964 | NamedDecl **NamedChain = |
| 965 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 966 | |
| 967 | int i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 968 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 969 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 970 | TemplateArgs); |
| 971 | if (!Next) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 972 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 974 | NamedChain[i++] = Next; |
| 975 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 976 | |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 977 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 978 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 979 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 980 | {NamedChain, D->getChainingSize()}); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 981 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 982 | for (const auto *Attr : D->attrs()) |
| 983 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 984 | |
| 985 | IndirectField->setImplicit(D->isImplicit()); |
| 986 | IndirectField->setAccess(D->getAccess()); |
| 987 | Owner->addDecl(IndirectField); |
| 988 | return IndirectField; |
| 989 | } |
| 990 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 991 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 992 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 993 | // parameters into the pattern type and checking the result. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 994 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 995 | TypeSourceInfo *InstTy; |
| 996 | // If this is an unsupported friend, don't bother substituting template |
| 997 | // arguments into it. The actual type referred to won't be used by any |
| 998 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 999 | // same info for the instantiated friend. |
| 1000 | if (D->isUnsupportedFriend()) { |
| 1001 | InstTy = Ty; |
| 1002 | } else { |
| 1003 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 1004 | D->getLocation(), DeclarationName()); |
| 1005 | } |
| 1006 | if (!InstTy) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1007 | return nullptr; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1008 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1009 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 1010 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1011 | if (!FD) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1012 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1014 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1015 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1016 | Owner->addDecl(FD); |
| 1017 | return FD; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1020 | NamedDecl *ND = D->getFriendDecl(); |
| 1021 | assert(ND && "friend decl must be a decl or a type!"); |
| 1022 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1023 | // All of the Visit implementations for the various potential friend |
| 1024 | // declarations have to be carefully written to work for friend |
| 1025 | // objects, with the most important detail being that the target |
| 1026 | // decl should almost certainly not be placed in Owner. |
| 1027 | Decl *NewND = Visit(ND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1028 | if (!NewND) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1030 | FriendDecl *FD = |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1031 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1032 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 1033 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1034 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1035 | Owner->addDecl(FD); |
| 1036 | return FD; |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1039 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1040 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1042 | // The expression in a static assertion is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1043 | EnterExpressionEvaluationContext Unevaluated( |
| 1044 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1045 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1046 | ExprResult InstantiatedAssertExpr |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1047 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1048 | if (InstantiatedAssertExpr.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1049 | return nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1050 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 1051 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1052 | InstantiatedAssertExpr.get(), |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 1053 | D->getMessage(), |
| 1054 | D->getRParenLoc(), |
| 1055 | D->isFailed()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1059 | EnumDecl *PrevDecl = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1060 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1061 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1062 | PatternPrev, |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1063 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1064 | if (!Prev) return nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1065 | PrevDecl = cast<EnumDecl>(Prev); |
| 1066 | } |
| 1067 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1068 | EnumDecl *Enum = |
| 1069 | EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 1070 | D->getLocation(), D->getIdentifier(), PrevDecl, |
| 1071 | D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1072 | if (D->isFixed()) { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1073 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1074 | // If we have type source information for the underlying type, it means it |
| 1075 | // has been explicitly set by the user. Perform substitution on it before |
| 1076 | // moving on. |
| 1077 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1078 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 1079 | DeclarationName()); |
| 1080 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1081 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1082 | else |
| 1083 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 1084 | } else { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1085 | assert(!D->getIntegerType()->isDependentType() |
| 1086 | && "Dependent type without type source info"); |
| 1087 | Enum->setIntegerType(D->getIntegerType()); |
| 1088 | } |
| 1089 | } |
| 1090 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1091 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 1092 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1093 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 1094 | Enum->setAccess(D->getAccess()); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1095 | // Forward the mangling number from the template to the instantiated decl. |
| 1096 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1097 | // See if the old tag was defined along with a declarator. |
| 1098 | // If it did, mark the new tag as being associated with that declarator. |
| 1099 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1100 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); |
| 1101 | // See if the old tag was defined along with a typedef. |
| 1102 | // If it did, mark the new tag as being associated with that typedef. |
| 1103 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1104 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1105 | if (SubstQualifier(D, Enum)) return nullptr; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1106 | Owner->addDecl(Enum); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1107 | |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1108 | EnumDecl *Def = D->getDefinition(); |
| 1109 | if (Def && Def != D) { |
| 1110 | // If this is an out-of-line definition of an enum member template, check |
| 1111 | // that the underlying types match in the instantiation of both |
| 1112 | // declarations. |
| 1113 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 1114 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1115 | QualType DefnUnderlying = |
| 1116 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 1117 | UnderlyingLoc, DeclarationName()); |
| 1118 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 1119 | DefnUnderlying, /*IsFixed=*/true, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1122 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1123 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 1124 | // specialization causes the implicit instantiation of the declarations, but |
| 1125 | // not the definitions of scoped member enumerations. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1126 | // |
| 1127 | // DR1484 clarifies that enumeration definitions inside of a template |
| 1128 | // declaration aren't considered entities that can be separately instantiated |
| 1129 | // from the rest of the entity they are declared inside of. |
| 1130 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 1131 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1132 | InstantiateEnumDefinition(Enum, Def); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1133 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1134 | |
| 1135 | return Enum; |
| 1136 | } |
| 1137 | |
| 1138 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 1139 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 1140 | Enum->startDefinition(); |
| 1141 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 1142 | // Update the location to refer to the definition. |
| 1143 | Enum->setLocation(Pattern->getLocation()); |
| 1144 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1145 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1146 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1147 | EnumConstantDecl *LastEnumConst = nullptr; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1148 | for (auto *EC : Pattern->enumerators()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1149 | // The specified value for the enumerator. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1150 | ExprResult Value((Expr *)nullptr); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1151 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1152 | // The enumerator's value expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1153 | EnterExpressionEvaluationContext Unevaluated( |
| 1154 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1156 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1157 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Drop the initial value and continue. |
| 1160 | bool isInvalid = false; |
| 1161 | if (Value.isInvalid()) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1162 | Value = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1163 | isInvalid = true; |
| 1164 | } |
| 1165 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | EnumConstantDecl *EnumConst |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1167 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 1168 | EC->getLocation(), EC->getIdentifier(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1169 | Value.get()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1170 | |
| 1171 | if (isInvalid) { |
| 1172 | if (EnumConst) |
| 1173 | EnumConst->setInvalidDecl(); |
| 1174 | Enum->setInvalidDecl(); |
| 1175 | } |
| 1176 | |
| 1177 | if (EnumConst) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1178 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1179 | |
John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 1180 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1181 | Enum->addDecl(EnumConst); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1182 | Enumerators.push_back(EnumConst); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1183 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1184 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1185 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 1186 | !Enum->isScoped()) { |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1187 | // If the enumeration is within a function or method, record the enum |
| 1188 | // constant as a local. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1189 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1190 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 1194 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1195 | Enumerators, nullptr, ParsedAttributesView()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1198 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1199 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1202 | Decl * |
| 1203 | TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 1204 | llvm_unreachable("BuiltinTemplateDecls cannot be instantiated."); |
| 1205 | } |
| 1206 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1207 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1208 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1209 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1210 | // Create a local instantiation scope for this class template, which |
| 1211 | // will contain the instantiations of the template parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1212 | LocalInstantiationScope Scope(SemaRef); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1213 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1214 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1216 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1217 | |
| 1218 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Instantiate the qualifier. We have to do this first in case |
| 1221 | // we're a friend declaration, because if we are then we need to put |
| 1222 | // the new declaration in the appropriate context. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1223 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 1224 | if (QualifierLoc) { |
| 1225 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1226 | TemplateArgs); |
| 1227 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1228 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1231 | CXXRecordDecl *PrevDecl = nullptr; |
| 1232 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1233 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1234 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1235 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1236 | if (!Found.empty()) { |
| 1237 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1238 | if (PrevClassTemplate) |
| 1239 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1240 | } |
| 1241 | } |
| 1242 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1243 | // If this isn't a friend, then it's a member template, in which |
| 1244 | // case we just want to build the instantiation in the |
| 1245 | // specialization. If it is a friend, we want to build it in |
| 1246 | // the appropriate context. |
| 1247 | DeclContext *DC = Owner; |
| 1248 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1249 | if (QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1250 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1251 | SS.Adopt(QualifierLoc); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1252 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1253 | if (!DC) return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1254 | } else { |
| 1255 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 1256 | Pattern->getDeclContext(), |
| 1257 | TemplateArgs); |
| 1258 | } |
| 1259 | |
| 1260 | // Look for a previous declaration of the template in the owning |
| 1261 | // context. |
| 1262 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1263 | Sema::LookupOrdinaryName, |
| 1264 | SemaRef.forRedeclarationInCurContext()); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1265 | SemaRef.LookupQualifiedName(R, DC); |
| 1266 | |
| 1267 | if (R.isSingleResult()) { |
| 1268 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 1269 | if (PrevClassTemplate) |
| 1270 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1271 | } |
| 1272 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1273 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1274 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 1275 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1276 | << QualifierLoc.getSourceRange(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1277 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1280 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1281 | if (PrevClassTemplate) { |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1282 | bool Complain = true; |
| 1283 | |
| 1284 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 1285 | // template for struct std::tr1::__detail::_Map_base, where the |
| 1286 | // template parameters of the friend declaration don't match the |
| 1287 | // template parameters of the original declaration. In this one |
| 1288 | // case, we don't complain about the ill-formed friend |
| 1289 | // declaration. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1290 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1291 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 1292 | DC->isNamespace() && |
| 1293 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1294 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 1295 | DeclContext *DCParent = DC->getParent(); |
| 1296 | if (DCParent->isNamespace() && |
| 1297 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 1298 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 1299 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1300 | Complain = false; |
| 1301 | } |
| 1302 | } |
| 1303 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1304 | TemplateParameterList *PrevParams |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1305 | = PrevClassTemplate->getMostRecentDecl()->getTemplateParameters(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Make sure the parameter lists match. |
| 1308 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1309 | Complain, |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1310 | Sema::TPL_TemplateMatch)) { |
| 1311 | if (Complain) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1312 | return nullptr; |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1313 | |
| 1314 | AdoptedPreviousTemplateParams = true; |
| 1315 | InstParams = PrevParams; |
| 1316 | } |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1317 | |
| 1318 | // Do some additional validation, then merge default arguments |
| 1319 | // from the existing declarations. |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1320 | if (!AdoptedPreviousTemplateParams && |
| 1321 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1322 | Sema::TPC_ClassTemplate)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1323 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1327 | CXXRecordDecl *RecordInst = CXXRecordDecl::Create( |
| 1328 | SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), |
| 1329 | Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, |
| 1330 | /*DelayTypeCreation=*/true); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1331 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1332 | if (QualifierLoc) |
| 1333 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1334 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1335 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, |
| 1336 | StartingScope); |
| 1337 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1338 | ClassTemplateDecl *Inst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1339 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1340 | D->getIdentifier(), InstParams, RecordInst); |
| 1341 | assert(!(isFriend && Owner->isDependentContext())); |
| 1342 | Inst->setPreviousDecl(PrevClassTemplate); |
| 1343 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1344 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1345 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1346 | if (isFriend) { |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1347 | if (PrevClassTemplate) |
| 1348 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1349 | else |
| 1350 | Inst->setAccess(D->getAccess()); |
| 1351 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1352 | Inst->setObjectOfFriendDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1353 | // TODO: do we want to track the instantiation progeny of this |
| 1354 | // friend target decl? |
| 1355 | } else { |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1356 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1357 | if (!PrevClassTemplate) |
| 1358 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1359 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1360 | |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1361 | // Trigger creation of the type for the instantiation. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1362 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1363 | Inst->getInjectedClassNameSpecialization()); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1365 | // Finish handling of friends. |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1366 | if (isFriend) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1367 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1368 | Inst->setLexicalDeclContext(Owner); |
| 1369 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1370 | return Inst; |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1371 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1372 | |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1373 | if (D->isOutOfLine()) { |
| 1374 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1375 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1376 | } |
| 1377 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1378 | Owner->addDecl(Inst); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1379 | |
| 1380 | if (!PrevClassTemplate) { |
| 1381 | // Queue up any out-of-line partial specializations of this member |
| 1382 | // class template; the client will force their instantiation once |
| 1383 | // the enclosing class has been instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1384 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1385 | D->getPartialSpecializations(PartialSpecs); |
| 1386 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1387 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1388 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1389 | } |
| 1390 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1391 | return Inst; |
| 1392 | } |
| 1393 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1394 | Decl * |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1395 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1396 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1397 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1399 | // Lookup the already-instantiated declaration in the instantiation |
| 1400 | // of the class template and return that. |
| 1401 | DeclContext::lookup_result Found |
| 1402 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1403 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1404 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1406 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1407 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1408 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1409 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1411 | if (ClassTemplatePartialSpecializationDecl *Result |
| 1412 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1413 | return Result; |
| 1414 | |
| 1415 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1418 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1419 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1420 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Create a local instantiation scope for this variable template, which |
| 1423 | // will contain the instantiations of the template parameters. |
| 1424 | LocalInstantiationScope Scope(SemaRef); |
| 1425 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1426 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1427 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1428 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1429 | |
| 1430 | VarDecl *Pattern = D->getTemplatedDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1431 | VarTemplateDecl *PrevVarTemplate = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1432 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1433 | if (getPreviousDeclForInstantiation(Pattern)) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1434 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1435 | if (!Found.empty()) |
| 1436 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1437 | } |
| 1438 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1439 | VarDecl *VarInst = |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1440 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1441 | /*InstantiatingVarTemplate=*/true)); |
Nick Lewycky | 6ca07ca | 2015-08-10 21:54:08 +0000 | [diff] [blame] | 1442 | if (!VarInst) return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1443 | |
| 1444 | DeclContext *DC = Owner; |
| 1445 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1446 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1447 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1448 | VarInst); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1449 | VarInst->setDescribedVarTemplate(Inst); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1450 | Inst->setPreviousDecl(PrevVarTemplate); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1451 | |
| 1452 | Inst->setAccess(D->getAccess()); |
| 1453 | if (!PrevVarTemplate) |
| 1454 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1455 | |
| 1456 | if (D->isOutOfLine()) { |
| 1457 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1458 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1459 | } |
| 1460 | |
| 1461 | Owner->addDecl(Inst); |
| 1462 | |
| 1463 | if (!PrevVarTemplate) { |
| 1464 | // Queue up any out-of-line partial specializations of this member |
| 1465 | // variable template; the client will force their instantiation once |
| 1466 | // the enclosing class has been instantiated. |
| 1467 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1468 | D->getPartialSpecializations(PartialSpecs); |
| 1469 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1470 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1471 | OutOfLineVarPartialSpecs.push_back( |
| 1472 | std::make_pair(Inst, PartialSpecs[I])); |
| 1473 | } |
| 1474 | |
| 1475 | return Inst; |
| 1476 | } |
| 1477 | |
| 1478 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1479 | VarTemplatePartialSpecializationDecl *D) { |
| 1480 | assert(D->isStaticDataMember() && |
| 1481 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1482 | |
| 1483 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1484 | |
| 1485 | // Lookup the already-instantiated declaration and return that. |
| 1486 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1487 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1488 | |
| 1489 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1490 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1491 | |
| 1492 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1493 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1494 | return Result; |
| 1495 | |
| 1496 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1497 | } |
| 1498 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1499 | Decl * |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1500 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1501 | // Create a local instantiation scope for this function template, which |
| 1502 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1503 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1504 | // itself. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1505 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1507 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1508 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1510 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1511 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1512 | FunctionDecl *Instantiated = nullptr; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1513 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1514 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1515 | InstParams)); |
| 1516 | else |
| 1517 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1518 | D->getTemplatedDecl(), |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1519 | InstParams)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1521 | if (!Instantiated) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1522 | return nullptr; |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1523 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | // Link the instantiated function template declaration to the function |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1525 | // template from which it was instantiated. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1526 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1527 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1528 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1529 | assert(InstTemplate && |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1530 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1531 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1532 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1533 | |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1534 | // Link the instantiation back to the pattern *unless* this is a |
| 1535 | // non-definition friend declaration. |
| 1536 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1537 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1538 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1539 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1540 | // Make declarations visible in the appropriate context. |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1541 | if (!isFriend) { |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1542 | Owner->addDecl(InstTemplate); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1543 | } else if (InstTemplate->getDeclContext()->isRecord() && |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1544 | !getPreviousDeclForInstantiation(D)) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1545 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1546 | } |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1548 | return InstTemplate; |
| 1549 | } |
| 1550 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1551 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1552 | CXXRecordDecl *PrevDecl = nullptr; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1553 | if (D->isInjectedClassName()) |
| 1554 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1555 | else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1556 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1557 | PatternPrev, |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1558 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1559 | if (!Prev) return nullptr; |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1560 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1561 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1562 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1563 | CXXRecordDecl *Record = CXXRecordDecl::Create( |
| 1564 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 1565 | D->getLocation(), D->getIdentifier(), PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Substitute the nested name specifier, if any. |
| 1568 | if (SubstQualifier(D, Record)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1569 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1570 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1571 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, |
| 1572 | StartingScope); |
| 1573 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1574 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1575 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1576 | // the tag decls introduced by friend class declarations don't have an access |
| 1577 | // specifier. Remove once this area of the code gets sorted out. |
| 1578 | if (D->getAccess() != AS_none) |
| 1579 | Record->setAccess(D->getAccess()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1580 | if (!D->isInjectedClassName()) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1581 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1582 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1583 | // If the original function was part of a friend declaration, |
| 1584 | // inherit its namespace state. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1585 | if (D->getFriendObjectKind()) |
| 1586 | Record->setObjectOfFriendDecl(); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1588 | // Make sure that anonymous structs and unions are recorded. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1589 | if (D->isAnonymousStructOrUnion()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1590 | Record->setAnonymousStructOrUnion(true); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1591 | |
| 1592 | if (D->isLocalClass()) |
| 1593 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1594 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1595 | // Forward the mangling number from the template to the instantiated decl. |
| 1596 | SemaRef.Context.setManglingNumber(Record, |
| 1597 | SemaRef.Context.getManglingNumber(D)); |
| 1598 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1599 | // See if the old tag was defined along with a declarator. |
| 1600 | // If it did, mark the new tag as being associated with that declarator. |
| 1601 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1602 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); |
| 1603 | |
| 1604 | // See if the old tag was defined along with a typedef. |
| 1605 | // If it did, mark the new tag as being associated with that typedef. |
| 1606 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1607 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); |
| 1608 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1609 | Owner->addDecl(Record); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1610 | |
| 1611 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1612 | // of the instantiation of their enclosing entity. |
| 1613 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1614 | Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1615 | |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1616 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1617 | TSK_ImplicitInstantiation, |
| 1618 | /*Complain=*/true); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1619 | |
Richard Smith | ece4758 | 2017-01-04 23:45:01 +0000 | [diff] [blame] | 1620 | // For nested local classes, we will instantiate the members when we |
| 1621 | // reach the end of the outermost (non-nested) local class. |
| 1622 | if (!D->isCXXClassMember()) |
| 1623 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1624 | TSK_ImplicitInstantiation); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1625 | |
| 1626 | // This class may have local implicit instantiations that need to be |
| 1627 | // performed within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1628 | LocalInstantiations.perform(); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1629 | } |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1630 | |
| 1631 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1632 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1633 | return Record; |
| 1634 | } |
| 1635 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1636 | /// Adjust the given function type for an instantiation of the |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1637 | /// given declaration, to cope with modifications to the function's type that |
| 1638 | /// aren't reflected in the type-source information. |
| 1639 | /// |
| 1640 | /// \param D The declaration we're instantiating. |
| 1641 | /// \param TInfo The already-instantiated type. |
| 1642 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1643 | FunctionDecl *D, |
| 1644 | TypeSourceInfo *TInfo) { |
Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1645 | const FunctionProtoType *OrigFunc |
| 1646 | = D->getType()->castAs<FunctionProtoType>(); |
| 1647 | const FunctionProtoType *NewFunc |
| 1648 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1649 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1650 | return TInfo->getType(); |
| 1651 | |
| 1652 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1653 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1654 | return Context.getFunctionType(NewFunc->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1655 | NewFunc->getParamTypes(), NewEPI); |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1658 | /// Normal class members are of more specific types and therefore |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1659 | /// don't make it here. This function serves three purposes: |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1660 | /// 1) instantiating function templates |
| 1661 | /// 2) substituting friend declarations |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1662 | /// 3) substituting deduction guide declarations for nested class templates |
Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1663 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1664 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1665 | // Check whether there is already a function template specialization for |
| 1666 | // this declaration. |
| 1667 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1668 | if (FunctionTemplate && !TemplateParams) { |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1669 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1671 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1672 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1673 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1675 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1676 | if (SpecFunc) |
| 1677 | return SpecFunc; |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1680 | bool isFriend; |
| 1681 | if (FunctionTemplate) |
| 1682 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1683 | else |
| 1684 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1685 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1686 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1687 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1688 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1689 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1690 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1692 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1693 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1694 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1695 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1696 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1698 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1699 | if (QualifierLoc) { |
| 1700 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1701 | TemplateArgs); |
| 1702 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1703 | return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1706 | // If we're instantiating a local function declaration, put the result |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1707 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1708 | // context. |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1709 | DeclContext *DC; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1710 | if (D->isLocalExternDecl()) { |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1711 | DC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1712 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1713 | } else if (isFriend && QualifierLoc) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1714 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1715 | SS.Adopt(QualifierLoc); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1716 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1717 | if (!DC) return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1718 | } else { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1719 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1720 | TemplateArgs); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1721 | } |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1722 | |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1723 | DeclarationNameInfo NameInfo |
| 1724 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1725 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1726 | FunctionDecl *Function; |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1727 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1728 | Function = CXXDeductionGuideDecl::Create( |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1729 | SemaRef.Context, DC, D->getInnerLocStart(), DGuide->isExplicit(), |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1730 | NameInfo, T, TInfo, D->getSourceRange().getEnd()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1731 | if (DGuide->isCopyDeductionCandidate()) |
| 1732 | cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1733 | Function->setAccess(D->getAccess()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1734 | } else { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1735 | Function = FunctionDecl::Create( |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1736 | SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1737 | D->getCanonicalDecl()->getStorageClass(), D->isInlineSpecified(), |
| 1738 | D->hasWrittenPrototype(), D->isConstexpr()); |
| 1739 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
| 1740 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1741 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1742 | if (D->isInlined()) |
| 1743 | Function->setImplicitlyInline(); |
| 1744 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1745 | if (QualifierLoc) |
| 1746 | Function->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1747 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1748 | if (D->isLocalExternDecl()) |
| 1749 | Function->setLocalExternDecl(); |
| 1750 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1751 | DeclContext *LexicalDC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1752 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1753 | assert(D->getDeclContext()->isFileContext()); |
| 1754 | LexicalDC = D->getDeclContext(); |
| 1755 | } |
| 1756 | |
| 1757 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1759 | // Attach the parameters |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1760 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1761 | if (Params[P]) |
| 1762 | Params[P]->setOwningFunction(Function); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1763 | Function->setParams(Params); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1764 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1765 | if (TemplateParams) { |
| 1766 | // Our resulting instantiation is actually a function template, since we |
| 1767 | // are substituting only the outer template parameters. For example, given |
| 1768 | // |
| 1769 | // template<typename T> |
| 1770 | // struct X { |
| 1771 | // template<typename U> friend void f(T, U); |
| 1772 | // }; |
| 1773 | // |
| 1774 | // X<int> x; |
| 1775 | // |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1776 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1777 | // which means substituting int for T, but leaving "f" as a friend function |
| 1778 | // template. |
| 1779 | // Build the function template itself. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1780 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1781 | Function->getLocation(), |
| 1782 | Function->getDeclName(), |
| 1783 | TemplateParams, Function); |
| 1784 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1785 | |
| 1786 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1787 | |
| 1788 | if (isFriend && D->isThisDeclarationADefinition()) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1789 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1790 | D->getDescribedFunctionTemplate()); |
| 1791 | } |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1792 | } else if (FunctionTemplate) { |
| 1793 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1794 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1795 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1796 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1797 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1798 | /*InsertPos=*/nullptr); |
Richard Smith | 152bcd2 | 2017-01-28 02:56:07 +0000 | [diff] [blame] | 1799 | } else if (isFriend && D->isThisDeclarationADefinition()) { |
| 1800 | // Do not connect the friend to the template unless it's actually a |
| 1801 | // definition. We don't want non-template functions to be marked as being |
| 1802 | // template instantiations. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1803 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1804 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1805 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1806 | if (isFriend) |
| 1807 | Function->setObjectOfFriendDecl(); |
| 1808 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1809 | if (InitFunctionInstantiation(Function, D)) |
| 1810 | Function->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1812 | bool IsExplicitSpecialization = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1813 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1814 | LookupResult Previous( |
| 1815 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1816 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1817 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1818 | D->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 1819 | : SemaRef.forRedeclarationInCurContext()); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1820 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1821 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1822 | = D->getDependentSpecializationInfo()) { |
| 1823 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1824 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1825 | // Instantiate the explicit template arguments. |
| 1826 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1827 | Info->getRAngleLoc()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1828 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1829 | ExplicitArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1830 | return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1831 | |
| 1832 | // Map the candidate templates to their instantiations. |
| 1833 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1834 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1835 | Info->getTemplate(I), |
| 1836 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1837 | if (!Temp) return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1838 | |
| 1839 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1840 | } |
| 1841 | |
| 1842 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1843 | &ExplicitArgs, |
| 1844 | Previous)) |
| 1845 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1846 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1847 | IsExplicitSpecialization = true; |
| 1848 | } else if (const ASTTemplateArgumentListInfo *Info = |
| 1849 | D->getTemplateSpecializationArgsAsWritten()) { |
| 1850 | // The name of this function was written as a template-id. |
| 1851 | SemaRef.LookupQualifiedName(Previous, DC); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1852 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1853 | // Instantiate the explicit template arguments. |
| 1854 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1855 | Info->getRAngleLoc()); |
| 1856 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1857 | ExplicitArgs, TemplateArgs)) |
| 1858 | return nullptr; |
| 1859 | |
| 1860 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1861 | &ExplicitArgs, |
| 1862 | Previous)) |
| 1863 | Function->setInvalidDecl(); |
| 1864 | |
| 1865 | IsExplicitSpecialization = true; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1866 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1867 | // Look only into the namespace where the friend would be declared to |
| 1868 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1869 | // as described in ActOnFriendFunctionDecl. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1870 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1871 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1872 | // In C++, the previous declaration we find might be a tag type |
| 1873 | // (class or enum). In this case, the new declaration will hide the |
| 1874 | // tag type. Note that this does does not apply if we're declaring a |
| 1875 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1876 | if (Previous.isSingleTagDecl()) |
| 1877 | Previous.clear(); |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1878 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1879 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1880 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1881 | IsExplicitSpecialization); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1882 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1883 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1884 | ? cast<NamedDecl>(FunctionTemplate) |
| 1885 | : Function); |
| 1886 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1887 | // If the original function was part of a friend declaration, |
| 1888 | // inherit its namespace state and add it to the owner. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1889 | if (isFriend) { |
Serge Pavlov | acfcd78 | 2018-12-06 09:35:04 +0000 | [diff] [blame] | 1890 | Function->setObjectOfFriendDecl(); |
| 1891 | if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) |
| 1892 | FT->setObjectOfFriendDecl(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1893 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1894 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1895 | bool QueuedInstantiation = false; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1896 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1897 | // C++11 [temp.friend]p4 (DR329): |
| 1898 | // When a function is defined in a friend function declaration in a class |
| 1899 | // template, the function is instantiated when the function is odr-used. |
| 1900 | // The same restrictions on multiple declarations and definitions that |
| 1901 | // apply to non-template function declarations and definitions also apply |
| 1902 | // to these implicit definitions. |
| 1903 | if (D->isThisDeclarationADefinition()) { |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1904 | SemaRef.CheckForFunctionRedefinition(Function); |
| 1905 | if (!Function->isInvalidDecl()) { |
| 1906 | for (auto R : Function->redecls()) { |
| 1907 | if (R == Function) |
| 1908 | continue; |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1909 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1910 | // If some prior declaration of this function has been used, we need |
| 1911 | // to instantiate its definition. |
| 1912 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1913 | if (MemberSpecializationInfo *MSInfo = |
| 1914 | Function->getMemberSpecializationInfo()) { |
| 1915 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1916 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1917 | MSInfo->setPointOfInstantiation(Loc); |
| 1918 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1919 | std::make_pair(Function, Loc)); |
| 1920 | QueuedInstantiation = true; |
| 1921 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1922 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1923 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
Richard Smith | f359765 | 2017-05-10 00:01:13 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Check the template parameter list against the previous declaration. The |
| 1929 | // goal here is to pick up default arguments added since the friend was |
| 1930 | // declared; we know the template parameter lists match, since otherwise |
| 1931 | // we would not have picked this template as the previous declaration. |
| 1932 | if (TemplateParams && FunctionTemplate->getPreviousDecl()) { |
| 1933 | SemaRef.CheckTemplateParameterList( |
| 1934 | TemplateParams, |
| 1935 | FunctionTemplate->getPreviousDecl()->getTemplateParameters(), |
| 1936 | Function->isThisDeclarationADefinition() |
| 1937 | ? Sema::TPC_FriendFunctionTemplateDefinition |
| 1938 | : Sema::TPC_FriendFunctionTemplate); |
| 1939 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1942 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1943 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1944 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1945 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1946 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1947 | PrincipalDecl->setNonMemberOperator(); |
| 1948 | |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1949 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1950 | return Function; |
| 1951 | } |
| 1952 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1953 | Decl * |
| 1954 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1955 | TemplateParameterList *TemplateParams, |
| 1956 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1957 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1958 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | // We are creating a function template specialization from a function |
| 1960 | // template. Check whether there is already a function template |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1961 | // specialization for this particular set of template arguments. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1962 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1964 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1965 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1966 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1968 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1969 | if (SpecFunc) |
| 1970 | return SpecFunc; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1973 | bool isFriend; |
| 1974 | if (FunctionTemplate) |
| 1975 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1976 | else |
| 1977 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1978 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1979 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1980 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1981 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1982 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1983 | |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1984 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1985 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1986 | unsigned NumTempParamLists = 0; |
| 1987 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
Benjamin Kramer | 9dc549b | 2015-08-04 14:46:06 +0000 | [diff] [blame] | 1988 | TempParamLists.resize(NumTempParamLists); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1989 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1990 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1991 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1992 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1993 | return nullptr; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1994 | TempParamLists[I] = InstParams; |
| 1995 | } |
| 1996 | } |
| 1997 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1998 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1999 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2000 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2001 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 2002 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2004 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 2005 | if (QualifierLoc) { |
| 2006 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2007 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2008 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2009 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | DeclContext *DC = Owner; |
| 2013 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2014 | if (QualifierLoc) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2015 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2016 | SS.Adopt(QualifierLoc); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2017 | DC = SemaRef.computeDeclContext(SS); |
John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 2018 | |
| 2019 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2020 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2021 | } else { |
| 2022 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 2023 | D->getDeclContext(), |
| 2024 | TemplateArgs); |
| 2025 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2026 | if (!DC) return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2029 | // Build the instantiated method declaration. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2030 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2031 | CXXMethodDecl *Method = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2033 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2034 | DeclarationNameInfo NameInfo |
| 2035 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2036 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2038 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | Constructor->isExplicit(), |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 2040 | Constructor->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 2041 | false, Constructor->isConstexpr()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2042 | Method->setRangeEnd(Constructor->getEndLoc()); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2043 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2044 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2045 | StartLoc, NameInfo, T, TInfo, |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 2046 | Destructor->isInlineSpecified(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2047 | false); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2048 | Method->setRangeEnd(Destructor->getEndLoc()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2049 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2050 | Method = CXXConversionDecl::Create( |
| 2051 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 2052 | Conversion->isInlineSpecified(), Conversion->isExplicit(), |
| 2053 | Conversion->isConstexpr(), Conversion->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2054 | } else { |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 2055 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2056 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, StartLoc, NameInfo, |
| 2057 | T, TInfo, SC, D->isInlineSpecified(), |
| 2058 | D->isConstexpr(), D->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2059 | } |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2060 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2061 | if (D->isInlined()) |
| 2062 | Method->setImplicitlyInline(); |
| 2063 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2064 | if (QualifierLoc) |
| 2065 | Method->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2067 | if (TemplateParams) { |
| 2068 | // Our resulting instantiation is actually a function template, since we |
| 2069 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | // |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2071 | // template<typename T> |
| 2072 | // struct X { |
| 2073 | // template<typename U> void f(T, U); |
| 2074 | // }; |
| 2075 | // |
| 2076 | // X<int> x; |
| 2077 | // |
| 2078 | // We are instantiating the member template "f" within X<int>, which means |
| 2079 | // substituting int for T, but leaving "f" as a member function template. |
| 2080 | // Build the function template itself. |
| 2081 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 2082 | Method->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | Method->getDeclName(), |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2084 | TemplateParams, Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2085 | if (isFriend) { |
| 2086 | FunctionTemplate->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2087 | FunctionTemplate->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2088 | } else if (D->isOutOfLine()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2090 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2091 | } else if (FunctionTemplate) { |
| 2092 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 2093 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2094 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2095 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2096 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2097 | /*InsertPos=*/nullptr); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2098 | } else if (!isFriend) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2099 | // Record that this is an instantiation of a member function. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2100 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2101 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2102 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | // If we are instantiating a member function defined |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2104 | // out-of-line, the instantiation will have the same lexical |
| 2105 | // context (which will be a namespace scope) as the template. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2106 | if (isFriend) { |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2107 | if (NumTempParamLists) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 2108 | Method->setTemplateParameterListsInfo( |
| 2109 | SemaRef.Context, |
| 2110 | llvm::makeArrayRef(TempParamLists.data(), NumTempParamLists)); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2111 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2112 | Method->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2113 | Method->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2114 | } else if (D->isOutOfLine()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2115 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2117 | // Attach the parameters |
| 2118 | for (unsigned P = 0; P < Params.size(); ++P) |
| 2119 | Params[P]->setOwningFunction(Method); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2120 | Method->setParams(Params); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2121 | |
| 2122 | if (InitMethodInstantiation(Method, D)) |
| 2123 | Method->setInvalidDecl(); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2124 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2125 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2126 | Sema::ForExternalRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2127 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2128 | bool IsExplicitSpecialization = false; |
| 2129 | |
| 2130 | // If the name of this function was written as a template-id, instantiate |
| 2131 | // the explicit template arguments. |
| 2132 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 2133 | = D->getDependentSpecializationInfo()) { |
| 2134 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 2135 | |
| 2136 | // Instantiate the explicit template arguments. |
| 2137 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2138 | Info->getRAngleLoc()); |
| 2139 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2140 | ExplicitArgs, TemplateArgs)) |
| 2141 | return nullptr; |
| 2142 | |
| 2143 | // Map the candidate templates to their instantiations. |
| 2144 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 2145 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 2146 | Info->getTemplate(I), |
| 2147 | TemplateArgs); |
| 2148 | if (!Temp) return nullptr; |
| 2149 | |
| 2150 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 2151 | } |
| 2152 | |
| 2153 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2154 | &ExplicitArgs, |
| 2155 | Previous)) |
| 2156 | Method->setInvalidDecl(); |
| 2157 | |
| 2158 | IsExplicitSpecialization = true; |
| 2159 | } else if (const ASTTemplateArgumentListInfo *Info = |
| 2160 | D->getTemplateSpecializationArgsAsWritten()) { |
| 2161 | SemaRef.LookupQualifiedName(Previous, DC); |
| 2162 | |
| 2163 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2164 | Info->getRAngleLoc()); |
| 2165 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2166 | ExplicitArgs, TemplateArgs)) |
| 2167 | return nullptr; |
| 2168 | |
| 2169 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2170 | &ExplicitArgs, |
| 2171 | Previous)) |
| 2172 | Method->setInvalidDecl(); |
| 2173 | |
| 2174 | IsExplicitSpecialization = true; |
| 2175 | } else if (!FunctionTemplate || TemplateParams || isFriend) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2176 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2178 | // In C++, the previous declaration we find might be a tag type |
| 2179 | // (class or enum). In this case, the new declaration will hide the |
| 2180 | // tag type. Note that this does does not apply if we're declaring a |
| 2181 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2182 | if (Previous.isSingleTagDecl()) |
| 2183 | Previous.clear(); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2184 | } |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2185 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2186 | if (!IsClassScopeSpecialization) |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2187 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, |
| 2188 | IsExplicitSpecialization); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 2190 | if (D->isPure()) |
| 2191 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 2192 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2193 | // Propagate access. For a non-friend declaration, the access is |
| 2194 | // whatever we're propagating from. For a friend, it should be the |
| 2195 | // previous declaration we just found. |
| 2196 | if (isFriend && Method->getPreviousDecl()) |
| 2197 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2198 | else |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2199 | Method->setAccess(D->getAccess()); |
| 2200 | if (FunctionTemplate) |
| 2201 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 2202 | |
Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 2203 | SemaRef.CheckOverrideControl(Method); |
| 2204 | |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2205 | // 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] | 2206 | if (D->isExplicitlyDefaulted()) |
| 2207 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2208 | if (D->isDeletedAsWritten()) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 2209 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2210 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2211 | // If there's a function template, let our caller handle it. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2212 | if (FunctionTemplate) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2213 | // do nothing |
| 2214 | |
| 2215 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2216 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2217 | // do nothing |
| 2218 | |
| 2219 | // Otherwise, check access to friends and make them visible. |
| 2220 | } else if (isFriend) { |
| 2221 | // We only need to re-check access for methods which we didn't |
| 2222 | // manage to match during parsing. |
| 2223 | if (!D->getPreviousDecl()) |
| 2224 | SemaRef.CheckFriendAccess(Method); |
| 2225 | |
| 2226 | Record->makeDeclVisibleInContext(Method); |
| 2227 | |
| 2228 | // Otherwise, add the declaration. We don't need to do this for |
| 2229 | // class-scope specializations because we'll have matched them with |
| 2230 | // the appropriate template. |
| 2231 | } else if (!IsClassScopeSpecialization) { |
| 2232 | Owner->addDecl(Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2233 | } |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2235 | return Method; |
| 2236 | } |
| 2237 | |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2238 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2239 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2242 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2243 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2246 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2247 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2250 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2251 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 2252 | /*ExpectParameterPack=*/ false); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2255 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 2256 | TemplateTypeParmDecl *D) { |
| 2257 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2258 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2260 | TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2261 | SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2262 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), |
| 2263 | D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack()); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2264 | Inst->setAccess(AS_public); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2265 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2266 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2267 | TypeSourceInfo *InstantiatedDefaultArg = |
| 2268 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 2269 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 2270 | if (InstantiatedDefaultArg) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2271 | Inst->setDefaultArgument(InstantiatedDefaultArg); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2272 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2275 | // scope. |
| 2276 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2277 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2278 | return Inst; |
| 2279 | } |
| 2280 | |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2281 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 2282 | NonTypeTemplateParmDecl *D) { |
| 2283 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2284 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2285 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 2286 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2287 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2288 | TypeSourceInfo *DI; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2289 | QualType T; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2290 | bool Invalid = false; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2291 | |
| 2292 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2293 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2294 | // expansion of types. Substitute into each of the expanded types. |
| 2295 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 2296 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 2297 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2298 | TypeSourceInfo *NewDI = |
| 2299 | SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, |
| 2300 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2301 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2302 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2303 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2304 | QualType NewT = |
| 2305 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2306 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2307 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2308 | |
| 2309 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2310 | ExpandedParameterPackTypes.push_back(NewT); |
| 2311 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2313 | IsExpandedParameterPack = true; |
| 2314 | DI = D->getTypeSourceInfo(); |
| 2315 | T = DI->getType(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2316 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2317 | // The non-type template parameter pack's type is a pack expansion of types. |
| 2318 | // Determine whether we need to expand this parameter pack into separate |
| 2319 | // types. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2320 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2321 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2322 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2323 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2325 | // Determine whether the set of unexpanded parameter packs can and should |
| 2326 | // be expanded. |
| 2327 | bool Expand = true; |
| 2328 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2329 | Optional<unsigned> OrigNumExpansions |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2330 | = Expansion.getTypePtr()->getNumExpansions(); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2331 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2332 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 2333 | Pattern.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 2334 | Unexpanded, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2335 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2336 | Expand, RetainExpansion, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2337 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2338 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2339 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2340 | if (Expand) { |
| 2341 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2342 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2343 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2344 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2345 | D->getDeclName()); |
| 2346 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2347 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2348 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2349 | QualType NewT = |
| 2350 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2351 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2352 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2353 | |
| 2354 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2355 | ExpandedParameterPackTypes.push_back(NewT); |
| 2356 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2358 | // Note that we have an expanded parameter pack. The "type" of this |
| 2359 | // expanded parameter pack is the original expansion type, but callers |
| 2360 | // will end up using the expanded parameter pack types for type-checking. |
| 2361 | IsExpandedParameterPack = true; |
| 2362 | DI = D->getTypeSourceInfo(); |
| 2363 | T = DI->getType(); |
| 2364 | } else { |
| 2365 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2366 | // pattern and create a new pack expansion type. |
| 2367 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2368 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2369 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2370 | D->getDeclName()); |
| 2371 | if (!NewPattern) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2372 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2373 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2374 | SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2375 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 2376 | NumExpansions); |
| 2377 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2378 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2380 | T = DI->getType(); |
| 2381 | } |
| 2382 | } else { |
| 2383 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2384 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2385 | D->getLocation(), D->getDeclName()); |
| 2386 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2387 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2389 | // Check that this type is acceptable for a non-type template parameter. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2390 | T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2391 | if (T.isNull()) { |
| 2392 | T = SemaRef.Context.IntTy; |
| 2393 | Invalid = true; |
| 2394 | } |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2395 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2397 | NonTypeTemplateParmDecl *Param; |
| 2398 | if (IsExpandedParameterPack) |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2399 | Param = NonTypeTemplateParmDecl::Create( |
| 2400 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2401 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2402 | D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2403 | ExpandedParameterPackTypesAsWritten); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2404 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2405 | Param = NonTypeTemplateParmDecl::Create( |
| 2406 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2407 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2408 | D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2409 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2410 | Param->setAccess(AS_public); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2411 | if (Invalid) |
| 2412 | Param->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2413 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2414 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2415 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 2416 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2417 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 2418 | if (!Value.isInvalid()) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2419 | Param->setDefaultArgument(Value.get()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2420 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2421 | |
| 2422 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2423 | // scope. |
| 2424 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2425 | return Param; |
| 2426 | } |
| 2427 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2428 | static void collectUnexpandedParameterPacks( |
| 2429 | Sema &S, |
| 2430 | TemplateParameterList *Params, |
| 2431 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2432 | for (const auto &P : *Params) { |
| 2433 | if (P->isTemplateParameterPack()) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2434 | continue; |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2435 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2436 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 2437 | Unexpanded); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2438 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2439 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 2440 | Unexpanded); |
| 2441 | } |
| 2442 | } |
| 2443 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2444 | Decl * |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2445 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 2446 | TemplateTemplateParmDecl *D) { |
| 2447 | // Instantiate the template parameter list of the template template parameter. |
| 2448 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 2449 | TemplateParameterList *InstParams; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2450 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 2451 | |
| 2452 | bool IsExpandedParameterPack = false; |
| 2453 | |
| 2454 | if (D->isExpandedParameterPack()) { |
| 2455 | // The template template parameter pack is an already-expanded pack |
| 2456 | // expansion of template parameters. Substitute into each of the expanded |
| 2457 | // parameters. |
| 2458 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 2459 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 2460 | I != N; ++I) { |
| 2461 | LocalInstantiationScope Scope(SemaRef); |
| 2462 | TemplateParameterList *Expansion = |
| 2463 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2464 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2465 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2466 | ExpandedParams.push_back(Expansion); |
| 2467 | } |
| 2468 | |
| 2469 | IsExpandedParameterPack = true; |
| 2470 | InstParams = TempParams; |
| 2471 | } else if (D->isPackExpansion()) { |
| 2472 | // The template template parameter pack expands to a pack of template |
| 2473 | // template parameters. Determine whether we need to expand this parameter |
| 2474 | // pack into separate parameters. |
| 2475 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2476 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2477 | Unexpanded); |
| 2478 | |
| 2479 | // Determine whether the set of unexpanded parameter packs can and should |
| 2480 | // be expanded. |
| 2481 | bool Expand = true; |
| 2482 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2483 | Optional<unsigned> NumExpansions; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2484 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2485 | TempParams->getSourceRange(), |
| 2486 | Unexpanded, |
| 2487 | TemplateArgs, |
| 2488 | Expand, RetainExpansion, |
| 2489 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2490 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2491 | |
| 2492 | if (Expand) { |
| 2493 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2494 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2495 | LocalInstantiationScope Scope(SemaRef); |
| 2496 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2497 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2498 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2499 | ExpandedParams.push_back(Expansion); |
| 2500 | } |
| 2501 | |
| 2502 | // Note that we have an expanded parameter pack. The "type" of this |
| 2503 | // expanded parameter pack is the original expansion type, but callers |
| 2504 | // will end up using the expanded parameter pack types for type-checking. |
| 2505 | IsExpandedParameterPack = true; |
| 2506 | InstParams = TempParams; |
| 2507 | } else { |
| 2508 | // We cannot fully expand the pack expansion now, so just substitute |
| 2509 | // into the pattern. |
| 2510 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2511 | |
| 2512 | LocalInstantiationScope Scope(SemaRef); |
| 2513 | InstParams = SubstTemplateParams(TempParams); |
| 2514 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2515 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2516 | } |
| 2517 | } else { |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2518 | // Perform the actual substitution of template parameters within a new, |
| 2519 | // local instantiation scope. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2520 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2521 | InstParams = SubstTemplateParams(TempParams); |
| 2522 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2523 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2526 | // Build the template template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2527 | TemplateTemplateParmDecl *Param; |
| 2528 | if (IsExpandedParameterPack) |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2529 | Param = TemplateTemplateParmDecl::Create( |
| 2530 | SemaRef.Context, Owner, D->getLocation(), |
| 2531 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2532 | D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2533 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2534 | Param = TemplateTemplateParmDecl::Create( |
| 2535 | SemaRef.Context, Owner, D->getLocation(), |
| 2536 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2537 | D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2538 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2539 | NestedNameSpecifierLoc QualifierLoc = |
| 2540 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2541 | QualifierLoc = |
| 2542 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2543 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2544 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2545 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2546 | if (!TName.isNull()) |
| 2547 | Param->setDefaultArgument( |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2548 | SemaRef.Context, |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2549 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2550 | D->getDefaultArgument().getTemplateQualifierLoc(), |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2551 | D->getDefaultArgument().getTemplateNameLoc())); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2552 | } |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2553 | Param->setAccess(AS_public); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2554 | |
| 2555 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2556 | // scope. |
| 2557 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2558 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2559 | return Param; |
| 2560 | } |
| 2561 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2562 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2563 | // Using directives are never dependent (and never contain any types or |
| 2564 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2566 | UsingDirectiveDecl *Inst |
| 2567 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2568 | D->getNamespaceKeyLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2569 | D->getQualifierLoc(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2570 | D->getIdentLocation(), |
| 2571 | D->getNominatedNamespace(), |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2572 | D->getCommonAncestor()); |
Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2573 | |
| 2574 | // Add the using directive to its declaration context |
| 2575 | // only if this is not a function or method. |
| 2576 | if (!Owner->isFunctionOrMethod()) |
| 2577 | Owner->addDecl(Inst); |
| 2578 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2579 | return Inst; |
| 2580 | } |
| 2581 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2582 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2583 | |
| 2584 | // The nested name specifier may be dependent, for example |
| 2585 | // template <typename T> struct t { |
| 2586 | // struct s1 { T f1(); }; |
| 2587 | // struct s2 : s1 { using s1::f1; }; |
| 2588 | // }; |
| 2589 | // template struct t<int>; |
| 2590 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2591 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2592 | NestedNameSpecifierLoc QualifierLoc |
| 2593 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2594 | TemplateArgs); |
| 2595 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2596 | return nullptr; |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2597 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2598 | // For an inheriting constructor declaration, the name of the using |
| 2599 | // declaration is the name of a constructor in this class, not in the |
| 2600 | // base class. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2601 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2602 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 2603 | if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) |
| 2604 | NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 2605 | SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2606 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2607 | // We only need to do redeclaration lookups if we're in a class |
| 2608 | // scope (in fact, it's not really even possible in non-class |
| 2609 | // scopes). |
| 2610 | bool CheckRedeclaration = Owner->isRecord(); |
| 2611 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2612 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2613 | Sema::ForVisibleRedeclaration); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2614 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2615 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2616 | D->getUsingLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2617 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2618 | NameInfo, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2619 | D->hasTypename()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2621 | CXXScopeSpec SS; |
| 2622 | SS.Adopt(QualifierLoc); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2623 | if (CheckRedeclaration) { |
| 2624 | Prev.setHideTags(false); |
| 2625 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2626 | |
| 2627 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2628 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2629 | D->hasTypename(), SS, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2630 | D->getLocation(), Prev)) |
| 2631 | NewUD->setInvalidDecl(); |
| 2632 | |
| 2633 | } |
| 2634 | |
| 2635 | if (!NewUD->isInvalidDecl() && |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 2636 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), |
| 2637 | SS, NameInfo, D->getLocation())) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2638 | NewUD->setInvalidDecl(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2639 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2640 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2641 | NewUD->setAccess(D->getAccess()); |
| 2642 | Owner->addDecl(NewUD); |
| 2643 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2644 | // Don't process the shadow decls for an invalid decl. |
| 2645 | if (NewUD->isInvalidDecl()) |
| 2646 | return NewUD; |
| 2647 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2648 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2649 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2650 | |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2651 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2652 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2653 | // Process the shadow decls. |
Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2654 | for (auto *Shadow : D->shadows()) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2655 | // FIXME: UsingShadowDecl doesn't preserve its immediate target, so |
| 2656 | // reconstruct it in the case where it matters. |
| 2657 | NamedDecl *OldTarget = Shadow->getTargetDecl(); |
| 2658 | if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) |
| 2659 | if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) |
| 2660 | OldTarget = BaseShadow; |
| 2661 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2662 | NamedDecl *InstTarget = |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2663 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2664 | Shadow->getLocation(), OldTarget, TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2665 | if (!InstTarget) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2666 | return nullptr; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2667 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2668 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2669 | if (CheckRedeclaration) { |
| 2670 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2671 | continue; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 2672 | } else if (UsingShadowDecl *OldPrev = |
| 2673 | getPreviousDeclForInstantiation(Shadow)) { |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2674 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2675 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2676 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2677 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2678 | UsingShadowDecl *InstShadow = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2679 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2680 | PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2681 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2682 | |
| 2683 | if (isFunctionScope) |
| 2684 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2685 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2686 | |
| 2687 | return NewUD; |
| 2688 | } |
| 2689 | |
| 2690 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2691 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2692 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2695 | Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( |
| 2696 | ConstructorUsingShadowDecl *D) { |
| 2697 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 2698 | return nullptr; |
| 2699 | } |
| 2700 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2701 | template <typename T> |
| 2702 | Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( |
| 2703 | T *D, bool InstantiatingPackElement) { |
| 2704 | // If this is a pack expansion, expand it now. |
| 2705 | if (D->isPackExpansion() && !InstantiatingPackElement) { |
| 2706 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2707 | SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); |
| 2708 | SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); |
| 2709 | |
| 2710 | // Determine whether the set of unexpanded parameter packs can and should |
| 2711 | // be expanded. |
| 2712 | bool Expand = true; |
| 2713 | bool RetainExpansion = false; |
| 2714 | Optional<unsigned> NumExpansions; |
| 2715 | if (SemaRef.CheckParameterPacksForExpansion( |
| 2716 | D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, |
| 2717 | Expand, RetainExpansion, NumExpansions)) |
| 2718 | return nullptr; |
| 2719 | |
| 2720 | // This declaration cannot appear within a function template signature, |
| 2721 | // so we can't have a partial argument list for a parameter pack. |
| 2722 | assert(!RetainExpansion && |
| 2723 | "should never need to retain an expansion for UsingPackDecl"); |
| 2724 | |
| 2725 | if (!Expand) { |
| 2726 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2727 | // pattern and create a new pack expansion. |
| 2728 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2729 | return instantiateUnresolvedUsingDecl(D, true); |
| 2730 | } |
| 2731 | |
| 2732 | // Within a function, we don't have any normal way to check for conflicts |
| 2733 | // between shadow declarations from different using declarations in the |
| 2734 | // same pack expansion, but this is always ill-formed because all expansions |
| 2735 | // must produce (conflicting) enumerators. |
| 2736 | // |
| 2737 | // Sadly we can't just reject this in the template definition because it |
| 2738 | // could be valid if the pack is empty or has exactly one expansion. |
| 2739 | if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { |
| 2740 | SemaRef.Diag(D->getEllipsisLoc(), |
| 2741 | diag::err_using_decl_redeclaration_expansion); |
| 2742 | return nullptr; |
| 2743 | } |
| 2744 | |
| 2745 | // Instantiate the slices of this pack and build a UsingPackDecl. |
| 2746 | SmallVector<NamedDecl*, 8> Expansions; |
| 2747 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2748 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2749 | Decl *Slice = instantiateUnresolvedUsingDecl(D, true); |
| 2750 | if (!Slice) |
| 2751 | return nullptr; |
| 2752 | // Note that we can still get unresolved using declarations here, if we |
| 2753 | // had arguments for all packs but the pattern also contained other |
| 2754 | // template arguments (this only happens during partial substitution, eg |
| 2755 | // into the body of a generic lambda in a function template). |
| 2756 | Expansions.push_back(cast<NamedDecl>(Slice)); |
| 2757 | } |
| 2758 | |
| 2759 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2760 | if (isDeclWithinFunction(D)) |
| 2761 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2762 | return NewD; |
| 2763 | } |
| 2764 | |
| 2765 | UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); |
| 2766 | SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); |
| 2767 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2768 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2769 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2770 | TemplateArgs); |
| 2771 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2772 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2774 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2775 | SS.Adopt(QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2777 | DeclarationNameInfo NameInfo |
| 2778 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2779 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2780 | // Produce a pack expansion only if we're not instantiating a particular |
| 2781 | // slice of a pack expansion. |
| 2782 | bool InstantiatingSlice = D->getEllipsisLoc().isValid() && |
| 2783 | SemaRef.ArgumentPackSubstitutionIndex != -1; |
| 2784 | SourceLocation EllipsisLoc = |
| 2785 | InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); |
| 2786 | |
| 2787 | NamedDecl *UD = SemaRef.BuildUsingDeclaration( |
| 2788 | /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2789 | /*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, |
| 2790 | ParsedAttributesView(), |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2791 | /*IsInstantiation*/ true); |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2792 | if (UD) |
| 2793 | SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); |
| 2794 | |
| 2795 | return UD; |
Richard Smith | 22a250c | 2016-12-19 04:08:53 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2798 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( |
| 2799 | UnresolvedUsingTypenameDecl *D) { |
| 2800 | return instantiateUnresolvedUsingDecl(D); |
| 2801 | } |
| 2802 | |
| 2803 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( |
| 2804 | UnresolvedUsingValueDecl *D) { |
| 2805 | return instantiateUnresolvedUsingDecl(D); |
| 2806 | } |
| 2807 | |
| 2808 | Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { |
| 2809 | SmallVector<NamedDecl*, 8> Expansions; |
| 2810 | for (auto *UD : D->expansions()) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2811 | if (NamedDecl *NewUD = |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2812 | SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2813 | Expansions.push_back(NewUD); |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2814 | else |
| 2815 | return nullptr; |
| 2816 | } |
| 2817 | |
| 2818 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2819 | if (isDeclWithinFunction(D)) |
| 2820 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2821 | return NewD; |
| 2822 | } |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2823 | |
| 2824 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2825 | ClassScopeFunctionSpecializationDecl *Decl) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2826 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nick Lewycky | 0b72773 | 2015-01-02 01:33:12 +0000 | [diff] [blame] | 2827 | CXXMethodDecl *NewFD = |
| 2828 | cast_or_null<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, nullptr, true)); |
| 2829 | if (!NewFD) |
| 2830 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2831 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2832 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 2833 | TemplateArgumentListInfo *ExplicitTemplateArgsPtr = nullptr; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2834 | if (Decl->hasExplicitTemplateArgs()) { |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2835 | if (SemaRef.Subst(Decl->templateArgs().getArgumentArray(), |
| 2836 | Decl->templateArgs().size(), ExplicitTemplateArgs, |
| 2837 | TemplateArgs)) |
| 2838 | return nullptr; |
| 2839 | ExplicitTemplateArgsPtr = &ExplicitTemplateArgs; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2842 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2843 | Sema::ForExternalRedeclaration); |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2844 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2845 | if (SemaRef.CheckFunctionTemplateSpecialization( |
| 2846 | NewFD, ExplicitTemplateArgsPtr, Previous)) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2847 | NewFD->setInvalidDecl(); |
| 2848 | return NewFD; |
| 2849 | } |
| 2850 | |
| 2851 | // Associate the specialization with the pattern. |
| 2852 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2853 | assert(Specialization && "Class scope Specialization is null"); |
| 2854 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2855 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 2856 | // FIXME: If this is a definition, check for redefinition errors! |
| 2857 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2858 | return NewFD; |
| 2859 | } |
| 2860 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2861 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2862 | OMPThreadPrivateDecl *D) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2863 | SmallVector<Expr *, 5> Vars; |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2864 | for (auto *I : D->varlists()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2865 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2866 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2867 | Vars.push_back(Var); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | OMPThreadPrivateDecl *TD = |
| 2871 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2872 | |
Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2873 | TD->setAccess(AS_public); |
| 2874 | Owner->addDecl(TD); |
| 2875 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2876 | return TD; |
| 2877 | } |
| 2878 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2879 | Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 2880 | llvm_unreachable( |
| 2881 | "Requires directive cannot be instantiated within a dependent context"); |
| 2882 | } |
| 2883 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2884 | Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( |
| 2885 | OMPDeclareReductionDecl *D) { |
| 2886 | // Instantiate type and check if it is allowed. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2887 | const bool RequiresInstantiation = |
| 2888 | D->getType()->isDependentType() || |
| 2889 | D->getType()->isInstantiationDependentType() || |
| 2890 | D->getType()->containsUnexpandedParameterPack(); |
| 2891 | QualType SubstReductionType; |
| 2892 | if (RequiresInstantiation) { |
| 2893 | SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( |
| 2894 | D->getLocation(), |
| 2895 | ParsedType::make(SemaRef.SubstType( |
| 2896 | D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); |
| 2897 | } else { |
| 2898 | SubstReductionType = D->getType(); |
| 2899 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2900 | if (SubstReductionType.isNull()) |
| 2901 | return nullptr; |
| 2902 | bool IsCorrect = !SubstReductionType.isNull(); |
| 2903 | // Create instantiated copy. |
| 2904 | std::pair<QualType, SourceLocation> ReductionTypes[] = { |
| 2905 | std::make_pair(SubstReductionType, D->getLocation())}; |
| 2906 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 2907 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 2908 | PrevDeclInScope = cast<OMPDeclareReductionDecl>( |
| 2909 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 2910 | ->get<Decl *>()); |
| 2911 | } |
| 2912 | auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( |
| 2913 | /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), |
| 2914 | PrevDeclInScope); |
| 2915 | auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2916 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); |
| 2917 | if (!RequiresInstantiation) { |
| 2918 | if (Expr *Combiner = D->getCombiner()) { |
| 2919 | NewDRD->setCombinerData(D->getCombinerIn(), D->getCombinerOut()); |
| 2920 | NewDRD->setCombiner(Combiner); |
| 2921 | if (Expr *Init = D->getInitializer()) { |
| 2922 | NewDRD->setInitializerData(D->getInitOrig(), D->getInitPriv()); |
| 2923 | NewDRD->setInitializer(Init, D->getInitializerKind()); |
| 2924 | } |
| 2925 | } |
| 2926 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( |
| 2927 | /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); |
| 2928 | return NewDRD; |
| 2929 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2930 | Expr *SubstCombiner = nullptr; |
| 2931 | Expr *SubstInitializer = nullptr; |
| 2932 | // Combiners instantiation sequence. |
| 2933 | if (D->getCombiner()) { |
| 2934 | SemaRef.ActOnOpenMPDeclareReductionCombinerStart( |
| 2935 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2936 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2937 | cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), |
| 2938 | cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); |
| 2939 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2940 | cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), |
| 2941 | cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); |
| 2942 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 2943 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2944 | ThisContext); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2945 | SubstCombiner = SemaRef.SubstExpr(D->getCombiner(), TemplateArgs).get(); |
| 2946 | SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); |
| 2947 | // Initializers instantiation sequence. |
| 2948 | if (D->getInitializer()) { |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2949 | VarDecl *OmpPrivParm = |
| 2950 | SemaRef.ActOnOpenMPDeclareReductionInitializerStart( |
| 2951 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2952 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2953 | cast<DeclRefExpr>(D->getInitOrig())->getDecl(), |
| 2954 | cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); |
| 2955 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2956 | cast<DeclRefExpr>(D->getInitPriv())->getDecl(), |
| 2957 | cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2958 | if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { |
| 2959 | SubstInitializer = |
| 2960 | SemaRef.SubstExpr(D->getInitializer(), TemplateArgs).get(); |
| 2961 | } else { |
| 2962 | IsCorrect = IsCorrect && OmpPrivParm->hasInit(); |
| 2963 | } |
| 2964 | SemaRef.ActOnOpenMPDeclareReductionInitializerEnd( |
| 2965 | NewDRD, SubstInitializer, OmpPrivParm); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2966 | } |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2967 | IsCorrect = |
| 2968 | IsCorrect && SubstCombiner && |
| 2969 | (!D->getInitializer() || |
| 2970 | (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && |
| 2971 | SubstInitializer) || |
| 2972 | (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && |
| 2973 | !SubstInitializer && !SubstInitializer)); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2974 | } else { |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2975 | IsCorrect = false; |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2976 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2977 | |
| 2978 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd(/*S=*/nullptr, DRD, |
| 2979 | IsCorrect); |
| 2980 | |
| 2981 | return NewDRD; |
| 2982 | } |
| 2983 | |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 2984 | Decl * |
| 2985 | TemplateDeclInstantiator::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { |
| 2986 | // Instantiate type and check if it is allowed. |
| 2987 | const bool RequiresInstantiation = |
| 2988 | D->getType()->isDependentType() || |
| 2989 | D->getType()->isInstantiationDependentType() || |
| 2990 | D->getType()->containsUnexpandedParameterPack(); |
| 2991 | QualType SubstMapperTy; |
| 2992 | DeclarationName VN = D->getVarName(); |
| 2993 | if (RequiresInstantiation) { |
| 2994 | SubstMapperTy = SemaRef.ActOnOpenMPDeclareMapperType( |
| 2995 | D->getLocation(), |
| 2996 | ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs, |
| 2997 | D->getLocation(), VN))); |
| 2998 | } else { |
| 2999 | SubstMapperTy = D->getType(); |
| 3000 | } |
| 3001 | if (SubstMapperTy.isNull()) |
| 3002 | return nullptr; |
| 3003 | // Create an instantiated copy of mapper. |
| 3004 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 3005 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 3006 | PrevDeclInScope = cast<OMPDeclareMapperDecl>( |
| 3007 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 3008 | ->get<Decl *>()); |
| 3009 | } |
| 3010 | OMPDeclareMapperDecl *NewDMD = SemaRef.ActOnOpenMPDeclareMapperDirectiveStart( |
| 3011 | /*S=*/nullptr, Owner, D->getDeclName(), SubstMapperTy, D->getLocation(), |
| 3012 | VN, D->getAccess(), PrevDeclInScope); |
| 3013 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDMD); |
| 3014 | SmallVector<OMPClause *, 6> Clauses; |
| 3015 | bool IsCorrect = true; |
| 3016 | if (!RequiresInstantiation) { |
| 3017 | // Copy the mapper variable. |
| 3018 | NewDMD->setMapperVarRef(D->getMapperVarRef()); |
| 3019 | // Copy map clauses from the original mapper. |
| 3020 | for (OMPClause *C : D->clauselists()) |
| 3021 | Clauses.push_back(C); |
| 3022 | } else { |
| 3023 | // Instantiate the mapper variable. |
| 3024 | DeclarationNameInfo DirName; |
| 3025 | SemaRef.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, /*S=*/nullptr, |
| 3026 | (*D->clauselist_begin())->getBeginLoc()); |
| 3027 | SemaRef.ActOnOpenMPDeclareMapperDirectiveVarDecl( |
| 3028 | NewDMD, /*S=*/nullptr, SubstMapperTy, D->getLocation(), VN); |
| 3029 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3030 | cast<DeclRefExpr>(D->getMapperVarRef())->getDecl(), |
| 3031 | cast<DeclRefExpr>(NewDMD->getMapperVarRef())->getDecl()); |
| 3032 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
| 3033 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
| 3034 | ThisContext); |
| 3035 | // Instantiate map clauses. |
| 3036 | for (OMPClause *C : D->clauselists()) { |
| 3037 | auto *OldC = cast<OMPMapClause>(C); |
| 3038 | SmallVector<Expr *, 4> NewVars; |
| 3039 | for (Expr *OE : OldC->varlists()) { |
| 3040 | Expr *NE = SemaRef.SubstExpr(OE, TemplateArgs).get(); |
| 3041 | if (!NE) { |
| 3042 | IsCorrect = false; |
| 3043 | break; |
| 3044 | } |
| 3045 | NewVars.push_back(NE); |
| 3046 | } |
| 3047 | if (!IsCorrect) |
| 3048 | break; |
Michael Kruse | 4304e9d | 2019-02-19 16:38:20 +0000 | [diff] [blame] | 3049 | NestedNameSpecifierLoc NewQualifierLoc = |
| 3050 | SemaRef.SubstNestedNameSpecifierLoc(OldC->getMapperQualifierLoc(), |
| 3051 | TemplateArgs); |
| 3052 | CXXScopeSpec SS; |
| 3053 | SS.Adopt(NewQualifierLoc); |
| 3054 | DeclarationNameInfo NewNameInfo = SemaRef.SubstDeclarationNameInfo( |
| 3055 | OldC->getMapperIdInfo(), TemplateArgs); |
| 3056 | OMPVarListLocTy Locs(OldC->getBeginLoc(), OldC->getLParenLoc(), |
| 3057 | OldC->getEndLoc()); |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 3058 | OMPClause *NewC = SemaRef.ActOnOpenMPMapClause( |
Michael Kruse | 4304e9d | 2019-02-19 16:38:20 +0000 | [diff] [blame] | 3059 | OldC->getMapTypeModifiers(), OldC->getMapTypeModifiersLoc(), SS, |
| 3060 | NewNameInfo, OldC->getMapType(), OldC->isImplicitMapType(), |
| 3061 | OldC->getMapLoc(), OldC->getColonLoc(), NewVars, Locs); |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 3062 | Clauses.push_back(NewC); |
| 3063 | } |
| 3064 | SemaRef.EndOpenMPDSABlock(nullptr); |
| 3065 | } |
| 3066 | (void)SemaRef.ActOnOpenMPDeclareMapperDirectiveEnd(NewDMD, /*S=*/nullptr, |
| 3067 | Clauses); |
| 3068 | if (!IsCorrect) |
| 3069 | return nullptr; |
| 3070 | return NewDMD; |
| 3071 | } |
| 3072 | |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 3073 | Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( |
| 3074 | OMPCapturedExprDecl * /*D*/) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 3075 | llvm_unreachable("Should not be met in templates"); |
| 3076 | } |
| 3077 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3078 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3079 | return VisitFunctionDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 3082 | Decl * |
| 3083 | TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
Richard Smith | 2600c63 | 2018-05-30 20:24:10 +0000 | [diff] [blame] | 3084 | Decl *Inst = VisitFunctionDecl(D, nullptr); |
| 3085 | if (Inst && !D->getDescribedFunctionTemplate()) |
| 3086 | Owner->addDecl(Inst); |
| 3087 | return Inst; |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3090 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3091 | return VisitCXXMethodDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 3095 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 3096 | } |
| 3097 | |
| 3098 | Decl * |
| 3099 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 3100 | ClassTemplateSpecializationDecl *D) { |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3101 | // As a MS extension, we permit class-scope explicit specialization |
| 3102 | // of member class templates. |
| 3103 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 3104 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 3105 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 3106 | "can only instantiate an explicit specialization " |
| 3107 | "for a member class template"); |
| 3108 | |
| 3109 | // Lookup the already-instantiated declaration in the instantiation |
| 3110 | // of the class template. FIXME: Diagnose or assert if this fails? |
| 3111 | DeclContext::lookup_result Found |
| 3112 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 3113 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3114 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3115 | ClassTemplateDecl *InstClassTemplate |
| 3116 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 3117 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3118 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3119 | |
| 3120 | // Substitute into the template arguments of the class template explicit |
| 3121 | // specialization. |
| 3122 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 3123 | castAs<TemplateSpecializationTypeLoc>(); |
| 3124 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 3125 | Loc.getRAngleLoc()); |
| 3126 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 3127 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 3128 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 3129 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 3130 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3131 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3132 | |
| 3133 | // Check that the template argument list is well-formed for this |
| 3134 | // class template. |
| 3135 | SmallVector<TemplateArgument, 4> Converted; |
| 3136 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 3137 | D->getLocation(), |
| 3138 | InstTemplateArgs, |
| 3139 | false, |
| 3140 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3141 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3142 | |
| 3143 | // Figure out where to insert this class template explicit specialization |
| 3144 | // in the member template's set of class template explicit specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3145 | void *InsertPos = nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3146 | ClassTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3147 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3148 | |
| 3149 | // Check whether we've already seen a conflicting instantiation of this |
| 3150 | // declaration (for instance, if there was a prior implicit instantiation). |
| 3151 | bool Ignored; |
| 3152 | if (PrevDecl && |
| 3153 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 3154 | D->getSpecializationKind(), |
| 3155 | PrevDecl, |
| 3156 | PrevDecl->getSpecializationKind(), |
| 3157 | PrevDecl->getPointOfInstantiation(), |
| 3158 | Ignored)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3159 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3160 | |
| 3161 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 3162 | // This happens in cases like: |
| 3163 | // |
| 3164 | // template<typename T, typename U> |
| 3165 | // struct Outer { |
| 3166 | // template<typename X> struct Inner; |
| 3167 | // template<> struct Inner<T> {}; |
| 3168 | // template<> struct Inner<U> {}; |
| 3169 | // }; |
| 3170 | // |
| 3171 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 3172 | // // have the same signature. |
| 3173 | if (PrevDecl && PrevDecl->getDefinition() && |
| 3174 | D->isThisDeclarationADefinition()) { |
| 3175 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 3176 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 3177 | diag::note_previous_definition); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3178 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3182 | ClassTemplateSpecializationDecl *InstD = |
| 3183 | ClassTemplateSpecializationDecl::Create( |
| 3184 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 3185 | D->getLocation(), InstClassTemplate, Converted, PrevDecl); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3186 | |
| 3187 | // Add this partial specialization to the set of class template partial |
| 3188 | // specializations. |
| 3189 | if (!PrevDecl) |
| 3190 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 3191 | |
| 3192 | // Substitute the nested name specifier, if any. |
| 3193 | if (SubstQualifier(D, InstD)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3194 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3195 | |
| 3196 | // Build the canonical type that describes the converted template |
| 3197 | // arguments of the class template explicit specialization. |
| 3198 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3199 | TemplateName(InstClassTemplate), Converted, |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3200 | SemaRef.Context.getRecordType(InstD)); |
| 3201 | |
| 3202 | // Build the fully-sugared type for this class template |
| 3203 | // specialization as the user wrote in the specialization |
| 3204 | // itself. This means that we'll pretty-print the type retrieved |
| 3205 | // from the specialization's declaration the way that the user |
| 3206 | // actually wrote the specialization, rather than formatting the |
| 3207 | // name based on the "canonical" representation used to store the |
| 3208 | // template arguments in the specialization. |
| 3209 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3210 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 3211 | CanonType); |
| 3212 | |
| 3213 | InstD->setAccess(D->getAccess()); |
| 3214 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 3215 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 3216 | InstD->setTypeAsWritten(WrittenTy); |
| 3217 | InstD->setExternLoc(D->getExternLoc()); |
| 3218 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 3219 | |
| 3220 | Owner->addDecl(InstD); |
| 3221 | |
| 3222 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 3223 | // We don't have support for lazy instantiation of an explicit specialization |
| 3224 | // yet, and MSVC eagerly instantiates in this case. |
| 3225 | if (D->isThisDeclarationADefinition() && |
| 3226 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 3227 | TSK_ImplicitInstantiation, |
| 3228 | /*Complain=*/true)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3229 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3230 | |
| 3231 | return InstD; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3234 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3235 | VarTemplateSpecializationDecl *D) { |
| 3236 | |
| 3237 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 3238 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 3239 | assert(VarTemplate && |
| 3240 | "A template specialization without specialized template?"); |
| 3241 | |
| 3242 | // Substitute the current template arguments. |
| 3243 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 3244 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 3245 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 3246 | |
| 3247 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 3248 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3249 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3250 | |
| 3251 | // Check that the template argument list is well-formed for this template. |
| 3252 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3253 | if (SemaRef.CheckTemplateArgumentList( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3254 | VarTemplate, VarTemplate->getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3255 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3256 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3257 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3258 | |
| 3259 | // Find the variable template specialization declaration that |
| 3260 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3261 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3262 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3263 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3264 | // If we already have a variable template specialization, return it. |
| 3265 | return VarSpec; |
| 3266 | |
| 3267 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, |
| 3268 | VarTemplateArgsInfo, Converted); |
| 3269 | } |
| 3270 | |
| 3271 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3272 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 3273 | const TemplateArgumentListInfo &TemplateArgsInfo, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 3274 | ArrayRef<TemplateArgument> Converted) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3275 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3276 | // Do substitution on the type of the declaration |
| 3277 | TypeSourceInfo *DI = |
| 3278 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 3279 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 3280 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3281 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3282 | |
| 3283 | if (DI->getType()->isFunctionType()) { |
| 3284 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 3285 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3286 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | // Build the instantiated declaration |
| 3290 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 3291 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3292 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3293 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3294 | if (InsertPos) |
| 3295 | VarTemplate->AddSpecialization(Var, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3296 | |
| 3297 | // Substitute the nested name specifier, if any. |
| 3298 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3299 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3300 | |
| 3301 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3302 | Owner, StartingScope); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3303 | |
| 3304 | return Var; |
| 3305 | } |
| 3306 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3307 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 3308 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 3309 | } |
| 3310 | |
| 3311 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 3312 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 3313 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 3314 | DiagnosticsEngine::Error, |
| 3315 | "cannot instantiate %0 yet"); |
| 3316 | SemaRef.Diag(D->getLocation(), DiagID) |
| 3317 | << D->getDeclKindName(); |
| 3318 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3319 | return nullptr; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 3323 | llvm_unreachable("Unexpected decl"); |
| 3324 | } |
| 3325 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3326 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3327 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 3328 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3329 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3330 | return nullptr; |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 3332 | return Instantiator.Visit(D); |
| 3333 | } |
| 3334 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3335 | /// Instantiates a nested template parameter list in the current |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3336 | /// instantiation context. |
| 3337 | /// |
| 3338 | /// \param L The parameter list to instantiate |
| 3339 | /// |
| 3340 | /// \returns NULL if there was an error |
| 3341 | TemplateParameterList * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3342 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3343 | // Get errors for all the parameters before bailing out. |
| 3344 | bool Invalid = false; |
| 3345 | |
| 3346 | unsigned N = L->size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3347 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3348 | ParamVector Params; |
| 3349 | Params.reserve(N); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 3350 | for (auto &P : *L) { |
| 3351 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3352 | Params.push_back(D); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 3353 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | // Clean up if we had an error. |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 3357 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3358 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3359 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3360 | // Note: we substitute into associated constraints later |
| 3361 | Expr *const UninstantiatedRequiresClause = L->getRequiresClause(); |
| 3362 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3363 | TemplateParameterList *InstL |
| 3364 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 3365 | L->getLAngleLoc(), Params, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3366 | L->getRAngleLoc(), |
| 3367 | UninstantiatedRequiresClause); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3368 | return InstL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | } |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3370 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 3371 | TemplateParameterList * |
| 3372 | Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, |
| 3373 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3374 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 3375 | return Instantiator.SubstTemplateParams(Params); |
| 3376 | } |
| 3377 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3378 | /// Instantiate the declaration of a class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3379 | /// specialization. |
| 3380 | /// |
| 3381 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 3382 | // specialized by the instantiation of \p PartialSpec. |
| 3383 | /// |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3384 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3385 | /// specialization that we are instantiating. |
| 3386 | /// |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3387 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3388 | /// NULL to indicate an error. |
| 3389 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3390 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 3391 | ClassTemplateDecl *ClassTemplate, |
| 3392 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3393 | // Create a local instantiation scope for this class template partial |
| 3394 | // specialization, which will contain the instantiations of the template |
| 3395 | // parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3396 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3398 | // Substitute into the template parameters of the class template partial |
| 3399 | // specialization. |
| 3400 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3401 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3402 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3403 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3404 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3405 | // Substitute into the template arguments of the class template partial |
| 3406 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3407 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3408 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3409 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3410 | TemplArgInfo->RAngleLoc); |
| 3411 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3412 | TemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 3413 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3414 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3416 | // Check that the template argument list is well-formed for this |
| 3417 | // class template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3418 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3419 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3420 | PartialSpec->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3421 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3422 | false, |
| 3423 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3424 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3425 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3426 | // Check these arguments are valid for a template partial specialization. |
| 3427 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3428 | PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), |
| 3429 | Converted)) |
| 3430 | return nullptr; |
| 3431 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3432 | // Figure out where to insert this class template partial specialization |
| 3433 | // in the member template's set of class template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3434 | void *InsertPos = nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3435 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3436 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3438 | // Build the canonical type that describes the converted template |
| 3439 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3440 | QualType CanonType |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3441 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3442 | Converted); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3443 | |
| 3444 | // Build the fully-sugared type for this class template |
| 3445 | // specialization as the user wrote in the specialization |
| 3446 | // itself. This means that we'll pretty-print the type retrieved |
| 3447 | // from the specialization's declaration the way that the user |
| 3448 | // actually wrote the specialization, rather than formatting the |
| 3449 | // name based on the "canonical" representation used to store the |
| 3450 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3451 | TypeSourceInfo *WrittenTy |
| 3452 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3453 | TemplateName(ClassTemplate), |
| 3454 | PartialSpec->getLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3455 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3456 | CanonType); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3458 | if (PrevDecl) { |
| 3459 | // We've already seen a partial specialization with the same template |
| 3460 | // parameters and template arguments. This can happen, for example, when |
| 3461 | // substituting the outer template arguments ends up causing two |
| 3462 | // class template partial specializations of a member class template |
| 3463 | // to have identical forms, e.g., |
| 3464 | // |
| 3465 | // template<typename T, typename U> |
| 3466 | // struct Outer { |
| 3467 | // template<typename X, typename Y> struct Inner; |
| 3468 | // template<typename Y> struct Inner<T, Y>; |
| 3469 | // template<typename Y> struct Inner<U, Y>; |
| 3470 | // }; |
| 3471 | // |
| 3472 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3473 | // // have the same signature. |
| 3474 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3475 | << WrittenTy->getType(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3476 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 3477 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3478 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3479 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3480 | |
| 3481 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3482 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3483 | ClassTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3484 | ClassTemplatePartialSpecializationDecl::Create( |
| 3485 | SemaRef.Context, PartialSpec->getTagKind(), Owner, |
| 3486 | PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, |
| 3487 | ClassTemplate, Converted, InstTemplateArgs, CanonType, nullptr); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3488 | // Substitute the nested name specifier, if any. |
| 3489 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3490 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3492 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 3493 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3494 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3495 | // Check the completed partial specialization. |
| 3496 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3497 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3498 | // Add this partial specialization to the set of class template partial |
| 3499 | // specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3500 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 3501 | /*InsertPos=*/nullptr); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3502 | return InstPartialSpec; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3505 | /// Instantiate the declaration of a variable template partial |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3506 | /// specialization. |
| 3507 | /// |
| 3508 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 3509 | /// specialized by the instantiation of \p PartialSpec. |
| 3510 | /// |
| 3511 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 3512 | /// specialization that we are instantiating. |
| 3513 | /// |
| 3514 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3515 | /// NULL to indicate an error. |
| 3516 | VarTemplatePartialSpecializationDecl * |
| 3517 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 3518 | VarTemplateDecl *VarTemplate, |
| 3519 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 3520 | // Create a local instantiation scope for this variable template partial |
| 3521 | // specialization, which will contain the instantiations of the template |
| 3522 | // parameters. |
| 3523 | LocalInstantiationScope Scope(SemaRef); |
| 3524 | |
| 3525 | // Substitute into the template parameters of the variable template partial |
| 3526 | // specialization. |
| 3527 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3528 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3529 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3530 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3531 | |
| 3532 | // Substitute into the template arguments of the variable template partial |
| 3533 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3534 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3535 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3536 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3537 | TemplArgInfo->RAngleLoc); |
| 3538 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3539 | TemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3540 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3541 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3542 | |
| 3543 | // Check that the template argument list is well-formed for this |
| 3544 | // class template. |
| 3545 | SmallVector<TemplateArgument, 4> Converted; |
| 3546 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 3547 | InstTemplateArgs, false, Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3548 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3549 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3550 | // Check these arguments are valid for a template partial specialization. |
| 3551 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3552 | PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), |
| 3553 | Converted)) |
| 3554 | return nullptr; |
| 3555 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3556 | // Figure out where to insert this variable template partial specialization |
| 3557 | // in the member template's set of variable template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3558 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3559 | VarTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3560 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3561 | |
| 3562 | // Build the canonical type that describes the converted template |
| 3563 | // arguments of the variable template partial specialization. |
| 3564 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3565 | TemplateName(VarTemplate), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3566 | |
| 3567 | // Build the fully-sugared type for this variable template |
| 3568 | // specialization as the user wrote in the specialization |
| 3569 | // itself. This means that we'll pretty-print the type retrieved |
| 3570 | // from the specialization's declaration the way that the user |
| 3571 | // actually wrote the specialization, rather than formatting the |
| 3572 | // name based on the "canonical" representation used to store the |
| 3573 | // template arguments in the specialization. |
| 3574 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3575 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 3576 | CanonType); |
| 3577 | |
| 3578 | if (PrevDecl) { |
| 3579 | // We've already seen a partial specialization with the same template |
| 3580 | // parameters and template arguments. This can happen, for example, when |
| 3581 | // substituting the outer template arguments ends up causing two |
| 3582 | // variable template partial specializations of a member variable template |
| 3583 | // to have identical forms, e.g., |
| 3584 | // |
| 3585 | // template<typename T, typename U> |
| 3586 | // struct Outer { |
| 3587 | // template<typename X, typename Y> pair<X,Y> p; |
| 3588 | // template<typename Y> pair<T, Y> p; |
| 3589 | // template<typename Y> pair<U, Y> p; |
| 3590 | // }; |
| 3591 | // |
| 3592 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3593 | // // have the same signature. |
| 3594 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3595 | diag::err_var_partial_spec_redeclared) |
| 3596 | << WrittenTy->getType(); |
| 3597 | SemaRef.Diag(PrevDecl->getLocation(), |
| 3598 | diag::note_var_prev_partial_spec_here); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3599 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | // Do substitution on the type of the declaration |
| 3603 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 3604 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 3605 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 3606 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3607 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3608 | |
| 3609 | if (DI->getType()->isFunctionType()) { |
| 3610 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3611 | diag::err_variable_instantiates_to_function) |
| 3612 | << PartialSpec->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3613 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | // Create the variable template partial specialization declaration. |
| 3617 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3618 | VarTemplatePartialSpecializationDecl::Create( |
| 3619 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 3620 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3621 | DI, PartialSpec->getStorageClass(), Converted, InstTemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Substitute the nested name specifier, if any. |
| 3624 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3625 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3626 | |
| 3627 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 3628 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 3629 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3630 | // Check the completed partial specialization. |
| 3631 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3632 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3633 | // Add this partial specialization to the set of variable template partial |
| 3634 | // specializations. The instantiation of the initializer is not necessary. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3635 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3636 | |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3637 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3638 | LateAttrs, Owner, StartingScope); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3639 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3640 | return InstPartialSpec; |
| 3641 | } |
| 3642 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3643 | TypeSourceInfo* |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3644 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3645 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3646 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 3647 | assert(OldTInfo && "substituting function without type source info"); |
| 3648 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3649 | |
| 3650 | CXXRecordDecl *ThisContext = nullptr; |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3651 | Qualifiers ThisTypeQuals; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3652 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3653 | ThisContext = cast<CXXRecordDecl>(Owner); |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 3654 | ThisTypeQuals = Method->getMethodQualifiers(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3655 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3656 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 3657 | TypeSourceInfo *NewTInfo |
| 3658 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 3659 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3660 | D->getDeclName(), |
| 3661 | ThisContext, ThisTypeQuals); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3662 | if (!NewTInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3663 | return nullptr; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3664 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3665 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 3666 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 3667 | if (NewTInfo != OldTInfo) { |
| 3668 | // Get parameters from the new type info. |
Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 3669 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3670 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3671 | unsigned NewIdx = 0; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3672 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3673 | OldIdx != NumOldParams; ++OldIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3674 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3675 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 3676 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3677 | Optional<unsigned> NumArgumentsInExpansion; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3678 | if (OldParam->isParameterPack()) |
| 3679 | NumArgumentsInExpansion = |
| 3680 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 3681 | TemplateArgs); |
| 3682 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3683 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3684 | // instantiated to a (still-dependent) parameter pack. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3685 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3686 | Params.push_back(NewParam); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3687 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 3688 | } else { |
| 3689 | // Parameter pack expansion: make the instantiation an argument pack. |
| 3690 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 3691 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3692 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3693 | Params.push_back(NewParam); |
| 3694 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 3695 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3696 | } |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3697 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3698 | } else { |
| 3699 | // The function type itself was not dependent and therefore no |
| 3700 | // substitution occurred. However, we still need to instantiate |
| 3701 | // the function parameters themselves. |
| 3702 | const FunctionProtoType *OldProto = |
| 3703 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3704 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 3705 | ++i) { |
| 3706 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3707 | if (!OldParam) { |
| 3708 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3709 | D, D->getLocation(), OldProto->getParamType(i))); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3710 | continue; |
| 3711 | } |
| 3712 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3713 | ParmVarDecl *Parm = |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3714 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3715 | if (!Parm) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3716 | return nullptr; |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3717 | Params.push_back(Parm); |
| 3718 | } |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3719 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3720 | } else { |
| 3721 | // If the type of this function, after ignoring parentheses, is not |
| 3722 | // *directly* a function type, then we're instantiating a function that |
| 3723 | // was declared via a typedef or with attributes, e.g., |
| 3724 | // |
| 3725 | // typedef int functype(int, int); |
| 3726 | // functype func; |
| 3727 | // int __cdecl meth(int, int); |
| 3728 | // |
| 3729 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 3730 | // synthesized in the method declaration. |
| 3731 | SmallVector<QualType, 4> ParamTypes; |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3732 | Sema::ExtParameterInfoBuilder ExtParamInfos; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3733 | if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, |
| 3734 | TemplateArgs, ParamTypes, &Params, |
| 3735 | ExtParamInfos)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3736 | return nullptr; |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3737 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3738 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3739 | return NewTInfo; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3742 | /// Introduce the instantiated function parameters into the local |
| 3743 | /// instantiation scope, and set the parameter names to those used |
| 3744 | /// in the template. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3745 | static bool addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3746 | const FunctionDecl *PatternDecl, |
| 3747 | LocalInstantiationScope &Scope, |
| 3748 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3749 | unsigned FParamIdx = 0; |
| 3750 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 3751 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 3752 | if (!PatternParam->isParameterPack()) { |
| 3753 | // Simple case: not a parameter pack. |
| 3754 | assert(FParamIdx < Function->getNumParams()); |
| 3755 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3756 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3757 | // If the parameter's type is not dependent, update it to match the type |
| 3758 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 3759 | // 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] | 3760 | // per core issue 1668. Substitute into the type from the pattern, in case |
| 3761 | // it's instantiation-dependent. |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3762 | // FIXME: Updating the type to work around this is at best fragile. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3763 | if (!PatternDecl->getType()->isDependentType()) { |
| 3764 | QualType T = S.SubstType(PatternParam->getType(), TemplateArgs, |
| 3765 | FunctionParam->getLocation(), |
| 3766 | FunctionParam->getDeclName()); |
| 3767 | if (T.isNull()) |
| 3768 | return true; |
| 3769 | FunctionParam->setType(T); |
| 3770 | } |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3771 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3772 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3773 | ++FParamIdx; |
| 3774 | continue; |
| 3775 | } |
| 3776 | |
| 3777 | // Expand the parameter pack. |
| 3778 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3779 | Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3780 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3781 | assert(NumArgumentsInExpansion && |
| 3782 | "should only be called when all template arguments are known"); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3783 | QualType PatternType = |
| 3784 | PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3785 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3786 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3787 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3788 | if (!PatternDecl->getType()->isDependentType()) { |
| 3789 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, Arg); |
| 3790 | QualType T = S.SubstType(PatternType, TemplateArgs, |
| 3791 | FunctionParam->getLocation(), |
| 3792 | FunctionParam->getDeclName()); |
| 3793 | if (T.isNull()) |
| 3794 | return true; |
| 3795 | FunctionParam->setType(T); |
| 3796 | } |
| 3797 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3798 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3799 | ++FParamIdx; |
| 3800 | } |
| 3801 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3802 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3803 | return false; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3807 | FunctionDecl *Decl) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3808 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3809 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3810 | return; |
| 3811 | |
| 3812 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3813 | InstantiatingTemplate::ExceptionSpecification()); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3814 | if (Inst.isInvalid()) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3815 | // We hit the instantiation depth limit. Clear the exception specification |
| 3816 | // so that our callers don't have to cope with EST_Uninstantiated. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3817 | UpdateExceptionSpec(Decl, EST_None); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3818 | return; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3819 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3820 | if (Inst.isAlreadyInstantiating()) { |
| 3821 | // This exception specification indirectly depends on itself. Reject. |
| 3822 | // FIXME: Corresponding rule in the standard? |
| 3823 | Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; |
| 3824 | UpdateExceptionSpec(Decl, EST_None); |
| 3825 | return; |
| 3826 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3827 | |
| 3828 | // Enter the scope of this instantiation. We don't use |
| 3829 | // PushDeclContext because we don't have a scope. |
| 3830 | Sema::ContextRAII savedContext(*this, Decl); |
| 3831 | LocalInstantiationScope Scope(*this); |
| 3832 | |
| 3833 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3834 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3835 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3836 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3837 | if (addInstantiatedParametersToScope(*this, Decl, Template, Scope, |
| 3838 | TemplateArgs)) { |
| 3839 | UpdateExceptionSpec(Decl, EST_None); |
| 3840 | return; |
| 3841 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3842 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3843 | SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), |
| 3844 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3847 | /// Initializes the common fields of an instantiation function |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3848 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3849 | /// |
| 3850 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | bool |
| 3852 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3853 | FunctionDecl *Tmpl) { |
David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3854 | if (Tmpl->isDeleted()) |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3855 | New->setDeletedAsWritten(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3857 | New->setImplicit(Tmpl->isImplicit()); |
| 3858 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3859 | // Forward the mangling number from the template to the instantiated decl. |
| 3860 | SemaRef.Context.setManglingNumber(New, |
| 3861 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3862 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3863 | // If we are performing substituting explicitly-specified template arguments |
| 3864 | // or deduced template arguments into a function template and we reach this |
| 3865 | // 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] | 3866 | // to keeping the new function template specialization. We therefore |
| 3867 | // convert the active template instantiation for the function template |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3868 | // into a template instantiation for this specific function template |
| 3869 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3870 | // further errors in the declaration itself. |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3871 | typedef Sema::CodeSynthesisContext ActiveInstType; |
| 3872 | ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3873 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3874 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3876 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3878 | "Deduction from the wrong function template?"); |
Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3879 | (void) FunTmpl; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3880 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3881 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3882 | ActiveInst.Entity = New; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3883 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3887 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3888 | assert(Proto && "Function template without prototype?"); |
| 3889 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3890 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3891 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3892 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3893 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3894 | // exception specification. |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3895 | // DR1484: Local classes and their members are instantiated along with the |
| 3896 | // containing function. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3897 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3898 | EPI.ExceptionSpec.Type != EST_None && |
| 3899 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3900 | EPI.ExceptionSpec.Type != EST_BasicNoexcept && |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 3901 | !Tmpl->isLexicallyWithinFunctionOrMethod()) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3902 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3903 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3904 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3905 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3906 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3907 | NewEST = EST_Unevaluated; |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3908 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3909 | // Mark the function has having an uninstantiated exception specification. |
| 3910 | const FunctionProtoType *NewProto |
| 3911 | = New->getType()->getAs<FunctionProtoType>(); |
| 3912 | assert(NewProto && "Template instantiation without function prototype?"); |
| 3913 | EPI = NewProto->getExtProtoInfo(); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3914 | EPI.ExceptionSpec.Type = NewEST; |
| 3915 | EPI.ExceptionSpec.SourceDecl = New; |
| 3916 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3917 | New->setType(SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3918 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3919 | } else { |
Faisal Vali | 40fd4ce | 2017-05-09 04:17:15 +0000 | [diff] [blame] | 3920 | Sema::ContextRAII SwitchContext(SemaRef, New); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3921 | SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3922 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3925 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3926 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3927 | Tmpl->isDefined(Definition); |
| 3928 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3929 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 3930 | LateAttrs, StartingScope); |
Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3932 | return false; |
| 3933 | } |
| 3934 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3935 | /// Initializes common fields of an instantiated method |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3936 | /// declaration (New) from the corresponding fields of its template |
| 3937 | /// (Tmpl). |
| 3938 | /// |
| 3939 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | bool |
| 3941 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3942 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3943 | if (InitFunctionInstantiation(New, Tmpl)) |
| 3944 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 3946 | if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) |
| 3947 | SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); |
| 3948 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3949 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3950 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3951 | New->setVirtualAsWritten(true); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3952 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3953 | // FIXME: New needs a pointer to Tmpl |
| 3954 | return false; |
| 3955 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3956 | |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 3957 | /// Instantiate (or find existing instantiation of) a function template with a |
| 3958 | /// given set of template arguments. |
| 3959 | /// |
| 3960 | /// Usually this should not be used, and template argument deduction should be |
| 3961 | /// used in its place. |
| 3962 | FunctionDecl * |
| 3963 | Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, |
| 3964 | const TemplateArgumentList *Args, |
| 3965 | SourceLocation Loc) { |
| 3966 | FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 3967 | |
| 3968 | sema::TemplateDeductionInfo Info(Loc); |
| 3969 | InstantiatingTemplate Inst( |
| 3970 | *this, Loc, FTD, Args->asArray(), |
| 3971 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 3972 | if (Inst.isInvalid()) |
| 3973 | return nullptr; |
| 3974 | |
| 3975 | ContextRAII SavedContext(*this, FD); |
| 3976 | MultiLevelTemplateArgumentList MArgs(*Args); |
| 3977 | |
| 3978 | return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); |
| 3979 | } |
| 3980 | |
Reid Kleckner | 61195e1 | 2017-01-05 01:08:22 +0000 | [diff] [blame] | 3981 | /// In the MS ABI, we need to instantiate default arguments of dllexported |
| 3982 | /// default constructors along with the constructor definition. This allows IR |
| 3983 | /// gen to emit a constructor closure which calls the default constructor with |
| 3984 | /// its default arguments. |
| 3985 | static void InstantiateDefaultCtorDefaultArgs(Sema &S, |
| 3986 | CXXConstructorDecl *Ctor) { |
| 3987 | assert(S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3988 | Ctor->isDefaultConstructor()); |
| 3989 | unsigned NumParams = Ctor->getNumParams(); |
| 3990 | if (NumParams == 0) |
| 3991 | return; |
| 3992 | DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); |
| 3993 | if (!Attr) |
| 3994 | return; |
| 3995 | for (unsigned I = 0; I != NumParams; ++I) { |
| 3996 | (void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, |
| 3997 | Ctor->getParamDecl(I)); |
| 3998 | S.DiscardCleanupsInEvaluationContext(); |
| 3999 | } |
| 4000 | } |
| 4001 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4002 | /// Instantiate the definition of the given function from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4003 | /// template. |
| 4004 | /// |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4005 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4006 | /// required. Note that this is not precisely a "point of instantiation" |
| 4007 | /// for the function, but it's close. |
| 4008 | /// |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4009 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4010 | /// function template specialization or member function of a class template |
| 4011 | /// specialization. |
| 4012 | /// |
| 4013 | /// \param Recursive if true, recursively instantiates any functions that |
| 4014 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4015 | /// |
| 4016 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 4017 | /// instantiation where the body of the function is required. Complain if |
| 4018 | /// there is no such body. |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 4019 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4020 | FunctionDecl *Function, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4021 | bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4022 | bool DefinitionRequired, |
| 4023 | bool AtEndOfTU) { |
Richard Smith | cb18957 | 2017-10-28 01:15:00 +0000 | [diff] [blame] | 4024 | if (Function->isInvalidDecl() || Function->isDefined() || |
| 4025 | isa<CXXDeductionGuideDecl>(Function)) |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 4026 | return; |
| 4027 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4028 | // Never instantiate an explicit specialization except if it is a class scope |
| 4029 | // explicit specialization. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4030 | TemplateSpecializationKind TSK = Function->getTemplateSpecializationKind(); |
| 4031 | if (TSK == TSK_ExplicitSpecialization && |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4032 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4033 | return; |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 4034 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4035 | // Find the function body that we'll be substituting. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 4036 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4037 | assert(PatternDecl && "instantiating a non-template"); |
| 4038 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4039 | const FunctionDecl *PatternDef = PatternDecl->getDefinition(); |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 4040 | Stmt *Pattern = nullptr; |
| 4041 | if (PatternDef) { |
| 4042 | Pattern = PatternDef->getBody(PatternDef); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4043 | PatternDecl = PatternDef; |
Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 4044 | if (PatternDef->willHaveBody()) |
| 4045 | PatternDef = nullptr; |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 4046 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4047 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4048 | // FIXME: We need to track the instantiation stack in order to know which |
| 4049 | // definitions should be visible within this instantiation. |
| 4050 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, |
| 4051 | Function->getInstantiatedFromMemberFunction(), |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4052 | PatternDecl, PatternDef, TSK, |
| 4053 | /*Complain*/DefinitionRequired)) { |
| 4054 | if (DefinitionRequired) |
| 4055 | Function->setInvalidDecl(); |
| 4056 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 4057 | // Try again at the end of the translation unit (at which point a |
| 4058 | // definition will be required). |
| 4059 | assert(!Recursive); |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 4060 | Function->setInstantiationIsPending(true); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4061 | PendingInstantiations.push_back( |
| 4062 | std::make_pair(Function, PointOfInstantiation)); |
| 4063 | } else if (TSK == TSK_ImplicitInstantiation) { |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4064 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4065 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4066 | Diag(PointOfInstantiation, diag::warn_func_template_missing) |
| 4067 | << Function; |
| 4068 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4069 | if (getLangOpts().CPlusPlus11) |
| 4070 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) |
| 4071 | << Function; |
| 4072 | } |
| 4073 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4074 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4075 | return; |
| 4076 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4077 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4078 | // Postpone late parsed template instantiations. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4079 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 4080 | !LateTemplateParser) { |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 4081 | Function->setInstantiationIsPending(true); |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 4082 | LateParsedInstantiations.push_back( |
| 4083 | std::make_pair(Function, PointOfInstantiation)); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4084 | return; |
| 4085 | } |
| 4086 | |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 4087 | // If we're performing recursive template instantiation, create our own |
| 4088 | // queue of pending implicit instantiations that we will instantiate later, |
| 4089 | // while we're still within our own instantiation context. |
| 4090 | // This has to happen before LateTemplateParser below is called, so that |
| 4091 | // it marks vtables used in late parsed templates as used. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4092 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4093 | /*Enabled=*/Recursive); |
| 4094 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 4095 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 4096 | // Call the LateTemplateParser callback if there is a need to late parse |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4097 | // a templated function definition. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4098 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4099 | LateTemplateParser) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 4100 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 4101 | if (PatternDecl->isFromASTFile()) |
| 4102 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 4103 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 4104 | auto LPTIter = LateParsedTemplateMap.find(PatternDecl); |
| 4105 | assert(LPTIter != LateParsedTemplateMap.end() && |
| 4106 | "missing LateParsedTemplate"); |
| 4107 | LateTemplateParser(OpaqueParser, *LPTIter->second); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4108 | Pattern = PatternDecl->getBody(PatternDecl); |
| 4109 | } |
| 4110 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4111 | // Note, we should never try to instantiate a deleted function template. |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4112 | assert((Pattern || PatternDecl->isDefaulted() || |
| 4113 | PatternDecl->hasSkippedBody()) && |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4114 | "unexpected kind of function template definition"); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4115 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4116 | // C++1y [temp.explicit]p10: |
| 4117 | // Except for inline functions, declarations with types deduced from their |
| 4118 | // initializer or return value, and class template specializations, other |
| 4119 | // explicit instantiation declarations have the effect of suppressing the |
| 4120 | // implicit instantiation of the entity to which they refer. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4121 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4122 | !PatternDecl->isInlined() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4123 | !PatternDecl->getReturnType()->getContainedAutoType()) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 4124 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | |
Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 4126 | if (PatternDecl->isInlined()) { |
| 4127 | // Function, and all later redeclarations of it (from imported modules, |
| 4128 | // for instance), are now implicitly inline. |
| 4129 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 4130 | D = D->getPreviousDecl()) { |
| 4131 | D->setImplicitlyInline(); |
| 4132 | if (D == Function) |
| 4133 | break; |
| 4134 | } |
| 4135 | } |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 4136 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 4137 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4138 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4139 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4140 | PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4141 | "instantiating function definition"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4142 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4143 | // The instantiation is visible here, even if it was first declared in an |
| 4144 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4145 | Function->setVisibleDespiteOwningModule(); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4146 | |
Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 4147 | // Copy the inner loc start from the pattern. |
| 4148 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 4149 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4150 | EnterExpressionEvaluationContext EvalContext( |
| 4151 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 4153 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4154 | // recorded, unless we're actually a member function within a local |
| 4155 | // class, in which case we need to merge our results with the parent |
| 4156 | // scope (of the enclosing function). |
| 4157 | bool MergeWithParentScope = false; |
| 4158 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 4159 | MergeWithParentScope = Rec->isLocalClass(); |
| 4160 | |
| 4161 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4162 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4163 | if (PatternDecl->isDefaulted()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4164 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4165 | else { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 4166 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4167 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
| 4168 | |
| 4169 | // Substitute into the qualifier; we can get a substitution failure here |
| 4170 | // through evil use of alias templates. |
| 4171 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 4172 | // of the) lexical context of the pattern? |
| 4173 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 4174 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4175 | ActOnStartOfFunctionDef(nullptr, Function); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4176 | |
| 4177 | // Enter the scope of this instantiation. We don't use |
| 4178 | // PushDeclContext because we don't have a scope. |
| 4179 | Sema::ContextRAII savedContext(*this, Function); |
| 4180 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 4181 | if (addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 4182 | TemplateArgs)) |
| 4183 | return; |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4184 | |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4185 | StmtResult Body; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4186 | if (PatternDecl->hasSkippedBody()) { |
| 4187 | ActOnSkippedFunctionBody(Function); |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4188 | Body = nullptr; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4189 | } else { |
Ilya Biryukov | 95f0d32 | 2017-12-28 13:05:46 +0000 | [diff] [blame] | 4190 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { |
| 4191 | // If this is a constructor, instantiate the member initializers. |
| 4192 | InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), |
| 4193 | TemplateArgs); |
| 4194 | |
| 4195 | // If this is an MS ABI dllexport default constructor, instantiate any |
| 4196 | // default arguments. |
| 4197 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4198 | Ctor->isDefaultConstructor()) { |
| 4199 | InstantiateDefaultCtorDefaultArgs(*this, Ctor); |
| 4200 | } |
| 4201 | } |
| 4202 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4203 | // Instantiate the function body. |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4204 | Body = SubstStmt(Pattern, TemplateArgs); |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4205 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4206 | if (Body.isInvalid()) |
| 4207 | Function->setInvalidDecl(); |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4208 | } |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4209 | // FIXME: finishing the function body while in an expression evaluation |
| 4210 | // context seems wrong. Investigate more. |
| 4211 | ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4212 | |
| 4213 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 4214 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4215 | if (auto *Listener = getASTMutationListener()) |
| 4216 | Listener->FunctionDefinitionInstantiated(Function); |
Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 4217 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4218 | savedContext.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 4221 | DeclGroupRef DG(Function); |
| 4222 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4224 | // This class may have local implicit instantiations that need to be |
| 4225 | // instantiation within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4226 | LocalInstantiations.perform(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4227 | Scope.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4228 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4231 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 4232 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 4233 | const TemplateArgumentList &TemplateArgList, |
| 4234 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 4235 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4236 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 4237 | LateInstantiatedAttrVec *LateAttrs, |
| 4238 | LocalInstantiationScope *StartingScope) { |
| 4239 | if (FromVar->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4240 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4241 | |
| 4242 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4243 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4244 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4245 | |
| 4246 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4247 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 4248 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4249 | // Instantiate the first declaration of the variable template: for a partial |
| 4250 | // specialization of a static data member template, the first declaration may |
| 4251 | // or may not be the declaration in the class; if it's in the class, we want |
| 4252 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 4253 | // we want to instantiate a definition. |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4254 | // |
| 4255 | // If we're instantiating an explicitly-specialized member template or member |
| 4256 | // partial specialization, don't do this. The member specialization completely |
| 4257 | // replaces the original declaration in this case. |
| 4258 | bool IsMemberSpec = false; |
| 4259 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 4260 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 4261 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 4262 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 4263 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 4264 | if (!IsMemberSpec) |
| 4265 | FromVar = FromVar->getFirstDecl(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4266 | |
Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 4267 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 4268 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 4269 | MultiLevelList); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4270 | |
| 4271 | // TODO: Set LateAttrs and StartingScope ... |
| 4272 | |
| 4273 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 4274 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 4275 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 4276 | } |
| 4277 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4278 | /// Instantiates a variable template specialization by completing it |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4279 | /// with appropriate type information and initializer. |
| 4280 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 4281 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 4282 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4283 | assert(PatternDecl->isThisDeclarationADefinition() && |
| 4284 | "don't have a definition to instantiate from"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4285 | |
| 4286 | // Do substitution on the type of the declaration |
| 4287 | TypeSourceInfo *DI = |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4288 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4289 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 4290 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4291 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4292 | |
| 4293 | // Update the type of this variable template specialization. |
| 4294 | VarSpec->setType(DI->getType()); |
| 4295 | |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4296 | // Convert the declaration into a definition now. |
| 4297 | VarSpec->setCompleteDefinition(); |
| 4298 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4299 | // Instantiate the initializer. |
| 4300 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 4301 | |
| 4302 | return VarSpec; |
| 4303 | } |
| 4304 | |
| 4305 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 4306 | /// Sets basic variable data and decides whether to postpone the |
| 4307 | /// variable instantiation. |
| 4308 | void Sema::BuildVariableInstantiation( |
| 4309 | VarDecl *NewVar, VarDecl *OldVar, |
| 4310 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4311 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 4312 | LocalInstantiationScope *StartingScope, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4313 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4314 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4315 | // If we are instantiating a local extern declaration, the |
| 4316 | // instantiation belongs lexically to the containing function. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4317 | // If we are instantiating a static data member defined |
| 4318 | // out-of-line, the instantiation will have the same lexical |
| 4319 | // context (which will be a namespace scope) as the template. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4320 | if (OldVar->isLocalExternDecl()) { |
| 4321 | NewVar->setLocalExternDecl(); |
| 4322 | NewVar->setLexicalDeclContext(Owner); |
| 4323 | } else if (OldVar->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4324 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 4325 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 4326 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 4327 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4328 | NewVar->setObjCForDecl(OldVar->isObjCForDecl()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4329 | NewVar->setConstexpr(OldVar->isConstexpr()); |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4330 | NewVar->setInitCapture(OldVar->isInitCapture()); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4331 | NewVar->setPreviousDeclInSameBlockScope( |
| 4332 | OldVar->isPreviousDeclInSameBlockScope()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4333 | NewVar->setAccess(OldVar->getAccess()); |
| 4334 | |
Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 4335 | if (!OldVar->isStaticDataMember()) { |
Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 4336 | if (OldVar->isUsed(false)) |
| 4337 | NewVar->setIsUsed(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4338 | NewVar->setReferenced(OldVar->isReferenced()); |
| 4339 | } |
| 4340 | |
| 4341 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 4342 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4343 | LookupResult Previous( |
| 4344 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 4345 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 4346 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4347 | NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 4348 | : forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4349 | |
Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 4350 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 4351 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 4352 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4353 | // We have a previous declaration. Use that one, so we merge with the |
| 4354 | // right type. |
| 4355 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 4356 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 4357 | Previous.addDecl(NewPrev); |
| 4358 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 4359 | OldVar->hasLinkage()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4360 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4361 | CheckVariableDeclaration(NewVar, Previous); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4362 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4363 | if (!InstantiatingVarTemplate) { |
| 4364 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 4365 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4366 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | if (!OldVar->isOutOfLine()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4370 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 4371 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 4372 | } |
| 4373 | |
| 4374 | // Link instantiations of static data members back to the template from |
| 4375 | // which they were instantiated. |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4376 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4377 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 4378 | TSK_ImplicitInstantiation); |
| 4379 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4380 | // Forward the mangling number from the template to the instantiated decl. |
| 4381 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4382 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4383 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4384 | // Delay instantiation of the initializer for variable templates or inline |
| 4385 | // static data members until a definition of the variable is needed. We need |
| 4386 | // it right away if the type contains 'auto'. |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4387 | if ((!isa<VarTemplateSpecializationDecl>(NewVar) && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4388 | !InstantiatingVarTemplate && |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 4389 | !(OldVar->isInline() && OldVar->isThisDeclarationADefinition() && |
| 4390 | !NewVar->isThisDeclarationADefinition())) || |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4391 | NewVar->getType()->isUndeducedType()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4392 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 4393 | |
| 4394 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 4395 | // will have been deferred. |
| 4396 | if (!NewVar->isInvalidDecl() && |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4397 | NewVar->getDeclContext()->isFunctionOrMethod() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4398 | OldVar->getType()->isDependentType()) |
| 4399 | DiagnoseUnusedDecl(NewVar); |
| 4400 | } |
| 4401 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4402 | /// Instantiate the initializer of a variable. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4403 | void Sema::InstantiateVariableInitializer( |
| 4404 | VarDecl *Var, VarDecl *OldVar, |
| 4405 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4406 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4407 | L->VariableDefinitionInstantiated(Var); |
| 4408 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4409 | // We propagate the 'inline' flag with the initializer, because it |
| 4410 | // would otherwise imply that the variable is a definition for a |
| 4411 | // non-static data member. |
| 4412 | if (OldVar->isInlineSpecified()) |
| 4413 | Var->setInlineSpecified(); |
| 4414 | else if (OldVar->isInline()) |
| 4415 | Var->setImplicitlyInline(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4416 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4417 | if (OldVar->getInit()) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 4418 | EnterExpressionEvaluationContext Evaluated( |
| 4419 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4420 | |
| 4421 | // Instantiate the initializer. |
Akira Hatanaka | b87faff | 2016-04-28 23:50:12 +0000 | [diff] [blame] | 4422 | ExprResult Init; |
| 4423 | |
| 4424 | { |
| 4425 | ContextRAII SwitchContext(*this, Var->getDeclContext()); |
| 4426 | Init = SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 4427 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 4428 | } |
| 4429 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4430 | if (!Init.isInvalid()) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4431 | Expr *InitExpr = Init.get(); |
| 4432 | |
Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 4433 | if (Var->hasAttr<DLLImportAttr>() && |
| 4434 | (!InitExpr || |
| 4435 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4436 | // Do not dynamically initialize dllimport variables. |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4437 | } else if (InitExpr) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4438 | bool DirectInit = OldVar->isDirectInit(); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4439 | AddInitializerToDecl(Var, InitExpr, DirectInit); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4440 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4441 | ActOnUninitializedDecl(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4442 | } else { |
| 4443 | // FIXME: Not too happy about invalidating the declaration |
| 4444 | // because of a bogus initializer. |
| 4445 | Var->setInvalidDecl(); |
| 4446 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4447 | } else { |
George Burgess IV | 18b28a8 | 2018-03-20 03:27:44 +0000 | [diff] [blame] | 4448 | // `inline` variables are a definition and declaration all in one; we won't |
| 4449 | // pick up an initializer from anywhere else. |
| 4450 | if (Var->isStaticDataMember() && !Var->isInline()) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4451 | if (!Var->isOutOfLine()) |
| 4452 | return; |
| 4453 | |
| 4454 | // If the declaration inside the class had an initializer, don't add |
| 4455 | // another one to the out-of-line definition. |
| 4456 | if (OldVar->getFirstDecl()->hasInit()) |
| 4457 | return; |
| 4458 | } |
| 4459 | |
| 4460 | // We'll add an initializer to a for-range declaration later. |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4461 | if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4462 | return; |
| 4463 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4464 | ActOnUninitializedDecl(Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4465 | } |
Artem Belevich | e9fa53a | 2018-06-06 22:37:25 +0000 | [diff] [blame] | 4466 | |
| 4467 | if (getLangOpts().CUDA) |
| 4468 | checkAllowedCUDAInitializer(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4471 | /// Instantiate the definition of the given variable from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4472 | /// template. |
| 4473 | /// |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4474 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4475 | /// required. Note that this is not precisely a "point of instantiation" |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4476 | /// for the variable, but it's close. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4477 | /// |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4478 | /// \param Var the already-instantiated declaration of a templated variable. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4479 | /// |
| 4480 | /// \param Recursive if true, recursively instantiates any functions that |
| 4481 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4482 | /// |
| 4483 | /// \param DefinitionRequired if true, then we are performing an explicit |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4484 | /// instantiation where a definition of the variable is required. Complain |
| 4485 | /// if there is no such definition. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4486 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 4487 | VarDecl *Var, bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4488 | bool DefinitionRequired, bool AtEndOfTU) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4489 | if (Var->isInvalidDecl()) |
| 4490 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4492 | VarTemplateSpecializationDecl *VarSpec = |
| 4493 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4494 | VarDecl *PatternDecl = nullptr, *Def = nullptr; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4495 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4496 | getTemplateInstantiationArgs(Var); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4498 | if (VarSpec) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4499 | // If this is a variable template specialization, make sure that it is |
| 4500 | // non-dependent, then find its instantiation pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4501 | bool InstantiationDependent = false; |
| 4502 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 4503 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 4504 | "Only instantiate variable template specializations that are " |
| 4505 | "not type-dependent"); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 4506 | (void)InstantiationDependent; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4507 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4508 | // Find the variable initialization that we'll be substituting. If the |
| 4509 | // pattern was instantiated from a member template, look back further to |
| 4510 | // find the real pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4511 | assert(VarSpec->getSpecializedTemplate() && |
| 4512 | "Specialization without specialized template?"); |
| 4513 | llvm::PointerUnion<VarTemplateDecl *, |
| 4514 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4515 | VarSpec->getSpecializedTemplateOrPartial(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4516 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4517 | VarTemplatePartialSpecializationDecl *Tmpl = |
| 4518 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); |
| 4519 | while (VarTemplatePartialSpecializationDecl *From = |
| 4520 | Tmpl->getInstantiatedFromMember()) { |
| 4521 | if (Tmpl->isMemberSpecialization()) |
| 4522 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4523 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4524 | Tmpl = From; |
| 4525 | } |
| 4526 | PatternDecl = Tmpl; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4527 | } else { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4528 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); |
| 4529 | while (VarTemplateDecl *From = |
| 4530 | Tmpl->getInstantiatedFromMemberTemplate()) { |
| 4531 | if (Tmpl->isMemberSpecialization()) |
| 4532 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4533 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4534 | Tmpl = From; |
| 4535 | } |
| 4536 | PatternDecl = Tmpl->getTemplatedDecl(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4537 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4538 | |
| 4539 | // If this is a static data member template, there might be an |
| 4540 | // uninstantiated initializer on the declaration. If so, instantiate |
| 4541 | // it now. |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4542 | // |
| 4543 | // FIXME: This largely duplicates what we would do below. The difference |
| 4544 | // is that along this path we may instantiate an initializer from an |
| 4545 | // in-class declaration of the template and instantiate the definition |
| 4546 | // from a separate out-of-class definition. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4547 | if (PatternDecl->isStaticDataMember() && |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 4548 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4549 | !Var->hasInit()) { |
| 4550 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 4551 | // code here. |
| 4552 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4553 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4554 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4555 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4556 | "instantiating variable initializer"); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4557 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4558 | // The instantiation is visible here, even if it was first declared in an |
| 4559 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4560 | Var->setVisibleDespiteOwningModule(); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4561 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4562 | // If we're performing recursive template instantiation, create our own |
| 4563 | // queue of pending implicit instantiations that we will instantiate |
| 4564 | // later, while we're still within our own instantiation context. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4565 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4566 | /*Enabled=*/Recursive); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4567 | LocalInstantiationScope Local(*this); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4568 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4569 | |
| 4570 | // Enter the scope of this instantiation. We don't use |
| 4571 | // PushDeclContext because we don't have a scope. |
| 4572 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 4573 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 4574 | PreviousContext.pop(); |
| 4575 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4576 | // This variable may have local implicit instantiations that need to be |
| 4577 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4578 | LocalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4579 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4580 | GlobalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | // Find actual definition |
| 4584 | Def = PatternDecl->getDefinition(getASTContext()); |
| 4585 | } else { |
| 4586 | // If this is a static data member, find its out-of-line definition. |
| 4587 | assert(Var->isStaticDataMember() && "not a static data member?"); |
| 4588 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); |
| 4589 | |
| 4590 | assert(PatternDecl && "data member was not instantiated from a template?"); |
| 4591 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4592 | Def = PatternDecl->getDefinition(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4595 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4596 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4597 | // If we don't have a definition of the variable template, we won't perform |
| 4598 | // any instantiation. Rather, we rely on the user to instantiate this |
| 4599 | // definition (or provide a specialization for it) in another translation |
| 4600 | // unit. |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4601 | if (!Def && !DefinitionRequired) { |
| 4602 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4603 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4604 | std::make_pair(Var, PointOfInstantiation)); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4605 | } else if (TSK == TSK_ImplicitInstantiation) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4606 | // Warn about missing definition at the end of translation unit. |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4607 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4608 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4609 | Diag(PointOfInstantiation, diag::warn_var_template_missing) |
| 4610 | << Var; |
| 4611 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4612 | if (getLangOpts().CPlusPlus11) |
| 4613 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; |
| 4614 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4615 | return; |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4620 | // FIXME: We need to track the instantiation stack in order to know which |
| 4621 | // definitions should be visible within this instantiation. |
| 4622 | // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). |
| 4623 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, |
| 4624 | /*InstantiatedFromMember*/false, |
| 4625 | PatternDecl, Def, TSK, |
| 4626 | /*Complain*/DefinitionRequired)) |
| 4627 | return; |
| 4628 | |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4629 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4630 | // Never instantiate an explicit specialization. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4631 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4632 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4633 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4634 | // C++11 [temp.explicit]p10: |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4635 | // Except for inline functions, const variables of literal types, variables |
| 4636 | // of reference types, [...] explicit instantiation declarations |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4637 | // have the effect of suppressing the implicit instantiation of the entity |
| 4638 | // to which they refer. |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4639 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 4640 | !Var->isUsableInConstantExpressions(getASTContext())) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4641 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4642 | |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4643 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 4644 | struct PassToConsumerRAII { |
| 4645 | ASTConsumer &Consumer; |
| 4646 | VarDecl *Var; |
| 4647 | |
| 4648 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 4649 | : Consumer(Consumer), Var(Var) { } |
| 4650 | |
| 4651 | ~PassToConsumerRAII() { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4652 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4653 | } |
| 4654 | } PassToConsumerRAII(Consumer, Var); |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4655 | |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4656 | // If we already have a definition, we're done. |
| 4657 | if (VarDecl *Def = Var->getDefinition()) { |
| 4658 | // We may be explicitly instantiating something we've already implicitly |
| 4659 | // instantiated. |
| 4660 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 4661 | PointOfInstantiation); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4662 | return; |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4663 | } |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4664 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4665 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4666 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4667 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4668 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4669 | "instantiating variable definition"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4671 | // If we're performing recursive template instantiation, create our own |
| 4672 | // queue of pending implicit instantiations that we will instantiate later, |
| 4673 | // 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); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4677 | // Enter the scope of this instantiation. We don't use |
| 4678 | // PushDeclContext because we don't have a scope. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4679 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4680 | LocalInstantiationScope Local(*this); |
John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 4681 | |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4682 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| 4683 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4684 | VarDecl *OldVar = Var; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4685 | if (Def->isStaticDataMember() && !Def->isOutOfLine()) { |
| 4686 | // We're instantiating an inline static data member whose definition was |
| 4687 | // provided inside the class. |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4688 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4689 | } else if (!VarSpec) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4690 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4691 | TemplateArgs)); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4692 | } else if (Var->isStaticDataMember() && |
| 4693 | Var->getLexicalDeclContext()->isRecord()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4694 | // We need to instantiate the definition of a static data member template, |
| 4695 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 4696 | // declaration of the definition. |
| 4697 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 4698 | TemplateArgs); |
| 4699 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4700 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4701 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 4702 | if (Var) { |
| 4703 | llvm::PointerUnion<VarTemplateDecl *, |
| 4704 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4705 | VarSpec->getSpecializedTemplateOrPartial(); |
| 4706 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 4707 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 4708 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 4709 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 4710 | |
| 4711 | // Merge the definition with the declaration. |
| 4712 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4713 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4714 | R.addDecl(OldVar); |
| 4715 | MergeVarDecl(Var, R); |
| 4716 | |
| 4717 | // Attach the initializer. |
| 4718 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4719 | } |
| 4720 | } else |
| 4721 | // Complete the existing variable's definition with an appropriately |
| 4722 | // substituted type and initializer. |
| 4723 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4724 | |
| 4725 | PreviousContext.pop(); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4726 | |
| 4727 | if (Var) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4728 | PassToConsumerRAII.Var = Var; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4729 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 4730 | OldVar->getPointOfInstantiation()); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4731 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4732 | |
| 4733 | // This variable may have local implicit instantiations that need to be |
| 4734 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4735 | LocalInstantiations.perform(); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4736 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4737 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4738 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4739 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4740 | void |
| 4741 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4742 | const CXXConstructorDecl *Tmpl, |
| 4743 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4744 | |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4745 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4746 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4747 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4748 | // Instantiate all the initializers. |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4749 | for (const auto *Init : Tmpl->inits()) { |
Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4750 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4751 | // ones. |
| 4752 | if (!Init->isWritten()) |
| 4753 | continue; |
| 4754 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4755 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4757 | if (Init->isPackExpansion()) { |
| 4758 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4759 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4760 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4761 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4762 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4763 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4764 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4765 | Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4766 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4767 | BaseTL.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4768 | Unexpanded, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4769 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4770 | RetainExpansion, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4771 | NumExpansions)) { |
| 4772 | AnyErrors = true; |
| 4773 | New->setInvalidDecl(); |
| 4774 | continue; |
| 4775 | } |
| 4776 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4777 | |
| 4778 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4779 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4780 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4781 | |
| 4782 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4783 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4784 | /*CXXDirectInit=*/true); |
| 4785 | if (TempInit.isInvalid()) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4786 | AnyErrors = true; |
| 4787 | break; |
| 4788 | } |
| 4789 | |
| 4790 | // Instantiate the base type. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4791 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4792 | TemplateArgs, |
| 4793 | Init->getSourceLocation(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4794 | New->getDeclName()); |
| 4795 | if (!BaseTInfo) { |
| 4796 | AnyErrors = true; |
| 4797 | break; |
| 4798 | } |
| 4799 | |
| 4800 | // Build the initializer. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4801 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4802 | BaseTInfo, TempInit.get(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4803 | New->getParent(), |
| 4804 | SourceLocation()); |
| 4805 | if (NewInit.isInvalid()) { |
| 4806 | AnyErrors = true; |
| 4807 | break; |
| 4808 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4810 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4811 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4813 | continue; |
| 4814 | } |
| 4815 | |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4816 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4817 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4818 | /*CXXDirectInit=*/true); |
| 4819 | if (TempInit.isInvalid()) { |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4820 | AnyErrors = true; |
| 4821 | continue; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4822 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4823 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4824 | MemInitResult NewInit; |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4825 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4826 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4827 | TemplateArgs, |
| 4828 | Init->getSourceLocation(), |
| 4829 | New->getDeclName()); |
| 4830 | if (!TInfo) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4831 | AnyErrors = true; |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4832 | New->setInvalidDecl(); |
| 4833 | continue; |
| 4834 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4836 | if (Init->isBaseInitializer()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4837 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4838 | New->getParent(), EllipsisLoc); |
| 4839 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4840 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4841 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4842 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4843 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4844 | Init->getMemberLocation(), |
| 4845 | Init->getMember(), |
| 4846 | TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4847 | if (!Member) { |
| 4848 | AnyErrors = true; |
| 4849 | New->setInvalidDecl(); |
| 4850 | continue; |
| 4851 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4852 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4853 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4854 | Init->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4855 | } else if (Init->isIndirectMemberInitializer()) { |
| 4856 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4857 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4858 | Init->getMemberLocation(), |
| 4859 | Init->getIndirectMember(), TemplateArgs)); |
| 4860 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4861 | if (!IndirectMember) { |
| 4862 | AnyErrors = true; |
| 4863 | New->setInvalidDecl(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4864 | continue; |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4865 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4866 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4867 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4868 | Init->getSourceLocation()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4871 | if (NewInit.isInvalid()) { |
| 4872 | AnyErrors = true; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4873 | New->setInvalidDecl(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4874 | } else { |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4875 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4876 | } |
| 4877 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4878 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4879 | // Assign all the initializers to the new constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4880 | ActOnMemInitializers(New, |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4881 | /*FIXME: ColonLoc */ |
| 4882 | SourceLocation(), |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4883 | NewInits, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4884 | AnyErrors); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4887 | // TODO: this could be templated if the various decl types used the |
| 4888 | // same method name. |
| 4889 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4890 | ClassTemplateDecl *Instance) { |
| 4891 | Pattern = Pattern->getCanonicalDecl(); |
| 4892 | |
| 4893 | do { |
| 4894 | Instance = Instance->getCanonicalDecl(); |
| 4895 | if (Pattern == Instance) return true; |
| 4896 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4897 | } while (Instance); |
| 4898 | |
| 4899 | return false; |
| 4900 | } |
| 4901 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4902 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 4903 | FunctionTemplateDecl *Instance) { |
| 4904 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4906 | do { |
| 4907 | Instance = Instance->getCanonicalDecl(); |
| 4908 | if (Pattern == Instance) return true; |
| 4909 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4910 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4912 | return false; |
| 4913 | } |
| 4914 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4915 | static bool |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4916 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 4917 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4918 | Pattern |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4919 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 4920 | do { |
| 4921 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 4922 | Instance->getCanonicalDecl()); |
| 4923 | if (Pattern == Instance) |
| 4924 | return true; |
| 4925 | Instance = Instance->getInstantiatedFromMember(); |
| 4926 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4928 | return false; |
| 4929 | } |
| 4930 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4931 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 4932 | CXXRecordDecl *Instance) { |
| 4933 | Pattern = Pattern->getCanonicalDecl(); |
| 4934 | |
| 4935 | do { |
| 4936 | Instance = Instance->getCanonicalDecl(); |
| 4937 | if (Pattern == Instance) return true; |
| 4938 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 4939 | } while (Instance); |
| 4940 | |
| 4941 | return false; |
| 4942 | } |
| 4943 | |
| 4944 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 4945 | FunctionDecl *Instance) { |
| 4946 | Pattern = Pattern->getCanonicalDecl(); |
| 4947 | |
| 4948 | do { |
| 4949 | Instance = Instance->getCanonicalDecl(); |
| 4950 | if (Pattern == Instance) return true; |
| 4951 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 4952 | } while (Instance); |
| 4953 | |
| 4954 | return false; |
| 4955 | } |
| 4956 | |
| 4957 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 4958 | EnumDecl *Instance) { |
| 4959 | Pattern = Pattern->getCanonicalDecl(); |
| 4960 | |
| 4961 | do { |
| 4962 | Instance = Instance->getCanonicalDecl(); |
| 4963 | if (Pattern == Instance) return true; |
| 4964 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 4965 | } while (Instance); |
| 4966 | |
| 4967 | return false; |
| 4968 | } |
| 4969 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4970 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 4971 | UsingShadowDecl *Instance, |
| 4972 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4973 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 4974 | Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4975 | } |
| 4976 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4977 | static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4978 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4979 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4982 | template<typename T> |
| 4983 | static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, |
| 4984 | ASTContext &Ctx) { |
| 4985 | // An unresolved using declaration can instantiate to an unresolved using |
| 4986 | // declaration, or to a using declaration or a using declaration pack. |
| 4987 | // |
| 4988 | // Multiple declarations can claim to be instantiated from an unresolved |
| 4989 | // using declaration if it's a pack expansion. We want the UsingPackDecl |
| 4990 | // in that case, not the individual UsingDecls within the pack. |
| 4991 | bool OtherIsPackExpansion; |
| 4992 | NamedDecl *OtherFrom; |
| 4993 | if (auto *OtherUUD = dyn_cast<T>(Other)) { |
| 4994 | OtherIsPackExpansion = OtherUUD->isPackExpansion(); |
| 4995 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); |
| 4996 | } else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { |
| 4997 | OtherIsPackExpansion = true; |
| 4998 | OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); |
| 4999 | } else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { |
| 5000 | OtherIsPackExpansion = false; |
| 5001 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); |
| 5002 | } else { |
| 5003 | return false; |
| 5004 | } |
| 5005 | return Pattern->isPackExpansion() == OtherIsPackExpansion && |
| 5006 | declaresSameEntity(OtherFrom, Pattern); |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5009 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 5010 | VarDecl *Instance) { |
| 5011 | assert(Instance->isStaticDataMember()); |
| 5012 | |
| 5013 | Pattern = Pattern->getCanonicalDecl(); |
| 5014 | |
| 5015 | do { |
| 5016 | Instance = Instance->getCanonicalDecl(); |
| 5017 | if (Pattern == Instance) return true; |
| 5018 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 5019 | } while (Instance); |
| 5020 | |
| 5021 | return false; |
| 5022 | } |
| 5023 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5024 | // Other is the prospective instantiation |
| 5025 | // D is the prospective pattern |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5026 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5027 | if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) |
| 5028 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5029 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5030 | if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) |
| 5031 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5032 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5033 | if (D->getKind() != Other->getKind()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 5034 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5035 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5036 | if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5037 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5038 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5039 | if (auto *Function = dyn_cast<FunctionDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5040 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5041 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5042 | if (auto *Enum = dyn_cast<EnumDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5043 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5044 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5045 | if (auto *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5046 | if (Var->isStaticDataMember()) |
| 5047 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 5048 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5049 | if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5050 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 5051 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5052 | if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 5053 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 5054 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5055 | if (auto *PartialSpec = |
| 5056 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5057 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 5058 | PartialSpec); |
| 5059 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5060 | if (auto *Field = dyn_cast<FieldDecl>(Other)) { |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 5061 | if (!Field->getDeclName()) { |
| 5062 | // This is an unnamed field. |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 5063 | return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 5064 | cast<FieldDecl>(D)); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 5065 | } |
| 5066 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5068 | if (auto *Using = dyn_cast<UsingDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5069 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 5070 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5071 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5072 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 5073 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5074 | return D->getDeclName() && |
| 5075 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | template<typename ForwardIterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5079 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5080 | NamedDecl *D, |
| 5081 | ForwardIterator first, |
| 5082 | ForwardIterator last) { |
| 5083 | for (; first != last; ++first) |
| 5084 | if (isInstantiationOf(Ctx, D, *first)) |
| 5085 | return cast<NamedDecl>(*first); |
| 5086 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5087 | return nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5090 | /// Finds the instantiation of the given declaration context |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5091 | /// within the current instantiation. |
| 5092 | /// |
| 5093 | /// \returns NULL if there was an error |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5094 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5095 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5096 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5097 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs, true); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5098 | return cast_or_null<DeclContext>(ID); |
| 5099 | } else return DC; |
| 5100 | } |
| 5101 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5102 | /// Find the instantiation of the given declaration within the |
Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 5103 | /// current instantiation. |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5104 | /// |
| 5105 | /// This routine is intended to be used when \p D is a declaration |
| 5106 | /// referenced from within a template, that needs to mapped into the |
| 5107 | /// corresponding declaration within an instantiation. For example, |
| 5108 | /// given: |
| 5109 | /// |
| 5110 | /// \code |
| 5111 | /// template<typename T> |
| 5112 | /// struct X { |
| 5113 | /// enum Kind { |
| 5114 | /// KnownValue = sizeof(T) |
| 5115 | /// }; |
| 5116 | /// |
| 5117 | /// bool getKind() const { return KnownValue; } |
| 5118 | /// }; |
| 5119 | /// |
| 5120 | /// template struct X<int>; |
| 5121 | /// \endcode |
| 5122 | /// |
Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 5123 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the |
| 5124 | /// \p EnumConstantDecl for \p KnownValue (which refers to |
| 5125 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation |
| 5126 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs |
| 5127 | /// this mapping from within the instantiation of <tt>X<int></tt>. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5128 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5129 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 5130 | bool FindingInstantiatedContext) { |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5131 | DeclContext *ParentDC = D->getDeclContext(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5132 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5133 | // parameters (p below) can have their ParentDC set to the translation-unit |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5134 | // - 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] | 5135 | // is Dependent or/and a FunctionOrMethod. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5136 | // For e.g. this code, during Template argument deduction tries to |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5137 | // 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] | 5138 | // the translation unit. |
| 5139 | // 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] | 5140 | // float baz(float(*)()) { return 0.0; } |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5141 | // Foo(baz); |
| 5142 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 5143 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 5144 | // For now: |
| 5145 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 5146 | // whose type is not instantiation dependent, do nothing to the decl |
| 5147 | // - otherwise find its instantiated decl. |
| 5148 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 5149 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 5150 | return D; |
Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 5151 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 5152 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 5153 | ((ParentDC->isFunctionOrMethod() || |
Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 5154 | isa<OMPDeclareReductionDecl>(ParentDC) || |
| 5155 | isa<OMPDeclareMapperDecl>(ParentDC)) && |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 5156 | ParentDC->isDependentContext()) || |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 5157 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5158 | // D is a local of some kind. Look into the map of local |
| 5159 | // declarations to their instantiations. |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5160 | if (CurrentInstantiationScope) { |
| 5161 | if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 5162 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 5163 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5164 | |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5165 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 5166 | assert(PackIdx != -1 && |
| 5167 | "found declaration pack but not pack expanding"); |
| 5168 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 5169 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 5170 | } |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 5173 | // If we're performing a partial substitution during template argument |
| 5174 | // deduction, we may not have values for template parameters yet. They |
| 5175 | // just map to themselves. |
| 5176 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 5177 | isa<TemplateTemplateParmDecl>(D)) |
| 5178 | return D; |
| 5179 | |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5180 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5181 | return nullptr; |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5182 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 5183 | // Normally this function only searches for already instantiated declaration |
| 5184 | // however we have to make an exclusion for local types used before |
| 5185 | // definition as in the code: |
| 5186 | // |
| 5187 | // template<typename T> void f1() { |
| 5188 | // void g1(struct x1); |
| 5189 | // struct x1 {}; |
| 5190 | // } |
| 5191 | // |
| 5192 | // In this case instantiation of the type of 'g1' requires definition of |
| 5193 | // 'x1', which is defined later. Error recovery may produce an enum used |
| 5194 | // before definition. In these cases we need to instantiate relevant |
| 5195 | // declarations here. |
| 5196 | bool NeedInstantiate = false; |
| 5197 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5198 | NeedInstantiate = RD->isLocalClass(); |
| 5199 | else |
| 5200 | NeedInstantiate = isa<EnumDecl>(D); |
| 5201 | if (NeedInstantiate) { |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 5202 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5203 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5204 | return cast<TypeDecl>(Inst); |
| 5205 | } |
| 5206 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5207 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 5208 | // been found yet. Lazily instantiate it and return it now. |
| 5209 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5210 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5211 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5212 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5213 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5214 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5215 | return cast<LabelDecl>(Inst); |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5216 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5217 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5218 | // For variable template specializations, update those that are still |
| 5219 | // type-dependent. |
| 5220 | if (VarTemplateSpecializationDecl *VarSpec = |
| 5221 | dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 5222 | bool InstantiationDependent = false; |
| 5223 | const TemplateArgumentListInfo &VarTemplateArgs = |
| 5224 | VarSpec->getTemplateArgsInfo(); |
| 5225 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
| 5226 | VarTemplateArgs, InstantiationDependent)) |
| 5227 | D = cast<NamedDecl>( |
| 5228 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); |
| 5229 | return D; |
| 5230 | } |
| 5231 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5232 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 5233 | if (!Record->isDependentContext()) |
| 5234 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5235 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5236 | // Determine whether this record is the "templated" declaration describing |
| 5237 | // a class template or class template partial specialization. |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5238 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5239 | if (ClassTemplate) |
| 5240 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 5241 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5242 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 5243 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5245 | // Walk the current context to find either the record or an instantiation of |
| 5246 | // it. |
| 5247 | DeclContext *DC = CurContext; |
| 5248 | while (!DC->isFileContext()) { |
| 5249 | // If we're performing substitution while we're inside the template |
| 5250 | // definition, we'll find our own context. We're done. |
| 5251 | if (DC->Equals(Record)) |
| 5252 | return Record; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5253 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5254 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 5255 | // Check whether we're in the process of instantiating a class template |
| 5256 | // specialization of the template we're mapping. |
| 5257 | if (ClassTemplateSpecializationDecl *InstSpec |
| 5258 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 5259 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 5260 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 5261 | return InstRecord; |
| 5262 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5263 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5264 | // Check whether we're in the process of instantiating a member class. |
| 5265 | if (isInstantiationOf(Record, InstRecord)) |
| 5266 | return InstRecord; |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5267 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5268 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5269 | // Move to the outer template scope. |
| 5270 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 5271 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 5272 | DC = FD->getLexicalDeclContext(); |
| 5273 | continue; |
| 5274 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5275 | // An implicit deduction guide acts as if it's within the class template |
| 5276 | // specialization described by its name and first N template params. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5277 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FD); |
| 5278 | if (Guide && Guide->isImplicit()) { |
| 5279 | TemplateDecl *TD = Guide->getDeducedTemplate(); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5280 | // Convert the arguments to an "as-written" list. |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5281 | TemplateArgumentListInfo Args(Loc, Loc); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5282 | for (TemplateArgument Arg : TemplateArgs.getInnermost().take_front( |
| 5283 | TD->getTemplateParameters()->size())) { |
| 5284 | ArrayRef<TemplateArgument> Unpacked(Arg); |
| 5285 | if (Arg.getKind() == TemplateArgument::Pack) |
| 5286 | Unpacked = Arg.pack_elements(); |
| 5287 | for (TemplateArgument UnpackedArg : Unpacked) |
| 5288 | Args.addArgument( |
| 5289 | getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); |
| 5290 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5291 | QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); |
| 5292 | if (T.isNull()) |
| 5293 | return nullptr; |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5294 | auto *SubstRecord = T->getAsCXXRecordDecl(); |
| 5295 | assert(SubstRecord && "class template id not a class type?"); |
| 5296 | // Check that this template-id names the primary template and not a |
| 5297 | // partial or explicit specialization. (In the latter cases, it's |
| 5298 | // meaningless to attempt to find an instantiation of D within the |
| 5299 | // specialization.) |
| 5300 | // FIXME: The standard doesn't say what should happen here. |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5301 | if (FindingInstantiatedContext && |
| 5302 | usesPartialOrExplicitSpecialization( |
| 5303 | Loc, cast<ClassTemplateSpecializationDecl>(SubstRecord))) { |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5304 | Diag(Loc, diag::err_specialization_not_primary_template) |
| 5305 | << T << (SubstRecord->getTemplateSpecializationKind() == |
| 5306 | TSK_ExplicitSpecialization); |
| 5307 | return nullptr; |
| 5308 | } |
| 5309 | DC = SubstRecord; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5310 | continue; |
| 5311 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5312 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5314 | DC = DC->getParent(); |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5315 | } |
Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 5316 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5317 | // Fall through to deal with other dependent record types (e.g., |
| 5318 | // anonymous unions in class templates). |
| 5319 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5320 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5321 | if (!ParentDC->isDependentContext()) |
| 5322 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5324 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5325 | if (!ParentDC) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5326 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5328 | if (ParentDC != D->getDeclContext()) { |
| 5329 | // We performed some kind of instantiation in the parent context, |
| 5330 | // so now we need to look into the instantiated parent context to |
| 5331 | // find the instantiation of the declaration D. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5332 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5333 | // If our context used to be dependent, we may need to instantiate |
| 5334 | // it before performing lookup into that context. |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5335 | bool IsBeingInstantiated = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5336 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5337 | if (!Spec->isDependentContext()) { |
| 5338 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5339 | const RecordType *Tag = T->getAs<RecordType>(); |
| 5340 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5341 | if (Tag->isBeingDefined()) |
| 5342 | IsBeingInstantiated = true; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5343 | if (!Tag->isBeingDefined() && |
| 5344 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5345 | return nullptr; |
Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 5346 | |
| 5347 | ParentDC = Tag->getDecl(); |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5348 | } |
| 5349 | } |
| 5350 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5351 | NamedDecl *Result = nullptr; |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5352 | // FIXME: If the name is a dependent name, this lookup won't necessarily |
| 5353 | // find it. Does that ever matter? |
Akira Hatanaka | 59e3b43 | 2017-01-31 19:53:32 +0000 | [diff] [blame] | 5354 | if (auto Name = D->getDeclName()) { |
| 5355 | DeclarationNameInfo NameInfo(Name, D->getLocation()); |
| 5356 | Name = SubstDeclarationNameInfo(NameInfo, TemplateArgs).getName(); |
| 5357 | if (!Name) |
| 5358 | return nullptr; |
| 5359 | DeclContext::lookup_result Found = ParentDC->lookup(Name); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5360 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5361 | } else { |
| 5362 | // Since we don't have a name for the entity we're looking for, |
| 5363 | // our only option is to walk through all of the declarations to |
| 5364 | // find that name. This will occur in a few cases: |
| 5365 | // |
| 5366 | // - anonymous struct/union within a template |
| 5367 | // - unnamed class/struct/union/enum within a template |
| 5368 | // |
| 5369 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5371 | ParentDC->decls_begin(), |
| 5372 | ParentDC->decls_end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5373 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5374 | |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5375 | if (!Result) { |
| 5376 | if (isa<UsingShadowDecl>(D)) { |
| 5377 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 5378 | } else if (Diags.hasErrorOccurred()) { |
| 5379 | // We've already complained about something, so most likely this |
| 5380 | // declaration failed to instantiate. There's no point in complaining |
| 5381 | // further, since this is normal in invalid code. |
| 5382 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5383 | // The class in which this member exists is currently being |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5384 | // instantiated, and we haven't gotten around to instantiating this |
| 5385 | // member yet. This can happen when the code uses forward declarations |
| 5386 | // of member classes, and introduces ordering dependencies via |
| 5387 | // template instantiation. |
| 5388 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 5389 | << D->getDeclName() |
| 5390 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 5391 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 5392 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 5393 | // This enumeration constant was found when the template was defined, |
| 5394 | // but can't be found in the instantiation. This can happen if an |
| 5395 | // unscoped enumeration member is explicitly specialized. |
| 5396 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 5397 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 5398 | TemplateArgs)); |
| 5399 | assert(Spec->getTemplateSpecializationKind() == |
| 5400 | TSK_ExplicitSpecialization); |
| 5401 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 5402 | << D->getDeclName() |
| 5403 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 5404 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 5405 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5406 | } else { |
| 5407 | // We should have found something, but didn't. |
| 5408 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 5409 | } |
| 5410 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5411 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5412 | D = Result; |
| 5413 | } |
| 5414 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5415 | return D; |
| 5416 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5417 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5418 | /// Performs template instantiation for all implicit template |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5419 | /// instantiations we have seen until this point. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 5420 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5421 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5422 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5423 | PendingImplicitInstantiation Inst; |
| 5424 | |
| 5425 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5426 | Inst = PendingInstantiations.front(); |
| 5427 | PendingInstantiations.pop_front(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5428 | } else { |
| 5429 | Inst = PendingLocalImplicitInstantiations.front(); |
| 5430 | PendingLocalImplicitInstantiations.pop_front(); |
| 5431 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5433 | // Instantiate function definitions |
| 5434 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5435 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 5436 | TSK_ExplicitInstantiationDefinition; |
Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 5437 | if (Function->isMultiVersion()) { |
| 5438 | getASTContext().forEachMultiversionedFunctionVersion( |
| 5439 | Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) { |
| 5440 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true, |
| 5441 | DefinitionRequired, true); |
| 5442 | if (CurFD->isDefined()) |
| 5443 | CurFD->setInstantiationIsPending(false); |
| 5444 | }); |
| 5445 | } else { |
| 5446 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true, |
| 5447 | DefinitionRequired, true); |
| 5448 | if (Function->isDefined()) |
| 5449 | Function->setInstantiationIsPending(false); |
| 5450 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5451 | continue; |
| 5452 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5453 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5454 | // Instantiate variable definitions |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5455 | VarDecl *Var = cast<VarDecl>(Inst.first); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5456 | |
| 5457 | assert((Var->isStaticDataMember() || |
| 5458 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 5459 | "Not a static data member, nor a variable template" |
| 5460 | " specialization?"); |
Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 5461 | |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5462 | // Don't try to instantiate declarations if the most recent redeclaration |
| 5463 | // is invalid. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5464 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5465 | continue; |
| 5466 | |
| 5467 | // Check if the most recent declaration has changed the specialization kind |
| 5468 | // and removed the need for implicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5469 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5470 | case TSK_Undeclared: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5471 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5472 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5473 | case TSK_ExplicitSpecialization: |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5474 | continue; // No longer need to instantiate this type. |
| 5475 | case TSK_ExplicitInstantiationDefinition: |
| 5476 | // We only need an instantiation if the pending instantiation *is* the |
| 5477 | // explicit instantiation. |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5478 | if (Var != Var->getMostRecentDecl()) |
| 5479 | continue; |
| 5480 | break; |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5481 | case TSK_ImplicitInstantiation: |
| 5482 | break; |
| 5483 | } |
| 5484 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 5485 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5486 | "instantiating variable definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5487 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 5488 | TSK_ExplicitInstantiationDefinition; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5489 | |
| 5490 | // Instantiate static data member definitions or variable template |
| 5491 | // specializations. |
| 5492 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 5493 | DefinitionRequired, true); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5494 | } |
| 5495 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5496 | |
| 5497 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 5498 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 5499 | for (auto DD : Pattern->ddiags()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5500 | switch (DD->getKind()) { |
| 5501 | case DependentDiagnostic::Access: |
| 5502 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 5503 | break; |
| 5504 | } |
| 5505 | } |
| 5506 | } |