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 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 347 | void Sema::InstantiateAttrsForDecl( |
| 348 | const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, |
| 349 | Decl *New, LateInstantiatedAttrVec *LateAttrs, |
| 350 | LocalInstantiationScope *OuterMostScope) { |
| 351 | if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { |
| 352 | for (const auto *TmplAttr : Tmpl->attrs()) { |
| 353 | // FIXME: If any of the special case versions from InstantiateAttrs become |
| 354 | // applicable to template declaration, we'll need to add them here. |
| 355 | CXXThisScopeRAII ThisScope( |
| 356 | *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 357 | Qualifiers(), ND->isCXXInstanceMember()); |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 358 | |
| 359 | Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( |
| 360 | TmplAttr, Context, *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 361 | if (NewAttr) |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 362 | New->addAttr(NewAttr); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 367 | static Sema::RetainOwnershipKind |
| 368 | attrToRetainOwnershipKind(const Attr *A) { |
| 369 | switch (A->getKind()) { |
| 370 | case clang::attr::CFConsumed: |
| 371 | return Sema::RetainOwnershipKind::CF; |
| 372 | case clang::attr::OSConsumed: |
| 373 | return Sema::RetainOwnershipKind::OS; |
| 374 | case clang::attr::NSConsumed: |
| 375 | return Sema::RetainOwnershipKind::NS; |
| 376 | default: |
| 377 | llvm_unreachable("Wrong argument supplied"); |
| 378 | } |
| 379 | } |
| 380 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 381 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 382 | const Decl *Tmpl, Decl *New, |
| 383 | LateInstantiatedAttrVec *LateAttrs, |
| 384 | LocalInstantiationScope *OuterMostScope) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 385 | for (const auto *TmplAttr : Tmpl->attrs()) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 386 | // FIXME: This should be generalized to more than just the AlignedAttr. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 387 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 388 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 389 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 390 | continue; |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 393 | const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr); |
| 394 | if (AssumeAligned) { |
| 395 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 396 | continue; |
| 397 | } |
| 398 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 399 | const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr); |
| 400 | if (AlignValue) { |
| 401 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 402 | continue; |
| 403 | } |
| 404 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 405 | if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { |
| 406 | instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); |
| 407 | continue; |
| 408 | } |
| 409 | |
| 410 | |
George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 411 | if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 412 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 413 | cast<FunctionDecl>(New)); |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { |
| 418 | instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, |
| 419 | cast<FunctionDecl>(New)); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 420 | continue; |
| 421 | } |
| 422 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 423 | if (const CUDALaunchBoundsAttr *CUDALaunchBounds = |
| 424 | dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { |
| 425 | instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, |
| 426 | *CUDALaunchBounds, New); |
| 427 | continue; |
| 428 | } |
| 429 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 430 | if (const ModeAttr *Mode = dyn_cast<ModeAttr>(TmplAttr)) { |
| 431 | instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); |
| 432 | continue; |
| 433 | } |
| 434 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 435 | if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { |
| 436 | instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); |
| 437 | continue; |
| 438 | } |
| 439 | |
Hans Wennborg | c2b7f7a | 2014-08-24 00:12:36 +0000 | [diff] [blame] | 440 | // Existing DLL attribute on the instantiation takes precedence. |
| 441 | if (TmplAttr->getKind() == attr::DLLExport || |
| 442 | TmplAttr->getKind() == attr::DLLImport) { |
| 443 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 444 | continue; |
| 445 | } |
| 446 | } |
| 447 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 448 | if (auto ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { |
| 449 | AddParameterABIAttr(ABIAttr->getRange(), New, ABIAttr->getABI(), |
| 450 | ABIAttr->getSpellingListIndex()); |
| 451 | continue; |
| 452 | } |
| 453 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 454 | if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || |
| 455 | isa<CFConsumedAttr>(TmplAttr)) { |
| 456 | AddXConsumedAttr(New, TmplAttr->getRange(), |
| 457 | TmplAttr->getSpellingListIndex(), |
| 458 | attrToRetainOwnershipKind(TmplAttr), |
| 459 | /*template instantiation=*/true); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 460 | continue; |
| 461 | } |
| 462 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 463 | assert(!TmplAttr->isPackExpansion()); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 464 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 465 | // Late parsed attributes must be instantiated and attached after the |
| 466 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 467 | LocalInstantiationScope *Saved = nullptr; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 468 | if (CurrentInstantiationScope) |
| 469 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 470 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 471 | } else { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 472 | // Allow 'this' within late-parsed attributes. |
| 473 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 474 | CXXRecordDecl *ThisContext = |
| 475 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 476 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 477 | ND && ND->isCXXInstanceMember()); |
| 478 | |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 479 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 480 | *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 481 | if (NewAttr) |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 482 | New->addAttr(NewAttr); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 483 | } |
Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 487 | /// Get the previous declaration of a declaration for the purposes of template |
| 488 | /// instantiation. If this finds a previous declaration, then the previous |
| 489 | /// declaration of the instantiation of D should be an instantiation of the |
| 490 | /// result of this function. |
| 491 | template<typename DeclT> |
| 492 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 493 | DeclT *Result = D->getPreviousDecl(); |
| 494 | |
| 495 | // If the declaration is within a class, and the previous declaration was |
| 496 | // merged from a different definition of that class, then we don't have a |
| 497 | // previous declaration for the purpose of template instantiation. |
| 498 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 499 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 500 | return nullptr; |
| 501 | |
| 502 | return Result; |
| 503 | } |
| 504 | |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 505 | Decl * |
| 506 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 507 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | Decl * |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 511 | TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { |
| 512 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 513 | } |
| 514 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 515 | Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( |
| 516 | PragmaDetectMismatchDecl *D) { |
| 517 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 518 | } |
| 519 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 520 | Decl * |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 521 | TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { |
| 522 | llvm_unreachable("extern \"C\" context cannot be instantiated"); |
| 523 | } |
| 524 | |
| 525 | Decl * |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 526 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 527 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 528 | D->getIdentifier()); |
| 529 | Owner->addDecl(Inst); |
| 530 | return Inst; |
| 531 | } |
| 532 | |
| 533 | Decl * |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 534 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 535 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 536 | } |
| 537 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 538 | Decl * |
| 539 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 540 | NamespaceAliasDecl *Inst |
| 541 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 542 | D->getNamespaceLoc(), |
| 543 | D->getAliasLoc(), |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 544 | D->getIdentifier(), |
| 545 | D->getQualifierLoc(), |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 546 | D->getTargetNameLoc(), |
| 547 | D->getNamespace()); |
| 548 | Owner->addDecl(Inst); |
| 549 | return Inst; |
| 550 | } |
| 551 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 552 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 553 | bool IsTypeAlias) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 554 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 555 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 556 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 557 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 558 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 559 | D->getLocation(), D->getDeclName()); |
| 560 | if (!DI) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 561 | Invalid = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 562 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 563 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 564 | } else { |
| 565 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 568 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 569 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 570 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 571 | // semantics. See LWG issue 2141 for more information on the bug. |
| 572 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 573 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 574 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 575 | DT->isReferenceType() && |
| 576 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 577 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 578 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 579 | SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 580 | // Fold it to the (non-reference) type which g++ would have produced. |
| 581 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 582 | DI->getType().getNonReferenceType()); |
| 583 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 584 | // Create the new typedef |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 585 | TypedefNameDecl *Typedef; |
| 586 | if (IsTypeAlias) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 587 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 588 | D->getLocation(), D->getIdentifier(), DI); |
| 589 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 590 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 591 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 592 | if (Invalid) |
| 593 | Typedef->setInvalidDecl(); |
| 594 | |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 595 | // If the old typedef was the name for linkage purposes of an anonymous |
| 596 | // tag decl, re-establish that relationship for the new typedef. |
| 597 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 598 | TagDecl *oldTag = oldTagType->getDecl(); |
Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 599 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 600 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 601 | assert(!newTag->hasNameForLinkage()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 602 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 603 | } |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 604 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 605 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 606 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 607 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 608 | TemplateArgs); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 609 | if (!InstPrev) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 610 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 611 | |
Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 612 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 613 | |
| 614 | // If the typedef types are not identical, reject them. |
| 615 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 616 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 617 | Typedef->setPreviousDecl(InstPrevTypedef); |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 618 | } |
| 619 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 620 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 621 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 622 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 624 | return Typedef; |
| 625 | } |
| 626 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 627 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 628 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 629 | if (Typedef) |
| 630 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 631 | return Typedef; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 635 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 636 | if (Typedef) |
| 637 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 638 | return Typedef; |
| 639 | } |
| 640 | |
| 641 | Decl * |
| 642 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 643 | // Create a local instantiation scope for this type alias template, which |
| 644 | // will contain the instantiations of the template parameters. |
| 645 | LocalInstantiationScope Scope(SemaRef); |
| 646 | |
| 647 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 648 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 649 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 650 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 651 | |
| 652 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 653 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 654 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 655 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 656 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 657 | if (!Found.empty()) { |
| 658 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 663 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 664 | if (!AliasInst) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 665 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 666 | |
| 667 | TypeAliasTemplateDecl *Inst |
| 668 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 669 | D->getDeclName(), InstParams, AliasInst); |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 670 | AliasInst->setDescribedAliasTemplate(Inst); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 671 | if (PrevAliasTemplate) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 672 | Inst->setPreviousDecl(PrevAliasTemplate); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 673 | |
| 674 | Inst->setAccess(D->getAccess()); |
| 675 | |
| 676 | if (!PrevAliasTemplate) |
| 677 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 678 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 679 | Owner->addDecl(Inst); |
| 680 | |
| 681 | return Inst; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 684 | Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 685 | auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 686 | D->getIdentifier()); |
Richard Smith | 81df9eb | 2017-10-02 22:43:36 +0000 | [diff] [blame] | 687 | NewBD->setReferenced(D->isReferenced()); |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 688 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); |
| 689 | return NewBD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 693 | // Transform the bindings first. |
| 694 | SmallVector<BindingDecl*, 16> NewBindings; |
| 695 | for (auto *OldBD : D->bindings()) |
| 696 | NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); |
| 697 | ArrayRef<BindingDecl*> NewBindingArray = NewBindings; |
| 698 | |
| 699 | auto *NewDD = cast_or_null<DecompositionDecl>( |
| 700 | VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); |
| 701 | |
| 702 | if (!NewDD || NewDD->isInvalidDecl()) |
| 703 | for (auto *NewBD : NewBindings) |
| 704 | NewBD->setInvalidDecl(); |
| 705 | |
| 706 | return NewDD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 709 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 710 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 713 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 714 | bool InstantiatingVarTemplate, |
| 715 | ArrayRef<BindingDecl*> *Bindings) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 716 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 717 | // Do substitution on the type of the declaration |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 718 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 719 | D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), |
| 720 | D->getDeclName(), /*AllowDeducedTST*/true); |
John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 721 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 722 | return nullptr; |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 724 | if (DI->getType()->isFunctionType()) { |
| 725 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 726 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 727 | return nullptr; |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 728 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 729 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 730 | DeclContext *DC = Owner; |
| 731 | if (D->isLocalExternDecl()) |
| 732 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 733 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 734 | // Build the instantiated declaration. |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 735 | VarDecl *Var; |
| 736 | if (Bindings) |
| 737 | Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 738 | D->getLocation(), DI->getType(), DI, |
| 739 | D->getStorageClass(), *Bindings); |
| 740 | else |
| 741 | Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 742 | D->getLocation(), D->getIdentifier(), DI->getType(), |
| 743 | DI, D->getStorageClass()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 745 | // In ARC, infer 'retaining' for variables of retainable type. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 746 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 747 | SemaRef.inferObjCARCLifetime(Var)) |
| 748 | Var->setInvalidDecl(); |
| 749 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 750 | // Substitute the nested name specifier, if any. |
| 751 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 752 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 754 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 755 | StartingScope, InstantiatingVarTemplate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 756 | |
| 757 | if (D->isNRVOVariable()) { |
| 758 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
Richard Trieu | 09c163b | 2018-03-15 03:00:55 +0000 | [diff] [blame] | 759 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, Sema::CES_Strict)) |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 760 | Var->setNRVOVariable(true); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 761 | } |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 762 | |
Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 763 | Var->setImplicit(D->isImplicit()); |
| 764 | |
Hans Wennborg | 262baa4 | 2018-10-31 10:34:46 +0000 | [diff] [blame] | 765 | if (Var->isStaticLocal()) |
| 766 | SemaRef.CheckStaticLocalForDllExport(Var); |
| 767 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 768 | return Var; |
| 769 | } |
| 770 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 771 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 772 | AccessSpecDecl* AD |
| 773 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 774 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 775 | Owner->addHiddenDecl(AD); |
| 776 | return AD; |
| 777 | } |
| 778 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 779 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 780 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 781 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 782 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 783 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 784 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 785 | D->getLocation(), D->getDeclName()); |
| 786 | if (!DI) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 787 | DI = D->getTypeSourceInfo(); |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 788 | Invalid = true; |
| 789 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 790 | // C++ [temp.arg.type]p3: |
| 791 | // If a declaration acquires a function type through a type |
| 792 | // dependent on a template-parameter and this causes a |
| 793 | // declaration that does not use the syntactic form of a |
| 794 | // function declarator to have function type, the program is |
| 795 | // ill-formed. |
| 796 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 797 | << DI->getType(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 798 | Invalid = true; |
| 799 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 800 | } else { |
| 801 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | Expr *BitWidth = D->getBitWidth(); |
| 805 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 806 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 807 | else if (BitWidth) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 808 | // The bit-width expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 809 | EnterExpressionEvaluationContext Unevaluated( |
| 810 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 812 | ExprResult InstantiatedBitWidth |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 813 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 814 | if (InstantiatedBitWidth.isInvalid()) { |
| 815 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 816 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 817 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 818 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 819 | } |
| 820 | |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 821 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 822 | DI->getType(), DI, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | cast<RecordDecl>(Owner), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 824 | D->getLocation(), |
| 825 | D->isMutable(), |
| 826 | BitWidth, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 827 | D->getInClassInitStyle(), |
Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 828 | D->getInnerLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 829 | D->getAccess(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 830 | nullptr); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 831 | if (!Field) { |
| 832 | cast<Decl>(Owner)->setInvalidDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 833 | return nullptr; |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 834 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 836 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 837 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 838 | if (Field->hasAttrs()) |
| 839 | SemaRef.CheckAlignasUnderalignment(Field); |
| 840 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 841 | if (Invalid) |
| 842 | Field->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 844 | if (!Field->getDeclName()) { |
| 845 | // Keep track of where this decl came from. |
| 846 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 847 | } |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 848 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 849 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 850 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 851 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 853 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 854 | Field->setImplicit(D->isImplicit()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 855 | Field->setAccess(D->getAccess()); |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 856 | Owner->addDecl(Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 857 | |
| 858 | return Field; |
| 859 | } |
| 860 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 861 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 862 | bool Invalid = false; |
| 863 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 864 | |
| 865 | if (DI->getType()->isVariablyModifiedType()) { |
| 866 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 867 | << D; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 868 | Invalid = true; |
| 869 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 870 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 871 | D->getLocation(), D->getDeclName()); |
| 872 | if (!DI) { |
| 873 | DI = D->getTypeSourceInfo(); |
| 874 | Invalid = true; |
| 875 | } else if (DI->getType()->isFunctionType()) { |
| 876 | // C++ [temp.arg.type]p3: |
| 877 | // If a declaration acquires a function type through a type |
| 878 | // dependent on a template-parameter and this causes a |
| 879 | // declaration that does not use the syntactic form of a |
| 880 | // function declarator to have function type, the program is |
| 881 | // ill-formed. |
| 882 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 883 | << DI->getType(); |
| 884 | Invalid = true; |
| 885 | } |
| 886 | } else { |
| 887 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 888 | } |
| 889 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 890 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 891 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 892 | DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 893 | |
| 894 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 895 | StartingScope); |
| 896 | |
| 897 | if (Invalid) |
| 898 | Property->setInvalidDecl(); |
| 899 | |
| 900 | Property->setAccess(D->getAccess()); |
| 901 | Owner->addDecl(Property); |
| 902 | |
| 903 | return Property; |
| 904 | } |
| 905 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 906 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 907 | NamedDecl **NamedChain = |
| 908 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 909 | |
| 910 | int i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 911 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 912 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 913 | TemplateArgs); |
| 914 | if (!Next) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 915 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 917 | NamedChain[i++] = Next; |
| 918 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 919 | |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 920 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 921 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 922 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 923 | {NamedChain, D->getChainingSize()}); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 924 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 925 | for (const auto *Attr : D->attrs()) |
| 926 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 927 | |
| 928 | IndirectField->setImplicit(D->isImplicit()); |
| 929 | IndirectField->setAccess(D->getAccess()); |
| 930 | Owner->addDecl(IndirectField); |
| 931 | return IndirectField; |
| 932 | } |
| 933 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 934 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 935 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 936 | // parameters into the pattern type and checking the result. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 937 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 938 | TypeSourceInfo *InstTy; |
| 939 | // If this is an unsupported friend, don't bother substituting template |
| 940 | // arguments into it. The actual type referred to won't be used by any |
| 941 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 942 | // same info for the instantiated friend. |
| 943 | if (D->isUnsupportedFriend()) { |
| 944 | InstTy = Ty; |
| 945 | } else { |
| 946 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 947 | D->getLocation(), DeclarationName()); |
| 948 | } |
| 949 | if (!InstTy) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 950 | return nullptr; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 951 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 952 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 953 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 954 | if (!FD) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 955 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 957 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 958 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 959 | Owner->addDecl(FD); |
| 960 | return FD; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 963 | NamedDecl *ND = D->getFriendDecl(); |
| 964 | assert(ND && "friend decl must be a decl or a type!"); |
| 965 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 966 | // All of the Visit implementations for the various potential friend |
| 967 | // declarations have to be carefully written to work for friend |
| 968 | // objects, with the most important detail being that the target |
| 969 | // decl should almost certainly not be placed in Owner. |
| 970 | Decl *NewND = Visit(ND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 971 | if (!NewND) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 973 | FriendDecl *FD = |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 974 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 975 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 976 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 977 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 978 | Owner->addDecl(FD); |
| 979 | return FD; |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 982 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 983 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 985 | // The expression in a static assertion is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 986 | EnterExpressionEvaluationContext Unevaluated( |
| 987 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 989 | ExprResult InstantiatedAssertExpr |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 990 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 991 | if (InstantiatedAssertExpr.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 992 | return nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 993 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 994 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 995 | InstantiatedAssertExpr.get(), |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 996 | D->getMessage(), |
| 997 | D->getRParenLoc(), |
| 998 | D->isFailed()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1002 | EnumDecl *PrevDecl = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1003 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1004 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1005 | PatternPrev, |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1006 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1007 | if (!Prev) return nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1008 | PrevDecl = cast<EnumDecl>(Prev); |
| 1009 | } |
| 1010 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1011 | EnumDecl *Enum = |
| 1012 | EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 1013 | D->getLocation(), D->getIdentifier(), PrevDecl, |
| 1014 | D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1015 | if (D->isFixed()) { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1016 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1017 | // If we have type source information for the underlying type, it means it |
| 1018 | // has been explicitly set by the user. Perform substitution on it before |
| 1019 | // moving on. |
| 1020 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1021 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 1022 | DeclarationName()); |
| 1023 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1024 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1025 | else |
| 1026 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 1027 | } else { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1028 | assert(!D->getIntegerType()->isDependentType() |
| 1029 | && "Dependent type without type source info"); |
| 1030 | Enum->setIntegerType(D->getIntegerType()); |
| 1031 | } |
| 1032 | } |
| 1033 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1034 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 1035 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1036 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 1037 | Enum->setAccess(D->getAccess()); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1038 | // Forward the mangling number from the template to the instantiated decl. |
| 1039 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1040 | // See if the old tag was defined along with a declarator. |
| 1041 | // If it did, mark the new tag as being associated with that declarator. |
| 1042 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1043 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); |
| 1044 | // See if the old tag was defined along with a typedef. |
| 1045 | // If it did, mark the new tag as being associated with that typedef. |
| 1046 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1047 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1048 | if (SubstQualifier(D, Enum)) return nullptr; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1049 | Owner->addDecl(Enum); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1050 | |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1051 | EnumDecl *Def = D->getDefinition(); |
| 1052 | if (Def && Def != D) { |
| 1053 | // If this is an out-of-line definition of an enum member template, check |
| 1054 | // that the underlying types match in the instantiation of both |
| 1055 | // declarations. |
| 1056 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 1057 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1058 | QualType DefnUnderlying = |
| 1059 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 1060 | UnderlyingLoc, DeclarationName()); |
| 1061 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 1062 | DefnUnderlying, /*IsFixed=*/true, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1065 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1066 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 1067 | // specialization causes the implicit instantiation of the declarations, but |
| 1068 | // not the definitions of scoped member enumerations. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1069 | // |
| 1070 | // DR1484 clarifies that enumeration definitions inside of a template |
| 1071 | // declaration aren't considered entities that can be separately instantiated |
| 1072 | // from the rest of the entity they are declared inside of. |
| 1073 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 1074 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1075 | InstantiateEnumDefinition(Enum, Def); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1076 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1077 | |
| 1078 | return Enum; |
| 1079 | } |
| 1080 | |
| 1081 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 1082 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 1083 | Enum->startDefinition(); |
| 1084 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 1085 | // Update the location to refer to the definition. |
| 1086 | Enum->setLocation(Pattern->getLocation()); |
| 1087 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1088 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1089 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1090 | EnumConstantDecl *LastEnumConst = nullptr; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1091 | for (auto *EC : Pattern->enumerators()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1092 | // The specified value for the enumerator. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1093 | ExprResult Value((Expr *)nullptr); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1094 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1095 | // The enumerator's value expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1096 | EnterExpressionEvaluationContext Unevaluated( |
| 1097 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1099 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1100 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1101 | |
| 1102 | // Drop the initial value and continue. |
| 1103 | bool isInvalid = false; |
| 1104 | if (Value.isInvalid()) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1105 | Value = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1106 | isInvalid = true; |
| 1107 | } |
| 1108 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | EnumConstantDecl *EnumConst |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1110 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 1111 | EC->getLocation(), EC->getIdentifier(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1112 | Value.get()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1113 | |
| 1114 | if (isInvalid) { |
| 1115 | if (EnumConst) |
| 1116 | EnumConst->setInvalidDecl(); |
| 1117 | Enum->setInvalidDecl(); |
| 1118 | } |
| 1119 | |
| 1120 | if (EnumConst) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1121 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1122 | |
John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 1123 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1124 | Enum->addDecl(EnumConst); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1125 | Enumerators.push_back(EnumConst); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1126 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1127 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1128 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 1129 | !Enum->isScoped()) { |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1130 | // If the enumeration is within a function or method, record the enum |
| 1131 | // constant as a local. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1132 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1133 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 1137 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1138 | Enumerators, nullptr, ParsedAttributesView()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1141 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1142 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1145 | Decl * |
| 1146 | TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 1147 | llvm_unreachable("BuiltinTemplateDecls cannot be instantiated."); |
| 1148 | } |
| 1149 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1150 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1151 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1152 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1153 | // Create a local instantiation scope for this class template, which |
| 1154 | // will contain the instantiations of the template parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1155 | LocalInstantiationScope Scope(SemaRef); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1156 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1157 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1159 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1160 | |
| 1161 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Instantiate the qualifier. We have to do this first in case |
| 1164 | // we're a friend declaration, because if we are then we need to put |
| 1165 | // the new declaration in the appropriate context. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1166 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 1167 | if (QualifierLoc) { |
| 1168 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1169 | TemplateArgs); |
| 1170 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1171 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1174 | CXXRecordDecl *PrevDecl = nullptr; |
| 1175 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1176 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1177 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1178 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1179 | if (!Found.empty()) { |
| 1180 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1181 | if (PrevClassTemplate) |
| 1182 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1183 | } |
| 1184 | } |
| 1185 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1186 | // If this isn't a friend, then it's a member template, in which |
| 1187 | // case we just want to build the instantiation in the |
| 1188 | // specialization. If it is a friend, we want to build it in |
| 1189 | // the appropriate context. |
| 1190 | DeclContext *DC = Owner; |
| 1191 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1192 | if (QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1193 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1194 | SS.Adopt(QualifierLoc); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1195 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1196 | if (!DC) return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1197 | } else { |
| 1198 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 1199 | Pattern->getDeclContext(), |
| 1200 | TemplateArgs); |
| 1201 | } |
| 1202 | |
| 1203 | // Look for a previous declaration of the template in the owning |
| 1204 | // context. |
| 1205 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1206 | Sema::LookupOrdinaryName, |
| 1207 | SemaRef.forRedeclarationInCurContext()); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1208 | SemaRef.LookupQualifiedName(R, DC); |
| 1209 | |
| 1210 | if (R.isSingleResult()) { |
| 1211 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 1212 | if (PrevClassTemplate) |
| 1213 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1214 | } |
| 1215 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1216 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1217 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 1218 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1219 | << QualifierLoc.getSourceRange(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1220 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1223 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1224 | if (PrevClassTemplate) { |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1225 | bool Complain = true; |
| 1226 | |
| 1227 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 1228 | // template for struct std::tr1::__detail::_Map_base, where the |
| 1229 | // template parameters of the friend declaration don't match the |
| 1230 | // template parameters of the original declaration. In this one |
| 1231 | // case, we don't complain about the ill-formed friend |
| 1232 | // declaration. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1233 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1234 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 1235 | DC->isNamespace() && |
| 1236 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1237 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 1238 | DeclContext *DCParent = DC->getParent(); |
| 1239 | if (DCParent->isNamespace() && |
| 1240 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 1241 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 1242 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1243 | Complain = false; |
| 1244 | } |
| 1245 | } |
| 1246 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1247 | TemplateParameterList *PrevParams |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1248 | = PrevClassTemplate->getMostRecentDecl()->getTemplateParameters(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Make sure the parameter lists match. |
| 1251 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1252 | Complain, |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1253 | Sema::TPL_TemplateMatch)) { |
| 1254 | if (Complain) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1255 | return nullptr; |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1256 | |
| 1257 | AdoptedPreviousTemplateParams = true; |
| 1258 | InstParams = PrevParams; |
| 1259 | } |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Do some additional validation, then merge default arguments |
| 1262 | // from the existing declarations. |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1263 | if (!AdoptedPreviousTemplateParams && |
| 1264 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1265 | Sema::TPC_ClassTemplate)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1266 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1270 | CXXRecordDecl *RecordInst = CXXRecordDecl::Create( |
| 1271 | SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), |
| 1272 | Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, |
| 1273 | /*DelayTypeCreation=*/true); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1274 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1275 | if (QualifierLoc) |
| 1276 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1277 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1278 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, |
| 1279 | StartingScope); |
| 1280 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1281 | ClassTemplateDecl *Inst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1282 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1283 | D->getIdentifier(), InstParams, RecordInst); |
| 1284 | assert(!(isFriend && Owner->isDependentContext())); |
| 1285 | Inst->setPreviousDecl(PrevClassTemplate); |
| 1286 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1287 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1288 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1289 | if (isFriend) { |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1290 | if (PrevClassTemplate) |
| 1291 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1292 | else |
| 1293 | Inst->setAccess(D->getAccess()); |
| 1294 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1295 | Inst->setObjectOfFriendDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1296 | // TODO: do we want to track the instantiation progeny of this |
| 1297 | // friend target decl? |
| 1298 | } else { |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1299 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1300 | if (!PrevClassTemplate) |
| 1301 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1302 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1304 | // Trigger creation of the type for the instantiation. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1305 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1306 | Inst->getInjectedClassNameSpecialization()); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1308 | // Finish handling of friends. |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1309 | if (isFriend) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1310 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1311 | Inst->setLexicalDeclContext(Owner); |
| 1312 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1313 | return Inst; |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1314 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1315 | |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1316 | if (D->isOutOfLine()) { |
| 1317 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1318 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1319 | } |
| 1320 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1321 | Owner->addDecl(Inst); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1322 | |
| 1323 | if (!PrevClassTemplate) { |
| 1324 | // Queue up any out-of-line partial specializations of this member |
| 1325 | // class template; the client will force their instantiation once |
| 1326 | // the enclosing class has been instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1327 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1328 | D->getPartialSpecializations(PartialSpecs); |
| 1329 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1330 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1331 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1332 | } |
| 1333 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1334 | return Inst; |
| 1335 | } |
| 1336 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1337 | Decl * |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1338 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1339 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1340 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1342 | // Lookup the already-instantiated declaration in the instantiation |
| 1343 | // of the class template and return that. |
| 1344 | DeclContext::lookup_result Found |
| 1345 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1346 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1347 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1349 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1350 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1351 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1352 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1354 | if (ClassTemplatePartialSpecializationDecl *Result |
| 1355 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1356 | return Result; |
| 1357 | |
| 1358 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1361 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1362 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1363 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Create a local instantiation scope for this variable template, which |
| 1366 | // will contain the instantiations of the template parameters. |
| 1367 | LocalInstantiationScope Scope(SemaRef); |
| 1368 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1369 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1370 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1371 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1372 | |
| 1373 | VarDecl *Pattern = D->getTemplatedDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1374 | VarTemplateDecl *PrevVarTemplate = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1375 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1376 | if (getPreviousDeclForInstantiation(Pattern)) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1377 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1378 | if (!Found.empty()) |
| 1379 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1380 | } |
| 1381 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1382 | VarDecl *VarInst = |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1383 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1384 | /*InstantiatingVarTemplate=*/true)); |
Nick Lewycky | 6ca07ca | 2015-08-10 21:54:08 +0000 | [diff] [blame] | 1385 | if (!VarInst) return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1386 | |
| 1387 | DeclContext *DC = Owner; |
| 1388 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1389 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1390 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1391 | VarInst); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1392 | VarInst->setDescribedVarTemplate(Inst); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1393 | Inst->setPreviousDecl(PrevVarTemplate); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1394 | |
| 1395 | Inst->setAccess(D->getAccess()); |
| 1396 | if (!PrevVarTemplate) |
| 1397 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1398 | |
| 1399 | if (D->isOutOfLine()) { |
| 1400 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1401 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1402 | } |
| 1403 | |
| 1404 | Owner->addDecl(Inst); |
| 1405 | |
| 1406 | if (!PrevVarTemplate) { |
| 1407 | // Queue up any out-of-line partial specializations of this member |
| 1408 | // variable template; the client will force their instantiation once |
| 1409 | // the enclosing class has been instantiated. |
| 1410 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1411 | D->getPartialSpecializations(PartialSpecs); |
| 1412 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1413 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1414 | OutOfLineVarPartialSpecs.push_back( |
| 1415 | std::make_pair(Inst, PartialSpecs[I])); |
| 1416 | } |
| 1417 | |
| 1418 | return Inst; |
| 1419 | } |
| 1420 | |
| 1421 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1422 | VarTemplatePartialSpecializationDecl *D) { |
| 1423 | assert(D->isStaticDataMember() && |
| 1424 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1425 | |
| 1426 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1427 | |
| 1428 | // Lookup the already-instantiated declaration and return that. |
| 1429 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1430 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1431 | |
| 1432 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1433 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1434 | |
| 1435 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1436 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1437 | return Result; |
| 1438 | |
| 1439 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1440 | } |
| 1441 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1442 | Decl * |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1443 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1444 | // Create a local instantiation scope for this function template, which |
| 1445 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1446 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1447 | // itself. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1448 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1449 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1450 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1451 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1453 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1454 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1455 | FunctionDecl *Instantiated = nullptr; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1456 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1457 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1458 | InstParams)); |
| 1459 | else |
| 1460 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1461 | D->getTemplatedDecl(), |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1462 | InstParams)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1464 | if (!Instantiated) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1465 | return nullptr; |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1466 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | // Link the instantiated function template declaration to the function |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1468 | // template from which it was instantiated. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1469 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1470 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1471 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1472 | assert(InstTemplate && |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1473 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1474 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1475 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1476 | |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1477 | // Link the instantiation back to the pattern *unless* this is a |
| 1478 | // non-definition friend declaration. |
| 1479 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1480 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1481 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1482 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1483 | // Make declarations visible in the appropriate context. |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1484 | if (!isFriend) { |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1485 | Owner->addDecl(InstTemplate); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1486 | } else if (InstTemplate->getDeclContext()->isRecord() && |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1487 | !getPreviousDeclForInstantiation(D)) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1488 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1489 | } |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1490 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1491 | return InstTemplate; |
| 1492 | } |
| 1493 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1494 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1495 | CXXRecordDecl *PrevDecl = nullptr; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1496 | if (D->isInjectedClassName()) |
| 1497 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1498 | else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1499 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1500 | PatternPrev, |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1501 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1502 | if (!Prev) return nullptr; |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1503 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1504 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1505 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1506 | CXXRecordDecl *Record = CXXRecordDecl::Create( |
| 1507 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 1508 | D->getLocation(), D->getIdentifier(), PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Substitute the nested name specifier, if any. |
| 1511 | if (SubstQualifier(D, Record)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1512 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1513 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1514 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, |
| 1515 | StartingScope); |
| 1516 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1517 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1518 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1519 | // the tag decls introduced by friend class declarations don't have an access |
| 1520 | // specifier. Remove once this area of the code gets sorted out. |
| 1521 | if (D->getAccess() != AS_none) |
| 1522 | Record->setAccess(D->getAccess()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1523 | if (!D->isInjectedClassName()) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1524 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1525 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1526 | // If the original function was part of a friend declaration, |
| 1527 | // inherit its namespace state. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1528 | if (D->getFriendObjectKind()) |
| 1529 | Record->setObjectOfFriendDecl(); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1531 | // Make sure that anonymous structs and unions are recorded. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1532 | if (D->isAnonymousStructOrUnion()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1533 | Record->setAnonymousStructOrUnion(true); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1534 | |
| 1535 | if (D->isLocalClass()) |
| 1536 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1537 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1538 | // Forward the mangling number from the template to the instantiated decl. |
| 1539 | SemaRef.Context.setManglingNumber(Record, |
| 1540 | SemaRef.Context.getManglingNumber(D)); |
| 1541 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1542 | // See if the old tag was defined along with a declarator. |
| 1543 | // If it did, mark the new tag as being associated with that declarator. |
| 1544 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1545 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); |
| 1546 | |
| 1547 | // See if the old tag was defined along with a typedef. |
| 1548 | // If it did, mark the new tag as being associated with that typedef. |
| 1549 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1550 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); |
| 1551 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1552 | Owner->addDecl(Record); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1553 | |
| 1554 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1555 | // of the instantiation of their enclosing entity. |
| 1556 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1557 | Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1558 | |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1559 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1560 | TSK_ImplicitInstantiation, |
| 1561 | /*Complain=*/true); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1562 | |
Richard Smith | ece4758 | 2017-01-04 23:45:01 +0000 | [diff] [blame] | 1563 | // For nested local classes, we will instantiate the members when we |
| 1564 | // reach the end of the outermost (non-nested) local class. |
| 1565 | if (!D->isCXXClassMember()) |
| 1566 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1567 | TSK_ImplicitInstantiation); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1568 | |
| 1569 | // This class may have local implicit instantiations that need to be |
| 1570 | // performed within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1571 | LocalInstantiations.perform(); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1572 | } |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1573 | |
| 1574 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1575 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1576 | return Record; |
| 1577 | } |
| 1578 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1579 | /// Adjust the given function type for an instantiation of the |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1580 | /// given declaration, to cope with modifications to the function's type that |
| 1581 | /// aren't reflected in the type-source information. |
| 1582 | /// |
| 1583 | /// \param D The declaration we're instantiating. |
| 1584 | /// \param TInfo The already-instantiated type. |
| 1585 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1586 | FunctionDecl *D, |
| 1587 | TypeSourceInfo *TInfo) { |
Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1588 | const FunctionProtoType *OrigFunc |
| 1589 | = D->getType()->castAs<FunctionProtoType>(); |
| 1590 | const FunctionProtoType *NewFunc |
| 1591 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1592 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1593 | return TInfo->getType(); |
| 1594 | |
| 1595 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1596 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1597 | return Context.getFunctionType(NewFunc->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1598 | NewFunc->getParamTypes(), NewEPI); |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1601 | /// Normal class members are of more specific types and therefore |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1602 | /// don't make it here. This function serves three purposes: |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1603 | /// 1) instantiating function templates |
| 1604 | /// 2) substituting friend declarations |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1605 | /// 3) substituting deduction guide declarations for nested class templates |
Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1606 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1607 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1608 | // Check whether there is already a function template specialization for |
| 1609 | // this declaration. |
| 1610 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1611 | if (FunctionTemplate && !TemplateParams) { |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1612 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1614 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1615 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1616 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1618 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1619 | if (SpecFunc) |
| 1620 | return SpecFunc; |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1623 | bool isFriend; |
| 1624 | if (FunctionTemplate) |
| 1625 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1626 | else |
| 1627 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1628 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1629 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1630 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1631 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1632 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1633 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1635 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1636 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1637 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1638 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1639 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1641 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1642 | if (QualifierLoc) { |
| 1643 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1644 | TemplateArgs); |
| 1645 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1646 | return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1649 | // If we're instantiating a local function declaration, put the result |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1650 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1651 | // context. |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1652 | DeclContext *DC; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1653 | if (D->isLocalExternDecl()) { |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1654 | DC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1655 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1656 | } else if (isFriend && QualifierLoc) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1657 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1658 | SS.Adopt(QualifierLoc); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1659 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1660 | if (!DC) return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1661 | } else { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1662 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1663 | TemplateArgs); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1664 | } |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1665 | |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1666 | DeclarationNameInfo NameInfo |
| 1667 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1668 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1669 | FunctionDecl *Function; |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1670 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1671 | Function = CXXDeductionGuideDecl::Create( |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1672 | SemaRef.Context, DC, D->getInnerLocStart(), DGuide->isExplicit(), |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1673 | NameInfo, T, TInfo, D->getSourceRange().getEnd()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1674 | if (DGuide->isCopyDeductionCandidate()) |
| 1675 | cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1676 | Function->setAccess(D->getAccess()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1677 | } else { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1678 | Function = FunctionDecl::Create( |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1679 | SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1680 | D->getCanonicalDecl()->getStorageClass(), D->isInlineSpecified(), |
| 1681 | D->hasWrittenPrototype(), D->isConstexpr()); |
| 1682 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
| 1683 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1684 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1685 | if (D->isInlined()) |
| 1686 | Function->setImplicitlyInline(); |
| 1687 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1688 | if (QualifierLoc) |
| 1689 | Function->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1690 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1691 | if (D->isLocalExternDecl()) |
| 1692 | Function->setLocalExternDecl(); |
| 1693 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1694 | DeclContext *LexicalDC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1695 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1696 | assert(D->getDeclContext()->isFileContext()); |
| 1697 | LexicalDC = D->getDeclContext(); |
| 1698 | } |
| 1699 | |
| 1700 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1702 | // Attach the parameters |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1703 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1704 | if (Params[P]) |
| 1705 | Params[P]->setOwningFunction(Function); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1706 | Function->setParams(Params); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1708 | if (TemplateParams) { |
| 1709 | // Our resulting instantiation is actually a function template, since we |
| 1710 | // are substituting only the outer template parameters. For example, given |
| 1711 | // |
| 1712 | // template<typename T> |
| 1713 | // struct X { |
| 1714 | // template<typename U> friend void f(T, U); |
| 1715 | // }; |
| 1716 | // |
| 1717 | // X<int> x; |
| 1718 | // |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1719 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1720 | // which means substituting int for T, but leaving "f" as a friend function |
| 1721 | // template. |
| 1722 | // Build the function template itself. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1723 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1724 | Function->getLocation(), |
| 1725 | Function->getDeclName(), |
| 1726 | TemplateParams, Function); |
| 1727 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1728 | |
| 1729 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (isFriend && D->isThisDeclarationADefinition()) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1732 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1733 | D->getDescribedFunctionTemplate()); |
| 1734 | } |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1735 | } else if (FunctionTemplate) { |
| 1736 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1737 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1738 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1739 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1740 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1741 | /*InsertPos=*/nullptr); |
Richard Smith | 152bcd2 | 2017-01-28 02:56:07 +0000 | [diff] [blame] | 1742 | } else if (isFriend && D->isThisDeclarationADefinition()) { |
| 1743 | // Do not connect the friend to the template unless it's actually a |
| 1744 | // definition. We don't want non-template functions to be marked as being |
| 1745 | // template instantiations. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1746 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1747 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1748 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1749 | if (isFriend) |
| 1750 | Function->setObjectOfFriendDecl(); |
| 1751 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1752 | if (InitFunctionInstantiation(Function, D)) |
| 1753 | Function->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1755 | bool IsExplicitSpecialization = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1756 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1757 | LookupResult Previous( |
| 1758 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1759 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1760 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1761 | D->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 1762 | : SemaRef.forRedeclarationInCurContext()); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1763 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1764 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1765 | = D->getDependentSpecializationInfo()) { |
| 1766 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1767 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1768 | // Instantiate the explicit template arguments. |
| 1769 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1770 | Info->getRAngleLoc()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1771 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1772 | ExplicitArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1773 | return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1774 | |
| 1775 | // Map the candidate templates to their instantiations. |
| 1776 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1777 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1778 | Info->getTemplate(I), |
| 1779 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1780 | if (!Temp) return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1781 | |
| 1782 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1783 | } |
| 1784 | |
| 1785 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1786 | &ExplicitArgs, |
| 1787 | Previous)) |
| 1788 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1789 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1790 | IsExplicitSpecialization = true; |
| 1791 | } else if (const ASTTemplateArgumentListInfo *Info = |
| 1792 | D->getTemplateSpecializationArgsAsWritten()) { |
| 1793 | // The name of this function was written as a template-id. |
| 1794 | SemaRef.LookupQualifiedName(Previous, DC); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1795 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1796 | // Instantiate the explicit template arguments. |
| 1797 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1798 | Info->getRAngleLoc()); |
| 1799 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1800 | ExplicitArgs, TemplateArgs)) |
| 1801 | return nullptr; |
| 1802 | |
| 1803 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1804 | &ExplicitArgs, |
| 1805 | Previous)) |
| 1806 | Function->setInvalidDecl(); |
| 1807 | |
| 1808 | IsExplicitSpecialization = true; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1809 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1810 | // Look only into the namespace where the friend would be declared to |
| 1811 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1812 | // as described in ActOnFriendFunctionDecl. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1813 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1815 | // In C++, the previous declaration we find might be a tag type |
| 1816 | // (class or enum). In this case, the new declaration will hide the |
| 1817 | // tag type. Note that this does does not apply if we're declaring a |
| 1818 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1819 | if (Previous.isSingleTagDecl()) |
| 1820 | Previous.clear(); |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1821 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1822 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1823 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1824 | IsExplicitSpecialization); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1825 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1826 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1827 | ? cast<NamedDecl>(FunctionTemplate) |
| 1828 | : Function); |
| 1829 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1830 | // If the original function was part of a friend declaration, |
| 1831 | // inherit its namespace state and add it to the owner. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1832 | if (isFriend) { |
Serge Pavlov | acfcd78 | 2018-12-06 09:35:04 +0000 | [diff] [blame] | 1833 | Function->setObjectOfFriendDecl(); |
| 1834 | if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) |
| 1835 | FT->setObjectOfFriendDecl(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1836 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1837 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1838 | bool QueuedInstantiation = false; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1839 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1840 | // C++11 [temp.friend]p4 (DR329): |
| 1841 | // When a function is defined in a friend function declaration in a class |
| 1842 | // template, the function is instantiated when the function is odr-used. |
| 1843 | // The same restrictions on multiple declarations and definitions that |
| 1844 | // apply to non-template function declarations and definitions also apply |
| 1845 | // to these implicit definitions. |
| 1846 | if (D->isThisDeclarationADefinition()) { |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1847 | SemaRef.CheckForFunctionRedefinition(Function); |
| 1848 | if (!Function->isInvalidDecl()) { |
| 1849 | for (auto R : Function->redecls()) { |
| 1850 | if (R == Function) |
| 1851 | continue; |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1852 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1853 | // If some prior declaration of this function has been used, we need |
| 1854 | // to instantiate its definition. |
| 1855 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1856 | if (MemberSpecializationInfo *MSInfo = |
| 1857 | Function->getMemberSpecializationInfo()) { |
| 1858 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1859 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1860 | MSInfo->setPointOfInstantiation(Loc); |
| 1861 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1862 | std::make_pair(Function, Loc)); |
| 1863 | QueuedInstantiation = true; |
| 1864 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1865 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1866 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | } |
Richard Smith | f359765 | 2017-05-10 00:01:13 +0000 | [diff] [blame] | 1870 | |
| 1871 | // Check the template parameter list against the previous declaration. The |
| 1872 | // goal here is to pick up default arguments added since the friend was |
| 1873 | // declared; we know the template parameter lists match, since otherwise |
| 1874 | // we would not have picked this template as the previous declaration. |
| 1875 | if (TemplateParams && FunctionTemplate->getPreviousDecl()) { |
| 1876 | SemaRef.CheckTemplateParameterList( |
| 1877 | TemplateParams, |
| 1878 | FunctionTemplate->getPreviousDecl()->getTemplateParameters(), |
| 1879 | Function->isThisDeclarationADefinition() |
| 1880 | ? Sema::TPC_FriendFunctionTemplateDefinition |
| 1881 | : Sema::TPC_FriendFunctionTemplate); |
| 1882 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1885 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1886 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1887 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1888 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1889 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1890 | PrincipalDecl->setNonMemberOperator(); |
| 1891 | |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1892 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1893 | return Function; |
| 1894 | } |
| 1895 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1896 | Decl * |
| 1897 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1898 | TemplateParameterList *TemplateParams, |
| 1899 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1900 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1901 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | // We are creating a function template specialization from a function |
| 1903 | // template. Check whether there is already a function template |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1904 | // specialization for this particular set of template arguments. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1905 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1907 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1908 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1909 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1911 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1912 | if (SpecFunc) |
| 1913 | return SpecFunc; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1916 | bool isFriend; |
| 1917 | if (FunctionTemplate) |
| 1918 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1919 | else |
| 1920 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1921 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1922 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1923 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1924 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1925 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1926 | |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1927 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1928 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1929 | unsigned NumTempParamLists = 0; |
| 1930 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
Benjamin Kramer | 9dc549b | 2015-08-04 14:46:06 +0000 | [diff] [blame] | 1931 | TempParamLists.resize(NumTempParamLists); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1932 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1933 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1934 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1935 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1936 | return nullptr; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1937 | TempParamLists[I] = InstParams; |
| 1938 | } |
| 1939 | } |
| 1940 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1941 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1942 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1943 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1944 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1945 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1947 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1948 | if (QualifierLoc) { |
| 1949 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1950 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1951 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1952 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | DeclContext *DC = Owner; |
| 1956 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1957 | if (QualifierLoc) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1958 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1959 | SS.Adopt(QualifierLoc); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1960 | DC = SemaRef.computeDeclContext(SS); |
John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1961 | |
| 1962 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1963 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1964 | } else { |
| 1965 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1966 | D->getDeclContext(), |
| 1967 | TemplateArgs); |
| 1968 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1969 | if (!DC) return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1972 | // Build the instantiated method declaration. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1973 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1974 | CXXMethodDecl *Method = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1976 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1977 | DeclarationNameInfo NameInfo |
| 1978 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1979 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1981 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | Constructor->isExplicit(), |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 1983 | Constructor->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1984 | false, Constructor->isConstexpr()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1985 | Method->setRangeEnd(Constructor->getEndLoc()); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1986 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1987 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1988 | StartLoc, NameInfo, T, TInfo, |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 1989 | Destructor->isInlineSpecified(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1990 | false); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1991 | Method->setRangeEnd(Destructor->getEndLoc()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1992 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1993 | Method = CXXConversionDecl::Create( |
| 1994 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 1995 | Conversion->isInlineSpecified(), Conversion->isExplicit(), |
| 1996 | Conversion->isConstexpr(), Conversion->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1997 | } else { |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1998 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1999 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, StartLoc, NameInfo, |
| 2000 | T, TInfo, SC, D->isInlineSpecified(), |
| 2001 | D->isConstexpr(), D->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2002 | } |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2003 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2004 | if (D->isInlined()) |
| 2005 | Method->setImplicitlyInline(); |
| 2006 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2007 | if (QualifierLoc) |
| 2008 | Method->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2010 | if (TemplateParams) { |
| 2011 | // Our resulting instantiation is actually a function template, since we |
| 2012 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | // |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2014 | // template<typename T> |
| 2015 | // struct X { |
| 2016 | // template<typename U> void f(T, U); |
| 2017 | // }; |
| 2018 | // |
| 2019 | // X<int> x; |
| 2020 | // |
| 2021 | // We are instantiating the member template "f" within X<int>, which means |
| 2022 | // substituting int for T, but leaving "f" as a member function template. |
| 2023 | // Build the function template itself. |
| 2024 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 2025 | Method->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | Method->getDeclName(), |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2027 | TemplateParams, Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2028 | if (isFriend) { |
| 2029 | FunctionTemplate->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2030 | FunctionTemplate->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2031 | } else if (D->isOutOfLine()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2033 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2034 | } else if (FunctionTemplate) { |
| 2035 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 2036 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2037 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2038 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2039 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2040 | /*InsertPos=*/nullptr); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2041 | } else if (!isFriend) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2042 | // Record that this is an instantiation of a member function. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2043 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2044 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2045 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | // If we are instantiating a member function defined |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2047 | // out-of-line, the instantiation will have the same lexical |
| 2048 | // context (which will be a namespace scope) as the template. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2049 | if (isFriend) { |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2050 | if (NumTempParamLists) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 2051 | Method->setTemplateParameterListsInfo( |
| 2052 | SemaRef.Context, |
| 2053 | llvm::makeArrayRef(TempParamLists.data(), NumTempParamLists)); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2054 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2055 | Method->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2056 | Method->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2057 | } else if (D->isOutOfLine()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2058 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2060 | // Attach the parameters |
| 2061 | for (unsigned P = 0; P < Params.size(); ++P) |
| 2062 | Params[P]->setOwningFunction(Method); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2063 | Method->setParams(Params); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2064 | |
| 2065 | if (InitMethodInstantiation(Method, D)) |
| 2066 | Method->setInvalidDecl(); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2067 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2068 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2069 | Sema::ForExternalRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2071 | bool IsExplicitSpecialization = false; |
| 2072 | |
| 2073 | // If the name of this function was written as a template-id, instantiate |
| 2074 | // the explicit template arguments. |
| 2075 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 2076 | = D->getDependentSpecializationInfo()) { |
| 2077 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 2078 | |
| 2079 | // Instantiate the explicit template arguments. |
| 2080 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2081 | Info->getRAngleLoc()); |
| 2082 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2083 | ExplicitArgs, TemplateArgs)) |
| 2084 | return nullptr; |
| 2085 | |
| 2086 | // Map the candidate templates to their instantiations. |
| 2087 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 2088 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 2089 | Info->getTemplate(I), |
| 2090 | TemplateArgs); |
| 2091 | if (!Temp) return nullptr; |
| 2092 | |
| 2093 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 2094 | } |
| 2095 | |
| 2096 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2097 | &ExplicitArgs, |
| 2098 | Previous)) |
| 2099 | Method->setInvalidDecl(); |
| 2100 | |
| 2101 | IsExplicitSpecialization = true; |
| 2102 | } else if (const ASTTemplateArgumentListInfo *Info = |
| 2103 | D->getTemplateSpecializationArgsAsWritten()) { |
| 2104 | SemaRef.LookupQualifiedName(Previous, DC); |
| 2105 | |
| 2106 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2107 | Info->getRAngleLoc()); |
| 2108 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2109 | ExplicitArgs, TemplateArgs)) |
| 2110 | return nullptr; |
| 2111 | |
| 2112 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2113 | &ExplicitArgs, |
| 2114 | Previous)) |
| 2115 | Method->setInvalidDecl(); |
| 2116 | |
| 2117 | IsExplicitSpecialization = true; |
| 2118 | } else if (!FunctionTemplate || TemplateParams || isFriend) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2119 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2120 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2121 | // In C++, the previous declaration we find might be a tag type |
| 2122 | // (class or enum). In this case, the new declaration will hide the |
| 2123 | // tag type. Note that this does does not apply if we're declaring a |
| 2124 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2125 | if (Previous.isSingleTagDecl()) |
| 2126 | Previous.clear(); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2127 | } |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2128 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2129 | if (!IsClassScopeSpecialization) |
Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2130 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, |
| 2131 | IsExplicitSpecialization); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 2133 | if (D->isPure()) |
| 2134 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 2135 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2136 | // Propagate access. For a non-friend declaration, the access is |
| 2137 | // whatever we're propagating from. For a friend, it should be the |
| 2138 | // previous declaration we just found. |
| 2139 | if (isFriend && Method->getPreviousDecl()) |
| 2140 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2141 | else |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2142 | Method->setAccess(D->getAccess()); |
| 2143 | if (FunctionTemplate) |
| 2144 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 2145 | |
Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 2146 | SemaRef.CheckOverrideControl(Method); |
| 2147 | |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2148 | // 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] | 2149 | if (D->isExplicitlyDefaulted()) |
| 2150 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2151 | if (D->isDeletedAsWritten()) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 2152 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2153 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2154 | // If there's a function template, let our caller handle it. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2155 | if (FunctionTemplate) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2156 | // do nothing |
| 2157 | |
| 2158 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2159 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2160 | // do nothing |
| 2161 | |
| 2162 | // Otherwise, check access to friends and make them visible. |
| 2163 | } else if (isFriend) { |
| 2164 | // We only need to re-check access for methods which we didn't |
| 2165 | // manage to match during parsing. |
| 2166 | if (!D->getPreviousDecl()) |
| 2167 | SemaRef.CheckFriendAccess(Method); |
| 2168 | |
| 2169 | Record->makeDeclVisibleInContext(Method); |
| 2170 | |
| 2171 | // Otherwise, add the declaration. We don't need to do this for |
| 2172 | // class-scope specializations because we'll have matched them with |
| 2173 | // the appropriate template. |
| 2174 | } else if (!IsClassScopeSpecialization) { |
| 2175 | Owner->addDecl(Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2176 | } |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2178 | return Method; |
| 2179 | } |
| 2180 | |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2181 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2182 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2185 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2186 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2189 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2190 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2193 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2194 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 2195 | /*ExpectParameterPack=*/ false); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2198 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 2199 | TemplateTypeParmDecl *D) { |
| 2200 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2201 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2203 | TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2204 | SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2205 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), |
| 2206 | D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack()); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2207 | Inst->setAccess(AS_public); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2208 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2209 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2210 | TypeSourceInfo *InstantiatedDefaultArg = |
| 2211 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 2212 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 2213 | if (InstantiatedDefaultArg) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2214 | Inst->setDefaultArgument(InstantiatedDefaultArg); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2215 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2216 | |
| 2217 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2218 | // scope. |
| 2219 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2220 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2221 | return Inst; |
| 2222 | } |
| 2223 | |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2224 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 2225 | NonTypeTemplateParmDecl *D) { |
| 2226 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2227 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2228 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 2229 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2230 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2231 | TypeSourceInfo *DI; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2232 | QualType T; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2233 | bool Invalid = false; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2234 | |
| 2235 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2236 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2237 | // expansion of types. Substitute into each of the expanded types. |
| 2238 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 2239 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 2240 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2241 | TypeSourceInfo *NewDI = |
| 2242 | SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, |
| 2243 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2244 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2245 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2246 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2247 | QualType NewT = |
| 2248 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2249 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2250 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2251 | |
| 2252 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2253 | ExpandedParameterPackTypes.push_back(NewT); |
| 2254 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2256 | IsExpandedParameterPack = true; |
| 2257 | DI = D->getTypeSourceInfo(); |
| 2258 | T = DI->getType(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2259 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2260 | // The non-type template parameter pack's type is a pack expansion of types. |
| 2261 | // Determine whether we need to expand this parameter pack into separate |
| 2262 | // types. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2263 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2264 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2265 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2266 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2268 | // Determine whether the set of unexpanded parameter packs can and should |
| 2269 | // be expanded. |
| 2270 | bool Expand = true; |
| 2271 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2272 | Optional<unsigned> OrigNumExpansions |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2273 | = Expansion.getTypePtr()->getNumExpansions(); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2274 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2275 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 2276 | Pattern.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 2277 | Unexpanded, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2278 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2279 | Expand, RetainExpansion, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2280 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2281 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2283 | if (Expand) { |
| 2284 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2285 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2286 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2287 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2288 | D->getDeclName()); |
| 2289 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2290 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2291 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2292 | QualType NewT = |
| 2293 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2294 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2295 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2296 | |
| 2297 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2298 | ExpandedParameterPackTypes.push_back(NewT); |
| 2299 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2300 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2301 | // Note that we have an expanded parameter pack. The "type" of this |
| 2302 | // expanded parameter pack is the original expansion type, but callers |
| 2303 | // will end up using the expanded parameter pack types for type-checking. |
| 2304 | IsExpandedParameterPack = true; |
| 2305 | DI = D->getTypeSourceInfo(); |
| 2306 | T = DI->getType(); |
| 2307 | } else { |
| 2308 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2309 | // pattern and create a new pack expansion type. |
| 2310 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2311 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2312 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2313 | D->getDeclName()); |
| 2314 | if (!NewPattern) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2315 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2316 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2317 | SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2318 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 2319 | NumExpansions); |
| 2320 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2321 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2323 | T = DI->getType(); |
| 2324 | } |
| 2325 | } else { |
| 2326 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2327 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2328 | D->getLocation(), D->getDeclName()); |
| 2329 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2330 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2332 | // Check that this type is acceptable for a non-type template parameter. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2333 | T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2334 | if (T.isNull()) { |
| 2335 | T = SemaRef.Context.IntTy; |
| 2336 | Invalid = true; |
| 2337 | } |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2338 | } |
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 | NonTypeTemplateParmDecl *Param; |
| 2341 | if (IsExpandedParameterPack) |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2342 | Param = NonTypeTemplateParmDecl::Create( |
| 2343 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2344 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2345 | D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2346 | ExpandedParameterPackTypesAsWritten); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2347 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2348 | Param = NonTypeTemplateParmDecl::Create( |
| 2349 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2350 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2351 | D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2353 | Param->setAccess(AS_public); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2354 | if (Invalid) |
| 2355 | Param->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2356 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2357 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2358 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 2359 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2360 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 2361 | if (!Value.isInvalid()) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2362 | Param->setDefaultArgument(Value.get()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2363 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2364 | |
| 2365 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2366 | // scope. |
| 2367 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2368 | return Param; |
| 2369 | } |
| 2370 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2371 | static void collectUnexpandedParameterPacks( |
| 2372 | Sema &S, |
| 2373 | TemplateParameterList *Params, |
| 2374 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2375 | for (const auto &P : *Params) { |
| 2376 | if (P->isTemplateParameterPack()) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2377 | continue; |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2378 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2379 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 2380 | Unexpanded); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2381 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2382 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 2383 | Unexpanded); |
| 2384 | } |
| 2385 | } |
| 2386 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2387 | Decl * |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2388 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 2389 | TemplateTemplateParmDecl *D) { |
| 2390 | // Instantiate the template parameter list of the template template parameter. |
| 2391 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 2392 | TemplateParameterList *InstParams; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2393 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 2394 | |
| 2395 | bool IsExpandedParameterPack = false; |
| 2396 | |
| 2397 | if (D->isExpandedParameterPack()) { |
| 2398 | // The template template parameter pack is an already-expanded pack |
| 2399 | // expansion of template parameters. Substitute into each of the expanded |
| 2400 | // parameters. |
| 2401 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 2402 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 2403 | I != N; ++I) { |
| 2404 | LocalInstantiationScope Scope(SemaRef); |
| 2405 | TemplateParameterList *Expansion = |
| 2406 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2407 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2408 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2409 | ExpandedParams.push_back(Expansion); |
| 2410 | } |
| 2411 | |
| 2412 | IsExpandedParameterPack = true; |
| 2413 | InstParams = TempParams; |
| 2414 | } else if (D->isPackExpansion()) { |
| 2415 | // The template template parameter pack expands to a pack of template |
| 2416 | // template parameters. Determine whether we need to expand this parameter |
| 2417 | // pack into separate parameters. |
| 2418 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2419 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2420 | Unexpanded); |
| 2421 | |
| 2422 | // Determine whether the set of unexpanded parameter packs can and should |
| 2423 | // be expanded. |
| 2424 | bool Expand = true; |
| 2425 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2426 | Optional<unsigned> NumExpansions; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2427 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2428 | TempParams->getSourceRange(), |
| 2429 | Unexpanded, |
| 2430 | TemplateArgs, |
| 2431 | Expand, RetainExpansion, |
| 2432 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2433 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2434 | |
| 2435 | if (Expand) { |
| 2436 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2437 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2438 | LocalInstantiationScope Scope(SemaRef); |
| 2439 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2440 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2441 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2442 | ExpandedParams.push_back(Expansion); |
| 2443 | } |
| 2444 | |
| 2445 | // Note that we have an expanded parameter pack. The "type" of this |
| 2446 | // expanded parameter pack is the original expansion type, but callers |
| 2447 | // will end up using the expanded parameter pack types for type-checking. |
| 2448 | IsExpandedParameterPack = true; |
| 2449 | InstParams = TempParams; |
| 2450 | } else { |
| 2451 | // We cannot fully expand the pack expansion now, so just substitute |
| 2452 | // into the pattern. |
| 2453 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2454 | |
| 2455 | LocalInstantiationScope Scope(SemaRef); |
| 2456 | InstParams = SubstTemplateParams(TempParams); |
| 2457 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2458 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } else { |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2461 | // Perform the actual substitution of template parameters within a new, |
| 2462 | // local instantiation scope. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2463 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2464 | InstParams = SubstTemplateParams(TempParams); |
| 2465 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2466 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2469 | // Build the template template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2470 | TemplateTemplateParmDecl *Param; |
| 2471 | if (IsExpandedParameterPack) |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2472 | Param = TemplateTemplateParmDecl::Create( |
| 2473 | SemaRef.Context, Owner, D->getLocation(), |
| 2474 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2475 | D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2476 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2477 | Param = TemplateTemplateParmDecl::Create( |
| 2478 | SemaRef.Context, Owner, D->getLocation(), |
| 2479 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2480 | D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2481 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2482 | NestedNameSpecifierLoc QualifierLoc = |
| 2483 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2484 | QualifierLoc = |
| 2485 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2486 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2487 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2488 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2489 | if (!TName.isNull()) |
| 2490 | Param->setDefaultArgument( |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2491 | SemaRef.Context, |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2492 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2493 | D->getDefaultArgument().getTemplateQualifierLoc(), |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2494 | D->getDefaultArgument().getTemplateNameLoc())); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2495 | } |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2496 | Param->setAccess(AS_public); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2497 | |
| 2498 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2499 | // scope. |
| 2500 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2501 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2502 | return Param; |
| 2503 | } |
| 2504 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2505 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2506 | // Using directives are never dependent (and never contain any types or |
| 2507 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2508 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2509 | UsingDirectiveDecl *Inst |
| 2510 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2511 | D->getNamespaceKeyLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2512 | D->getQualifierLoc(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2513 | D->getIdentLocation(), |
| 2514 | D->getNominatedNamespace(), |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2515 | D->getCommonAncestor()); |
Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2516 | |
| 2517 | // Add the using directive to its declaration context |
| 2518 | // only if this is not a function or method. |
| 2519 | if (!Owner->isFunctionOrMethod()) |
| 2520 | Owner->addDecl(Inst); |
| 2521 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2522 | return Inst; |
| 2523 | } |
| 2524 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2525 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2526 | |
| 2527 | // The nested name specifier may be dependent, for example |
| 2528 | // template <typename T> struct t { |
| 2529 | // struct s1 { T f1(); }; |
| 2530 | // struct s2 : s1 { using s1::f1; }; |
| 2531 | // }; |
| 2532 | // template struct t<int>; |
| 2533 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2534 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2535 | NestedNameSpecifierLoc QualifierLoc |
| 2536 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2537 | TemplateArgs); |
| 2538 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2539 | return nullptr; |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2540 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2541 | // For an inheriting constructor declaration, the name of the using |
| 2542 | // declaration is the name of a constructor in this class, not in the |
| 2543 | // base class. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2544 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2545 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 2546 | if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) |
| 2547 | NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 2548 | SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2549 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2550 | // We only need to do redeclaration lookups if we're in a class |
| 2551 | // scope (in fact, it's not really even possible in non-class |
| 2552 | // scopes). |
| 2553 | bool CheckRedeclaration = Owner->isRecord(); |
| 2554 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2555 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2556 | Sema::ForVisibleRedeclaration); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2557 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2558 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2559 | D->getUsingLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2560 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2561 | NameInfo, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2562 | D->hasTypename()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2563 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2564 | CXXScopeSpec SS; |
| 2565 | SS.Adopt(QualifierLoc); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2566 | if (CheckRedeclaration) { |
| 2567 | Prev.setHideTags(false); |
| 2568 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2569 | |
| 2570 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2571 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2572 | D->hasTypename(), SS, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2573 | D->getLocation(), Prev)) |
| 2574 | NewUD->setInvalidDecl(); |
| 2575 | |
| 2576 | } |
| 2577 | |
| 2578 | if (!NewUD->isInvalidDecl() && |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 2579 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), |
| 2580 | SS, NameInfo, D->getLocation())) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2581 | NewUD->setInvalidDecl(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2582 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2583 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2584 | NewUD->setAccess(D->getAccess()); |
| 2585 | Owner->addDecl(NewUD); |
| 2586 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2587 | // Don't process the shadow decls for an invalid decl. |
| 2588 | if (NewUD->isInvalidDecl()) |
| 2589 | return NewUD; |
| 2590 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2591 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2592 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2593 | |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2594 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2595 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2596 | // Process the shadow decls. |
Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2597 | for (auto *Shadow : D->shadows()) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2598 | // FIXME: UsingShadowDecl doesn't preserve its immediate target, so |
| 2599 | // reconstruct it in the case where it matters. |
| 2600 | NamedDecl *OldTarget = Shadow->getTargetDecl(); |
| 2601 | if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) |
| 2602 | if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) |
| 2603 | OldTarget = BaseShadow; |
| 2604 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2605 | NamedDecl *InstTarget = |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2606 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2607 | Shadow->getLocation(), OldTarget, TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2608 | if (!InstTarget) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2609 | return nullptr; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2610 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2611 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2612 | if (CheckRedeclaration) { |
| 2613 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2614 | continue; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 2615 | } else if (UsingShadowDecl *OldPrev = |
| 2616 | getPreviousDeclForInstantiation(Shadow)) { |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2617 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2618 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2619 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2620 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2621 | UsingShadowDecl *InstShadow = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2622 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2623 | PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2624 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2625 | |
| 2626 | if (isFunctionScope) |
| 2627 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2628 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2629 | |
| 2630 | return NewUD; |
| 2631 | } |
| 2632 | |
| 2633 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2634 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2635 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2638 | Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( |
| 2639 | ConstructorUsingShadowDecl *D) { |
| 2640 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 2641 | return nullptr; |
| 2642 | } |
| 2643 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2644 | template <typename T> |
| 2645 | Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( |
| 2646 | T *D, bool InstantiatingPackElement) { |
| 2647 | // If this is a pack expansion, expand it now. |
| 2648 | if (D->isPackExpansion() && !InstantiatingPackElement) { |
| 2649 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2650 | SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); |
| 2651 | SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); |
| 2652 | |
| 2653 | // Determine whether the set of unexpanded parameter packs can and should |
| 2654 | // be expanded. |
| 2655 | bool Expand = true; |
| 2656 | bool RetainExpansion = false; |
| 2657 | Optional<unsigned> NumExpansions; |
| 2658 | if (SemaRef.CheckParameterPacksForExpansion( |
| 2659 | D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, |
| 2660 | Expand, RetainExpansion, NumExpansions)) |
| 2661 | return nullptr; |
| 2662 | |
| 2663 | // This declaration cannot appear within a function template signature, |
| 2664 | // so we can't have a partial argument list for a parameter pack. |
| 2665 | assert(!RetainExpansion && |
| 2666 | "should never need to retain an expansion for UsingPackDecl"); |
| 2667 | |
| 2668 | if (!Expand) { |
| 2669 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2670 | // pattern and create a new pack expansion. |
| 2671 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2672 | return instantiateUnresolvedUsingDecl(D, true); |
| 2673 | } |
| 2674 | |
| 2675 | // Within a function, we don't have any normal way to check for conflicts |
| 2676 | // between shadow declarations from different using declarations in the |
| 2677 | // same pack expansion, but this is always ill-formed because all expansions |
| 2678 | // must produce (conflicting) enumerators. |
| 2679 | // |
| 2680 | // Sadly we can't just reject this in the template definition because it |
| 2681 | // could be valid if the pack is empty or has exactly one expansion. |
| 2682 | if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { |
| 2683 | SemaRef.Diag(D->getEllipsisLoc(), |
| 2684 | diag::err_using_decl_redeclaration_expansion); |
| 2685 | return nullptr; |
| 2686 | } |
| 2687 | |
| 2688 | // Instantiate the slices of this pack and build a UsingPackDecl. |
| 2689 | SmallVector<NamedDecl*, 8> Expansions; |
| 2690 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2691 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2692 | Decl *Slice = instantiateUnresolvedUsingDecl(D, true); |
| 2693 | if (!Slice) |
| 2694 | return nullptr; |
| 2695 | // Note that we can still get unresolved using declarations here, if we |
| 2696 | // had arguments for all packs but the pattern also contained other |
| 2697 | // template arguments (this only happens during partial substitution, eg |
| 2698 | // into the body of a generic lambda in a function template). |
| 2699 | Expansions.push_back(cast<NamedDecl>(Slice)); |
| 2700 | } |
| 2701 | |
| 2702 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2703 | if (isDeclWithinFunction(D)) |
| 2704 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2705 | return NewD; |
| 2706 | } |
| 2707 | |
| 2708 | UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); |
| 2709 | SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); |
| 2710 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2711 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2712 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2713 | TemplateArgs); |
| 2714 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2715 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2717 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2718 | SS.Adopt(QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2720 | DeclarationNameInfo NameInfo |
| 2721 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2722 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2723 | // Produce a pack expansion only if we're not instantiating a particular |
| 2724 | // slice of a pack expansion. |
| 2725 | bool InstantiatingSlice = D->getEllipsisLoc().isValid() && |
| 2726 | SemaRef.ArgumentPackSubstitutionIndex != -1; |
| 2727 | SourceLocation EllipsisLoc = |
| 2728 | InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); |
| 2729 | |
| 2730 | NamedDecl *UD = SemaRef.BuildUsingDeclaration( |
| 2731 | /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2732 | /*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, |
| 2733 | ParsedAttributesView(), |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2734 | /*IsInstantiation*/ true); |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2735 | if (UD) |
| 2736 | SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); |
| 2737 | |
| 2738 | return UD; |
Richard Smith | 22a250c | 2016-12-19 04:08:53 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2741 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( |
| 2742 | UnresolvedUsingTypenameDecl *D) { |
| 2743 | return instantiateUnresolvedUsingDecl(D); |
| 2744 | } |
| 2745 | |
| 2746 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( |
| 2747 | UnresolvedUsingValueDecl *D) { |
| 2748 | return instantiateUnresolvedUsingDecl(D); |
| 2749 | } |
| 2750 | |
| 2751 | Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { |
| 2752 | SmallVector<NamedDecl*, 8> Expansions; |
| 2753 | for (auto *UD : D->expansions()) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2754 | if (NamedDecl *NewUD = |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2755 | SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2756 | Expansions.push_back(NewUD); |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2757 | else |
| 2758 | return nullptr; |
| 2759 | } |
| 2760 | |
| 2761 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2762 | if (isDeclWithinFunction(D)) |
| 2763 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2764 | return NewD; |
| 2765 | } |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2766 | |
| 2767 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2768 | ClassScopeFunctionSpecializationDecl *Decl) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2769 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nick Lewycky | 0b72773 | 2015-01-02 01:33:12 +0000 | [diff] [blame] | 2770 | CXXMethodDecl *NewFD = |
| 2771 | cast_or_null<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, nullptr, true)); |
| 2772 | if (!NewFD) |
| 2773 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2774 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2775 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 2776 | TemplateArgumentListInfo *ExplicitTemplateArgsPtr = nullptr; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2777 | if (Decl->hasExplicitTemplateArgs()) { |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2778 | if (SemaRef.Subst(Decl->templateArgs().getArgumentArray(), |
| 2779 | Decl->templateArgs().size(), ExplicitTemplateArgs, |
| 2780 | TemplateArgs)) |
| 2781 | return nullptr; |
| 2782 | ExplicitTemplateArgsPtr = &ExplicitTemplateArgs; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2785 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2786 | Sema::ForExternalRedeclaration); |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2787 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2788 | if (SemaRef.CheckFunctionTemplateSpecialization( |
| 2789 | NewFD, ExplicitTemplateArgsPtr, Previous)) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2790 | NewFD->setInvalidDecl(); |
| 2791 | return NewFD; |
| 2792 | } |
| 2793 | |
| 2794 | // Associate the specialization with the pattern. |
| 2795 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2796 | assert(Specialization && "Class scope Specialization is null"); |
| 2797 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2798 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 2799 | // FIXME: If this is a definition, check for redefinition errors! |
| 2800 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2801 | return NewFD; |
| 2802 | } |
| 2803 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2804 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2805 | OMPThreadPrivateDecl *D) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2806 | SmallVector<Expr *, 5> Vars; |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2807 | for (auto *I : D->varlists()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2808 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2809 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2810 | Vars.push_back(Var); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | OMPThreadPrivateDecl *TD = |
| 2814 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2815 | |
Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2816 | TD->setAccess(AS_public); |
| 2817 | Owner->addDecl(TD); |
| 2818 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2819 | return TD; |
| 2820 | } |
| 2821 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2822 | Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 2823 | llvm_unreachable( |
| 2824 | "Requires directive cannot be instantiated within a dependent context"); |
| 2825 | } |
| 2826 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2827 | Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( |
| 2828 | OMPDeclareReductionDecl *D) { |
| 2829 | // Instantiate type and check if it is allowed. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2830 | const bool RequiresInstantiation = |
| 2831 | D->getType()->isDependentType() || |
| 2832 | D->getType()->isInstantiationDependentType() || |
| 2833 | D->getType()->containsUnexpandedParameterPack(); |
| 2834 | QualType SubstReductionType; |
| 2835 | if (RequiresInstantiation) { |
| 2836 | SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( |
| 2837 | D->getLocation(), |
| 2838 | ParsedType::make(SemaRef.SubstType( |
| 2839 | D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); |
| 2840 | } else { |
| 2841 | SubstReductionType = D->getType(); |
| 2842 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2843 | if (SubstReductionType.isNull()) |
| 2844 | return nullptr; |
| 2845 | bool IsCorrect = !SubstReductionType.isNull(); |
| 2846 | // Create instantiated copy. |
| 2847 | std::pair<QualType, SourceLocation> ReductionTypes[] = { |
| 2848 | std::make_pair(SubstReductionType, D->getLocation())}; |
| 2849 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 2850 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 2851 | PrevDeclInScope = cast<OMPDeclareReductionDecl>( |
| 2852 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 2853 | ->get<Decl *>()); |
| 2854 | } |
| 2855 | auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( |
| 2856 | /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), |
| 2857 | PrevDeclInScope); |
| 2858 | auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2859 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); |
| 2860 | if (!RequiresInstantiation) { |
| 2861 | if (Expr *Combiner = D->getCombiner()) { |
| 2862 | NewDRD->setCombinerData(D->getCombinerIn(), D->getCombinerOut()); |
| 2863 | NewDRD->setCombiner(Combiner); |
| 2864 | if (Expr *Init = D->getInitializer()) { |
| 2865 | NewDRD->setInitializerData(D->getInitOrig(), D->getInitPriv()); |
| 2866 | NewDRD->setInitializer(Init, D->getInitializerKind()); |
| 2867 | } |
| 2868 | } |
| 2869 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( |
| 2870 | /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); |
| 2871 | return NewDRD; |
| 2872 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2873 | Expr *SubstCombiner = nullptr; |
| 2874 | Expr *SubstInitializer = nullptr; |
| 2875 | // Combiners instantiation sequence. |
| 2876 | if (D->getCombiner()) { |
| 2877 | SemaRef.ActOnOpenMPDeclareReductionCombinerStart( |
| 2878 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2879 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2880 | cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), |
| 2881 | cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); |
| 2882 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2883 | cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), |
| 2884 | cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); |
| 2885 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 2886 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2887 | ThisContext); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2888 | SubstCombiner = SemaRef.SubstExpr(D->getCombiner(), TemplateArgs).get(); |
| 2889 | SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); |
| 2890 | // Initializers instantiation sequence. |
| 2891 | if (D->getInitializer()) { |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2892 | VarDecl *OmpPrivParm = |
| 2893 | SemaRef.ActOnOpenMPDeclareReductionInitializerStart( |
| 2894 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2895 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2896 | cast<DeclRefExpr>(D->getInitOrig())->getDecl(), |
| 2897 | cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); |
| 2898 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2899 | cast<DeclRefExpr>(D->getInitPriv())->getDecl(), |
| 2900 | cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2901 | if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { |
| 2902 | SubstInitializer = |
| 2903 | SemaRef.SubstExpr(D->getInitializer(), TemplateArgs).get(); |
| 2904 | } else { |
| 2905 | IsCorrect = IsCorrect && OmpPrivParm->hasInit(); |
| 2906 | } |
| 2907 | SemaRef.ActOnOpenMPDeclareReductionInitializerEnd( |
| 2908 | NewDRD, SubstInitializer, OmpPrivParm); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2909 | } |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2910 | IsCorrect = |
| 2911 | IsCorrect && SubstCombiner && |
| 2912 | (!D->getInitializer() || |
| 2913 | (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && |
| 2914 | SubstInitializer) || |
| 2915 | (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && |
| 2916 | !SubstInitializer && !SubstInitializer)); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2917 | } else { |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2918 | IsCorrect = false; |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2919 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2920 | |
| 2921 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd(/*S=*/nullptr, DRD, |
| 2922 | IsCorrect); |
| 2923 | |
| 2924 | return NewDRD; |
| 2925 | } |
| 2926 | |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 2927 | Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( |
| 2928 | OMPCapturedExprDecl * /*D*/) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 2929 | llvm_unreachable("Should not be met in templates"); |
| 2930 | } |
| 2931 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2932 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2933 | return VisitFunctionDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2936 | Decl * |
| 2937 | TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
Richard Smith | 2600c63 | 2018-05-30 20:24:10 +0000 | [diff] [blame] | 2938 | Decl *Inst = VisitFunctionDecl(D, nullptr); |
| 2939 | if (Inst && !D->getDescribedFunctionTemplate()) |
| 2940 | Owner->addDecl(Inst); |
| 2941 | return Inst; |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2944 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2945 | return VisitCXXMethodDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 2949 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 2950 | } |
| 2951 | |
| 2952 | Decl * |
| 2953 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 2954 | ClassTemplateSpecializationDecl *D) { |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2955 | // As a MS extension, we permit class-scope explicit specialization |
| 2956 | // of member class templates. |
| 2957 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 2958 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 2959 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 2960 | "can only instantiate an explicit specialization " |
| 2961 | "for a member class template"); |
| 2962 | |
| 2963 | // Lookup the already-instantiated declaration in the instantiation |
| 2964 | // of the class template. FIXME: Diagnose or assert if this fails? |
| 2965 | DeclContext::lookup_result Found |
| 2966 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 2967 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2968 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2969 | ClassTemplateDecl *InstClassTemplate |
| 2970 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 2971 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2972 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2973 | |
| 2974 | // Substitute into the template arguments of the class template explicit |
| 2975 | // specialization. |
| 2976 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 2977 | castAs<TemplateSpecializationTypeLoc>(); |
| 2978 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 2979 | Loc.getRAngleLoc()); |
| 2980 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 2981 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 2982 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 2983 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 2984 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2985 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2986 | |
| 2987 | // Check that the template argument list is well-formed for this |
| 2988 | // class template. |
| 2989 | SmallVector<TemplateArgument, 4> Converted; |
| 2990 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 2991 | D->getLocation(), |
| 2992 | InstTemplateArgs, |
| 2993 | false, |
| 2994 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2995 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2996 | |
| 2997 | // Figure out where to insert this class template explicit specialization |
| 2998 | // in the member template's set of class template explicit specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2999 | void *InsertPos = nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3000 | ClassTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3001 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3002 | |
| 3003 | // Check whether we've already seen a conflicting instantiation of this |
| 3004 | // declaration (for instance, if there was a prior implicit instantiation). |
| 3005 | bool Ignored; |
| 3006 | if (PrevDecl && |
| 3007 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 3008 | D->getSpecializationKind(), |
| 3009 | PrevDecl, |
| 3010 | PrevDecl->getSpecializationKind(), |
| 3011 | PrevDecl->getPointOfInstantiation(), |
| 3012 | Ignored)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3013 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3014 | |
| 3015 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 3016 | // This happens in cases like: |
| 3017 | // |
| 3018 | // template<typename T, typename U> |
| 3019 | // struct Outer { |
| 3020 | // template<typename X> struct Inner; |
| 3021 | // template<> struct Inner<T> {}; |
| 3022 | // template<> struct Inner<U> {}; |
| 3023 | // }; |
| 3024 | // |
| 3025 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 3026 | // // have the same signature. |
| 3027 | if (PrevDecl && PrevDecl->getDefinition() && |
| 3028 | D->isThisDeclarationADefinition()) { |
| 3029 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 3030 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 3031 | diag::note_previous_definition); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3032 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3036 | ClassTemplateSpecializationDecl *InstD = |
| 3037 | ClassTemplateSpecializationDecl::Create( |
| 3038 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 3039 | D->getLocation(), InstClassTemplate, Converted, PrevDecl); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3040 | |
| 3041 | // Add this partial specialization to the set of class template partial |
| 3042 | // specializations. |
| 3043 | if (!PrevDecl) |
| 3044 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 3045 | |
| 3046 | // Substitute the nested name specifier, if any. |
| 3047 | if (SubstQualifier(D, InstD)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3048 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3049 | |
| 3050 | // Build the canonical type that describes the converted template |
| 3051 | // arguments of the class template explicit specialization. |
| 3052 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3053 | TemplateName(InstClassTemplate), Converted, |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3054 | SemaRef.Context.getRecordType(InstD)); |
| 3055 | |
| 3056 | // Build the fully-sugared type for this class template |
| 3057 | // specialization as the user wrote in the specialization |
| 3058 | // itself. This means that we'll pretty-print the type retrieved |
| 3059 | // from the specialization's declaration the way that the user |
| 3060 | // actually wrote the specialization, rather than formatting the |
| 3061 | // name based on the "canonical" representation used to store the |
| 3062 | // template arguments in the specialization. |
| 3063 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3064 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 3065 | CanonType); |
| 3066 | |
| 3067 | InstD->setAccess(D->getAccess()); |
| 3068 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 3069 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 3070 | InstD->setTypeAsWritten(WrittenTy); |
| 3071 | InstD->setExternLoc(D->getExternLoc()); |
| 3072 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 3073 | |
| 3074 | Owner->addDecl(InstD); |
| 3075 | |
| 3076 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 3077 | // We don't have support for lazy instantiation of an explicit specialization |
| 3078 | // yet, and MSVC eagerly instantiates in this case. |
| 3079 | if (D->isThisDeclarationADefinition() && |
| 3080 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 3081 | TSK_ImplicitInstantiation, |
| 3082 | /*Complain=*/true)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3083 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3084 | |
| 3085 | return InstD; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3088 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3089 | VarTemplateSpecializationDecl *D) { |
| 3090 | |
| 3091 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 3092 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 3093 | assert(VarTemplate && |
| 3094 | "A template specialization without specialized template?"); |
| 3095 | |
| 3096 | // Substitute the current template arguments. |
| 3097 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 3098 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 3099 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 3100 | |
| 3101 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 3102 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3103 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3104 | |
| 3105 | // Check that the template argument list is well-formed for this template. |
| 3106 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3107 | if (SemaRef.CheckTemplateArgumentList( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3108 | VarTemplate, VarTemplate->getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3109 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3110 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3111 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3112 | |
| 3113 | // Find the variable template specialization declaration that |
| 3114 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3115 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3116 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3117 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3118 | // If we already have a variable template specialization, return it. |
| 3119 | return VarSpec; |
| 3120 | |
| 3121 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, |
| 3122 | VarTemplateArgsInfo, Converted); |
| 3123 | } |
| 3124 | |
| 3125 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3126 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 3127 | const TemplateArgumentListInfo &TemplateArgsInfo, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 3128 | ArrayRef<TemplateArgument> Converted) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3129 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3130 | // Do substitution on the type of the declaration |
| 3131 | TypeSourceInfo *DI = |
| 3132 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 3133 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 3134 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3135 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3136 | |
| 3137 | if (DI->getType()->isFunctionType()) { |
| 3138 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 3139 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3140 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | // Build the instantiated declaration |
| 3144 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 3145 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3146 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3147 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3148 | if (InsertPos) |
| 3149 | VarTemplate->AddSpecialization(Var, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3150 | |
| 3151 | // Substitute the nested name specifier, if any. |
| 3152 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3153 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3154 | |
| 3155 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3156 | Owner, StartingScope); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3157 | |
| 3158 | return Var; |
| 3159 | } |
| 3160 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3161 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 3162 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 3163 | } |
| 3164 | |
| 3165 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 3166 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 3167 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 3168 | DiagnosticsEngine::Error, |
| 3169 | "cannot instantiate %0 yet"); |
| 3170 | SemaRef.Diag(D->getLocation(), DiagID) |
| 3171 | << D->getDeclKindName(); |
| 3172 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3173 | return nullptr; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 3177 | llvm_unreachable("Unexpected decl"); |
| 3178 | } |
| 3179 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3180 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3181 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 3182 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3183 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3184 | return nullptr; |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 3186 | return Instantiator.Visit(D); |
| 3187 | } |
| 3188 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3189 | /// Instantiates a nested template parameter list in the current |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3190 | /// instantiation context. |
| 3191 | /// |
| 3192 | /// \param L The parameter list to instantiate |
| 3193 | /// |
| 3194 | /// \returns NULL if there was an error |
| 3195 | TemplateParameterList * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3196 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3197 | // Get errors for all the parameters before bailing out. |
| 3198 | bool Invalid = false; |
| 3199 | |
| 3200 | unsigned N = L->size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3201 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3202 | ParamVector Params; |
| 3203 | Params.reserve(N); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 3204 | for (auto &P : *L) { |
| 3205 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3206 | Params.push_back(D); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 3207 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | // Clean up if we had an error. |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 3211 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3212 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3213 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3214 | // Note: we substitute into associated constraints later |
| 3215 | Expr *const UninstantiatedRequiresClause = L->getRequiresClause(); |
| 3216 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3217 | TemplateParameterList *InstL |
| 3218 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 3219 | L->getLAngleLoc(), Params, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3220 | L->getRAngleLoc(), |
| 3221 | UninstantiatedRequiresClause); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3222 | return InstL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3223 | } |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3224 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 3225 | TemplateParameterList * |
| 3226 | Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, |
| 3227 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3228 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 3229 | return Instantiator.SubstTemplateParams(Params); |
| 3230 | } |
| 3231 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3232 | /// Instantiate the declaration of a class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3233 | /// specialization. |
| 3234 | /// |
| 3235 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 3236 | // specialized by the instantiation of \p PartialSpec. |
| 3237 | /// |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3238 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3239 | /// specialization that we are instantiating. |
| 3240 | /// |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3241 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3242 | /// NULL to indicate an error. |
| 3243 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3244 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 3245 | ClassTemplateDecl *ClassTemplate, |
| 3246 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3247 | // Create a local instantiation scope for this class template partial |
| 3248 | // specialization, which will contain the instantiations of the template |
| 3249 | // parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3250 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3251 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3252 | // Substitute into the template parameters of the class template partial |
| 3253 | // specialization. |
| 3254 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3255 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3256 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3257 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3259 | // Substitute into the template arguments of the class template partial |
| 3260 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3261 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3262 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3263 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3264 | TemplArgInfo->RAngleLoc); |
| 3265 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3266 | TemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 3267 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3268 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3269 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3270 | // Check that the template argument list is well-formed for this |
| 3271 | // class template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3272 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3273 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3274 | PartialSpec->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3275 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3276 | false, |
| 3277 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3278 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3279 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3280 | // Check these arguments are valid for a template partial specialization. |
| 3281 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3282 | PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), |
| 3283 | Converted)) |
| 3284 | return nullptr; |
| 3285 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3286 | // Figure out where to insert this class template partial specialization |
| 3287 | // in the member template's set of class template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3288 | void *InsertPos = nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3289 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3290 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3292 | // Build the canonical type that describes the converted template |
| 3293 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3294 | QualType CanonType |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3295 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3296 | Converted); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Build the fully-sugared type for this class template |
| 3299 | // specialization as the user wrote in the specialization |
| 3300 | // itself. This means that we'll pretty-print the type retrieved |
| 3301 | // from the specialization's declaration the way that the user |
| 3302 | // actually wrote the specialization, rather than formatting the |
| 3303 | // name based on the "canonical" representation used to store the |
| 3304 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3305 | TypeSourceInfo *WrittenTy |
| 3306 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3307 | TemplateName(ClassTemplate), |
| 3308 | PartialSpec->getLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3309 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3310 | CanonType); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3311 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3312 | if (PrevDecl) { |
| 3313 | // We've already seen a partial specialization with the same template |
| 3314 | // parameters and template arguments. This can happen, for example, when |
| 3315 | // substituting the outer template arguments ends up causing two |
| 3316 | // class template partial specializations of a member class template |
| 3317 | // to have identical forms, e.g., |
| 3318 | // |
| 3319 | // template<typename T, typename U> |
| 3320 | // struct Outer { |
| 3321 | // template<typename X, typename Y> struct Inner; |
| 3322 | // template<typename Y> struct Inner<T, Y>; |
| 3323 | // template<typename Y> struct Inner<U, Y>; |
| 3324 | // }; |
| 3325 | // |
| 3326 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3327 | // // have the same signature. |
| 3328 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3329 | << WrittenTy->getType(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3330 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 3331 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3332 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3333 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3334 | |
| 3335 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3336 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3337 | ClassTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3338 | ClassTemplatePartialSpecializationDecl::Create( |
| 3339 | SemaRef.Context, PartialSpec->getTagKind(), Owner, |
| 3340 | PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, |
| 3341 | ClassTemplate, Converted, InstTemplateArgs, CanonType, nullptr); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3342 | // Substitute the nested name specifier, if any. |
| 3343 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3344 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3346 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 3347 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3348 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3349 | // Check the completed partial specialization. |
| 3350 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3351 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3352 | // Add this partial specialization to the set of class template partial |
| 3353 | // specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3354 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 3355 | /*InsertPos=*/nullptr); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3356 | return InstPartialSpec; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3359 | /// Instantiate the declaration of a variable template partial |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3360 | /// specialization. |
| 3361 | /// |
| 3362 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 3363 | /// specialized by the instantiation of \p PartialSpec. |
| 3364 | /// |
| 3365 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 3366 | /// specialization that we are instantiating. |
| 3367 | /// |
| 3368 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3369 | /// NULL to indicate an error. |
| 3370 | VarTemplatePartialSpecializationDecl * |
| 3371 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 3372 | VarTemplateDecl *VarTemplate, |
| 3373 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 3374 | // Create a local instantiation scope for this variable template partial |
| 3375 | // specialization, which will contain the instantiations of the template |
| 3376 | // parameters. |
| 3377 | LocalInstantiationScope Scope(SemaRef); |
| 3378 | |
| 3379 | // Substitute into the template parameters of the variable template partial |
| 3380 | // specialization. |
| 3381 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3382 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3383 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3384 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3385 | |
| 3386 | // Substitute into the template arguments of the variable template partial |
| 3387 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3388 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3389 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3390 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3391 | TemplArgInfo->RAngleLoc); |
| 3392 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3393 | TemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3394 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3395 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3396 | |
| 3397 | // Check that the template argument list is well-formed for this |
| 3398 | // class template. |
| 3399 | SmallVector<TemplateArgument, 4> Converted; |
| 3400 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 3401 | InstTemplateArgs, false, Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3402 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3403 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3404 | // Check these arguments are valid for a template partial specialization. |
| 3405 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3406 | PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), |
| 3407 | Converted)) |
| 3408 | return nullptr; |
| 3409 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3410 | // Figure out where to insert this variable template partial specialization |
| 3411 | // in the member template's set of variable template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3412 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3413 | VarTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3414 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3415 | |
| 3416 | // Build the canonical type that describes the converted template |
| 3417 | // arguments of the variable template partial specialization. |
| 3418 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3419 | TemplateName(VarTemplate), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3420 | |
| 3421 | // Build the fully-sugared type for this variable template |
| 3422 | // specialization as the user wrote in the specialization |
| 3423 | // itself. This means that we'll pretty-print the type retrieved |
| 3424 | // from the specialization's declaration the way that the user |
| 3425 | // actually wrote the specialization, rather than formatting the |
| 3426 | // name based on the "canonical" representation used to store the |
| 3427 | // template arguments in the specialization. |
| 3428 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3429 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 3430 | CanonType); |
| 3431 | |
| 3432 | if (PrevDecl) { |
| 3433 | // We've already seen a partial specialization with the same template |
| 3434 | // parameters and template arguments. This can happen, for example, when |
| 3435 | // substituting the outer template arguments ends up causing two |
| 3436 | // variable template partial specializations of a member variable template |
| 3437 | // to have identical forms, e.g., |
| 3438 | // |
| 3439 | // template<typename T, typename U> |
| 3440 | // struct Outer { |
| 3441 | // template<typename X, typename Y> pair<X,Y> p; |
| 3442 | // template<typename Y> pair<T, Y> p; |
| 3443 | // template<typename Y> pair<U, Y> p; |
| 3444 | // }; |
| 3445 | // |
| 3446 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3447 | // // have the same signature. |
| 3448 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3449 | diag::err_var_partial_spec_redeclared) |
| 3450 | << WrittenTy->getType(); |
| 3451 | SemaRef.Diag(PrevDecl->getLocation(), |
| 3452 | diag::note_var_prev_partial_spec_here); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3453 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | // Do substitution on the type of the declaration |
| 3457 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 3458 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 3459 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 3460 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3461 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3462 | |
| 3463 | if (DI->getType()->isFunctionType()) { |
| 3464 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3465 | diag::err_variable_instantiates_to_function) |
| 3466 | << PartialSpec->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3467 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | // Create the variable template partial specialization declaration. |
| 3471 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3472 | VarTemplatePartialSpecializationDecl::Create( |
| 3473 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 3474 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3475 | DI, PartialSpec->getStorageClass(), Converted, InstTemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Substitute the nested name specifier, if any. |
| 3478 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3479 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3480 | |
| 3481 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 3482 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 3483 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3484 | // Check the completed partial specialization. |
| 3485 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3486 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3487 | // Add this partial specialization to the set of variable template partial |
| 3488 | // specializations. The instantiation of the initializer is not necessary. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3489 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3490 | |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3491 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3492 | LateAttrs, Owner, StartingScope); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3493 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3494 | return InstPartialSpec; |
| 3495 | } |
| 3496 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3497 | TypeSourceInfo* |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3498 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3499 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3500 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 3501 | assert(OldTInfo && "substituting function without type source info"); |
| 3502 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3503 | |
| 3504 | CXXRecordDecl *ThisContext = nullptr; |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3505 | Qualifiers ThisTypeQuals; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3506 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3507 | ThisContext = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3508 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 3509 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3510 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 3511 | TypeSourceInfo *NewTInfo |
| 3512 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 3513 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3514 | D->getDeclName(), |
| 3515 | ThisContext, ThisTypeQuals); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3516 | if (!NewTInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3517 | return nullptr; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3518 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3519 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 3520 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 3521 | if (NewTInfo != OldTInfo) { |
| 3522 | // Get parameters from the new type info. |
Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 3523 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3524 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3525 | unsigned NewIdx = 0; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3526 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3527 | OldIdx != NumOldParams; ++OldIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3528 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3529 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 3530 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3531 | Optional<unsigned> NumArgumentsInExpansion; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3532 | if (OldParam->isParameterPack()) |
| 3533 | NumArgumentsInExpansion = |
| 3534 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 3535 | TemplateArgs); |
| 3536 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3537 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3538 | // instantiated to a (still-dependent) parameter pack. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3539 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3540 | Params.push_back(NewParam); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3541 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 3542 | } else { |
| 3543 | // Parameter pack expansion: make the instantiation an argument pack. |
| 3544 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 3545 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3546 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3547 | Params.push_back(NewParam); |
| 3548 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 3549 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3550 | } |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3551 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3552 | } else { |
| 3553 | // The function type itself was not dependent and therefore no |
| 3554 | // substitution occurred. However, we still need to instantiate |
| 3555 | // the function parameters themselves. |
| 3556 | const FunctionProtoType *OldProto = |
| 3557 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3558 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 3559 | ++i) { |
| 3560 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3561 | if (!OldParam) { |
| 3562 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3563 | D, D->getLocation(), OldProto->getParamType(i))); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3564 | continue; |
| 3565 | } |
| 3566 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3567 | ParmVarDecl *Parm = |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3568 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3569 | if (!Parm) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3570 | return nullptr; |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3571 | Params.push_back(Parm); |
| 3572 | } |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3573 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3574 | } else { |
| 3575 | // If the type of this function, after ignoring parentheses, is not |
| 3576 | // *directly* a function type, then we're instantiating a function that |
| 3577 | // was declared via a typedef or with attributes, e.g., |
| 3578 | // |
| 3579 | // typedef int functype(int, int); |
| 3580 | // functype func; |
| 3581 | // int __cdecl meth(int, int); |
| 3582 | // |
| 3583 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 3584 | // synthesized in the method declaration. |
| 3585 | SmallVector<QualType, 4> ParamTypes; |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3586 | Sema::ExtParameterInfoBuilder ExtParamInfos; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3587 | if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, |
| 3588 | TemplateArgs, ParamTypes, &Params, |
| 3589 | ExtParamInfos)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3590 | return nullptr; |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3591 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3592 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3593 | return NewTInfo; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3596 | /// Introduce the instantiated function parameters into the local |
| 3597 | /// instantiation scope, and set the parameter names to those used |
| 3598 | /// in the template. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3599 | static bool addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3600 | const FunctionDecl *PatternDecl, |
| 3601 | LocalInstantiationScope &Scope, |
| 3602 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3603 | unsigned FParamIdx = 0; |
| 3604 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 3605 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 3606 | if (!PatternParam->isParameterPack()) { |
| 3607 | // Simple case: not a parameter pack. |
| 3608 | assert(FParamIdx < Function->getNumParams()); |
| 3609 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3610 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3611 | // If the parameter's type is not dependent, update it to match the type |
| 3612 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 3613 | // 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] | 3614 | // per core issue 1668. Substitute into the type from the pattern, in case |
| 3615 | // it's instantiation-dependent. |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3616 | // FIXME: Updating the type to work around this is at best fragile. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3617 | if (!PatternDecl->getType()->isDependentType()) { |
| 3618 | QualType T = S.SubstType(PatternParam->getType(), TemplateArgs, |
| 3619 | FunctionParam->getLocation(), |
| 3620 | FunctionParam->getDeclName()); |
| 3621 | if (T.isNull()) |
| 3622 | return true; |
| 3623 | FunctionParam->setType(T); |
| 3624 | } |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3625 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3626 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3627 | ++FParamIdx; |
| 3628 | continue; |
| 3629 | } |
| 3630 | |
| 3631 | // Expand the parameter pack. |
| 3632 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3633 | Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3634 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3635 | assert(NumArgumentsInExpansion && |
| 3636 | "should only be called when all template arguments are known"); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3637 | QualType PatternType = |
| 3638 | PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3639 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3640 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3641 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3642 | if (!PatternDecl->getType()->isDependentType()) { |
| 3643 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, Arg); |
| 3644 | QualType T = S.SubstType(PatternType, TemplateArgs, |
| 3645 | FunctionParam->getLocation(), |
| 3646 | FunctionParam->getDeclName()); |
| 3647 | if (T.isNull()) |
| 3648 | return true; |
| 3649 | FunctionParam->setType(T); |
| 3650 | } |
| 3651 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3652 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3653 | ++FParamIdx; |
| 3654 | } |
| 3655 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3656 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3657 | return false; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3661 | FunctionDecl *Decl) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3662 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3663 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3664 | return; |
| 3665 | |
| 3666 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3667 | InstantiatingTemplate::ExceptionSpecification()); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3668 | if (Inst.isInvalid()) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3669 | // We hit the instantiation depth limit. Clear the exception specification |
| 3670 | // so that our callers don't have to cope with EST_Uninstantiated. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3671 | UpdateExceptionSpec(Decl, EST_None); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3672 | return; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3673 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3674 | if (Inst.isAlreadyInstantiating()) { |
| 3675 | // This exception specification indirectly depends on itself. Reject. |
| 3676 | // FIXME: Corresponding rule in the standard? |
| 3677 | Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; |
| 3678 | UpdateExceptionSpec(Decl, EST_None); |
| 3679 | return; |
| 3680 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3681 | |
| 3682 | // Enter the scope of this instantiation. We don't use |
| 3683 | // PushDeclContext because we don't have a scope. |
| 3684 | Sema::ContextRAII savedContext(*this, Decl); |
| 3685 | LocalInstantiationScope Scope(*this); |
| 3686 | |
| 3687 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3688 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3689 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3690 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3691 | if (addInstantiatedParametersToScope(*this, Decl, Template, Scope, |
| 3692 | TemplateArgs)) { |
| 3693 | UpdateExceptionSpec(Decl, EST_None); |
| 3694 | return; |
| 3695 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3696 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3697 | SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), |
| 3698 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3701 | /// Initializes the common fields of an instantiation function |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3702 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3703 | /// |
| 3704 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3705 | bool |
| 3706 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3707 | FunctionDecl *Tmpl) { |
David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3708 | if (Tmpl->isDeleted()) |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3709 | New->setDeletedAsWritten(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3711 | New->setImplicit(Tmpl->isImplicit()); |
| 3712 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3713 | // Forward the mangling number from the template to the instantiated decl. |
| 3714 | SemaRef.Context.setManglingNumber(New, |
| 3715 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3716 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3717 | // If we are performing substituting explicitly-specified template arguments |
| 3718 | // or deduced template arguments into a function template and we reach this |
| 3719 | // 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] | 3720 | // to keeping the new function template specialization. We therefore |
| 3721 | // convert the active template instantiation for the function template |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3722 | // into a template instantiation for this specific function template |
| 3723 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3724 | // further errors in the declaration itself. |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3725 | typedef Sema::CodeSynthesisContext ActiveInstType; |
| 3726 | ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3727 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3728 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3730 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3732 | "Deduction from the wrong function template?"); |
Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3733 | (void) FunTmpl; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3734 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3735 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3736 | ActiveInst.Entity = New; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3737 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3738 | } |
| 3739 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3741 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3742 | assert(Proto && "Function template without prototype?"); |
| 3743 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3744 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3745 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3746 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3747 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3748 | // exception specification. |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3749 | // DR1484: Local classes and their members are instantiated along with the |
| 3750 | // containing function. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3751 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3752 | EPI.ExceptionSpec.Type != EST_None && |
| 3753 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3754 | EPI.ExceptionSpec.Type != EST_BasicNoexcept && |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 3755 | !Tmpl->isLexicallyWithinFunctionOrMethod()) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3756 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3757 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3758 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3759 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3760 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3761 | NewEST = EST_Unevaluated; |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3762 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3763 | // Mark the function has having an uninstantiated exception specification. |
| 3764 | const FunctionProtoType *NewProto |
| 3765 | = New->getType()->getAs<FunctionProtoType>(); |
| 3766 | assert(NewProto && "Template instantiation without function prototype?"); |
| 3767 | EPI = NewProto->getExtProtoInfo(); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3768 | EPI.ExceptionSpec.Type = NewEST; |
| 3769 | EPI.ExceptionSpec.SourceDecl = New; |
| 3770 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3771 | New->setType(SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3772 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3773 | } else { |
Faisal Vali | 40fd4ce | 2017-05-09 04:17:15 +0000 | [diff] [blame] | 3774 | Sema::ContextRAII SwitchContext(SemaRef, New); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3775 | SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3776 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3779 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3780 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3781 | Tmpl->isDefined(Definition); |
| 3782 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3783 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 3784 | LateAttrs, StartingScope); |
Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3785 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3786 | return false; |
| 3787 | } |
| 3788 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3789 | /// Initializes common fields of an instantiated method |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3790 | /// declaration (New) from the corresponding fields of its template |
| 3791 | /// (Tmpl). |
| 3792 | /// |
| 3793 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3794 | bool |
| 3795 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3796 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3797 | if (InitFunctionInstantiation(New, Tmpl)) |
| 3798 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3799 | |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 3800 | if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) |
| 3801 | SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); |
| 3802 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3803 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3804 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3805 | New->setVirtualAsWritten(true); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3806 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3807 | // FIXME: New needs a pointer to Tmpl |
| 3808 | return false; |
| 3809 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3810 | |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 3811 | /// Instantiate (or find existing instantiation of) a function template with a |
| 3812 | /// given set of template arguments. |
| 3813 | /// |
| 3814 | /// Usually this should not be used, and template argument deduction should be |
| 3815 | /// used in its place. |
| 3816 | FunctionDecl * |
| 3817 | Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, |
| 3818 | const TemplateArgumentList *Args, |
| 3819 | SourceLocation Loc) { |
| 3820 | FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 3821 | |
| 3822 | sema::TemplateDeductionInfo Info(Loc); |
| 3823 | InstantiatingTemplate Inst( |
| 3824 | *this, Loc, FTD, Args->asArray(), |
| 3825 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 3826 | if (Inst.isInvalid()) |
| 3827 | return nullptr; |
| 3828 | |
| 3829 | ContextRAII SavedContext(*this, FD); |
| 3830 | MultiLevelTemplateArgumentList MArgs(*Args); |
| 3831 | |
| 3832 | return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); |
| 3833 | } |
| 3834 | |
Reid Kleckner | 61195e1 | 2017-01-05 01:08:22 +0000 | [diff] [blame] | 3835 | /// In the MS ABI, we need to instantiate default arguments of dllexported |
| 3836 | /// default constructors along with the constructor definition. This allows IR |
| 3837 | /// gen to emit a constructor closure which calls the default constructor with |
| 3838 | /// its default arguments. |
| 3839 | static void InstantiateDefaultCtorDefaultArgs(Sema &S, |
| 3840 | CXXConstructorDecl *Ctor) { |
| 3841 | assert(S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3842 | Ctor->isDefaultConstructor()); |
| 3843 | unsigned NumParams = Ctor->getNumParams(); |
| 3844 | if (NumParams == 0) |
| 3845 | return; |
| 3846 | DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); |
| 3847 | if (!Attr) |
| 3848 | return; |
| 3849 | for (unsigned I = 0; I != NumParams; ++I) { |
| 3850 | (void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, |
| 3851 | Ctor->getParamDecl(I)); |
| 3852 | S.DiscardCleanupsInEvaluationContext(); |
| 3853 | } |
| 3854 | } |
| 3855 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3856 | /// Instantiate the definition of the given function from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3857 | /// template. |
| 3858 | /// |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3859 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3860 | /// required. Note that this is not precisely a "point of instantiation" |
| 3861 | /// for the function, but it's close. |
| 3862 | /// |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3863 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3864 | /// function template specialization or member function of a class template |
| 3865 | /// specialization. |
| 3866 | /// |
| 3867 | /// \param Recursive if true, recursively instantiates any functions that |
| 3868 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3869 | /// |
| 3870 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3871 | /// instantiation where the body of the function is required. Complain if |
| 3872 | /// there is no such body. |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3873 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3874 | FunctionDecl *Function, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3875 | bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 3876 | bool DefinitionRequired, |
| 3877 | bool AtEndOfTU) { |
Richard Smith | cb18957 | 2017-10-28 01:15:00 +0000 | [diff] [blame] | 3878 | if (Function->isInvalidDecl() || Function->isDefined() || |
| 3879 | isa<CXXDeductionGuideDecl>(Function)) |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3880 | return; |
| 3881 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3882 | // Never instantiate an explicit specialization except if it is a class scope |
| 3883 | // explicit specialization. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3884 | TemplateSpecializationKind TSK = Function->getTemplateSpecializationKind(); |
| 3885 | if (TSK == TSK_ExplicitSpecialization && |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3886 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3887 | return; |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3888 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3889 | // Find the function body that we'll be substituting. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3890 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3891 | assert(PatternDecl && "instantiating a non-template"); |
| 3892 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3893 | const FunctionDecl *PatternDef = PatternDecl->getDefinition(); |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 3894 | Stmt *Pattern = nullptr; |
| 3895 | if (PatternDef) { |
| 3896 | Pattern = PatternDef->getBody(PatternDef); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3897 | PatternDecl = PatternDef; |
Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 3898 | if (PatternDef->willHaveBody()) |
| 3899 | PatternDef = nullptr; |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 3900 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3901 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3902 | // FIXME: We need to track the instantiation stack in order to know which |
| 3903 | // definitions should be visible within this instantiation. |
| 3904 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, |
| 3905 | Function->getInstantiatedFromMemberFunction(), |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3906 | PatternDecl, PatternDef, TSK, |
| 3907 | /*Complain*/DefinitionRequired)) { |
| 3908 | if (DefinitionRequired) |
| 3909 | Function->setInvalidDecl(); |
| 3910 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 3911 | // Try again at the end of the translation unit (at which point a |
| 3912 | // definition will be required). |
| 3913 | assert(!Recursive); |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 3914 | Function->setInstantiationIsPending(true); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3915 | PendingInstantiations.push_back( |
| 3916 | std::make_pair(Function, PointOfInstantiation)); |
| 3917 | } else if (TSK == TSK_ImplicitInstantiation) { |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 3918 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3919 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3920 | Diag(PointOfInstantiation, diag::warn_func_template_missing) |
| 3921 | << Function; |
| 3922 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 3923 | if (getLangOpts().CPlusPlus11) |
| 3924 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) |
| 3925 | << Function; |
| 3926 | } |
| 3927 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3928 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3929 | return; |
| 3930 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3931 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3932 | // Postpone late parsed template instantiations. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3933 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 3934 | !LateTemplateParser) { |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 3935 | Function->setInstantiationIsPending(true); |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 3936 | LateParsedInstantiations.push_back( |
| 3937 | std::make_pair(Function, PointOfInstantiation)); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3938 | return; |
| 3939 | } |
| 3940 | |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3941 | // If we're performing recursive template instantiation, create our own |
| 3942 | // queue of pending implicit instantiations that we will instantiate later, |
| 3943 | // while we're still within our own instantiation context. |
| 3944 | // This has to happen before LateTemplateParser below is called, so that |
| 3945 | // it marks vtables used in late parsed templates as used. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 3946 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 3947 | /*Enabled=*/Recursive); |
| 3948 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3949 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 3950 | // Call the LateTemplateParser callback if there is a need to late parse |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3951 | // a templated function definition. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3952 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3953 | LateTemplateParser) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3954 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 3955 | if (PatternDecl->isFromASTFile()) |
| 3956 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 3957 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 3958 | auto LPTIter = LateParsedTemplateMap.find(PatternDecl); |
| 3959 | assert(LPTIter != LateParsedTemplateMap.end() && |
| 3960 | "missing LateParsedTemplate"); |
| 3961 | LateTemplateParser(OpaqueParser, *LPTIter->second); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3962 | Pattern = PatternDecl->getBody(PatternDecl); |
| 3963 | } |
| 3964 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3965 | // Note, we should never try to instantiate a deleted function template. |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3966 | assert((Pattern || PatternDecl->isDefaulted() || |
| 3967 | PatternDecl->hasSkippedBody()) && |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3968 | "unexpected kind of function template definition"); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3969 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3970 | // C++1y [temp.explicit]p10: |
| 3971 | // Except for inline functions, declarations with types deduced from their |
| 3972 | // initializer or return value, and class template specializations, other |
| 3973 | // explicit instantiation declarations have the effect of suppressing the |
| 3974 | // implicit instantiation of the entity to which they refer. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3975 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3976 | !PatternDecl->isInlined() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3977 | !PatternDecl->getReturnType()->getContainedAutoType()) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3978 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | |
Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 3980 | if (PatternDecl->isInlined()) { |
| 3981 | // Function, and all later redeclarations of it (from imported modules, |
| 3982 | // for instance), are now implicitly inline. |
| 3983 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 3984 | D = D->getPreviousDecl()) { |
| 3985 | D->setImplicitlyInline(); |
| 3986 | if (D == Function) |
| 3987 | break; |
| 3988 | } |
| 3989 | } |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3991 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3992 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3993 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 3994 | PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 3995 | "instantiating function definition"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3996 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3997 | // The instantiation is visible here, even if it was first declared in an |
| 3998 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 3999 | Function->setVisibleDespiteOwningModule(); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4000 | |
Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 4001 | // Copy the inner loc start from the pattern. |
| 4002 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 4003 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4004 | EnterExpressionEvaluationContext EvalContext( |
| 4005 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 4006 | |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 4007 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4008 | // recorded, unless we're actually a member function within a local |
| 4009 | // class, in which case we need to merge our results with the parent |
| 4010 | // scope (of the enclosing function). |
| 4011 | bool MergeWithParentScope = false; |
| 4012 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 4013 | MergeWithParentScope = Rec->isLocalClass(); |
| 4014 | |
| 4015 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4017 | if (PatternDecl->isDefaulted()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4018 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4019 | else { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 4020 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4021 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
| 4022 | |
| 4023 | // Substitute into the qualifier; we can get a substitution failure here |
| 4024 | // through evil use of alias templates. |
| 4025 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 4026 | // of the) lexical context of the pattern? |
| 4027 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 4028 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4029 | ActOnStartOfFunctionDef(nullptr, Function); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4030 | |
| 4031 | // Enter the scope of this instantiation. We don't use |
| 4032 | // PushDeclContext because we don't have a scope. |
| 4033 | Sema::ContextRAII savedContext(*this, Function); |
| 4034 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 4035 | if (addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 4036 | TemplateArgs)) |
| 4037 | return; |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4038 | |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4039 | StmtResult Body; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4040 | if (PatternDecl->hasSkippedBody()) { |
| 4041 | ActOnSkippedFunctionBody(Function); |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4042 | Body = nullptr; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4043 | } else { |
Ilya Biryukov | 95f0d32 | 2017-12-28 13:05:46 +0000 | [diff] [blame] | 4044 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { |
| 4045 | // If this is a constructor, instantiate the member initializers. |
| 4046 | InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), |
| 4047 | TemplateArgs); |
| 4048 | |
| 4049 | // If this is an MS ABI dllexport default constructor, instantiate any |
| 4050 | // default arguments. |
| 4051 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4052 | Ctor->isDefaultConstructor()) { |
| 4053 | InstantiateDefaultCtorDefaultArgs(*this, Ctor); |
| 4054 | } |
| 4055 | } |
| 4056 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4057 | // Instantiate the function body. |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4058 | Body = SubstStmt(Pattern, TemplateArgs); |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4059 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4060 | if (Body.isInvalid()) |
| 4061 | Function->setInvalidDecl(); |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4062 | } |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4063 | // FIXME: finishing the function body while in an expression evaluation |
| 4064 | // context seems wrong. Investigate more. |
| 4065 | ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4066 | |
| 4067 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 4068 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4069 | if (auto *Listener = getASTMutationListener()) |
| 4070 | Listener->FunctionDefinitionInstantiated(Function); |
Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 4071 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4072 | savedContext.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 4075 | DeclGroupRef DG(Function); |
| 4076 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4078 | // This class may have local implicit instantiations that need to be |
| 4079 | // instantiation within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4080 | LocalInstantiations.perform(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4081 | Scope.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4082 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4085 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 4086 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 4087 | const TemplateArgumentList &TemplateArgList, |
| 4088 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 4089 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4090 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 4091 | LateInstantiatedAttrVec *LateAttrs, |
| 4092 | LocalInstantiationScope *StartingScope) { |
| 4093 | if (FromVar->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4094 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4095 | |
| 4096 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4097 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4098 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4099 | |
| 4100 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4101 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 4102 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4103 | // Instantiate the first declaration of the variable template: for a partial |
| 4104 | // specialization of a static data member template, the first declaration may |
| 4105 | // or may not be the declaration in the class; if it's in the class, we want |
| 4106 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 4107 | // we want to instantiate a definition. |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4108 | // |
| 4109 | // If we're instantiating an explicitly-specialized member template or member |
| 4110 | // partial specialization, don't do this. The member specialization completely |
| 4111 | // replaces the original declaration in this case. |
| 4112 | bool IsMemberSpec = false; |
| 4113 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 4114 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 4115 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 4116 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 4117 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 4118 | if (!IsMemberSpec) |
| 4119 | FromVar = FromVar->getFirstDecl(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4120 | |
Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 4121 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 4122 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 4123 | MultiLevelList); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4124 | |
| 4125 | // TODO: Set LateAttrs and StartingScope ... |
| 4126 | |
| 4127 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 4128 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 4129 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 4130 | } |
| 4131 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4132 | /// Instantiates a variable template specialization by completing it |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4133 | /// with appropriate type information and initializer. |
| 4134 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 4135 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 4136 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4137 | assert(PatternDecl->isThisDeclarationADefinition() && |
| 4138 | "don't have a definition to instantiate from"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4139 | |
| 4140 | // Do substitution on the type of the declaration |
| 4141 | TypeSourceInfo *DI = |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4142 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4143 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 4144 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4145 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4146 | |
| 4147 | // Update the type of this variable template specialization. |
| 4148 | VarSpec->setType(DI->getType()); |
| 4149 | |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4150 | // Convert the declaration into a definition now. |
| 4151 | VarSpec->setCompleteDefinition(); |
| 4152 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4153 | // Instantiate the initializer. |
| 4154 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 4155 | |
| 4156 | return VarSpec; |
| 4157 | } |
| 4158 | |
| 4159 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 4160 | /// Sets basic variable data and decides whether to postpone the |
| 4161 | /// variable instantiation. |
| 4162 | void Sema::BuildVariableInstantiation( |
| 4163 | VarDecl *NewVar, VarDecl *OldVar, |
| 4164 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4165 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 4166 | LocalInstantiationScope *StartingScope, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4167 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4168 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4169 | // If we are instantiating a local extern declaration, the |
| 4170 | // instantiation belongs lexically to the containing function. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4171 | // If we are instantiating a static data member defined |
| 4172 | // out-of-line, the instantiation will have the same lexical |
| 4173 | // context (which will be a namespace scope) as the template. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4174 | if (OldVar->isLocalExternDecl()) { |
| 4175 | NewVar->setLocalExternDecl(); |
| 4176 | NewVar->setLexicalDeclContext(Owner); |
| 4177 | } else if (OldVar->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4178 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 4179 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 4180 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 4181 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4182 | NewVar->setObjCForDecl(OldVar->isObjCForDecl()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4183 | NewVar->setConstexpr(OldVar->isConstexpr()); |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4184 | NewVar->setInitCapture(OldVar->isInitCapture()); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4185 | NewVar->setPreviousDeclInSameBlockScope( |
| 4186 | OldVar->isPreviousDeclInSameBlockScope()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4187 | NewVar->setAccess(OldVar->getAccess()); |
| 4188 | |
Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 4189 | if (!OldVar->isStaticDataMember()) { |
Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 4190 | if (OldVar->isUsed(false)) |
| 4191 | NewVar->setIsUsed(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4192 | NewVar->setReferenced(OldVar->isReferenced()); |
| 4193 | } |
| 4194 | |
| 4195 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 4196 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4197 | LookupResult Previous( |
| 4198 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 4199 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 4200 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4201 | NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 4202 | : forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4203 | |
Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 4204 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 4205 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 4206 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4207 | // We have a previous declaration. Use that one, so we merge with the |
| 4208 | // right type. |
| 4209 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 4210 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 4211 | Previous.addDecl(NewPrev); |
| 4212 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 4213 | OldVar->hasLinkage()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4214 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4215 | CheckVariableDeclaration(NewVar, Previous); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4216 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4217 | if (!InstantiatingVarTemplate) { |
| 4218 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 4219 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4220 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | if (!OldVar->isOutOfLine()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4224 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 4225 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 4226 | } |
| 4227 | |
| 4228 | // Link instantiations of static data members back to the template from |
| 4229 | // which they were instantiated. |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4230 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4231 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 4232 | TSK_ImplicitInstantiation); |
| 4233 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4234 | // Forward the mangling number from the template to the instantiated decl. |
| 4235 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4236 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4237 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4238 | // Delay instantiation of the initializer for variable templates or inline |
| 4239 | // static data members until a definition of the variable is needed. We need |
| 4240 | // it right away if the type contains 'auto'. |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4241 | if ((!isa<VarTemplateSpecializationDecl>(NewVar) && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4242 | !InstantiatingVarTemplate && |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 4243 | !(OldVar->isInline() && OldVar->isThisDeclarationADefinition() && |
| 4244 | !NewVar->isThisDeclarationADefinition())) || |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4245 | NewVar->getType()->isUndeducedType()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4246 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 4247 | |
| 4248 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 4249 | // will have been deferred. |
| 4250 | if (!NewVar->isInvalidDecl() && |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4251 | NewVar->getDeclContext()->isFunctionOrMethod() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4252 | OldVar->getType()->isDependentType()) |
| 4253 | DiagnoseUnusedDecl(NewVar); |
| 4254 | } |
| 4255 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4256 | /// Instantiate the initializer of a variable. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4257 | void Sema::InstantiateVariableInitializer( |
| 4258 | VarDecl *Var, VarDecl *OldVar, |
| 4259 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4260 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4261 | L->VariableDefinitionInstantiated(Var); |
| 4262 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4263 | // We propagate the 'inline' flag with the initializer, because it |
| 4264 | // would otherwise imply that the variable is a definition for a |
| 4265 | // non-static data member. |
| 4266 | if (OldVar->isInlineSpecified()) |
| 4267 | Var->setInlineSpecified(); |
| 4268 | else if (OldVar->isInline()) |
| 4269 | Var->setImplicitlyInline(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4270 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4271 | if (OldVar->getInit()) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 4272 | EnterExpressionEvaluationContext Evaluated( |
| 4273 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4274 | |
| 4275 | // Instantiate the initializer. |
Akira Hatanaka | b87faff | 2016-04-28 23:50:12 +0000 | [diff] [blame] | 4276 | ExprResult Init; |
| 4277 | |
| 4278 | { |
| 4279 | ContextRAII SwitchContext(*this, Var->getDeclContext()); |
| 4280 | Init = SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 4281 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 4282 | } |
| 4283 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4284 | if (!Init.isInvalid()) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4285 | Expr *InitExpr = Init.get(); |
| 4286 | |
Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 4287 | if (Var->hasAttr<DLLImportAttr>() && |
| 4288 | (!InitExpr || |
| 4289 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4290 | // Do not dynamically initialize dllimport variables. |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4291 | } else if (InitExpr) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4292 | bool DirectInit = OldVar->isDirectInit(); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4293 | AddInitializerToDecl(Var, InitExpr, DirectInit); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4294 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4295 | ActOnUninitializedDecl(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4296 | } else { |
| 4297 | // FIXME: Not too happy about invalidating the declaration |
| 4298 | // because of a bogus initializer. |
| 4299 | Var->setInvalidDecl(); |
| 4300 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4301 | } else { |
George Burgess IV | 18b28a8 | 2018-03-20 03:27:44 +0000 | [diff] [blame] | 4302 | // `inline` variables are a definition and declaration all in one; we won't |
| 4303 | // pick up an initializer from anywhere else. |
| 4304 | if (Var->isStaticDataMember() && !Var->isInline()) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4305 | if (!Var->isOutOfLine()) |
| 4306 | return; |
| 4307 | |
| 4308 | // If the declaration inside the class had an initializer, don't add |
| 4309 | // another one to the out-of-line definition. |
| 4310 | if (OldVar->getFirstDecl()->hasInit()) |
| 4311 | return; |
| 4312 | } |
| 4313 | |
| 4314 | // We'll add an initializer to a for-range declaration later. |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4315 | if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4316 | return; |
| 4317 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4318 | ActOnUninitializedDecl(Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4319 | } |
Artem Belevich | e9fa53a | 2018-06-06 22:37:25 +0000 | [diff] [blame] | 4320 | |
| 4321 | if (getLangOpts().CUDA) |
| 4322 | checkAllowedCUDAInitializer(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4325 | /// Instantiate the definition of the given variable from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4326 | /// template. |
| 4327 | /// |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4328 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4329 | /// required. Note that this is not precisely a "point of instantiation" |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4330 | /// for the variable, but it's close. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4331 | /// |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4332 | /// \param Var the already-instantiated declaration of a templated variable. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4333 | /// |
| 4334 | /// \param Recursive if true, recursively instantiates any functions that |
| 4335 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4336 | /// |
| 4337 | /// \param DefinitionRequired if true, then we are performing an explicit |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4338 | /// instantiation where a definition of the variable is required. Complain |
| 4339 | /// if there is no such definition. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4340 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 4341 | VarDecl *Var, bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4342 | bool DefinitionRequired, bool AtEndOfTU) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4343 | if (Var->isInvalidDecl()) |
| 4344 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4346 | VarTemplateSpecializationDecl *VarSpec = |
| 4347 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4348 | VarDecl *PatternDecl = nullptr, *Def = nullptr; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4349 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4350 | getTemplateInstantiationArgs(Var); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4351 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4352 | if (VarSpec) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4353 | // If this is a variable template specialization, make sure that it is |
| 4354 | // non-dependent, then find its instantiation pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4355 | bool InstantiationDependent = false; |
| 4356 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 4357 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 4358 | "Only instantiate variable template specializations that are " |
| 4359 | "not type-dependent"); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 4360 | (void)InstantiationDependent; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4361 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4362 | // Find the variable initialization that we'll be substituting. If the |
| 4363 | // pattern was instantiated from a member template, look back further to |
| 4364 | // find the real pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4365 | assert(VarSpec->getSpecializedTemplate() && |
| 4366 | "Specialization without specialized template?"); |
| 4367 | llvm::PointerUnion<VarTemplateDecl *, |
| 4368 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4369 | VarSpec->getSpecializedTemplateOrPartial(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4370 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4371 | VarTemplatePartialSpecializationDecl *Tmpl = |
| 4372 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); |
| 4373 | while (VarTemplatePartialSpecializationDecl *From = |
| 4374 | Tmpl->getInstantiatedFromMember()) { |
| 4375 | if (Tmpl->isMemberSpecialization()) |
| 4376 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4377 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4378 | Tmpl = From; |
| 4379 | } |
| 4380 | PatternDecl = Tmpl; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4381 | } else { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4382 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); |
| 4383 | while (VarTemplateDecl *From = |
| 4384 | Tmpl->getInstantiatedFromMemberTemplate()) { |
| 4385 | if (Tmpl->isMemberSpecialization()) |
| 4386 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4387 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4388 | Tmpl = From; |
| 4389 | } |
| 4390 | PatternDecl = Tmpl->getTemplatedDecl(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4391 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4392 | |
| 4393 | // If this is a static data member template, there might be an |
| 4394 | // uninstantiated initializer on the declaration. If so, instantiate |
| 4395 | // it now. |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4396 | // |
| 4397 | // FIXME: This largely duplicates what we would do below. The difference |
| 4398 | // is that along this path we may instantiate an initializer from an |
| 4399 | // in-class declaration of the template and instantiate the definition |
| 4400 | // from a separate out-of-class definition. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4401 | if (PatternDecl->isStaticDataMember() && |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 4402 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4403 | !Var->hasInit()) { |
| 4404 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 4405 | // code here. |
| 4406 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4407 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4408 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4409 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4410 | "instantiating variable initializer"); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4411 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4412 | // The instantiation is visible here, even if it was first declared in an |
| 4413 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4414 | Var->setVisibleDespiteOwningModule(); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4415 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4416 | // If we're performing recursive template instantiation, create our own |
| 4417 | // queue of pending implicit instantiations that we will instantiate |
| 4418 | // later, while we're still within our own instantiation context. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4419 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4420 | /*Enabled=*/Recursive); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4421 | LocalInstantiationScope Local(*this); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4422 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4423 | |
| 4424 | // Enter the scope of this instantiation. We don't use |
| 4425 | // PushDeclContext because we don't have a scope. |
| 4426 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 4427 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 4428 | PreviousContext.pop(); |
| 4429 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4430 | // This variable may have local implicit instantiations that need to be |
| 4431 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4432 | LocalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4433 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4434 | GlobalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | // Find actual definition |
| 4438 | Def = PatternDecl->getDefinition(getASTContext()); |
| 4439 | } else { |
| 4440 | // If this is a static data member, find its out-of-line definition. |
| 4441 | assert(Var->isStaticDataMember() && "not a static data member?"); |
| 4442 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); |
| 4443 | |
| 4444 | assert(PatternDecl && "data member was not instantiated from a template?"); |
| 4445 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4446 | Def = PatternDecl->getDefinition(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4449 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4450 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4451 | // If we don't have a definition of the variable template, we won't perform |
| 4452 | // any instantiation. Rather, we rely on the user to instantiate this |
| 4453 | // definition (or provide a specialization for it) in another translation |
| 4454 | // unit. |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4455 | if (!Def && !DefinitionRequired) { |
| 4456 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4457 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4458 | std::make_pair(Var, PointOfInstantiation)); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4459 | } else if (TSK == TSK_ImplicitInstantiation) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4460 | // Warn about missing definition at the end of translation unit. |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4461 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4462 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4463 | Diag(PointOfInstantiation, diag::warn_var_template_missing) |
| 4464 | << Var; |
| 4465 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4466 | if (getLangOpts().CPlusPlus11) |
| 4467 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; |
| 4468 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4469 | return; |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4474 | // FIXME: We need to track the instantiation stack in order to know which |
| 4475 | // definitions should be visible within this instantiation. |
| 4476 | // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). |
| 4477 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, |
| 4478 | /*InstantiatedFromMember*/false, |
| 4479 | PatternDecl, Def, TSK, |
| 4480 | /*Complain*/DefinitionRequired)) |
| 4481 | return; |
| 4482 | |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4483 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4484 | // Never instantiate an explicit specialization. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4485 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4486 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4487 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4488 | // C++11 [temp.explicit]p10: |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4489 | // Except for inline functions, const variables of literal types, variables |
| 4490 | // of reference types, [...] explicit instantiation declarations |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4491 | // have the effect of suppressing the implicit instantiation of the entity |
| 4492 | // to which they refer. |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4493 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 4494 | !Var->isUsableInConstantExpressions(getASTContext())) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4495 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4497 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 4498 | struct PassToConsumerRAII { |
| 4499 | ASTConsumer &Consumer; |
| 4500 | VarDecl *Var; |
| 4501 | |
| 4502 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 4503 | : Consumer(Consumer), Var(Var) { } |
| 4504 | |
| 4505 | ~PassToConsumerRAII() { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4506 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4507 | } |
| 4508 | } PassToConsumerRAII(Consumer, Var); |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4509 | |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4510 | // If we already have a definition, we're done. |
| 4511 | if (VarDecl *Def = Var->getDefinition()) { |
| 4512 | // We may be explicitly instantiating something we've already implicitly |
| 4513 | // instantiated. |
| 4514 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 4515 | PointOfInstantiation); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4516 | return; |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4517 | } |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4519 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4520 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4521 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4522 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4523 | "instantiating variable definition"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4525 | // If we're performing recursive template instantiation, create our own |
| 4526 | // queue of pending implicit instantiations that we will instantiate later, |
| 4527 | // while we're still within our own instantiation context. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4528 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4529 | /*Enabled=*/Recursive); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4531 | // Enter the scope of this instantiation. We don't use |
| 4532 | // PushDeclContext because we don't have a scope. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4533 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4534 | LocalInstantiationScope Local(*this); |
John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 4535 | |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4536 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| 4537 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4538 | VarDecl *OldVar = Var; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4539 | if (Def->isStaticDataMember() && !Def->isOutOfLine()) { |
| 4540 | // We're instantiating an inline static data member whose definition was |
| 4541 | // provided inside the class. |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4542 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4543 | } else if (!VarSpec) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4544 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4545 | TemplateArgs)); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4546 | } else if (Var->isStaticDataMember() && |
| 4547 | Var->getLexicalDeclContext()->isRecord()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4548 | // We need to instantiate the definition of a static data member template, |
| 4549 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 4550 | // declaration of the definition. |
| 4551 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 4552 | TemplateArgs); |
| 4553 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4554 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4555 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 4556 | if (Var) { |
| 4557 | llvm::PointerUnion<VarTemplateDecl *, |
| 4558 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4559 | VarSpec->getSpecializedTemplateOrPartial(); |
| 4560 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 4561 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 4562 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 4563 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 4564 | |
| 4565 | // Merge the definition with the declaration. |
| 4566 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4567 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4568 | R.addDecl(OldVar); |
| 4569 | MergeVarDecl(Var, R); |
| 4570 | |
| 4571 | // Attach the initializer. |
| 4572 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4573 | } |
| 4574 | } else |
| 4575 | // Complete the existing variable's definition with an appropriately |
| 4576 | // substituted type and initializer. |
| 4577 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4578 | |
| 4579 | PreviousContext.pop(); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4580 | |
| 4581 | if (Var) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4582 | PassToConsumerRAII.Var = Var; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4583 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 4584 | OldVar->getPointOfInstantiation()); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4585 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4586 | |
| 4587 | // This variable may have local implicit instantiations that need to be |
| 4588 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4589 | LocalInstantiations.perform(); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4590 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4591 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4592 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4593 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4594 | void |
| 4595 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4596 | const CXXConstructorDecl *Tmpl, |
| 4597 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4598 | |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4599 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4600 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4601 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4602 | // Instantiate all the initializers. |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4603 | for (const auto *Init : Tmpl->inits()) { |
Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4604 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4605 | // ones. |
| 4606 | if (!Init->isWritten()) |
| 4607 | continue; |
| 4608 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4609 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4610 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4611 | if (Init->isPackExpansion()) { |
| 4612 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4613 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4614 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4615 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4616 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4617 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4618 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4619 | Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4620 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4621 | BaseTL.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4622 | Unexpanded, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4623 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4624 | RetainExpansion, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4625 | NumExpansions)) { |
| 4626 | AnyErrors = true; |
| 4627 | New->setInvalidDecl(); |
| 4628 | continue; |
| 4629 | } |
| 4630 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4631 | |
| 4632 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4633 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4634 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4635 | |
| 4636 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4637 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4638 | /*CXXDirectInit=*/true); |
| 4639 | if (TempInit.isInvalid()) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4640 | AnyErrors = true; |
| 4641 | break; |
| 4642 | } |
| 4643 | |
| 4644 | // Instantiate the base type. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4645 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4646 | TemplateArgs, |
| 4647 | Init->getSourceLocation(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4648 | New->getDeclName()); |
| 4649 | if (!BaseTInfo) { |
| 4650 | AnyErrors = true; |
| 4651 | break; |
| 4652 | } |
| 4653 | |
| 4654 | // Build the initializer. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4655 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4656 | BaseTInfo, TempInit.get(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4657 | New->getParent(), |
| 4658 | SourceLocation()); |
| 4659 | if (NewInit.isInvalid()) { |
| 4660 | AnyErrors = true; |
| 4661 | break; |
| 4662 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4663 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4664 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4665 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4667 | continue; |
| 4668 | } |
| 4669 | |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4670 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4671 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4672 | /*CXXDirectInit=*/true); |
| 4673 | if (TempInit.isInvalid()) { |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4674 | AnyErrors = true; |
| 4675 | continue; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4676 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4677 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4678 | MemInitResult NewInit; |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4679 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4680 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4681 | TemplateArgs, |
| 4682 | Init->getSourceLocation(), |
| 4683 | New->getDeclName()); |
| 4684 | if (!TInfo) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4685 | AnyErrors = true; |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4686 | New->setInvalidDecl(); |
| 4687 | continue; |
| 4688 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4690 | if (Init->isBaseInitializer()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4691 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4692 | New->getParent(), EllipsisLoc); |
| 4693 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4694 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4695 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4696 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4697 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4698 | Init->getMemberLocation(), |
| 4699 | Init->getMember(), |
| 4700 | TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4701 | if (!Member) { |
| 4702 | AnyErrors = true; |
| 4703 | New->setInvalidDecl(); |
| 4704 | continue; |
| 4705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4706 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4707 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4708 | Init->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4709 | } else if (Init->isIndirectMemberInitializer()) { |
| 4710 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4711 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4712 | Init->getMemberLocation(), |
| 4713 | Init->getIndirectMember(), TemplateArgs)); |
| 4714 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4715 | if (!IndirectMember) { |
| 4716 | AnyErrors = true; |
| 4717 | New->setInvalidDecl(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4718 | continue; |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4719 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4720 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4721 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4722 | Init->getSourceLocation()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4725 | if (NewInit.isInvalid()) { |
| 4726 | AnyErrors = true; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4727 | New->setInvalidDecl(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4728 | } else { |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4729 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4730 | } |
| 4731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4733 | // Assign all the initializers to the new constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4734 | ActOnMemInitializers(New, |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4735 | /*FIXME: ColonLoc */ |
| 4736 | SourceLocation(), |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4737 | NewInits, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4738 | AnyErrors); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4741 | // TODO: this could be templated if the various decl types used the |
| 4742 | // same method name. |
| 4743 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4744 | ClassTemplateDecl *Instance) { |
| 4745 | Pattern = Pattern->getCanonicalDecl(); |
| 4746 | |
| 4747 | do { |
| 4748 | Instance = Instance->getCanonicalDecl(); |
| 4749 | if (Pattern == Instance) return true; |
| 4750 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4751 | } while (Instance); |
| 4752 | |
| 4753 | return false; |
| 4754 | } |
| 4755 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4756 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 4757 | FunctionTemplateDecl *Instance) { |
| 4758 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4760 | do { |
| 4761 | Instance = Instance->getCanonicalDecl(); |
| 4762 | if (Pattern == Instance) return true; |
| 4763 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4764 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4766 | return false; |
| 4767 | } |
| 4768 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4769 | static bool |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4770 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 4771 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4772 | Pattern |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4773 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 4774 | do { |
| 4775 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 4776 | Instance->getCanonicalDecl()); |
| 4777 | if (Pattern == Instance) |
| 4778 | return true; |
| 4779 | Instance = Instance->getInstantiatedFromMember(); |
| 4780 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4781 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
| 4784 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4785 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 4786 | CXXRecordDecl *Instance) { |
| 4787 | Pattern = Pattern->getCanonicalDecl(); |
| 4788 | |
| 4789 | do { |
| 4790 | Instance = Instance->getCanonicalDecl(); |
| 4791 | if (Pattern == Instance) return true; |
| 4792 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 4793 | } while (Instance); |
| 4794 | |
| 4795 | return false; |
| 4796 | } |
| 4797 | |
| 4798 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 4799 | FunctionDecl *Instance) { |
| 4800 | Pattern = Pattern->getCanonicalDecl(); |
| 4801 | |
| 4802 | do { |
| 4803 | Instance = Instance->getCanonicalDecl(); |
| 4804 | if (Pattern == Instance) return true; |
| 4805 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 4806 | } while (Instance); |
| 4807 | |
| 4808 | return false; |
| 4809 | } |
| 4810 | |
| 4811 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 4812 | EnumDecl *Instance) { |
| 4813 | Pattern = Pattern->getCanonicalDecl(); |
| 4814 | |
| 4815 | do { |
| 4816 | Instance = Instance->getCanonicalDecl(); |
| 4817 | if (Pattern == Instance) return true; |
| 4818 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 4819 | } while (Instance); |
| 4820 | |
| 4821 | return false; |
| 4822 | } |
| 4823 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4824 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 4825 | UsingShadowDecl *Instance, |
| 4826 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4827 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 4828 | Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4831 | static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4832 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4833 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4836 | template<typename T> |
| 4837 | static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, |
| 4838 | ASTContext &Ctx) { |
| 4839 | // An unresolved using declaration can instantiate to an unresolved using |
| 4840 | // declaration, or to a using declaration or a using declaration pack. |
| 4841 | // |
| 4842 | // Multiple declarations can claim to be instantiated from an unresolved |
| 4843 | // using declaration if it's a pack expansion. We want the UsingPackDecl |
| 4844 | // in that case, not the individual UsingDecls within the pack. |
| 4845 | bool OtherIsPackExpansion; |
| 4846 | NamedDecl *OtherFrom; |
| 4847 | if (auto *OtherUUD = dyn_cast<T>(Other)) { |
| 4848 | OtherIsPackExpansion = OtherUUD->isPackExpansion(); |
| 4849 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); |
| 4850 | } else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { |
| 4851 | OtherIsPackExpansion = true; |
| 4852 | OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); |
| 4853 | } else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { |
| 4854 | OtherIsPackExpansion = false; |
| 4855 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); |
| 4856 | } else { |
| 4857 | return false; |
| 4858 | } |
| 4859 | return Pattern->isPackExpansion() == OtherIsPackExpansion && |
| 4860 | declaresSameEntity(OtherFrom, Pattern); |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4863 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 4864 | VarDecl *Instance) { |
| 4865 | assert(Instance->isStaticDataMember()); |
| 4866 | |
| 4867 | Pattern = Pattern->getCanonicalDecl(); |
| 4868 | |
| 4869 | do { |
| 4870 | Instance = Instance->getCanonicalDecl(); |
| 4871 | if (Pattern == Instance) return true; |
| 4872 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 4873 | } while (Instance); |
| 4874 | |
| 4875 | return false; |
| 4876 | } |
| 4877 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4878 | // Other is the prospective instantiation |
| 4879 | // D is the prospective pattern |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4880 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4881 | if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) |
| 4882 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4883 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4884 | if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) |
| 4885 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4886 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4887 | if (D->getKind() != Other->getKind()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4888 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4889 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4890 | if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4891 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4893 | if (auto *Function = dyn_cast<FunctionDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4894 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4895 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4896 | if (auto *Enum = dyn_cast<EnumDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4897 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4898 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4899 | if (auto *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4900 | if (Var->isStaticDataMember()) |
| 4901 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 4902 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4903 | if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4904 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 4905 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4906 | if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4907 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 4908 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4909 | if (auto *PartialSpec = |
| 4910 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4911 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 4912 | PartialSpec); |
| 4913 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4914 | if (auto *Field = dyn_cast<FieldDecl>(Other)) { |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4915 | if (!Field->getDeclName()) { |
| 4916 | // This is an unnamed field. |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4917 | return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 4918 | cast<FieldDecl>(D)); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4919 | } |
| 4920 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4922 | if (auto *Using = dyn_cast<UsingDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4923 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 4924 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4925 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4926 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 4927 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4928 | return D->getDeclName() && |
| 4929 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | template<typename ForwardIterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4933 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4934 | NamedDecl *D, |
| 4935 | ForwardIterator first, |
| 4936 | ForwardIterator last) { |
| 4937 | for (; first != last; ++first) |
| 4938 | if (isInstantiationOf(Ctx, D, *first)) |
| 4939 | return cast<NamedDecl>(*first); |
| 4940 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4941 | return nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4944 | /// Finds the instantiation of the given declaration context |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4945 | /// within the current instantiation. |
| 4946 | /// |
| 4947 | /// \returns NULL if there was an error |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4948 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4949 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4950 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 4951 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs, true); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4952 | return cast_or_null<DeclContext>(ID); |
| 4953 | } else return DC; |
| 4954 | } |
| 4955 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4956 | /// Find the instantiation of the given declaration within the |
Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 4957 | /// current instantiation. |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4958 | /// |
| 4959 | /// This routine is intended to be used when \p D is a declaration |
| 4960 | /// referenced from within a template, that needs to mapped into the |
| 4961 | /// corresponding declaration within an instantiation. For example, |
| 4962 | /// given: |
| 4963 | /// |
| 4964 | /// \code |
| 4965 | /// template<typename T> |
| 4966 | /// struct X { |
| 4967 | /// enum Kind { |
| 4968 | /// KnownValue = sizeof(T) |
| 4969 | /// }; |
| 4970 | /// |
| 4971 | /// bool getKind() const { return KnownValue; } |
| 4972 | /// }; |
| 4973 | /// |
| 4974 | /// template struct X<int>; |
| 4975 | /// \endcode |
| 4976 | /// |
Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 4977 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the |
| 4978 | /// \p EnumConstantDecl for \p KnownValue (which refers to |
| 4979 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation |
| 4980 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs |
| 4981 | /// this mapping from within the instantiation of <tt>X<int></tt>. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4982 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 4983 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4984 | bool FindingInstantiatedContext) { |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4985 | DeclContext *ParentDC = D->getDeclContext(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4986 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4987 | // parameters (p below) can have their ParentDC set to the translation-unit |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4988 | // - 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] | 4989 | // is Dependent or/and a FunctionOrMethod. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4990 | // For e.g. this code, during Template argument deduction tries to |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4991 | // 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] | 4992 | // the translation unit. |
| 4993 | // 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] | 4994 | // float baz(float(*)()) { return 0.0; } |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4995 | // Foo(baz); |
| 4996 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 4997 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 4998 | // For now: |
| 4999 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 5000 | // whose type is not instantiation dependent, do nothing to the decl |
| 5001 | // - otherwise find its instantiated decl. |
| 5002 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 5003 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 5004 | return D; |
Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 5005 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 5006 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 5007 | ((ParentDC->isFunctionOrMethod() || |
| 5008 | isa<OMPDeclareReductionDecl>(ParentDC)) && |
| 5009 | ParentDC->isDependentContext()) || |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 5010 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5011 | // D is a local of some kind. Look into the map of local |
| 5012 | // declarations to their instantiations. |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5013 | if (CurrentInstantiationScope) { |
| 5014 | if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 5015 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 5016 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5017 | |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5018 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 5019 | assert(PackIdx != -1 && |
| 5020 | "found declaration pack but not pack expanding"); |
| 5021 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 5022 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 5023 | } |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 5026 | // If we're performing a partial substitution during template argument |
| 5027 | // deduction, we may not have values for template parameters yet. They |
| 5028 | // just map to themselves. |
| 5029 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 5030 | isa<TemplateTemplateParmDecl>(D)) |
| 5031 | return D; |
| 5032 | |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5033 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5034 | return nullptr; |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5035 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 5036 | // Normally this function only searches for already instantiated declaration |
| 5037 | // however we have to make an exclusion for local types used before |
| 5038 | // definition as in the code: |
| 5039 | // |
| 5040 | // template<typename T> void f1() { |
| 5041 | // void g1(struct x1); |
| 5042 | // struct x1 {}; |
| 5043 | // } |
| 5044 | // |
| 5045 | // In this case instantiation of the type of 'g1' requires definition of |
| 5046 | // 'x1', which is defined later. Error recovery may produce an enum used |
| 5047 | // before definition. In these cases we need to instantiate relevant |
| 5048 | // declarations here. |
| 5049 | bool NeedInstantiate = false; |
| 5050 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5051 | NeedInstantiate = RD->isLocalClass(); |
| 5052 | else |
| 5053 | NeedInstantiate = isa<EnumDecl>(D); |
| 5054 | if (NeedInstantiate) { |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 5055 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5056 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5057 | return cast<TypeDecl>(Inst); |
| 5058 | } |
| 5059 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5060 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 5061 | // been found yet. Lazily instantiate it and return it now. |
| 5062 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5063 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5064 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5065 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5066 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5067 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5068 | return cast<LabelDecl>(Inst); |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5069 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5070 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5071 | // For variable template specializations, update those that are still |
| 5072 | // type-dependent. |
| 5073 | if (VarTemplateSpecializationDecl *VarSpec = |
| 5074 | dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 5075 | bool InstantiationDependent = false; |
| 5076 | const TemplateArgumentListInfo &VarTemplateArgs = |
| 5077 | VarSpec->getTemplateArgsInfo(); |
| 5078 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
| 5079 | VarTemplateArgs, InstantiationDependent)) |
| 5080 | D = cast<NamedDecl>( |
| 5081 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); |
| 5082 | return D; |
| 5083 | } |
| 5084 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5085 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 5086 | if (!Record->isDependentContext()) |
| 5087 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5089 | // Determine whether this record is the "templated" declaration describing |
| 5090 | // a class template or class template partial specialization. |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5091 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5092 | if (ClassTemplate) |
| 5093 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 5094 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5095 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 5096 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5097 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5098 | // Walk the current context to find either the record or an instantiation of |
| 5099 | // it. |
| 5100 | DeclContext *DC = CurContext; |
| 5101 | while (!DC->isFileContext()) { |
| 5102 | // If we're performing substitution while we're inside the template |
| 5103 | // definition, we'll find our own context. We're done. |
| 5104 | if (DC->Equals(Record)) |
| 5105 | return Record; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5106 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5107 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 5108 | // Check whether we're in the process of instantiating a class template |
| 5109 | // specialization of the template we're mapping. |
| 5110 | if (ClassTemplateSpecializationDecl *InstSpec |
| 5111 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 5112 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 5113 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 5114 | return InstRecord; |
| 5115 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5116 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5117 | // Check whether we're in the process of instantiating a member class. |
| 5118 | if (isInstantiationOf(Record, InstRecord)) |
| 5119 | return InstRecord; |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5120 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5121 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5122 | // Move to the outer template scope. |
| 5123 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 5124 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 5125 | DC = FD->getLexicalDeclContext(); |
| 5126 | continue; |
| 5127 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5128 | // An implicit deduction guide acts as if it's within the class template |
| 5129 | // specialization described by its name and first N template params. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5130 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FD); |
| 5131 | if (Guide && Guide->isImplicit()) { |
| 5132 | TemplateDecl *TD = Guide->getDeducedTemplate(); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5133 | // Convert the arguments to an "as-written" list. |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5134 | TemplateArgumentListInfo Args(Loc, Loc); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5135 | for (TemplateArgument Arg : TemplateArgs.getInnermost().take_front( |
| 5136 | TD->getTemplateParameters()->size())) { |
| 5137 | ArrayRef<TemplateArgument> Unpacked(Arg); |
| 5138 | if (Arg.getKind() == TemplateArgument::Pack) |
| 5139 | Unpacked = Arg.pack_elements(); |
| 5140 | for (TemplateArgument UnpackedArg : Unpacked) |
| 5141 | Args.addArgument( |
| 5142 | getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); |
| 5143 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5144 | QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); |
| 5145 | if (T.isNull()) |
| 5146 | return nullptr; |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5147 | auto *SubstRecord = T->getAsCXXRecordDecl(); |
| 5148 | assert(SubstRecord && "class template id not a class type?"); |
| 5149 | // Check that this template-id names the primary template and not a |
| 5150 | // partial or explicit specialization. (In the latter cases, it's |
| 5151 | // meaningless to attempt to find an instantiation of D within the |
| 5152 | // specialization.) |
| 5153 | // FIXME: The standard doesn't say what should happen here. |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5154 | if (FindingInstantiatedContext && |
| 5155 | usesPartialOrExplicitSpecialization( |
| 5156 | Loc, cast<ClassTemplateSpecializationDecl>(SubstRecord))) { |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5157 | Diag(Loc, diag::err_specialization_not_primary_template) |
| 5158 | << T << (SubstRecord->getTemplateSpecializationKind() == |
| 5159 | TSK_ExplicitSpecialization); |
| 5160 | return nullptr; |
| 5161 | } |
| 5162 | DC = SubstRecord; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5163 | continue; |
| 5164 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5165 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5166 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5167 | DC = DC->getParent(); |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5168 | } |
Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5170 | // Fall through to deal with other dependent record types (e.g., |
| 5171 | // anonymous unions in class templates). |
| 5172 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5174 | if (!ParentDC->isDependentContext()) |
| 5175 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5176 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5177 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5178 | if (!ParentDC) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5179 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5181 | if (ParentDC != D->getDeclContext()) { |
| 5182 | // We performed some kind of instantiation in the parent context, |
| 5183 | // so now we need to look into the instantiated parent context to |
| 5184 | // find the instantiation of the declaration D. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5185 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5186 | // If our context used to be dependent, we may need to instantiate |
| 5187 | // it before performing lookup into that context. |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5188 | bool IsBeingInstantiated = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5189 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5190 | if (!Spec->isDependentContext()) { |
| 5191 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5192 | const RecordType *Tag = T->getAs<RecordType>(); |
| 5193 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5194 | if (Tag->isBeingDefined()) |
| 5195 | IsBeingInstantiated = true; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5196 | if (!Tag->isBeingDefined() && |
| 5197 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5198 | return nullptr; |
Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 5199 | |
| 5200 | ParentDC = Tag->getDecl(); |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5201 | } |
| 5202 | } |
| 5203 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5204 | NamedDecl *Result = nullptr; |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5205 | // FIXME: If the name is a dependent name, this lookup won't necessarily |
| 5206 | // find it. Does that ever matter? |
Akira Hatanaka | 59e3b43 | 2017-01-31 19:53:32 +0000 | [diff] [blame] | 5207 | if (auto Name = D->getDeclName()) { |
| 5208 | DeclarationNameInfo NameInfo(Name, D->getLocation()); |
| 5209 | Name = SubstDeclarationNameInfo(NameInfo, TemplateArgs).getName(); |
| 5210 | if (!Name) |
| 5211 | return nullptr; |
| 5212 | DeclContext::lookup_result Found = ParentDC->lookup(Name); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5213 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5214 | } else { |
| 5215 | // Since we don't have a name for the entity we're looking for, |
| 5216 | // our only option is to walk through all of the declarations to |
| 5217 | // find that name. This will occur in a few cases: |
| 5218 | // |
| 5219 | // - anonymous struct/union within a template |
| 5220 | // - unnamed class/struct/union/enum within a template |
| 5221 | // |
| 5222 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5223 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5224 | ParentDC->decls_begin(), |
| 5225 | ParentDC->decls_end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5227 | |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5228 | if (!Result) { |
| 5229 | if (isa<UsingShadowDecl>(D)) { |
| 5230 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 5231 | } else if (Diags.hasErrorOccurred()) { |
| 5232 | // We've already complained about something, so most likely this |
| 5233 | // declaration failed to instantiate. There's no point in complaining |
| 5234 | // further, since this is normal in invalid code. |
| 5235 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5236 | // The class in which this member exists is currently being |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5237 | // instantiated, and we haven't gotten around to instantiating this |
| 5238 | // member yet. This can happen when the code uses forward declarations |
| 5239 | // of member classes, and introduces ordering dependencies via |
| 5240 | // template instantiation. |
| 5241 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 5242 | << D->getDeclName() |
| 5243 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 5244 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 5245 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 5246 | // This enumeration constant was found when the template was defined, |
| 5247 | // but can't be found in the instantiation. This can happen if an |
| 5248 | // unscoped enumeration member is explicitly specialized. |
| 5249 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 5250 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 5251 | TemplateArgs)); |
| 5252 | assert(Spec->getTemplateSpecializationKind() == |
| 5253 | TSK_ExplicitSpecialization); |
| 5254 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 5255 | << D->getDeclName() |
| 5256 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 5257 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 5258 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5259 | } else { |
| 5260 | // We should have found something, but didn't. |
| 5261 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 5262 | } |
| 5263 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5264 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5265 | D = Result; |
| 5266 | } |
| 5267 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5268 | return D; |
| 5269 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5270 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5271 | /// Performs template instantiation for all implicit template |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5272 | /// instantiations we have seen until this point. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 5273 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5274 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5275 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5276 | PendingImplicitInstantiation Inst; |
| 5277 | |
| 5278 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5279 | Inst = PendingInstantiations.front(); |
| 5280 | PendingInstantiations.pop_front(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5281 | } else { |
| 5282 | Inst = PendingLocalImplicitInstantiations.front(); |
| 5283 | PendingLocalImplicitInstantiations.pop_front(); |
| 5284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5285 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5286 | // Instantiate function definitions |
| 5287 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5288 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 5289 | TSK_ExplicitInstantiationDefinition; |
Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 5290 | if (Function->isMultiVersion()) { |
| 5291 | getASTContext().forEachMultiversionedFunctionVersion( |
| 5292 | Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) { |
| 5293 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true, |
| 5294 | DefinitionRequired, true); |
| 5295 | if (CurFD->isDefined()) |
| 5296 | CurFD->setInstantiationIsPending(false); |
| 5297 | }); |
| 5298 | } else { |
| 5299 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true, |
| 5300 | DefinitionRequired, true); |
| 5301 | if (Function->isDefined()) |
| 5302 | Function->setInstantiationIsPending(false); |
| 5303 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5304 | continue; |
| 5305 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5307 | // Instantiate variable definitions |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5308 | VarDecl *Var = cast<VarDecl>(Inst.first); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5309 | |
| 5310 | assert((Var->isStaticDataMember() || |
| 5311 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 5312 | "Not a static data member, nor a variable template" |
| 5313 | " specialization?"); |
Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 5314 | |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5315 | // Don't try to instantiate declarations if the most recent redeclaration |
| 5316 | // is invalid. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5317 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5318 | continue; |
| 5319 | |
| 5320 | // Check if the most recent declaration has changed the specialization kind |
| 5321 | // and removed the need for implicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5322 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5323 | case TSK_Undeclared: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5324 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5325 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5326 | case TSK_ExplicitSpecialization: |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5327 | continue; // No longer need to instantiate this type. |
| 5328 | case TSK_ExplicitInstantiationDefinition: |
| 5329 | // We only need an instantiation if the pending instantiation *is* the |
| 5330 | // explicit instantiation. |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5331 | if (Var != Var->getMostRecentDecl()) |
| 5332 | continue; |
| 5333 | break; |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5334 | case TSK_ImplicitInstantiation: |
| 5335 | break; |
| 5336 | } |
| 5337 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 5338 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5339 | "instantiating variable definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5340 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 5341 | TSK_ExplicitInstantiationDefinition; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5342 | |
| 5343 | // Instantiate static data member definitions or variable template |
| 5344 | // specializations. |
| 5345 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 5346 | DefinitionRequired, true); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5347 | } |
| 5348 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5349 | |
| 5350 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 5351 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 5352 | for (auto DD : Pattern->ddiags()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5353 | switch (DD->getKind()) { |
| 5354 | case DependentDiagnostic::Access: |
| 5355 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 5356 | break; |
| 5357 | } |
| 5358 | } |
| 5359 | } |