Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation for declarations. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 18 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 6131b44 | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyDeclStackTrace.h" |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Initialization.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Lookup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Sema/Template.h" |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 26 | #include "clang/Sema/TemplateInstCallback.h" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 30 | static bool isDeclWithinFunction(const Decl *D) { |
| 31 | const DeclContext *DC = D->getDeclContext(); |
| 32 | if (DC->isFunctionOrMethod()) |
| 33 | return true; |
| 34 | |
| 35 | if (DC->isRecord()) |
| 36 | return cast<CXXRecordDecl>(DC)->isLocalClass(); |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 41 | template<typename DeclT> |
| 42 | static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, |
| 43 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 44 | if (!OldDecl->getQualifierLoc()) |
| 45 | return false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 46 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 47 | assert((NewDecl->getFriendObjectKind() || |
| 48 | !OldDecl->getLexicalDeclContext()->isDependentContext()) && |
| 49 | "non-friend with qualified name defined in dependent context"); |
| 50 | Sema::ContextRAII SavedContext( |
| 51 | SemaRef, |
| 52 | const_cast<DeclContext *>(NewDecl->getFriendObjectKind() |
| 53 | ? NewDecl->getLexicalDeclContext() |
| 54 | : OldDecl->getLexicalDeclContext())); |
| 55 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 56 | NestedNameSpecifierLoc NewQualifierLoc |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 57 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 58 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 60 | if (!NewQualifierLoc) |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 61 | return true; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 63 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 67 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 68 | DeclaratorDecl *NewDecl) { |
| 69 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 70 | } |
| 71 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 72 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 73 | TagDecl *NewDecl) { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 74 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 77 | // Include attribute instantiation code. |
| 78 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 79 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 80 | static void instantiateDependentAlignedAttr( |
| 81 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 82 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 83 | if (Aligned->isAlignmentExpr()) { |
| 84 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 85 | EnterExpressionEvaluationContext Unevaluated( |
| 86 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 87 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 88 | if (!Result.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 89 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 90 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 91 | } else { |
| 92 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 93 | TemplateArgs, Aligned->getLocation(), |
| 94 | DeclarationName()); |
| 95 | if (Result) |
| 96 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 97 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void instantiateDependentAlignedAttr( |
| 102 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 103 | const AlignedAttr *Aligned, Decl *New) { |
| 104 | if (!Aligned->isPackExpansion()) { |
| 105 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 110 | if (Aligned->isAlignmentExpr()) |
| 111 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 112 | Unexpanded); |
| 113 | else |
| 114 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 115 | Unexpanded); |
| 116 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 117 | |
| 118 | // Determine whether we can expand this attribute pack yet. |
| 119 | bool Expand = true, RetainExpansion = false; |
| 120 | Optional<unsigned> NumExpansions; |
| 121 | // FIXME: Use the actual location of the ellipsis. |
| 122 | SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 123 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 124 | Unexpanded, TemplateArgs, Expand, |
| 125 | RetainExpansion, NumExpansions)) |
| 126 | return; |
| 127 | |
| 128 | if (!Expand) { |
| 129 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 130 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 131 | } else { |
| 132 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 133 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 134 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 139 | static void instantiateDependentAssumeAlignedAttr( |
| 140 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 141 | const AssumeAlignedAttr *Aligned, Decl *New) { |
| 142 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 143 | EnterExpressionEvaluationContext Unevaluated( |
| 144 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 145 | |
| 146 | Expr *E, *OE = nullptr; |
| 147 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 148 | if (Result.isInvalid()) |
| 149 | return; |
| 150 | E = Result.getAs<Expr>(); |
| 151 | |
| 152 | if (Aligned->getOffset()) { |
| 153 | Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); |
| 154 | if (Result.isInvalid()) |
| 155 | return; |
| 156 | OE = Result.getAs<Expr>(); |
| 157 | } |
| 158 | |
| 159 | S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE, |
| 160 | Aligned->getSpellingListIndex()); |
| 161 | } |
| 162 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 163 | static void instantiateDependentAlignValueAttr( |
| 164 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 165 | const AlignValueAttr *Aligned, Decl *New) { |
| 166 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 167 | EnterExpressionEvaluationContext Unevaluated( |
| 168 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 169 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 170 | if (!Result.isInvalid()) |
| 171 | S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
| 172 | Aligned->getSpellingListIndex()); |
| 173 | } |
| 174 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 175 | static void instantiateDependentAllocAlignAttr( |
| 176 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 177 | const AllocAlignAttr *Align, Decl *New) { |
| 178 | Expr *Param = IntegerLiteral::Create( |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 179 | S.getASTContext(), |
| 180 | llvm::APInt(64, Align->getParamIndex().getSourceIndex()), |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 181 | S.getASTContext().UnsignedLongLongTy, Align->getLocation()); |
| 182 | S.AddAllocAlignAttr(Align->getLocation(), New, Param, |
| 183 | Align->getSpellingListIndex()); |
| 184 | } |
| 185 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 186 | static Expr *instantiateDependentFunctionAttrCondition( |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 187 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 188 | const Attr *A, Expr *OldCond, const Decl *Tmpl, FunctionDecl *New) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 189 | Expr *Cond = nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 190 | { |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 191 | Sema::ContextRAII SwitchContext(S, New); |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 192 | EnterExpressionEvaluationContext Unevaluated( |
| 193 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 194 | ExprResult Result = S.SubstExpr(OldCond, TemplateArgs); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 195 | if (Result.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 196 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 197 | Cond = Result.getAs<Expr>(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 198 | } |
George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 199 | if (!Cond->isTypeDependent()) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 200 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 201 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 202 | return nullptr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 203 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | SmallVector<PartialDiagnosticAt, 8> Diags; |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 207 | if (OldCond->isValueDependent() && !Cond->isValueDependent() && |
| 208 | !Expr::isPotentialConstantExprUnevaluated(Cond, New, Diags)) { |
| 209 | S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A; |
| 210 | for (const auto &P : Diags) |
| 211 | S.Diag(P.first, P.second); |
| 212 | return nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 213 | } |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 214 | return Cond; |
| 215 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 216 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 217 | static void instantiateDependentEnableIfAttr( |
| 218 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 219 | const EnableIfAttr *EIA, const Decl *Tmpl, FunctionDecl *New) { |
| 220 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 221 | S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); |
| 222 | |
| 223 | if (Cond) |
| 224 | New->addAttr(new (S.getASTContext()) EnableIfAttr( |
| 225 | EIA->getLocation(), S.getASTContext(), Cond, EIA->getMessage(), |
| 226 | EIA->getSpellingListIndex())); |
| 227 | } |
| 228 | |
| 229 | static void instantiateDependentDiagnoseIfAttr( |
| 230 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 231 | const DiagnoseIfAttr *DIA, const Decl *Tmpl, FunctionDecl *New) { |
| 232 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 233 | S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New); |
| 234 | |
| 235 | if (Cond) |
| 236 | New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( |
| 237 | DIA->getLocation(), S.getASTContext(), Cond, DIA->getMessage(), |
| 238 | DIA->getDiagnosticType(), DIA->getArgDependent(), New, |
| 239 | DIA->getSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 242 | // Constructs and adds to New a new instance of CUDALaunchBoundsAttr using |
| 243 | // template A as the base and arguments from TemplateArgs. |
| 244 | static void instantiateDependentCUDALaunchBoundsAttr( |
| 245 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 246 | const CUDALaunchBoundsAttr &Attr, Decl *New) { |
| 247 | // The alignment expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 248 | EnterExpressionEvaluationContext Unevaluated( |
| 249 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 250 | |
| 251 | ExprResult Result = S.SubstExpr(Attr.getMaxThreads(), TemplateArgs); |
| 252 | if (Result.isInvalid()) |
| 253 | return; |
| 254 | Expr *MaxThreads = Result.getAs<Expr>(); |
| 255 | |
| 256 | Expr *MinBlocks = nullptr; |
| 257 | if (Attr.getMinBlocks()) { |
| 258 | Result = S.SubstExpr(Attr.getMinBlocks(), TemplateArgs); |
| 259 | if (Result.isInvalid()) |
| 260 | return; |
| 261 | MinBlocks = Result.getAs<Expr>(); |
| 262 | } |
| 263 | |
| 264 | S.AddLaunchBoundsAttr(Attr.getLocation(), New, MaxThreads, MinBlocks, |
| 265 | Attr.getSpellingListIndex()); |
| 266 | } |
| 267 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 268 | static void |
| 269 | instantiateDependentModeAttr(Sema &S, |
| 270 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 271 | const ModeAttr &Attr, Decl *New) { |
| 272 | S.AddModeAttr(Attr.getRange(), New, Attr.getMode(), |
| 273 | Attr.getSpellingListIndex(), /*InInstantiation=*/true); |
| 274 | } |
| 275 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 276 | /// Instantiation of 'declare simd' attribute and its arguments. |
| 277 | static void instantiateOMPDeclareSimdDeclAttr( |
| 278 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 279 | const OMPDeclareSimdDeclAttr &Attr, Decl *New) { |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 280 | // Allow 'this' in clauses with varlists. |
| 281 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) |
| 282 | New = FTD->getTemplatedDecl(); |
| 283 | auto *FD = cast<FunctionDecl>(New); |
| 284 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 285 | SmallVector<Expr *, 4> Uniforms, Aligneds, Alignments, Linears, Steps; |
| 286 | SmallVector<unsigned, 4> LinModifiers; |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 287 | |
| 288 | auto &&Subst = [&](Expr *E) -> ExprResult { |
| 289 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 290 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 291 | Sema::ContextRAII SavedContext(S, FD); |
| 292 | LocalInstantiationScope Local(S); |
| 293 | if (FD->getNumParams() > PVD->getFunctionScopeIndex()) |
| 294 | Local.InstantiatedLocal( |
| 295 | PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); |
| 296 | return S.SubstExpr(E, TemplateArgs); |
| 297 | } |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 298 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 299 | FD->isCXXInstanceMember()); |
| 300 | return S.SubstExpr(E, TemplateArgs); |
| 301 | }; |
| 302 | |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 303 | ExprResult Simdlen; |
| 304 | if (auto *E = Attr.getSimdlen()) |
| 305 | Simdlen = Subst(E); |
| 306 | |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 307 | if (Attr.uniforms_size() > 0) { |
| 308 | for(auto *E : Attr.uniforms()) { |
| 309 | ExprResult Inst = Subst(E); |
| 310 | if (Inst.isInvalid()) |
| 311 | continue; |
| 312 | Uniforms.push_back(Inst.get()); |
| 313 | } |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 316 | auto AI = Attr.alignments_begin(); |
| 317 | for (auto *E : Attr.aligneds()) { |
| 318 | ExprResult Inst = Subst(E); |
| 319 | if (Inst.isInvalid()) |
| 320 | continue; |
| 321 | Aligneds.push_back(Inst.get()); |
| 322 | Inst = ExprEmpty(); |
| 323 | if (*AI) |
| 324 | Inst = S.SubstExpr(*AI, TemplateArgs); |
| 325 | Alignments.push_back(Inst.get()); |
| 326 | ++AI; |
| 327 | } |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 328 | |
| 329 | auto SI = Attr.steps_begin(); |
| 330 | for (auto *E : Attr.linears()) { |
| 331 | ExprResult Inst = Subst(E); |
| 332 | if (Inst.isInvalid()) |
| 333 | continue; |
| 334 | Linears.push_back(Inst.get()); |
| 335 | Inst = ExprEmpty(); |
| 336 | if (*SI) |
| 337 | Inst = S.SubstExpr(*SI, TemplateArgs); |
| 338 | Steps.push_back(Inst.get()); |
| 339 | ++SI; |
| 340 | } |
| 341 | LinModifiers.append(Attr.modifiers_begin(), Attr.modifiers_end()); |
Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 342 | (void)S.ActOnOpenMPDeclareSimdDirective( |
| 343 | S.ConvertDeclToDeclGroup(New), Attr.getBranchState(), Simdlen.get(), |
Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 344 | Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps, |
| 345 | Attr.getRange()); |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 348 | void Sema::InstantiateAttrsForDecl( |
| 349 | const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, |
| 350 | Decl *New, LateInstantiatedAttrVec *LateAttrs, |
| 351 | LocalInstantiationScope *OuterMostScope) { |
| 352 | if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { |
| 353 | for (const auto *TmplAttr : Tmpl->attrs()) { |
| 354 | // FIXME: If any of the special case versions from InstantiateAttrs become |
| 355 | // applicable to template declaration, we'll need to add them here. |
| 356 | CXXThisScopeRAII ThisScope( |
| 357 | *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 358 | Qualifiers(), ND->isCXXInstanceMember()); |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 359 | |
| 360 | Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( |
| 361 | TmplAttr, Context, *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 362 | if (NewAttr) |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 363 | New->addAttr(NewAttr); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 368 | static Sema::RetainOwnershipKind |
| 369 | attrToRetainOwnershipKind(const Attr *A) { |
| 370 | switch (A->getKind()) { |
| 371 | case clang::attr::CFConsumed: |
| 372 | return Sema::RetainOwnershipKind::CF; |
| 373 | case clang::attr::OSConsumed: |
| 374 | return Sema::RetainOwnershipKind::OS; |
| 375 | case clang::attr::NSConsumed: |
| 376 | return Sema::RetainOwnershipKind::NS; |
| 377 | default: |
| 378 | llvm_unreachable("Wrong argument supplied"); |
| 379 | } |
| 380 | } |
| 381 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 382 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 383 | const Decl *Tmpl, Decl *New, |
| 384 | LateInstantiatedAttrVec *LateAttrs, |
| 385 | LocalInstantiationScope *OuterMostScope) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 386 | for (const auto *TmplAttr : Tmpl->attrs()) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 387 | // FIXME: This should be generalized to more than just the AlignedAttr. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 388 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 389 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 390 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 391 | continue; |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 394 | const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr); |
| 395 | if (AssumeAligned) { |
| 396 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 397 | continue; |
| 398 | } |
| 399 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 400 | const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr); |
| 401 | if (AlignValue) { |
| 402 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 403 | continue; |
| 404 | } |
| 405 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 406 | if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { |
| 407 | instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | |
George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 412 | if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 413 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 414 | cast<FunctionDecl>(New)); |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { |
| 419 | instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, |
| 420 | cast<FunctionDecl>(New)); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 421 | continue; |
| 422 | } |
| 423 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 424 | if (const CUDALaunchBoundsAttr *CUDALaunchBounds = |
| 425 | dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { |
| 426 | instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, |
| 427 | *CUDALaunchBounds, New); |
| 428 | continue; |
| 429 | } |
| 430 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 431 | if (const ModeAttr *Mode = dyn_cast<ModeAttr>(TmplAttr)) { |
| 432 | instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); |
| 433 | continue; |
| 434 | } |
| 435 | |
Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 436 | if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { |
| 437 | instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); |
| 438 | continue; |
| 439 | } |
| 440 | |
Hans Wennborg | c2b7f7a | 2014-08-24 00:12:36 +0000 | [diff] [blame] | 441 | // Existing DLL attribute on the instantiation takes precedence. |
| 442 | if (TmplAttr->getKind() == attr::DLLExport || |
| 443 | TmplAttr->getKind() == attr::DLLImport) { |
| 444 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 445 | continue; |
| 446 | } |
| 447 | } |
| 448 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 449 | if (auto ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { |
| 450 | AddParameterABIAttr(ABIAttr->getRange(), New, ABIAttr->getABI(), |
| 451 | ABIAttr->getSpellingListIndex()); |
| 452 | continue; |
| 453 | } |
| 454 | |
George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 455 | if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || |
| 456 | isa<CFConsumedAttr>(TmplAttr)) { |
| 457 | AddXConsumedAttr(New, TmplAttr->getRange(), |
| 458 | TmplAttr->getSpellingListIndex(), |
| 459 | attrToRetainOwnershipKind(TmplAttr), |
| 460 | /*template instantiation=*/true); |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 461 | continue; |
| 462 | } |
| 463 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 464 | assert(!TmplAttr->isPackExpansion()); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 465 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 466 | // Late parsed attributes must be instantiated and attached after the |
| 467 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 468 | LocalInstantiationScope *Saved = nullptr; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 469 | if (CurrentInstantiationScope) |
| 470 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 471 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 472 | } else { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 473 | // Allow 'this' within late-parsed attributes. |
| 474 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 475 | CXXRecordDecl *ThisContext = |
| 476 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 477 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 478 | ND && ND->isCXXInstanceMember()); |
| 479 | |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 480 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 481 | *this, TemplateArgs); |
Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 482 | if (NewAttr) |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 483 | New->addAttr(NewAttr); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 484 | } |
Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 488 | /// Get the previous declaration of a declaration for the purposes of template |
| 489 | /// instantiation. If this finds a previous declaration, then the previous |
| 490 | /// declaration of the instantiation of D should be an instantiation of the |
| 491 | /// result of this function. |
| 492 | template<typename DeclT> |
| 493 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 494 | DeclT *Result = D->getPreviousDecl(); |
| 495 | |
| 496 | // If the declaration is within a class, and the previous declaration was |
| 497 | // merged from a different definition of that class, then we don't have a |
| 498 | // previous declaration for the purpose of template instantiation. |
| 499 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 500 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 501 | return nullptr; |
| 502 | |
| 503 | return Result; |
| 504 | } |
| 505 | |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 506 | Decl * |
| 507 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 508 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | Decl * |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 512 | TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { |
| 513 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 514 | } |
| 515 | |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 516 | Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( |
| 517 | PragmaDetectMismatchDecl *D) { |
| 518 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 519 | } |
| 520 | |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 521 | Decl * |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 522 | TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { |
| 523 | llvm_unreachable("extern \"C\" context cannot be instantiated"); |
| 524 | } |
| 525 | |
| 526 | Decl * |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 527 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 528 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 529 | D->getIdentifier()); |
| 530 | Owner->addDecl(Inst); |
| 531 | return Inst; |
| 532 | } |
| 533 | |
| 534 | Decl * |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 535 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 536 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 537 | } |
| 538 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 539 | Decl * |
| 540 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 541 | NamespaceAliasDecl *Inst |
| 542 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 543 | D->getNamespaceLoc(), |
| 544 | D->getAliasLoc(), |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 545 | D->getIdentifier(), |
| 546 | D->getQualifierLoc(), |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 547 | D->getTargetNameLoc(), |
| 548 | D->getNamespace()); |
| 549 | Owner->addDecl(Inst); |
| 550 | return Inst; |
| 551 | } |
| 552 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 553 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 554 | bool IsTypeAlias) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 555 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 556 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 557 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 558 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 559 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 560 | D->getLocation(), D->getDeclName()); |
| 561 | if (!DI) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 562 | Invalid = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 563 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 564 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 565 | } else { |
| 566 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 567 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 569 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 570 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 571 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 572 | // semantics. See LWG issue 2141 for more information on the bug. |
| 573 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 574 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 575 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 576 | DT->isReferenceType() && |
| 577 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 578 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 579 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 580 | SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 581 | // Fold it to the (non-reference) type which g++ would have produced. |
| 582 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 583 | DI->getType().getNonReferenceType()); |
| 584 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 585 | // Create the new typedef |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 586 | TypedefNameDecl *Typedef; |
| 587 | if (IsTypeAlias) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 588 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 589 | D->getLocation(), D->getIdentifier(), DI); |
| 590 | else |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 591 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 592 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 593 | if (Invalid) |
| 594 | Typedef->setInvalidDecl(); |
| 595 | |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 596 | // If the old typedef was the name for linkage purposes of an anonymous |
| 597 | // tag decl, re-establish that relationship for the new typedef. |
| 598 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 599 | TagDecl *oldTag = oldTagType->getDecl(); |
Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 600 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 601 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 602 | assert(!newTag->hasNameForLinkage()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 603 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 604 | } |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 605 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 606 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 607 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 608 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 609 | TemplateArgs); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 610 | if (!InstPrev) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 611 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 612 | |
Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 613 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 614 | |
| 615 | // If the typedef types are not identical, reject them. |
| 616 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 617 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 618 | Typedef->setPreviousDecl(InstPrevTypedef); |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 619 | } |
| 620 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 621 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 622 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 623 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 625 | return Typedef; |
| 626 | } |
| 627 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 628 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 629 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 630 | if (Typedef) |
| 631 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 632 | return Typedef; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 636 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 637 | if (Typedef) |
| 638 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 639 | return Typedef; |
| 640 | } |
| 641 | |
| 642 | Decl * |
| 643 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 644 | // Create a local instantiation scope for this type alias template, which |
| 645 | // will contain the instantiations of the template parameters. |
| 646 | LocalInstantiationScope Scope(SemaRef); |
| 647 | |
| 648 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 649 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 650 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 651 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 652 | |
| 653 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 654 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 655 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 656 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 657 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 658 | if (!Found.empty()) { |
| 659 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
| 663 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 664 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 665 | if (!AliasInst) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 666 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 667 | |
| 668 | TypeAliasTemplateDecl *Inst |
| 669 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 670 | D->getDeclName(), InstParams, AliasInst); |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 671 | AliasInst->setDescribedAliasTemplate(Inst); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 672 | if (PrevAliasTemplate) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 673 | Inst->setPreviousDecl(PrevAliasTemplate); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 674 | |
| 675 | Inst->setAccess(D->getAccess()); |
| 676 | |
| 677 | if (!PrevAliasTemplate) |
| 678 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 679 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 680 | Owner->addDecl(Inst); |
| 681 | |
| 682 | return Inst; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 685 | Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 686 | auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 687 | D->getIdentifier()); |
Richard Smith | 81df9eb | 2017-10-02 22:43:36 +0000 | [diff] [blame] | 688 | NewBD->setReferenced(D->isReferenced()); |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 689 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); |
| 690 | return NewBD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 694 | // Transform the bindings first. |
| 695 | SmallVector<BindingDecl*, 16> NewBindings; |
| 696 | for (auto *OldBD : D->bindings()) |
| 697 | NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); |
| 698 | ArrayRef<BindingDecl*> NewBindingArray = NewBindings; |
| 699 | |
| 700 | auto *NewDD = cast_or_null<DecompositionDecl>( |
| 701 | VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); |
| 702 | |
| 703 | if (!NewDD || NewDD->isInvalidDecl()) |
| 704 | for (auto *NewBD : NewBindings) |
| 705 | NewBD->setInvalidDecl(); |
| 706 | |
| 707 | return NewDD; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 710 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 711 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 714 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 715 | bool InstantiatingVarTemplate, |
| 716 | ArrayRef<BindingDecl*> *Bindings) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 717 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 718 | // Do substitution on the type of the declaration |
Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 719 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 720 | D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), |
| 721 | D->getDeclName(), /*AllowDeducedTST*/true); |
John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 722 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 723 | return nullptr; |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 725 | if (DI->getType()->isFunctionType()) { |
| 726 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 727 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 728 | return nullptr; |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 729 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 730 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 731 | DeclContext *DC = Owner; |
| 732 | if (D->isLocalExternDecl()) |
| 733 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 734 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 735 | // Build the instantiated declaration. |
Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 736 | VarDecl *Var; |
| 737 | if (Bindings) |
| 738 | Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 739 | D->getLocation(), DI->getType(), DI, |
| 740 | D->getStorageClass(), *Bindings); |
| 741 | else |
| 742 | Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 743 | D->getLocation(), D->getIdentifier(), DI->getType(), |
| 744 | DI, D->getStorageClass()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 746 | // In ARC, infer 'retaining' for variables of retainable type. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 747 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 748 | SemaRef.inferObjCARCLifetime(Var)) |
| 749 | Var->setInvalidDecl(); |
| 750 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 751 | // Substitute the nested name specifier, if any. |
| 752 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 753 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 755 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 756 | StartingScope, InstantiatingVarTemplate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 757 | |
| 758 | if (D->isNRVOVariable()) { |
| 759 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
Richard Trieu | 09c163b | 2018-03-15 03:00:55 +0000 | [diff] [blame] | 760 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, Sema::CES_Strict)) |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 761 | Var->setNRVOVariable(true); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 762 | } |
Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 763 | |
Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 764 | Var->setImplicit(D->isImplicit()); |
| 765 | |
Hans Wennborg | 262baa4 | 2018-10-31 10:34:46 +0000 | [diff] [blame] | 766 | if (Var->isStaticLocal()) |
| 767 | SemaRef.CheckStaticLocalForDllExport(Var); |
| 768 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 769 | return Var; |
| 770 | } |
| 771 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 772 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 773 | AccessSpecDecl* AD |
| 774 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 775 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 776 | Owner->addHiddenDecl(AD); |
| 777 | return AD; |
| 778 | } |
| 779 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 780 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 781 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 782 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 783 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 784 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 785 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 786 | D->getLocation(), D->getDeclName()); |
| 787 | if (!DI) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 788 | DI = D->getTypeSourceInfo(); |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 789 | Invalid = true; |
| 790 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 791 | // C++ [temp.arg.type]p3: |
| 792 | // If a declaration acquires a function type through a type |
| 793 | // dependent on a template-parameter and this causes a |
| 794 | // declaration that does not use the syntactic form of a |
| 795 | // function declarator to have function type, the program is |
| 796 | // ill-formed. |
| 797 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 798 | << DI->getType(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 799 | Invalid = true; |
| 800 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 801 | } else { |
| 802 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | Expr *BitWidth = D->getBitWidth(); |
| 806 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 807 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 808 | else if (BitWidth) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 809 | // The bit-width expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 810 | EnterExpressionEvaluationContext Unevaluated( |
| 811 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 813 | ExprResult InstantiatedBitWidth |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 814 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 815 | if (InstantiatedBitWidth.isInvalid()) { |
| 816 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 817 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 818 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 819 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 820 | } |
| 821 | |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 822 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 823 | DI->getType(), DI, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | cast<RecordDecl>(Owner), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 825 | D->getLocation(), |
| 826 | D->isMutable(), |
| 827 | BitWidth, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 828 | D->getInClassInitStyle(), |
Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 829 | D->getInnerLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 830 | D->getAccess(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 831 | nullptr); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 832 | if (!Field) { |
| 833 | cast<Decl>(Owner)->setInvalidDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 834 | return nullptr; |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 835 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 837 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 838 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 839 | if (Field->hasAttrs()) |
| 840 | SemaRef.CheckAlignasUnderalignment(Field); |
| 841 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 842 | if (Invalid) |
| 843 | Field->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 845 | if (!Field->getDeclName()) { |
| 846 | // Keep track of where this decl came from. |
| 847 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 848 | } |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 849 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 850 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 851 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 852 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 855 | Field->setImplicit(D->isImplicit()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 856 | Field->setAccess(D->getAccess()); |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 857 | Owner->addDecl(Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 858 | |
| 859 | return Field; |
| 860 | } |
| 861 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 862 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 863 | bool Invalid = false; |
| 864 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 865 | |
| 866 | if (DI->getType()->isVariablyModifiedType()) { |
| 867 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 868 | << D; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 869 | Invalid = true; |
| 870 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 871 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 872 | D->getLocation(), D->getDeclName()); |
| 873 | if (!DI) { |
| 874 | DI = D->getTypeSourceInfo(); |
| 875 | Invalid = true; |
| 876 | } else if (DI->getType()->isFunctionType()) { |
| 877 | // C++ [temp.arg.type]p3: |
| 878 | // If a declaration acquires a function type through a type |
| 879 | // dependent on a template-parameter and this causes a |
| 880 | // declaration that does not use the syntactic form of a |
| 881 | // function declarator to have function type, the program is |
| 882 | // ill-formed. |
| 883 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 884 | << DI->getType(); |
| 885 | Invalid = true; |
| 886 | } |
| 887 | } else { |
| 888 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 889 | } |
| 890 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 891 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 892 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 893 | DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 894 | |
| 895 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 896 | StartingScope); |
| 897 | |
| 898 | if (Invalid) |
| 899 | Property->setInvalidDecl(); |
| 900 | |
| 901 | Property->setAccess(D->getAccess()); |
| 902 | Owner->addDecl(Property); |
| 903 | |
| 904 | return Property; |
| 905 | } |
| 906 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 907 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 908 | NamedDecl **NamedChain = |
| 909 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 910 | |
| 911 | int i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 912 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 913 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 914 | TemplateArgs); |
| 915 | if (!Next) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 916 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 918 | NamedChain[i++] = Next; |
| 919 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 920 | |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 921 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 922 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 923 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 924 | {NamedChain, D->getChainingSize()}); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 925 | |
NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 926 | for (const auto *Attr : D->attrs()) |
| 927 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 928 | |
| 929 | IndirectField->setImplicit(D->isImplicit()); |
| 930 | IndirectField->setAccess(D->getAccess()); |
| 931 | Owner->addDecl(IndirectField); |
| 932 | return IndirectField; |
| 933 | } |
| 934 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 935 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 936 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 937 | // parameters into the pattern type and checking the result. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 938 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 939 | TypeSourceInfo *InstTy; |
| 940 | // If this is an unsupported friend, don't bother substituting template |
| 941 | // arguments into it. The actual type referred to won't be used by any |
| 942 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 943 | // same info for the instantiated friend. |
| 944 | if (D->isUnsupportedFriend()) { |
| 945 | InstTy = Ty; |
| 946 | } else { |
| 947 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 948 | D->getLocation(), DeclarationName()); |
| 949 | } |
| 950 | if (!InstTy) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 951 | return nullptr; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 952 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 953 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 954 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 955 | if (!FD) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 956 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 958 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 959 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 960 | Owner->addDecl(FD); |
| 961 | return FD; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 964 | NamedDecl *ND = D->getFriendDecl(); |
| 965 | assert(ND && "friend decl must be a decl or a type!"); |
| 966 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 967 | // All of the Visit implementations for the various potential friend |
| 968 | // declarations have to be carefully written to work for friend |
| 969 | // objects, with the most important detail being that the target |
| 970 | // decl should almost certainly not be placed in Owner. |
| 971 | Decl *NewND = Visit(ND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 972 | if (!NewND) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 974 | FriendDecl *FD = |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 975 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 976 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 977 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 978 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 979 | Owner->addDecl(FD); |
| 980 | return FD; |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 983 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 984 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 986 | // The expression in a static assertion is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 987 | EnterExpressionEvaluationContext Unevaluated( |
| 988 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 990 | ExprResult InstantiatedAssertExpr |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 991 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 992 | if (InstantiatedAssertExpr.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 993 | return nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 994 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 995 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 996 | InstantiatedAssertExpr.get(), |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 997 | D->getMessage(), |
| 998 | D->getRParenLoc(), |
| 999 | D->isFailed()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1003 | EnumDecl *PrevDecl = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1004 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1005 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1006 | PatternPrev, |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1007 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1008 | if (!Prev) return nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1009 | PrevDecl = cast<EnumDecl>(Prev); |
| 1010 | } |
| 1011 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1012 | EnumDecl *Enum = |
| 1013 | EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 1014 | D->getLocation(), D->getIdentifier(), PrevDecl, |
| 1015 | D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1016 | if (D->isFixed()) { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1017 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1018 | // If we have type source information for the underlying type, it means it |
| 1019 | // has been explicitly set by the user. Perform substitution on it before |
| 1020 | // moving on. |
| 1021 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1022 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 1023 | DeclarationName()); |
| 1024 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1025 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1026 | else |
| 1027 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 1028 | } else { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1029 | assert(!D->getIntegerType()->isDependentType() |
| 1030 | && "Dependent type without type source info"); |
| 1031 | Enum->setIntegerType(D->getIntegerType()); |
| 1032 | } |
| 1033 | } |
| 1034 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1035 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 1036 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1037 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 1038 | Enum->setAccess(D->getAccess()); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1039 | // Forward the mangling number from the template to the instantiated decl. |
| 1040 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1041 | // See if the old tag was defined along with a declarator. |
| 1042 | // If it did, mark the new tag as being associated with that declarator. |
| 1043 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1044 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); |
| 1045 | // See if the old tag was defined along with a typedef. |
| 1046 | // If it did, mark the new tag as being associated with that typedef. |
| 1047 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1048 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1049 | if (SubstQualifier(D, Enum)) return nullptr; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1050 | Owner->addDecl(Enum); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1051 | |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1052 | EnumDecl *Def = D->getDefinition(); |
| 1053 | if (Def && Def != D) { |
| 1054 | // If this is an out-of-line definition of an enum member template, check |
| 1055 | // that the underlying types match in the instantiation of both |
| 1056 | // declarations. |
| 1057 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 1058 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1059 | QualType DefnUnderlying = |
| 1060 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 1061 | UnderlyingLoc, DeclarationName()); |
| 1062 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 1063 | DefnUnderlying, /*IsFixed=*/true, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1066 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1067 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 1068 | // specialization causes the implicit instantiation of the declarations, but |
| 1069 | // not the definitions of scoped member enumerations. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1070 | // |
| 1071 | // DR1484 clarifies that enumeration definitions inside of a template |
| 1072 | // declaration aren't considered entities that can be separately instantiated |
| 1073 | // from the rest of the entity they are declared inside of. |
| 1074 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 1075 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1076 | InstantiateEnumDefinition(Enum, Def); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1077 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1078 | |
| 1079 | return Enum; |
| 1080 | } |
| 1081 | |
| 1082 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 1083 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 1084 | Enum->startDefinition(); |
| 1085 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 1086 | // Update the location to refer to the definition. |
| 1087 | Enum->setLocation(Pattern->getLocation()); |
| 1088 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1089 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1090 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1091 | EnumConstantDecl *LastEnumConst = nullptr; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1092 | for (auto *EC : Pattern->enumerators()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1093 | // The specified value for the enumerator. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1094 | ExprResult Value((Expr *)nullptr); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1095 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1096 | // The enumerator's value expression is a constant expression. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1097 | EnterExpressionEvaluationContext Unevaluated( |
| 1098 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1100 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1101 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1102 | |
| 1103 | // Drop the initial value and continue. |
| 1104 | bool isInvalid = false; |
| 1105 | if (Value.isInvalid()) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1106 | Value = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1107 | isInvalid = true; |
| 1108 | } |
| 1109 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | EnumConstantDecl *EnumConst |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1111 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 1112 | EC->getLocation(), EC->getIdentifier(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1113 | Value.get()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (isInvalid) { |
| 1116 | if (EnumConst) |
| 1117 | EnumConst->setInvalidDecl(); |
| 1118 | Enum->setInvalidDecl(); |
| 1119 | } |
| 1120 | |
| 1121 | if (EnumConst) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1122 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1123 | |
John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 1124 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1125 | Enum->addDecl(EnumConst); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1126 | Enumerators.push_back(EnumConst); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1127 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1128 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1129 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 1130 | !Enum->isScoped()) { |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1131 | // If the enumeration is within a function or method, record the enum |
| 1132 | // constant as a local. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1133 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1134 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 1138 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1139 | Enumerators, nullptr, ParsedAttributesView()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1142 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1143 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1146 | Decl * |
| 1147 | TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 1148 | llvm_unreachable("BuiltinTemplateDecls cannot be instantiated."); |
| 1149 | } |
| 1150 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1151 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1152 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1153 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1154 | // Create a local instantiation scope for this class template, which |
| 1155 | // will contain the instantiations of the template parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1156 | LocalInstantiationScope Scope(SemaRef); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1157 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1158 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1160 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1161 | |
| 1162 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Instantiate the qualifier. We have to do this first in case |
| 1165 | // we're a friend declaration, because if we are then we need to put |
| 1166 | // the new declaration in the appropriate context. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1167 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 1168 | if (QualifierLoc) { |
| 1169 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1170 | TemplateArgs); |
| 1171 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1172 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1175 | CXXRecordDecl *PrevDecl = nullptr; |
| 1176 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1177 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1178 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1179 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1180 | if (!Found.empty()) { |
| 1181 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1182 | if (PrevClassTemplate) |
| 1183 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1184 | } |
| 1185 | } |
| 1186 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1187 | // If this isn't a friend, then it's a member template, in which |
| 1188 | // case we just want to build the instantiation in the |
| 1189 | // specialization. If it is a friend, we want to build it in |
| 1190 | // the appropriate context. |
| 1191 | DeclContext *DC = Owner; |
| 1192 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1193 | if (QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1194 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1195 | SS.Adopt(QualifierLoc); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1196 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1197 | if (!DC) return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1198 | } else { |
| 1199 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 1200 | Pattern->getDeclContext(), |
| 1201 | TemplateArgs); |
| 1202 | } |
| 1203 | |
| 1204 | // Look for a previous declaration of the template in the owning |
| 1205 | // context. |
| 1206 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1207 | Sema::LookupOrdinaryName, |
| 1208 | SemaRef.forRedeclarationInCurContext()); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1209 | SemaRef.LookupQualifiedName(R, DC); |
| 1210 | |
| 1211 | if (R.isSingleResult()) { |
| 1212 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 1213 | if (PrevClassTemplate) |
| 1214 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1215 | } |
| 1216 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1217 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1218 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 1219 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1220 | << QualifierLoc.getSourceRange(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1221 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1224 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1225 | if (PrevClassTemplate) { |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1226 | bool Complain = true; |
| 1227 | |
| 1228 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 1229 | // template for struct std::tr1::__detail::_Map_base, where the |
| 1230 | // template parameters of the friend declaration don't match the |
| 1231 | // template parameters of the original declaration. In this one |
| 1232 | // case, we don't complain about the ill-formed friend |
| 1233 | // declaration. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1234 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1235 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 1236 | DC->isNamespace() && |
| 1237 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1238 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 1239 | DeclContext *DCParent = DC->getParent(); |
| 1240 | if (DCParent->isNamespace() && |
| 1241 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 1242 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 1243 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1244 | Complain = false; |
| 1245 | } |
| 1246 | } |
| 1247 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1248 | TemplateParameterList *PrevParams |
Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1249 | = PrevClassTemplate->getMostRecentDecl()->getTemplateParameters(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Make sure the parameter lists match. |
| 1252 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1253 | Complain, |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1254 | Sema::TPL_TemplateMatch)) { |
| 1255 | if (Complain) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1256 | return nullptr; |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1257 | |
| 1258 | AdoptedPreviousTemplateParams = true; |
| 1259 | InstParams = PrevParams; |
| 1260 | } |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Do some additional validation, then merge default arguments |
| 1263 | // from the existing declarations. |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1264 | if (!AdoptedPreviousTemplateParams && |
| 1265 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1266 | Sema::TPC_ClassTemplate)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1267 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1271 | CXXRecordDecl *RecordInst = CXXRecordDecl::Create( |
| 1272 | SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), |
| 1273 | Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, |
| 1274 | /*DelayTypeCreation=*/true); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1276 | if (QualifierLoc) |
| 1277 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1278 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1279 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, |
| 1280 | StartingScope); |
| 1281 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1282 | ClassTemplateDecl *Inst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1283 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1284 | D->getIdentifier(), InstParams, RecordInst); |
| 1285 | assert(!(isFriend && Owner->isDependentContext())); |
| 1286 | Inst->setPreviousDecl(PrevClassTemplate); |
| 1287 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1288 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1289 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1290 | if (isFriend) { |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1291 | if (PrevClassTemplate) |
| 1292 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1293 | else |
| 1294 | Inst->setAccess(D->getAccess()); |
| 1295 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1296 | Inst->setObjectOfFriendDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1297 | // TODO: do we want to track the instantiation progeny of this |
| 1298 | // friend target decl? |
| 1299 | } else { |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1300 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1301 | if (!PrevClassTemplate) |
| 1302 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1303 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1305 | // Trigger creation of the type for the instantiation. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1306 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1307 | Inst->getInjectedClassNameSpecialization()); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1309 | // Finish handling of friends. |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1310 | if (isFriend) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1311 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1312 | Inst->setLexicalDeclContext(Owner); |
| 1313 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1314 | return Inst; |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1315 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1316 | |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1317 | if (D->isOutOfLine()) { |
| 1318 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1319 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1320 | } |
| 1321 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1322 | Owner->addDecl(Inst); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (!PrevClassTemplate) { |
| 1325 | // Queue up any out-of-line partial specializations of this member |
| 1326 | // class template; the client will force their instantiation once |
| 1327 | // the enclosing class has been instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1328 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1329 | D->getPartialSpecializations(PartialSpecs); |
| 1330 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1331 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1332 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1333 | } |
| 1334 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1335 | return Inst; |
| 1336 | } |
| 1337 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1338 | Decl * |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1339 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1340 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1341 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1343 | // Lookup the already-instantiated declaration in the instantiation |
| 1344 | // of the class template and return that. |
| 1345 | DeclContext::lookup_result Found |
| 1346 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1347 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1348 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1350 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1351 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1352 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1353 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1355 | if (ClassTemplatePartialSpecializationDecl *Result |
| 1356 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1357 | return Result; |
| 1358 | |
| 1359 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1362 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1363 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1364 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Create a local instantiation scope for this variable template, which |
| 1367 | // will contain the instantiations of the template parameters. |
| 1368 | LocalInstantiationScope Scope(SemaRef); |
| 1369 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1370 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1371 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1372 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1373 | |
| 1374 | VarDecl *Pattern = D->getTemplatedDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1375 | VarTemplateDecl *PrevVarTemplate = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1376 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1377 | if (getPreviousDeclForInstantiation(Pattern)) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1378 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1379 | if (!Found.empty()) |
| 1380 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1381 | } |
| 1382 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1383 | VarDecl *VarInst = |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1384 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1385 | /*InstantiatingVarTemplate=*/true)); |
Nick Lewycky | 6ca07ca | 2015-08-10 21:54:08 +0000 | [diff] [blame] | 1386 | if (!VarInst) return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1387 | |
| 1388 | DeclContext *DC = Owner; |
| 1389 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1390 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1391 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1392 | VarInst); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1393 | VarInst->setDescribedVarTemplate(Inst); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1394 | Inst->setPreviousDecl(PrevVarTemplate); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1395 | |
| 1396 | Inst->setAccess(D->getAccess()); |
| 1397 | if (!PrevVarTemplate) |
| 1398 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1399 | |
| 1400 | if (D->isOutOfLine()) { |
| 1401 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1402 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1403 | } |
| 1404 | |
| 1405 | Owner->addDecl(Inst); |
| 1406 | |
| 1407 | if (!PrevVarTemplate) { |
| 1408 | // Queue up any out-of-line partial specializations of this member |
| 1409 | // variable template; the client will force their instantiation once |
| 1410 | // the enclosing class has been instantiated. |
| 1411 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1412 | D->getPartialSpecializations(PartialSpecs); |
| 1413 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1414 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1415 | OutOfLineVarPartialSpecs.push_back( |
| 1416 | std::make_pair(Inst, PartialSpecs[I])); |
| 1417 | } |
| 1418 | |
| 1419 | return Inst; |
| 1420 | } |
| 1421 | |
| 1422 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1423 | VarTemplatePartialSpecializationDecl *D) { |
| 1424 | assert(D->isStaticDataMember() && |
| 1425 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1426 | |
| 1427 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1428 | |
| 1429 | // Lookup the already-instantiated declaration and return that. |
| 1430 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1431 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1432 | |
| 1433 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1434 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1435 | |
| 1436 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1437 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1438 | return Result; |
| 1439 | |
| 1440 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1441 | } |
| 1442 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1443 | Decl * |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1444 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1445 | // Create a local instantiation scope for this function template, which |
| 1446 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1447 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1448 | // itself. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1449 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1450 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1451 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1452 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1454 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1455 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1456 | FunctionDecl *Instantiated = nullptr; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1457 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1458 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1459 | InstParams)); |
| 1460 | else |
| 1461 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1462 | D->getTemplatedDecl(), |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1463 | InstParams)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1464 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1465 | if (!Instantiated) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1466 | return nullptr; |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1467 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | // Link the instantiated function template declaration to the function |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1469 | // template from which it was instantiated. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1470 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1471 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1472 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1473 | assert(InstTemplate && |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1474 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1475 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1476 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1477 | |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1478 | // Link the instantiation back to the pattern *unless* this is a |
| 1479 | // non-definition friend declaration. |
| 1480 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1481 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1482 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1483 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1484 | // Make declarations visible in the appropriate context. |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1485 | if (!isFriend) { |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1486 | Owner->addDecl(InstTemplate); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1487 | } else if (InstTemplate->getDeclContext()->isRecord() && |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1488 | !getPreviousDeclForInstantiation(D)) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1489 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1490 | } |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1492 | return InstTemplate; |
| 1493 | } |
| 1494 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1495 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1496 | CXXRecordDecl *PrevDecl = nullptr; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1497 | if (D->isInjectedClassName()) |
| 1498 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1499 | else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1500 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1501 | PatternPrev, |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1502 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1503 | if (!Prev) return nullptr; |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1504 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1505 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1506 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1507 | CXXRecordDecl *Record = CXXRecordDecl::Create( |
| 1508 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 1509 | D->getLocation(), D->getIdentifier(), PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Substitute the nested name specifier, if any. |
| 1512 | if (SubstQualifier(D, Record)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1513 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1514 | |
Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1515 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, |
| 1516 | StartingScope); |
| 1517 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1518 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1519 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1520 | // the tag decls introduced by friend class declarations don't have an access |
| 1521 | // specifier. Remove once this area of the code gets sorted out. |
| 1522 | if (D->getAccess() != AS_none) |
| 1523 | Record->setAccess(D->getAccess()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1524 | if (!D->isInjectedClassName()) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1525 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1526 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1527 | // If the original function was part of a friend declaration, |
| 1528 | // inherit its namespace state. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1529 | if (D->getFriendObjectKind()) |
| 1530 | Record->setObjectOfFriendDecl(); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1531 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1532 | // Make sure that anonymous structs and unions are recorded. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1533 | if (D->isAnonymousStructOrUnion()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1534 | Record->setAnonymousStructOrUnion(true); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1535 | |
| 1536 | if (D->isLocalClass()) |
| 1537 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1538 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1539 | // Forward the mangling number from the template to the instantiated decl. |
| 1540 | SemaRef.Context.setManglingNumber(Record, |
| 1541 | SemaRef.Context.getManglingNumber(D)); |
| 1542 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1543 | // See if the old tag was defined along with a declarator. |
| 1544 | // If it did, mark the new tag as being associated with that declarator. |
| 1545 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1546 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); |
| 1547 | |
| 1548 | // See if the old tag was defined along with a typedef. |
| 1549 | // If it did, mark the new tag as being associated with that typedef. |
| 1550 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1551 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); |
| 1552 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1553 | Owner->addDecl(Record); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1554 | |
| 1555 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1556 | // of the instantiation of their enclosing entity. |
| 1557 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1558 | Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1559 | |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1560 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1561 | TSK_ImplicitInstantiation, |
| 1562 | /*Complain=*/true); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1563 | |
Richard Smith | ece4758 | 2017-01-04 23:45:01 +0000 | [diff] [blame] | 1564 | // For nested local classes, we will instantiate the members when we |
| 1565 | // reach the end of the outermost (non-nested) local class. |
| 1566 | if (!D->isCXXClassMember()) |
| 1567 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1568 | TSK_ImplicitInstantiation); |
Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1569 | |
| 1570 | // This class may have local implicit instantiations that need to be |
| 1571 | // performed within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1572 | LocalInstantiations.perform(); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1573 | } |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1574 | |
| 1575 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1576 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1577 | return Record; |
| 1578 | } |
| 1579 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1580 | /// Adjust the given function type for an instantiation of the |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1581 | /// given declaration, to cope with modifications to the function's type that |
| 1582 | /// aren't reflected in the type-source information. |
| 1583 | /// |
| 1584 | /// \param D The declaration we're instantiating. |
| 1585 | /// \param TInfo The already-instantiated type. |
| 1586 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1587 | FunctionDecl *D, |
| 1588 | TypeSourceInfo *TInfo) { |
Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1589 | const FunctionProtoType *OrigFunc |
| 1590 | = D->getType()->castAs<FunctionProtoType>(); |
| 1591 | const FunctionProtoType *NewFunc |
| 1592 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1593 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1594 | return TInfo->getType(); |
| 1595 | |
| 1596 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1597 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1598 | return Context.getFunctionType(NewFunc->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1599 | NewFunc->getParamTypes(), NewEPI); |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1602 | /// Normal class members are of more specific types and therefore |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1603 | /// don't make it here. This function serves three purposes: |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1604 | /// 1) instantiating function templates |
| 1605 | /// 2) substituting friend declarations |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1606 | /// 3) substituting deduction guide declarations for nested class templates |
Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1607 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1608 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1609 | // Check whether there is already a function template specialization for |
| 1610 | // this declaration. |
| 1611 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1612 | if (FunctionTemplate && !TemplateParams) { |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1613 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1615 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1616 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1617 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1619 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1620 | if (SpecFunc) |
| 1621 | return SpecFunc; |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1622 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1624 | bool isFriend; |
| 1625 | if (FunctionTemplate) |
| 1626 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1627 | else |
| 1628 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1629 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1630 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1631 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1632 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1633 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1634 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1636 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1637 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1638 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1639 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1640 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1641 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1642 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1643 | if (QualifierLoc) { |
| 1644 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1645 | TemplateArgs); |
| 1646 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1647 | return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1650 | // If we're instantiating a local function declaration, put the result |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1651 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1652 | // context. |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1653 | DeclContext *DC; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1654 | if (D->isLocalExternDecl()) { |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1655 | DC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1656 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1657 | } else if (isFriend && QualifierLoc) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1658 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1659 | SS.Adopt(QualifierLoc); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1660 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1661 | if (!DC) return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1662 | } else { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1663 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1664 | TemplateArgs); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1665 | } |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1666 | |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1667 | DeclarationNameInfo NameInfo |
| 1668 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1669 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1670 | FunctionDecl *Function; |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1671 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1672 | Function = CXXDeductionGuideDecl::Create( |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1673 | SemaRef.Context, DC, D->getInnerLocStart(), DGuide->isExplicit(), |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1674 | NameInfo, T, TInfo, D->getSourceRange().getEnd()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1675 | if (DGuide->isCopyDeductionCandidate()) |
| 1676 | cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1677 | Function->setAccess(D->getAccess()); |
Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1678 | } else { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1679 | Function = FunctionDecl::Create( |
Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1680 | SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1681 | D->getCanonicalDecl()->getStorageClass(), D->isInlineSpecified(), |
| 1682 | D->hasWrittenPrototype(), D->isConstexpr()); |
| 1683 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
| 1684 | } |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1685 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1686 | if (D->isInlined()) |
| 1687 | Function->setImplicitlyInline(); |
| 1688 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1689 | if (QualifierLoc) |
| 1690 | Function->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1691 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1692 | if (D->isLocalExternDecl()) |
| 1693 | Function->setLocalExternDecl(); |
| 1694 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1695 | DeclContext *LexicalDC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1696 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1697 | assert(D->getDeclContext()->isFileContext()); |
| 1698 | LexicalDC = D->getDeclContext(); |
| 1699 | } |
| 1700 | |
| 1701 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1703 | // Attach the parameters |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1704 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1705 | if (Params[P]) |
| 1706 | Params[P]->setOwningFunction(Function); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1707 | Function->setParams(Params); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1708 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1709 | if (TemplateParams) { |
| 1710 | // Our resulting instantiation is actually a function template, since we |
| 1711 | // are substituting only the outer template parameters. For example, given |
| 1712 | // |
| 1713 | // template<typename T> |
| 1714 | // struct X { |
| 1715 | // template<typename U> friend void f(T, U); |
| 1716 | // }; |
| 1717 | // |
| 1718 | // X<int> x; |
| 1719 | // |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1720 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1721 | // which means substituting int for T, but leaving "f" as a friend function |
| 1722 | // template. |
| 1723 | // Build the function template itself. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1724 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1725 | Function->getLocation(), |
| 1726 | Function->getDeclName(), |
| 1727 | TemplateParams, Function); |
| 1728 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1729 | |
| 1730 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (isFriend && D->isThisDeclarationADefinition()) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1733 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1734 | D->getDescribedFunctionTemplate()); |
| 1735 | } |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1736 | } else if (FunctionTemplate) { |
| 1737 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1738 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1739 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1740 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1741 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1742 | /*InsertPos=*/nullptr); |
Richard Smith | 152bcd2 | 2017-01-28 02:56:07 +0000 | [diff] [blame] | 1743 | } else if (isFriend && D->isThisDeclarationADefinition()) { |
| 1744 | // Do not connect the friend to the template unless it's actually a |
| 1745 | // definition. We don't want non-template functions to be marked as being |
| 1746 | // template instantiations. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1747 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1748 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1750 | if (InitFunctionInstantiation(Function, D)) |
| 1751 | Function->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1753 | bool isExplicitSpecialization = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1754 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1755 | LookupResult Previous( |
| 1756 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1757 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1758 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1759 | D->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 1760 | : SemaRef.forRedeclarationInCurContext()); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1761 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1762 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1763 | = D->getDependentSpecializationInfo()) { |
| 1764 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1765 | |
| 1766 | // This needs to be set now for future sanity. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1767 | Function->setObjectOfFriendDecl(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1768 | |
| 1769 | // Instantiate the explicit template arguments. |
| 1770 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1771 | Info->getRAngleLoc()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1772 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1773 | ExplicitArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1774 | return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1775 | |
| 1776 | // Map the candidate templates to their instantiations. |
| 1777 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1778 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1779 | Info->getTemplate(I), |
| 1780 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1781 | if (!Temp) return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1782 | |
| 1783 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1784 | } |
| 1785 | |
| 1786 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1787 | &ExplicitArgs, |
| 1788 | Previous)) |
| 1789 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1790 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1791 | isExplicitSpecialization = true; |
| 1792 | |
| 1793 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1794 | // Look only into the namespace where the friend would be declared to |
| 1795 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1796 | // as described in ActOnFriendFunctionDecl. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1797 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1799 | // In C++, the previous declaration we find might be a tag type |
| 1800 | // (class or enum). In this case, the new declaration will hide the |
| 1801 | // tag type. Note that this does does not apply if we're declaring a |
| 1802 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1803 | if (Previous.isSingleTagDecl()) |
| 1804 | Previous.clear(); |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1805 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1806 | |
Serge Pavlov | 25dbe1a | 2017-06-21 12:46:57 +0000 | [diff] [blame] | 1807 | if (isFriend) |
| 1808 | Function->setObjectOfFriendDecl(); |
| 1809 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1810 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1811 | isExplicitSpecialization); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1812 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1813 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1814 | ? cast<NamedDecl>(FunctionTemplate) |
| 1815 | : Function); |
| 1816 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1817 | // If the original function was part of a friend declaration, |
| 1818 | // inherit its namespace state and add it to the owner. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1819 | if (isFriend) { |
Serge Pavlov | acfcd78 | 2018-12-06 09:35:04 +0000 | [diff] [blame] | 1820 | Function->setObjectOfFriendDecl(); |
| 1821 | if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) |
| 1822 | FT->setObjectOfFriendDecl(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1823 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1824 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1825 | bool QueuedInstantiation = false; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1826 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1827 | // C++11 [temp.friend]p4 (DR329): |
| 1828 | // When a function is defined in a friend function declaration in a class |
| 1829 | // template, the function is instantiated when the function is odr-used. |
| 1830 | // The same restrictions on multiple declarations and definitions that |
| 1831 | // apply to non-template function declarations and definitions also apply |
| 1832 | // to these implicit definitions. |
| 1833 | if (D->isThisDeclarationADefinition()) { |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1834 | SemaRef.CheckForFunctionRedefinition(Function); |
| 1835 | if (!Function->isInvalidDecl()) { |
| 1836 | for (auto R : Function->redecls()) { |
| 1837 | if (R == Function) |
| 1838 | continue; |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1839 | |
Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1840 | // If some prior declaration of this function has been used, we need |
| 1841 | // to instantiate its definition. |
| 1842 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1843 | if (MemberSpecializationInfo *MSInfo = |
| 1844 | Function->getMemberSpecializationInfo()) { |
| 1845 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1846 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1847 | MSInfo->setPointOfInstantiation(Loc); |
| 1848 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1849 | std::make_pair(Function, Loc)); |
| 1850 | QueuedInstantiation = true; |
| 1851 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1852 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1853 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | } |
Richard Smith | f359765 | 2017-05-10 00:01:13 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Check the template parameter list against the previous declaration. The |
| 1859 | // goal here is to pick up default arguments added since the friend was |
| 1860 | // declared; we know the template parameter lists match, since otherwise |
| 1861 | // we would not have picked this template as the previous declaration. |
| 1862 | if (TemplateParams && FunctionTemplate->getPreviousDecl()) { |
| 1863 | SemaRef.CheckTemplateParameterList( |
| 1864 | TemplateParams, |
| 1865 | FunctionTemplate->getPreviousDecl()->getTemplateParameters(), |
| 1866 | Function->isThisDeclarationADefinition() |
| 1867 | ? Sema::TPC_FriendFunctionTemplateDefinition |
| 1868 | : Sema::TPC_FriendFunctionTemplate); |
| 1869 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1872 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1873 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1874 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1875 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1876 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1877 | PrincipalDecl->setNonMemberOperator(); |
| 1878 | |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1879 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1880 | return Function; |
| 1881 | } |
| 1882 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1883 | Decl * |
| 1884 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1885 | TemplateParameterList *TemplateParams, |
| 1886 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1887 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1888 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | // We are creating a function template specialization from a function |
| 1890 | // template. Check whether there is already a function template |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1891 | // specialization for this particular set of template arguments. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1892 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1894 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1895 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1896 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1898 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1899 | if (SpecFunc) |
| 1900 | return SpecFunc; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1903 | bool isFriend; |
| 1904 | if (FunctionTemplate) |
| 1905 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1906 | else |
| 1907 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1908 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1909 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1910 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1911 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1912 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1913 | |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1914 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1915 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1916 | unsigned NumTempParamLists = 0; |
| 1917 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
Benjamin Kramer | 9dc549b | 2015-08-04 14:46:06 +0000 | [diff] [blame] | 1918 | TempParamLists.resize(NumTempParamLists); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1919 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1920 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1921 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1922 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1923 | return nullptr; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1924 | TempParamLists[I] = InstParams; |
| 1925 | } |
| 1926 | } |
| 1927 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1928 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1929 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1930 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1931 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1932 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1934 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1935 | if (QualifierLoc) { |
| 1936 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1937 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1938 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1939 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | DeclContext *DC = Owner; |
| 1943 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1944 | if (QualifierLoc) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1945 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1946 | SS.Adopt(QualifierLoc); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1947 | DC = SemaRef.computeDeclContext(SS); |
John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1948 | |
| 1949 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1950 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1951 | } else { |
| 1952 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1953 | D->getDeclContext(), |
| 1954 | TemplateArgs); |
| 1955 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1956 | if (!DC) return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1959 | // Build the instantiated method declaration. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1960 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1961 | CXXMethodDecl *Method = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1963 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1964 | DeclarationNameInfo NameInfo |
| 1965 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1966 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1968 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | Constructor->isExplicit(), |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 1970 | Constructor->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1971 | false, Constructor->isConstexpr()); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1972 | Method->setRangeEnd(Constructor->getEndLoc()); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1973 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1974 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1975 | StartLoc, NameInfo, T, TInfo, |
Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 1976 | Destructor->isInlineSpecified(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1977 | false); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1978 | Method->setRangeEnd(Destructor->getEndLoc()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1979 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1980 | Method = CXXConversionDecl::Create( |
| 1981 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 1982 | Conversion->isInlineSpecified(), Conversion->isExplicit(), |
| 1983 | Conversion->isConstexpr(), Conversion->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1984 | } else { |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1985 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1986 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, StartLoc, NameInfo, |
| 1987 | T, TInfo, SC, D->isInlineSpecified(), |
| 1988 | D->isConstexpr(), D->getEndLoc()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1989 | } |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1990 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1991 | if (D->isInlined()) |
| 1992 | Method->setImplicitlyInline(); |
| 1993 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1994 | if (QualifierLoc) |
| 1995 | Method->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1996 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1997 | if (TemplateParams) { |
| 1998 | // Our resulting instantiation is actually a function template, since we |
| 1999 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | // |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2001 | // template<typename T> |
| 2002 | // struct X { |
| 2003 | // template<typename U> void f(T, U); |
| 2004 | // }; |
| 2005 | // |
| 2006 | // X<int> x; |
| 2007 | // |
| 2008 | // We are instantiating the member template "f" within X<int>, which means |
| 2009 | // substituting int for T, but leaving "f" as a member function template. |
| 2010 | // Build the function template itself. |
| 2011 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 2012 | Method->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | Method->getDeclName(), |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2014 | TemplateParams, Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2015 | if (isFriend) { |
| 2016 | FunctionTemplate->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2017 | FunctionTemplate->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2018 | } else if (D->isOutOfLine()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2020 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2021 | } else if (FunctionTemplate) { |
| 2022 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 2023 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2024 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2025 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2026 | Innermost), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2027 | /*InsertPos=*/nullptr); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2028 | } else if (!isFriend) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2029 | // Record that this is an instantiation of a member function. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2030 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2031 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2032 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | // If we are instantiating a member function defined |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2034 | // out-of-line, the instantiation will have the same lexical |
| 2035 | // context (which will be a namespace scope) as the template. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2036 | if (isFriend) { |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2037 | if (NumTempParamLists) |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 2038 | Method->setTemplateParameterListsInfo( |
| 2039 | SemaRef.Context, |
| 2040 | llvm::makeArrayRef(TempParamLists.data(), NumTempParamLists)); |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2041 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2042 | Method->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2043 | Method->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2044 | } else if (D->isOutOfLine()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2045 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2047 | // Attach the parameters |
| 2048 | for (unsigned P = 0; P < Params.size(); ++P) |
| 2049 | Params[P]->setOwningFunction(Method); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2050 | Method->setParams(Params); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2051 | |
| 2052 | if (InitMethodInstantiation(Method, D)) |
| 2053 | Method->setInvalidDecl(); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2054 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2055 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2056 | Sema::ForExternalRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2058 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 2059 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2061 | // In C++, the previous declaration we find might be a tag type |
| 2062 | // (class or enum). In this case, the new declaration will hide the |
| 2063 | // tag type. Note that this does does not apply if we're declaring a |
| 2064 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2065 | if (Previous.isSingleTagDecl()) |
| 2066 | Previous.clear(); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2067 | } |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2068 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2069 | if (!IsClassScopeSpecialization) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2070 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, false); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 2072 | if (D->isPure()) |
| 2073 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 2074 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2075 | // Propagate access. For a non-friend declaration, the access is |
| 2076 | // whatever we're propagating from. For a friend, it should be the |
| 2077 | // previous declaration we just found. |
| 2078 | if (isFriend && Method->getPreviousDecl()) |
| 2079 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2080 | else |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2081 | Method->setAccess(D->getAccess()); |
| 2082 | if (FunctionTemplate) |
| 2083 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 2084 | |
Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 2085 | SemaRef.CheckOverrideControl(Method); |
| 2086 | |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2087 | // 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] | 2088 | if (D->isExplicitlyDefaulted()) |
| 2089 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2090 | if (D->isDeletedAsWritten()) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 2091 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2092 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2093 | // If there's a function template, let our caller handle it. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2094 | if (FunctionTemplate) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2095 | // do nothing |
| 2096 | |
| 2097 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2098 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2099 | // do nothing |
| 2100 | |
| 2101 | // Otherwise, check access to friends and make them visible. |
| 2102 | } else if (isFriend) { |
| 2103 | // We only need to re-check access for methods which we didn't |
| 2104 | // manage to match during parsing. |
| 2105 | if (!D->getPreviousDecl()) |
| 2106 | SemaRef.CheckFriendAccess(Method); |
| 2107 | |
| 2108 | Record->makeDeclVisibleInContext(Method); |
| 2109 | |
| 2110 | // Otherwise, add the declaration. We don't need to do this for |
| 2111 | // class-scope specializations because we'll have matched them with |
| 2112 | // the appropriate template. |
| 2113 | } else if (!IsClassScopeSpecialization) { |
| 2114 | Owner->addDecl(Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2115 | } |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2117 | return Method; |
| 2118 | } |
| 2119 | |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2120 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2121 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2124 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2125 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2128 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2129 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2132 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2133 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 2134 | /*ExpectParameterPack=*/ false); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2137 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 2138 | TemplateTypeParmDecl *D) { |
| 2139 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2140 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2142 | TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2143 | SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2144 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), |
| 2145 | D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack()); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2146 | Inst->setAccess(AS_public); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2147 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2148 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2149 | TypeSourceInfo *InstantiatedDefaultArg = |
| 2150 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 2151 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 2152 | if (InstantiatedDefaultArg) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2153 | Inst->setDefaultArgument(InstantiatedDefaultArg); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2154 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2155 | |
| 2156 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2157 | // scope. |
| 2158 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2159 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2160 | return Inst; |
| 2161 | } |
| 2162 | |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2163 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 2164 | NonTypeTemplateParmDecl *D) { |
| 2165 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2166 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2167 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 2168 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2169 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2170 | TypeSourceInfo *DI; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2171 | QualType T; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2172 | bool Invalid = false; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2173 | |
| 2174 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2175 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2176 | // expansion of types. Substitute into each of the expanded types. |
| 2177 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 2178 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 2179 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2180 | TypeSourceInfo *NewDI = |
| 2181 | SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, |
| 2182 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2183 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2184 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2185 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2186 | QualType NewT = |
| 2187 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2188 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2189 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2190 | |
| 2191 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2192 | ExpandedParameterPackTypes.push_back(NewT); |
| 2193 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2195 | IsExpandedParameterPack = true; |
| 2196 | DI = D->getTypeSourceInfo(); |
| 2197 | T = DI->getType(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2198 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2199 | // The non-type template parameter pack's type is a pack expansion of types. |
| 2200 | // Determine whether we need to expand this parameter pack into separate |
| 2201 | // types. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2202 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2203 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2204 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2205 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2206 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2207 | // Determine whether the set of unexpanded parameter packs can and should |
| 2208 | // be expanded. |
| 2209 | bool Expand = true; |
| 2210 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2211 | Optional<unsigned> OrigNumExpansions |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2212 | = Expansion.getTypePtr()->getNumExpansions(); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2213 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2214 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 2215 | Pattern.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 2216 | Unexpanded, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2217 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2218 | Expand, RetainExpansion, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2219 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2220 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2221 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2222 | if (Expand) { |
| 2223 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2224 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2225 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2226 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2227 | D->getDeclName()); |
| 2228 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2229 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2230 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2231 | QualType NewT = |
| 2232 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2233 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2234 | return nullptr; |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2235 | |
| 2236 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2237 | ExpandedParameterPackTypes.push_back(NewT); |
| 2238 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2239 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2240 | // Note that we have an expanded parameter pack. The "type" of this |
| 2241 | // expanded parameter pack is the original expansion type, but callers |
| 2242 | // will end up using the expanded parameter pack types for type-checking. |
| 2243 | IsExpandedParameterPack = true; |
| 2244 | DI = D->getTypeSourceInfo(); |
| 2245 | T = DI->getType(); |
| 2246 | } else { |
| 2247 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2248 | // pattern and create a new pack expansion type. |
| 2249 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2250 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2251 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2252 | D->getDeclName()); |
| 2253 | if (!NewPattern) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2254 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2255 | |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2256 | SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2257 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 2258 | NumExpansions); |
| 2259 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2260 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2261 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2262 | T = DI->getType(); |
| 2263 | } |
| 2264 | } else { |
| 2265 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2266 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2267 | D->getLocation(), D->getDeclName()); |
| 2268 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2269 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2271 | // Check that this type is acceptable for a non-type template parameter. |
Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2272 | T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2273 | if (T.isNull()) { |
| 2274 | T = SemaRef.Context.IntTy; |
| 2275 | Invalid = true; |
| 2276 | } |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2277 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2279 | NonTypeTemplateParmDecl *Param; |
| 2280 | if (IsExpandedParameterPack) |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2281 | Param = NonTypeTemplateParmDecl::Create( |
| 2282 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2283 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2284 | D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2285 | ExpandedParameterPackTypesAsWritten); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2286 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2287 | Param = NonTypeTemplateParmDecl::Create( |
| 2288 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2289 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2290 | D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2292 | Param->setAccess(AS_public); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2293 | if (Invalid) |
| 2294 | Param->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2295 | |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2296 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2297 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 2298 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2299 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 2300 | if (!Value.isInvalid()) |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2301 | Param->setDefaultArgument(Value.get()); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2302 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2303 | |
| 2304 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2305 | // scope. |
| 2306 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2307 | return Param; |
| 2308 | } |
| 2309 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2310 | static void collectUnexpandedParameterPacks( |
| 2311 | Sema &S, |
| 2312 | TemplateParameterList *Params, |
| 2313 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2314 | for (const auto &P : *Params) { |
| 2315 | if (P->isTemplateParameterPack()) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2316 | continue; |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2317 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2318 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 2319 | Unexpanded); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2320 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2321 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 2322 | Unexpanded); |
| 2323 | } |
| 2324 | } |
| 2325 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2326 | Decl * |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2327 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 2328 | TemplateTemplateParmDecl *D) { |
| 2329 | // Instantiate the template parameter list of the template template parameter. |
| 2330 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 2331 | TemplateParameterList *InstParams; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2332 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 2333 | |
| 2334 | bool IsExpandedParameterPack = false; |
| 2335 | |
| 2336 | if (D->isExpandedParameterPack()) { |
| 2337 | // The template template parameter pack is an already-expanded pack |
| 2338 | // expansion of template parameters. Substitute into each of the expanded |
| 2339 | // parameters. |
| 2340 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 2341 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 2342 | I != N; ++I) { |
| 2343 | LocalInstantiationScope Scope(SemaRef); |
| 2344 | TemplateParameterList *Expansion = |
| 2345 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2346 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2347 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2348 | ExpandedParams.push_back(Expansion); |
| 2349 | } |
| 2350 | |
| 2351 | IsExpandedParameterPack = true; |
| 2352 | InstParams = TempParams; |
| 2353 | } else if (D->isPackExpansion()) { |
| 2354 | // The template template parameter pack expands to a pack of template |
| 2355 | // template parameters. Determine whether we need to expand this parameter |
| 2356 | // pack into separate parameters. |
| 2357 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2358 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2359 | Unexpanded); |
| 2360 | |
| 2361 | // Determine whether the set of unexpanded parameter packs can and should |
| 2362 | // be expanded. |
| 2363 | bool Expand = true; |
| 2364 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2365 | Optional<unsigned> NumExpansions; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2366 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2367 | TempParams->getSourceRange(), |
| 2368 | Unexpanded, |
| 2369 | TemplateArgs, |
| 2370 | Expand, RetainExpansion, |
| 2371 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2372 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2373 | |
| 2374 | if (Expand) { |
| 2375 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2376 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2377 | LocalInstantiationScope Scope(SemaRef); |
| 2378 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2379 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2380 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2381 | ExpandedParams.push_back(Expansion); |
| 2382 | } |
| 2383 | |
| 2384 | // Note that we have an expanded parameter pack. The "type" of this |
| 2385 | // expanded parameter pack is the original expansion type, but callers |
| 2386 | // will end up using the expanded parameter pack types for type-checking. |
| 2387 | IsExpandedParameterPack = true; |
| 2388 | InstParams = TempParams; |
| 2389 | } else { |
| 2390 | // We cannot fully expand the pack expansion now, so just substitute |
| 2391 | // into the pattern. |
| 2392 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2393 | |
| 2394 | LocalInstantiationScope Scope(SemaRef); |
| 2395 | InstParams = SubstTemplateParams(TempParams); |
| 2396 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2397 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2398 | } |
| 2399 | } else { |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2400 | // Perform the actual substitution of template parameters within a new, |
| 2401 | // local instantiation scope. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2402 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2403 | InstParams = SubstTemplateParams(TempParams); |
| 2404 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2405 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2408 | // Build the template template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2409 | TemplateTemplateParmDecl *Param; |
| 2410 | if (IsExpandedParameterPack) |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2411 | Param = TemplateTemplateParmDecl::Create( |
| 2412 | SemaRef.Context, Owner, D->getLocation(), |
| 2413 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2414 | D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2415 | else |
Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2416 | Param = TemplateTemplateParmDecl::Create( |
| 2417 | SemaRef.Context, Owner, D->getLocation(), |
| 2418 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2419 | D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); |
Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2420 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2421 | NestedNameSpecifierLoc QualifierLoc = |
| 2422 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2423 | QualifierLoc = |
| 2424 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2425 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2426 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2427 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2428 | if (!TName.isNull()) |
| 2429 | Param->setDefaultArgument( |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2430 | SemaRef.Context, |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2431 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2432 | D->getDefaultArgument().getTemplateQualifierLoc(), |
Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2433 | D->getDefaultArgument().getTemplateNameLoc())); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2434 | } |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2435 | Param->setAccess(AS_public); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2436 | |
| 2437 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2438 | // scope. |
| 2439 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2440 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2441 | return Param; |
| 2442 | } |
| 2443 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2444 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2445 | // Using directives are never dependent (and never contain any types or |
| 2446 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2447 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2448 | UsingDirectiveDecl *Inst |
| 2449 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2450 | D->getNamespaceKeyLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2451 | D->getQualifierLoc(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2452 | D->getIdentLocation(), |
| 2453 | D->getNominatedNamespace(), |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2454 | D->getCommonAncestor()); |
Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Add the using directive to its declaration context |
| 2457 | // only if this is not a function or method. |
| 2458 | if (!Owner->isFunctionOrMethod()) |
| 2459 | Owner->addDecl(Inst); |
| 2460 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2461 | return Inst; |
| 2462 | } |
| 2463 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2464 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2465 | |
| 2466 | // The nested name specifier may be dependent, for example |
| 2467 | // template <typename T> struct t { |
| 2468 | // struct s1 { T f1(); }; |
| 2469 | // struct s2 : s1 { using s1::f1; }; |
| 2470 | // }; |
| 2471 | // template struct t<int>; |
| 2472 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2473 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2474 | NestedNameSpecifierLoc QualifierLoc |
| 2475 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2476 | TemplateArgs); |
| 2477 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2478 | return nullptr; |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2479 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2480 | // For an inheriting constructor declaration, the name of the using |
| 2481 | // declaration is the name of a constructor in this class, not in the |
| 2482 | // base class. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2483 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2484 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 2485 | if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) |
| 2486 | NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 2487 | SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2488 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2489 | // We only need to do redeclaration lookups if we're in a class |
| 2490 | // scope (in fact, it's not really even possible in non-class |
| 2491 | // scopes). |
| 2492 | bool CheckRedeclaration = Owner->isRecord(); |
| 2493 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2494 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2495 | Sema::ForVisibleRedeclaration); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2496 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2497 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2498 | D->getUsingLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2499 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2500 | NameInfo, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2501 | D->hasTypename()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2502 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2503 | CXXScopeSpec SS; |
| 2504 | SS.Adopt(QualifierLoc); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2505 | if (CheckRedeclaration) { |
| 2506 | Prev.setHideTags(false); |
| 2507 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2508 | |
| 2509 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2510 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2511 | D->hasTypename(), SS, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2512 | D->getLocation(), Prev)) |
| 2513 | NewUD->setInvalidDecl(); |
| 2514 | |
| 2515 | } |
| 2516 | |
| 2517 | if (!NewUD->isInvalidDecl() && |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 2518 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), |
| 2519 | SS, NameInfo, D->getLocation())) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2520 | NewUD->setInvalidDecl(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2521 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2522 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2523 | NewUD->setAccess(D->getAccess()); |
| 2524 | Owner->addDecl(NewUD); |
| 2525 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2526 | // Don't process the shadow decls for an invalid decl. |
| 2527 | if (NewUD->isInvalidDecl()) |
| 2528 | return NewUD; |
| 2529 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2530 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2531 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2532 | |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2533 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2534 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2535 | // Process the shadow decls. |
Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2536 | for (auto *Shadow : D->shadows()) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2537 | // FIXME: UsingShadowDecl doesn't preserve its immediate target, so |
| 2538 | // reconstruct it in the case where it matters. |
| 2539 | NamedDecl *OldTarget = Shadow->getTargetDecl(); |
| 2540 | if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) |
| 2541 | if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) |
| 2542 | OldTarget = BaseShadow; |
| 2543 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2544 | NamedDecl *InstTarget = |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2545 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2546 | Shadow->getLocation(), OldTarget, TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2547 | if (!InstTarget) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2548 | return nullptr; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2549 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2550 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2551 | if (CheckRedeclaration) { |
| 2552 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2553 | continue; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 2554 | } else if (UsingShadowDecl *OldPrev = |
| 2555 | getPreviousDeclForInstantiation(Shadow)) { |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2556 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2557 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2558 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2559 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2560 | UsingShadowDecl *InstShadow = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2561 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2562 | PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2563 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2564 | |
| 2565 | if (isFunctionScope) |
| 2566 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2567 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2568 | |
| 2569 | return NewUD; |
| 2570 | } |
| 2571 | |
| 2572 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2573 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2574 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2577 | Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( |
| 2578 | ConstructorUsingShadowDecl *D) { |
| 2579 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 2580 | return nullptr; |
| 2581 | } |
| 2582 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2583 | template <typename T> |
| 2584 | Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( |
| 2585 | T *D, bool InstantiatingPackElement) { |
| 2586 | // If this is a pack expansion, expand it now. |
| 2587 | if (D->isPackExpansion() && !InstantiatingPackElement) { |
| 2588 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2589 | SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); |
| 2590 | SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); |
| 2591 | |
| 2592 | // Determine whether the set of unexpanded parameter packs can and should |
| 2593 | // be expanded. |
| 2594 | bool Expand = true; |
| 2595 | bool RetainExpansion = false; |
| 2596 | Optional<unsigned> NumExpansions; |
| 2597 | if (SemaRef.CheckParameterPacksForExpansion( |
| 2598 | D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, |
| 2599 | Expand, RetainExpansion, NumExpansions)) |
| 2600 | return nullptr; |
| 2601 | |
| 2602 | // This declaration cannot appear within a function template signature, |
| 2603 | // so we can't have a partial argument list for a parameter pack. |
| 2604 | assert(!RetainExpansion && |
| 2605 | "should never need to retain an expansion for UsingPackDecl"); |
| 2606 | |
| 2607 | if (!Expand) { |
| 2608 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2609 | // pattern and create a new pack expansion. |
| 2610 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2611 | return instantiateUnresolvedUsingDecl(D, true); |
| 2612 | } |
| 2613 | |
| 2614 | // Within a function, we don't have any normal way to check for conflicts |
| 2615 | // between shadow declarations from different using declarations in the |
| 2616 | // same pack expansion, but this is always ill-formed because all expansions |
| 2617 | // must produce (conflicting) enumerators. |
| 2618 | // |
| 2619 | // Sadly we can't just reject this in the template definition because it |
| 2620 | // could be valid if the pack is empty or has exactly one expansion. |
| 2621 | if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { |
| 2622 | SemaRef.Diag(D->getEllipsisLoc(), |
| 2623 | diag::err_using_decl_redeclaration_expansion); |
| 2624 | return nullptr; |
| 2625 | } |
| 2626 | |
| 2627 | // Instantiate the slices of this pack and build a UsingPackDecl. |
| 2628 | SmallVector<NamedDecl*, 8> Expansions; |
| 2629 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2630 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2631 | Decl *Slice = instantiateUnresolvedUsingDecl(D, true); |
| 2632 | if (!Slice) |
| 2633 | return nullptr; |
| 2634 | // Note that we can still get unresolved using declarations here, if we |
| 2635 | // had arguments for all packs but the pattern also contained other |
| 2636 | // template arguments (this only happens during partial substitution, eg |
| 2637 | // into the body of a generic lambda in a function template). |
| 2638 | Expansions.push_back(cast<NamedDecl>(Slice)); |
| 2639 | } |
| 2640 | |
| 2641 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2642 | if (isDeclWithinFunction(D)) |
| 2643 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2644 | return NewD; |
| 2645 | } |
| 2646 | |
| 2647 | UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); |
| 2648 | SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); |
| 2649 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2650 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2651 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2652 | TemplateArgs); |
| 2653 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2654 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2655 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2656 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2657 | SS.Adopt(QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2659 | DeclarationNameInfo NameInfo |
| 2660 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2661 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2662 | // Produce a pack expansion only if we're not instantiating a particular |
| 2663 | // slice of a pack expansion. |
| 2664 | bool InstantiatingSlice = D->getEllipsisLoc().isValid() && |
| 2665 | SemaRef.ArgumentPackSubstitutionIndex != -1; |
| 2666 | SourceLocation EllipsisLoc = |
| 2667 | InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); |
| 2668 | |
| 2669 | NamedDecl *UD = SemaRef.BuildUsingDeclaration( |
| 2670 | /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2671 | /*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, |
| 2672 | ParsedAttributesView(), |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2673 | /*IsInstantiation*/ true); |
Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2674 | if (UD) |
| 2675 | SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); |
| 2676 | |
| 2677 | return UD; |
Richard Smith | 22a250c | 2016-12-19 04:08:53 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2680 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( |
| 2681 | UnresolvedUsingTypenameDecl *D) { |
| 2682 | return instantiateUnresolvedUsingDecl(D); |
| 2683 | } |
| 2684 | |
| 2685 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( |
| 2686 | UnresolvedUsingValueDecl *D) { |
| 2687 | return instantiateUnresolvedUsingDecl(D); |
| 2688 | } |
| 2689 | |
| 2690 | Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { |
| 2691 | SmallVector<NamedDecl*, 8> Expansions; |
| 2692 | for (auto *UD : D->expansions()) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2693 | if (NamedDecl *NewUD = |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2694 | SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2695 | Expansions.push_back(NewUD); |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2696 | else |
| 2697 | return nullptr; |
| 2698 | } |
| 2699 | |
| 2700 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2701 | if (isDeclWithinFunction(D)) |
| 2702 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2703 | return NewD; |
| 2704 | } |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2705 | |
| 2706 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2707 | ClassScopeFunctionSpecializationDecl *Decl) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2708 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nick Lewycky | 0b72773 | 2015-01-02 01:33:12 +0000 | [diff] [blame] | 2709 | CXXMethodDecl *NewFD = |
| 2710 | cast_or_null<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, nullptr, true)); |
| 2711 | if (!NewFD) |
| 2712 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2713 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2714 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 2715 | TemplateArgumentListInfo *ExplicitTemplateArgsPtr = nullptr; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2716 | if (Decl->hasExplicitTemplateArgs()) { |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2717 | if (SemaRef.Subst(Decl->templateArgs().getArgumentArray(), |
| 2718 | Decl->templateArgs().size(), ExplicitTemplateArgs, |
| 2719 | TemplateArgs)) |
| 2720 | return nullptr; |
| 2721 | ExplicitTemplateArgsPtr = &ExplicitTemplateArgs; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2724 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2725 | Sema::ForExternalRedeclaration); |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2726 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2727 | if (SemaRef.CheckFunctionTemplateSpecialization( |
| 2728 | NewFD, ExplicitTemplateArgsPtr, Previous)) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2729 | NewFD->setInvalidDecl(); |
| 2730 | return NewFD; |
| 2731 | } |
| 2732 | |
| 2733 | // Associate the specialization with the pattern. |
| 2734 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2735 | assert(Specialization && "Class scope Specialization is null"); |
| 2736 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2737 | |
Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 2738 | // FIXME: If this is a definition, check for redefinition errors! |
| 2739 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2740 | return NewFD; |
| 2741 | } |
| 2742 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2743 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2744 | OMPThreadPrivateDecl *D) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2745 | SmallVector<Expr *, 5> Vars; |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2746 | for (auto *I : D->varlists()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2747 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2748 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2749 | Vars.push_back(Var); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | OMPThreadPrivateDecl *TD = |
| 2753 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2754 | |
Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2755 | TD->setAccess(AS_public); |
| 2756 | Owner->addDecl(TD); |
| 2757 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2758 | return TD; |
| 2759 | } |
| 2760 | |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2761 | Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 2762 | llvm_unreachable( |
| 2763 | "Requires directive cannot be instantiated within a dependent context"); |
| 2764 | } |
| 2765 | |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2766 | Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( |
| 2767 | OMPDeclareReductionDecl *D) { |
| 2768 | // Instantiate type and check if it is allowed. |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2769 | const bool RequiresInstantiation = |
| 2770 | D->getType()->isDependentType() || |
| 2771 | D->getType()->isInstantiationDependentType() || |
| 2772 | D->getType()->containsUnexpandedParameterPack(); |
| 2773 | QualType SubstReductionType; |
| 2774 | if (RequiresInstantiation) { |
| 2775 | SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( |
| 2776 | D->getLocation(), |
| 2777 | ParsedType::make(SemaRef.SubstType( |
| 2778 | D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); |
| 2779 | } else { |
| 2780 | SubstReductionType = D->getType(); |
| 2781 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2782 | if (SubstReductionType.isNull()) |
| 2783 | return nullptr; |
| 2784 | bool IsCorrect = !SubstReductionType.isNull(); |
| 2785 | // Create instantiated copy. |
| 2786 | std::pair<QualType, SourceLocation> ReductionTypes[] = { |
| 2787 | std::make_pair(SubstReductionType, D->getLocation())}; |
| 2788 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 2789 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 2790 | PrevDeclInScope = cast<OMPDeclareReductionDecl>( |
| 2791 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 2792 | ->get<Decl *>()); |
| 2793 | } |
| 2794 | auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( |
| 2795 | /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), |
| 2796 | PrevDeclInScope); |
| 2797 | auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2798 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); |
| 2799 | if (!RequiresInstantiation) { |
| 2800 | if (Expr *Combiner = D->getCombiner()) { |
| 2801 | NewDRD->setCombinerData(D->getCombinerIn(), D->getCombinerOut()); |
| 2802 | NewDRD->setCombiner(Combiner); |
| 2803 | if (Expr *Init = D->getInitializer()) { |
| 2804 | NewDRD->setInitializerData(D->getInitOrig(), D->getInitPriv()); |
| 2805 | NewDRD->setInitializer(Init, D->getInitializerKind()); |
| 2806 | } |
| 2807 | } |
| 2808 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( |
| 2809 | /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); |
| 2810 | return NewDRD; |
| 2811 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2812 | Expr *SubstCombiner = nullptr; |
| 2813 | Expr *SubstInitializer = nullptr; |
| 2814 | // Combiners instantiation sequence. |
| 2815 | if (D->getCombiner()) { |
| 2816 | SemaRef.ActOnOpenMPDeclareReductionCombinerStart( |
| 2817 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2818 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2819 | cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), |
| 2820 | cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); |
| 2821 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2822 | cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), |
| 2823 | cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); |
| 2824 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 2825 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2826 | ThisContext); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2827 | SubstCombiner = SemaRef.SubstExpr(D->getCombiner(), TemplateArgs).get(); |
| 2828 | SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); |
| 2829 | // Initializers instantiation sequence. |
| 2830 | if (D->getInitializer()) { |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2831 | VarDecl *OmpPrivParm = |
| 2832 | SemaRef.ActOnOpenMPDeclareReductionInitializerStart( |
| 2833 | /*S=*/nullptr, NewDRD); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2834 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2835 | cast<DeclRefExpr>(D->getInitOrig())->getDecl(), |
| 2836 | cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); |
| 2837 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 2838 | cast<DeclRefExpr>(D->getInitPriv())->getDecl(), |
| 2839 | cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2840 | if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { |
| 2841 | SubstInitializer = |
| 2842 | SemaRef.SubstExpr(D->getInitializer(), TemplateArgs).get(); |
| 2843 | } else { |
| 2844 | IsCorrect = IsCorrect && OmpPrivParm->hasInit(); |
| 2845 | } |
| 2846 | SemaRef.ActOnOpenMPDeclareReductionInitializerEnd( |
| 2847 | NewDRD, SubstInitializer, OmpPrivParm); |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2848 | } |
Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 2849 | IsCorrect = |
| 2850 | IsCorrect && SubstCombiner && |
| 2851 | (!D->getInitializer() || |
| 2852 | (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && |
| 2853 | SubstInitializer) || |
| 2854 | (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && |
| 2855 | !SubstInitializer && !SubstInitializer)); |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2856 | } else { |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2857 | IsCorrect = false; |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2858 | } |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2859 | |
| 2860 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd(/*S=*/nullptr, DRD, |
| 2861 | IsCorrect); |
| 2862 | |
| 2863 | return NewDRD; |
| 2864 | } |
| 2865 | |
Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 2866 | Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( |
| 2867 | OMPCapturedExprDecl * /*D*/) { |
Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 2868 | llvm_unreachable("Should not be met in templates"); |
| 2869 | } |
| 2870 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2871 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2872 | return VisitFunctionDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2875 | Decl * |
| 2876 | TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
Richard Smith | 2600c63 | 2018-05-30 20:24:10 +0000 | [diff] [blame] | 2877 | Decl *Inst = VisitFunctionDecl(D, nullptr); |
| 2878 | if (Inst && !D->getDescribedFunctionTemplate()) |
| 2879 | Owner->addDecl(Inst); |
| 2880 | return Inst; |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2883 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2884 | return VisitCXXMethodDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 2888 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 2889 | } |
| 2890 | |
| 2891 | Decl * |
| 2892 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 2893 | ClassTemplateSpecializationDecl *D) { |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2894 | // As a MS extension, we permit class-scope explicit specialization |
| 2895 | // of member class templates. |
| 2896 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 2897 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 2898 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 2899 | "can only instantiate an explicit specialization " |
| 2900 | "for a member class template"); |
| 2901 | |
| 2902 | // Lookup the already-instantiated declaration in the instantiation |
| 2903 | // of the class template. FIXME: Diagnose or assert if this fails? |
| 2904 | DeclContext::lookup_result Found |
| 2905 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 2906 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2907 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2908 | ClassTemplateDecl *InstClassTemplate |
| 2909 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 2910 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2911 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2912 | |
| 2913 | // Substitute into the template arguments of the class template explicit |
| 2914 | // specialization. |
| 2915 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 2916 | castAs<TemplateSpecializationTypeLoc>(); |
| 2917 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 2918 | Loc.getRAngleLoc()); |
| 2919 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 2920 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 2921 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 2922 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 2923 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2924 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2925 | |
| 2926 | // Check that the template argument list is well-formed for this |
| 2927 | // class template. |
| 2928 | SmallVector<TemplateArgument, 4> Converted; |
| 2929 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 2930 | D->getLocation(), |
| 2931 | InstTemplateArgs, |
| 2932 | false, |
| 2933 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2934 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2935 | |
| 2936 | // Figure out where to insert this class template explicit specialization |
| 2937 | // in the member template's set of class template explicit specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2938 | void *InsertPos = nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2939 | ClassTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2940 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2941 | |
| 2942 | // Check whether we've already seen a conflicting instantiation of this |
| 2943 | // declaration (for instance, if there was a prior implicit instantiation). |
| 2944 | bool Ignored; |
| 2945 | if (PrevDecl && |
| 2946 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 2947 | D->getSpecializationKind(), |
| 2948 | PrevDecl, |
| 2949 | PrevDecl->getSpecializationKind(), |
| 2950 | PrevDecl->getPointOfInstantiation(), |
| 2951 | Ignored)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2952 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2953 | |
| 2954 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 2955 | // This happens in cases like: |
| 2956 | // |
| 2957 | // template<typename T, typename U> |
| 2958 | // struct Outer { |
| 2959 | // template<typename X> struct Inner; |
| 2960 | // template<> struct Inner<T> {}; |
| 2961 | // template<> struct Inner<U> {}; |
| 2962 | // }; |
| 2963 | // |
| 2964 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 2965 | // // have the same signature. |
| 2966 | if (PrevDecl && PrevDecl->getDefinition() && |
| 2967 | D->isThisDeclarationADefinition()) { |
| 2968 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 2969 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 2970 | diag::note_previous_definition); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2971 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2975 | ClassTemplateSpecializationDecl *InstD = |
| 2976 | ClassTemplateSpecializationDecl::Create( |
| 2977 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 2978 | D->getLocation(), InstClassTemplate, Converted, PrevDecl); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Add this partial specialization to the set of class template partial |
| 2981 | // specializations. |
| 2982 | if (!PrevDecl) |
| 2983 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 2984 | |
| 2985 | // Substitute the nested name specifier, if any. |
| 2986 | if (SubstQualifier(D, InstD)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2987 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2988 | |
| 2989 | // Build the canonical type that describes the converted template |
| 2990 | // arguments of the class template explicit specialization. |
| 2991 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 2992 | TemplateName(InstClassTemplate), Converted, |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2993 | SemaRef.Context.getRecordType(InstD)); |
| 2994 | |
| 2995 | // Build the fully-sugared type for this class template |
| 2996 | // specialization as the user wrote in the specialization |
| 2997 | // itself. This means that we'll pretty-print the type retrieved |
| 2998 | // from the specialization's declaration the way that the user |
| 2999 | // actually wrote the specialization, rather than formatting the |
| 3000 | // name based on the "canonical" representation used to store the |
| 3001 | // template arguments in the specialization. |
| 3002 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3003 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 3004 | CanonType); |
| 3005 | |
| 3006 | InstD->setAccess(D->getAccess()); |
| 3007 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 3008 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 3009 | InstD->setTypeAsWritten(WrittenTy); |
| 3010 | InstD->setExternLoc(D->getExternLoc()); |
| 3011 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 3012 | |
| 3013 | Owner->addDecl(InstD); |
| 3014 | |
| 3015 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 3016 | // We don't have support for lazy instantiation of an explicit specialization |
| 3017 | // yet, and MSVC eagerly instantiates in this case. |
| 3018 | if (D->isThisDeclarationADefinition() && |
| 3019 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 3020 | TSK_ImplicitInstantiation, |
| 3021 | /*Complain=*/true)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3022 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3023 | |
| 3024 | return InstD; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3027 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3028 | VarTemplateSpecializationDecl *D) { |
| 3029 | |
| 3030 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 3031 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 3032 | assert(VarTemplate && |
| 3033 | "A template specialization without specialized template?"); |
| 3034 | |
| 3035 | // Substitute the current template arguments. |
| 3036 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 3037 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 3038 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 3039 | |
| 3040 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 3041 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3042 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Check that the template argument list is well-formed for this template. |
| 3045 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3046 | if (SemaRef.CheckTemplateArgumentList( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3047 | VarTemplate, VarTemplate->getBeginLoc(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3048 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 3049 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3050 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3051 | |
| 3052 | // Find the variable template specialization declaration that |
| 3053 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3054 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3055 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3056 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3057 | // If we already have a variable template specialization, return it. |
| 3058 | return VarSpec; |
| 3059 | |
| 3060 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, |
| 3061 | VarTemplateArgsInfo, Converted); |
| 3062 | } |
| 3063 | |
| 3064 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3065 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 3066 | const TemplateArgumentListInfo &TemplateArgsInfo, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 3067 | ArrayRef<TemplateArgument> Converted) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3068 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3069 | // Do substitution on the type of the declaration |
| 3070 | TypeSourceInfo *DI = |
| 3071 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 3072 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 3073 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3074 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3075 | |
| 3076 | if (DI->getType()->isFunctionType()) { |
| 3077 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 3078 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3079 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | // Build the instantiated declaration |
| 3083 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 3084 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3085 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3086 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3087 | if (InsertPos) |
| 3088 | VarTemplate->AddSpecialization(Var, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3089 | |
| 3090 | // Substitute the nested name specifier, if any. |
| 3091 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3092 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3093 | |
| 3094 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3095 | Owner, StartingScope); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3096 | |
| 3097 | return Var; |
| 3098 | } |
| 3099 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3100 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 3101 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 3102 | } |
| 3103 | |
| 3104 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 3105 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 3106 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 3107 | DiagnosticsEngine::Error, |
| 3108 | "cannot instantiate %0 yet"); |
| 3109 | SemaRef.Diag(D->getLocation(), DiagID) |
| 3110 | << D->getDeclKindName(); |
| 3111 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3112 | return nullptr; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 3116 | llvm_unreachable("Unexpected decl"); |
| 3117 | } |
| 3118 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3119 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3120 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 3121 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3122 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3123 | return nullptr; |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3124 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 3125 | return Instantiator.Visit(D); |
| 3126 | } |
| 3127 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3128 | /// Instantiates a nested template parameter list in the current |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3129 | /// instantiation context. |
| 3130 | /// |
| 3131 | /// \param L The parameter list to instantiate |
| 3132 | /// |
| 3133 | /// \returns NULL if there was an error |
| 3134 | TemplateParameterList * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3135 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3136 | // Get errors for all the parameters before bailing out. |
| 3137 | bool Invalid = false; |
| 3138 | |
| 3139 | unsigned N = L->size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3140 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3141 | ParamVector Params; |
| 3142 | Params.reserve(N); |
Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 3143 | for (auto &P : *L) { |
| 3144 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3145 | Params.push_back(D); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 3146 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | // Clean up if we had an error. |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 3150 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3151 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3152 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3153 | // Note: we substitute into associated constraints later |
| 3154 | Expr *const UninstantiatedRequiresClause = L->getRequiresClause(); |
| 3155 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3156 | TemplateParameterList *InstL |
| 3157 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 3158 | L->getLAngleLoc(), Params, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3159 | L->getRAngleLoc(), |
| 3160 | UninstantiatedRequiresClause); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3161 | return InstL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3162 | } |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3163 | |
Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 3164 | TemplateParameterList * |
| 3165 | Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, |
| 3166 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3167 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 3168 | return Instantiator.SubstTemplateParams(Params); |
| 3169 | } |
| 3170 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3171 | /// Instantiate the declaration of a class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3172 | /// specialization. |
| 3173 | /// |
| 3174 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 3175 | // specialized by the instantiation of \p PartialSpec. |
| 3176 | /// |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3177 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3178 | /// specialization that we are instantiating. |
| 3179 | /// |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3180 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3181 | /// NULL to indicate an error. |
| 3182 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3183 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 3184 | ClassTemplateDecl *ClassTemplate, |
| 3185 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3186 | // Create a local instantiation scope for this class template partial |
| 3187 | // specialization, which will contain the instantiations of the template |
| 3188 | // parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3189 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3191 | // Substitute into the template parameters of the class template partial |
| 3192 | // specialization. |
| 3193 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3194 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3195 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3196 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3198 | // Substitute into the template arguments of the class template partial |
| 3199 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3200 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3201 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3202 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3203 | TemplArgInfo->RAngleLoc); |
| 3204 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3205 | TemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 3206 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3207 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3209 | // Check that the template argument list is well-formed for this |
| 3210 | // class template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3211 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3212 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3213 | PartialSpec->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3214 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3215 | false, |
| 3216 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3217 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3218 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3219 | // Check these arguments are valid for a template partial specialization. |
| 3220 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3221 | PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), |
| 3222 | Converted)) |
| 3223 | return nullptr; |
| 3224 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3225 | // Figure out where to insert this class template partial specialization |
| 3226 | // in the member template's set of class template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3227 | void *InsertPos = nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3228 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3229 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3230 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3231 | // Build the canonical type that describes the converted template |
| 3232 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3233 | QualType CanonType |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3234 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3235 | Converted); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3236 | |
| 3237 | // Build the fully-sugared type for this class template |
| 3238 | // specialization as the user wrote in the specialization |
| 3239 | // itself. This means that we'll pretty-print the type retrieved |
| 3240 | // from the specialization's declaration the way that the user |
| 3241 | // actually wrote the specialization, rather than formatting the |
| 3242 | // name based on the "canonical" representation used to store the |
| 3243 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3244 | TypeSourceInfo *WrittenTy |
| 3245 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3246 | TemplateName(ClassTemplate), |
| 3247 | PartialSpec->getLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3248 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3249 | CanonType); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3250 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3251 | if (PrevDecl) { |
| 3252 | // We've already seen a partial specialization with the same template |
| 3253 | // parameters and template arguments. This can happen, for example, when |
| 3254 | // substituting the outer template arguments ends up causing two |
| 3255 | // class template partial specializations of a member class template |
| 3256 | // to have identical forms, e.g., |
| 3257 | // |
| 3258 | // template<typename T, typename U> |
| 3259 | // struct Outer { |
| 3260 | // template<typename X, typename Y> struct Inner; |
| 3261 | // template<typename Y> struct Inner<T, Y>; |
| 3262 | // template<typename Y> struct Inner<U, Y>; |
| 3263 | // }; |
| 3264 | // |
| 3265 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3266 | // // have the same signature. |
| 3267 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3268 | << WrittenTy->getType(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3269 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 3270 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3271 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3272 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3273 | |
| 3274 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3275 | // Create the class template partial specialization declaration. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3276 | ClassTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3277 | ClassTemplatePartialSpecializationDecl::Create( |
| 3278 | SemaRef.Context, PartialSpec->getTagKind(), Owner, |
| 3279 | PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, |
| 3280 | ClassTemplate, Converted, InstTemplateArgs, CanonType, nullptr); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3281 | // Substitute the nested name specifier, if any. |
| 3282 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3283 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3285 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 3286 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3287 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3288 | // Check the completed partial specialization. |
| 3289 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3290 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3291 | // Add this partial specialization to the set of class template partial |
| 3292 | // specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3293 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 3294 | /*InsertPos=*/nullptr); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3295 | return InstPartialSpec; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3298 | /// Instantiate the declaration of a variable template partial |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3299 | /// specialization. |
| 3300 | /// |
| 3301 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 3302 | /// specialized by the instantiation of \p PartialSpec. |
| 3303 | /// |
| 3304 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 3305 | /// specialization that we are instantiating. |
| 3306 | /// |
| 3307 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3308 | /// NULL to indicate an error. |
| 3309 | VarTemplatePartialSpecializationDecl * |
| 3310 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 3311 | VarTemplateDecl *VarTemplate, |
| 3312 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 3313 | // Create a local instantiation scope for this variable template partial |
| 3314 | // specialization, which will contain the instantiations of the template |
| 3315 | // parameters. |
| 3316 | LocalInstantiationScope Scope(SemaRef); |
| 3317 | |
| 3318 | // Substitute into the template parameters of the variable template partial |
| 3319 | // specialization. |
| 3320 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3321 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3322 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3323 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3324 | |
| 3325 | // Substitute into the template arguments of the variable template partial |
| 3326 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3327 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3328 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3329 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3330 | TemplArgInfo->RAngleLoc); |
| 3331 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3332 | TemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3333 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3334 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3335 | |
| 3336 | // Check that the template argument list is well-formed for this |
| 3337 | // class template. |
| 3338 | SmallVector<TemplateArgument, 4> Converted; |
| 3339 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 3340 | InstTemplateArgs, false, Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3341 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3342 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3343 | // Check these arguments are valid for a template partial specialization. |
| 3344 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3345 | PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), |
| 3346 | Converted)) |
| 3347 | return nullptr; |
| 3348 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3349 | // Figure out where to insert this variable template partial specialization |
| 3350 | // in the member template's set of variable template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3351 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3352 | VarTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3353 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3354 | |
| 3355 | // Build the canonical type that describes the converted template |
| 3356 | // arguments of the variable template partial specialization. |
| 3357 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3358 | TemplateName(VarTemplate), Converted); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3359 | |
| 3360 | // Build the fully-sugared type for this variable template |
| 3361 | // specialization as the user wrote in the specialization |
| 3362 | // itself. This means that we'll pretty-print the type retrieved |
| 3363 | // from the specialization's declaration the way that the user |
| 3364 | // actually wrote the specialization, rather than formatting the |
| 3365 | // name based on the "canonical" representation used to store the |
| 3366 | // template arguments in the specialization. |
| 3367 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3368 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 3369 | CanonType); |
| 3370 | |
| 3371 | if (PrevDecl) { |
| 3372 | // We've already seen a partial specialization with the same template |
| 3373 | // parameters and template arguments. This can happen, for example, when |
| 3374 | // substituting the outer template arguments ends up causing two |
| 3375 | // variable template partial specializations of a member variable template |
| 3376 | // to have identical forms, e.g., |
| 3377 | // |
| 3378 | // template<typename T, typename U> |
| 3379 | // struct Outer { |
| 3380 | // template<typename X, typename Y> pair<X,Y> p; |
| 3381 | // template<typename Y> pair<T, Y> p; |
| 3382 | // template<typename Y> pair<U, Y> p; |
| 3383 | // }; |
| 3384 | // |
| 3385 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3386 | // // have the same signature. |
| 3387 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3388 | diag::err_var_partial_spec_redeclared) |
| 3389 | << WrittenTy->getType(); |
| 3390 | SemaRef.Diag(PrevDecl->getLocation(), |
| 3391 | diag::note_var_prev_partial_spec_here); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3392 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | // Do substitution on the type of the declaration |
| 3396 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 3397 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 3398 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 3399 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3400 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3401 | |
| 3402 | if (DI->getType()->isFunctionType()) { |
| 3403 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3404 | diag::err_variable_instantiates_to_function) |
| 3405 | << PartialSpec->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3406 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | // Create the variable template partial specialization declaration. |
| 3410 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3411 | VarTemplatePartialSpecializationDecl::Create( |
| 3412 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 3413 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3414 | DI, PartialSpec->getStorageClass(), Converted, InstTemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3415 | |
| 3416 | // Substitute the nested name specifier, if any. |
| 3417 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3418 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3419 | |
| 3420 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 3421 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 3422 | |
Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3423 | // Check the completed partial specialization. |
| 3424 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3425 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3426 | // Add this partial specialization to the set of variable template partial |
| 3427 | // specializations. The instantiation of the initializer is not necessary. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3428 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3429 | |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3430 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3431 | LateAttrs, Owner, StartingScope); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3432 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3433 | return InstPartialSpec; |
| 3434 | } |
| 3435 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3436 | TypeSourceInfo* |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3437 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3438 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3439 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 3440 | assert(OldTInfo && "substituting function without type source info"); |
| 3441 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3442 | |
| 3443 | CXXRecordDecl *ThisContext = nullptr; |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3444 | Qualifiers ThisTypeQuals; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3445 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3446 | ThisContext = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3447 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 3448 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3449 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 3450 | TypeSourceInfo *NewTInfo |
| 3451 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 3452 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3453 | D->getDeclName(), |
| 3454 | ThisContext, ThisTypeQuals); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3455 | if (!NewTInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3456 | return nullptr; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3457 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3458 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 3459 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 3460 | if (NewTInfo != OldTInfo) { |
| 3461 | // Get parameters from the new type info. |
Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 3462 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3463 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3464 | unsigned NewIdx = 0; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3465 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3466 | OldIdx != NumOldParams; ++OldIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3467 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3468 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 3469 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3470 | Optional<unsigned> NumArgumentsInExpansion; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3471 | if (OldParam->isParameterPack()) |
| 3472 | NumArgumentsInExpansion = |
| 3473 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 3474 | TemplateArgs); |
| 3475 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3476 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3477 | // instantiated to a (still-dependent) parameter pack. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3478 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3479 | Params.push_back(NewParam); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3480 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 3481 | } else { |
| 3482 | // Parameter pack expansion: make the instantiation an argument pack. |
| 3483 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 3484 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3485 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3486 | Params.push_back(NewParam); |
| 3487 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 3488 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3489 | } |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3490 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3491 | } else { |
| 3492 | // The function type itself was not dependent and therefore no |
| 3493 | // substitution occurred. However, we still need to instantiate |
| 3494 | // the function parameters themselves. |
| 3495 | const FunctionProtoType *OldProto = |
| 3496 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3497 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 3498 | ++i) { |
| 3499 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3500 | if (!OldParam) { |
| 3501 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3502 | D, D->getLocation(), OldProto->getParamType(i))); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3503 | continue; |
| 3504 | } |
| 3505 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3506 | ParmVarDecl *Parm = |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3507 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3508 | if (!Parm) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3509 | return nullptr; |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3510 | Params.push_back(Parm); |
| 3511 | } |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3512 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3513 | } else { |
| 3514 | // If the type of this function, after ignoring parentheses, is not |
| 3515 | // *directly* a function type, then we're instantiating a function that |
| 3516 | // was declared via a typedef or with attributes, e.g., |
| 3517 | // |
| 3518 | // typedef int functype(int, int); |
| 3519 | // functype func; |
| 3520 | // int __cdecl meth(int, int); |
| 3521 | // |
| 3522 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 3523 | // synthesized in the method declaration. |
| 3524 | SmallVector<QualType, 4> ParamTypes; |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3525 | Sema::ExtParameterInfoBuilder ExtParamInfos; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3526 | if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, |
| 3527 | TemplateArgs, ParamTypes, &Params, |
| 3528 | ExtParamInfos)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3529 | return nullptr; |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3530 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3531 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3532 | return NewTInfo; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3535 | /// Introduce the instantiated function parameters into the local |
| 3536 | /// instantiation scope, and set the parameter names to those used |
| 3537 | /// in the template. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3538 | static bool addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3539 | const FunctionDecl *PatternDecl, |
| 3540 | LocalInstantiationScope &Scope, |
| 3541 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3542 | unsigned FParamIdx = 0; |
| 3543 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 3544 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 3545 | if (!PatternParam->isParameterPack()) { |
| 3546 | // Simple case: not a parameter pack. |
| 3547 | assert(FParamIdx < Function->getNumParams()); |
| 3548 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3549 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3550 | // If the parameter's type is not dependent, update it to match the type |
| 3551 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 3552 | // 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] | 3553 | // per core issue 1668. Substitute into the type from the pattern, in case |
| 3554 | // it's instantiation-dependent. |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3555 | // FIXME: Updating the type to work around this is at best fragile. |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3556 | if (!PatternDecl->getType()->isDependentType()) { |
| 3557 | QualType T = S.SubstType(PatternParam->getType(), TemplateArgs, |
| 3558 | FunctionParam->getLocation(), |
| 3559 | FunctionParam->getDeclName()); |
| 3560 | if (T.isNull()) |
| 3561 | return true; |
| 3562 | FunctionParam->setType(T); |
| 3563 | } |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3564 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3565 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3566 | ++FParamIdx; |
| 3567 | continue; |
| 3568 | } |
| 3569 | |
| 3570 | // Expand the parameter pack. |
| 3571 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3572 | Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3573 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3574 | assert(NumArgumentsInExpansion && |
| 3575 | "should only be called when all template arguments are known"); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3576 | QualType PatternType = |
| 3577 | PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3578 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3579 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3580 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3581 | if (!PatternDecl->getType()->isDependentType()) { |
| 3582 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, Arg); |
| 3583 | QualType T = S.SubstType(PatternType, TemplateArgs, |
| 3584 | FunctionParam->getLocation(), |
| 3585 | FunctionParam->getDeclName()); |
| 3586 | if (T.isNull()) |
| 3587 | return true; |
| 3588 | FunctionParam->setType(T); |
| 3589 | } |
| 3590 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3591 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3592 | ++FParamIdx; |
| 3593 | } |
| 3594 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3595 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3596 | return false; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3600 | FunctionDecl *Decl) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3601 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3602 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3603 | return; |
| 3604 | |
| 3605 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3606 | InstantiatingTemplate::ExceptionSpecification()); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3607 | if (Inst.isInvalid()) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3608 | // We hit the instantiation depth limit. Clear the exception specification |
| 3609 | // so that our callers don't have to cope with EST_Uninstantiated. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3610 | UpdateExceptionSpec(Decl, EST_None); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3611 | return; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3612 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3613 | if (Inst.isAlreadyInstantiating()) { |
| 3614 | // This exception specification indirectly depends on itself. Reject. |
| 3615 | // FIXME: Corresponding rule in the standard? |
| 3616 | Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; |
| 3617 | UpdateExceptionSpec(Decl, EST_None); |
| 3618 | return; |
| 3619 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3620 | |
| 3621 | // Enter the scope of this instantiation. We don't use |
| 3622 | // PushDeclContext because we don't have a scope. |
| 3623 | Sema::ContextRAII savedContext(*this, Decl); |
| 3624 | LocalInstantiationScope Scope(*this); |
| 3625 | |
| 3626 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3627 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3628 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3629 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3630 | if (addInstantiatedParametersToScope(*this, Decl, Template, Scope, |
| 3631 | TemplateArgs)) { |
| 3632 | UpdateExceptionSpec(Decl, EST_None); |
| 3633 | return; |
| 3634 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3635 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3636 | SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), |
| 3637 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3640 | /// Initializes the common fields of an instantiation function |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3641 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3642 | /// |
| 3643 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3644 | bool |
| 3645 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3646 | FunctionDecl *Tmpl) { |
David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3647 | if (Tmpl->isDeleted()) |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3648 | New->setDeletedAsWritten(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3650 | New->setImplicit(Tmpl->isImplicit()); |
| 3651 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3652 | // Forward the mangling number from the template to the instantiated decl. |
| 3653 | SemaRef.Context.setManglingNumber(New, |
| 3654 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3655 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3656 | // If we are performing substituting explicitly-specified template arguments |
| 3657 | // or deduced template arguments into a function template and we reach this |
| 3658 | // 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] | 3659 | // to keeping the new function template specialization. We therefore |
| 3660 | // convert the active template instantiation for the function template |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3661 | // into a template instantiation for this specific function template |
| 3662 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3663 | // further errors in the declaration itself. |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3664 | typedef Sema::CodeSynthesisContext ActiveInstType; |
| 3665 | ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3666 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3667 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3669 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3671 | "Deduction from the wrong function template?"); |
Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3672 | (void) FunTmpl; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3673 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3674 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3675 | ActiveInst.Entity = New; |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3676 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3677 | } |
| 3678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3680 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3681 | assert(Proto && "Function template without prototype?"); |
| 3682 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3683 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3684 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3685 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3686 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3687 | // exception specification. |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3688 | // DR1484: Local classes and their members are instantiated along with the |
| 3689 | // containing function. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3690 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3691 | EPI.ExceptionSpec.Type != EST_None && |
| 3692 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3693 | EPI.ExceptionSpec.Type != EST_BasicNoexcept && |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 3694 | !Tmpl->isLexicallyWithinFunctionOrMethod()) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3695 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3696 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3697 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3698 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3699 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3700 | NewEST = EST_Unevaluated; |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3701 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3702 | // Mark the function has having an uninstantiated exception specification. |
| 3703 | const FunctionProtoType *NewProto |
| 3704 | = New->getType()->getAs<FunctionProtoType>(); |
| 3705 | assert(NewProto && "Template instantiation without function prototype?"); |
| 3706 | EPI = NewProto->getExtProtoInfo(); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3707 | EPI.ExceptionSpec.Type = NewEST; |
| 3708 | EPI.ExceptionSpec.SourceDecl = New; |
| 3709 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3710 | New->setType(SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3711 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3712 | } else { |
Faisal Vali | 40fd4ce | 2017-05-09 04:17:15 +0000 | [diff] [blame] | 3713 | Sema::ContextRAII SwitchContext(SemaRef, New); |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3714 | SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3715 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3718 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3719 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3720 | Tmpl->isDefined(Definition); |
| 3721 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3722 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 3723 | LateAttrs, StartingScope); |
Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3724 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3725 | return false; |
| 3726 | } |
| 3727 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3728 | /// Initializes common fields of an instantiated method |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3729 | /// declaration (New) from the corresponding fields of its template |
| 3730 | /// (Tmpl). |
| 3731 | /// |
| 3732 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | bool |
| 3734 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3735 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3736 | if (InitFunctionInstantiation(New, Tmpl)) |
| 3737 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 3739 | if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) |
| 3740 | SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); |
| 3741 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3742 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3743 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3744 | New->setVirtualAsWritten(true); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3746 | // FIXME: New needs a pointer to Tmpl |
| 3747 | return false; |
| 3748 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3749 | |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 3750 | /// Instantiate (or find existing instantiation of) a function template with a |
| 3751 | /// given set of template arguments. |
| 3752 | /// |
| 3753 | /// Usually this should not be used, and template argument deduction should be |
| 3754 | /// used in its place. |
| 3755 | FunctionDecl * |
| 3756 | Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, |
| 3757 | const TemplateArgumentList *Args, |
| 3758 | SourceLocation Loc) { |
| 3759 | FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 3760 | |
| 3761 | sema::TemplateDeductionInfo Info(Loc); |
| 3762 | InstantiatingTemplate Inst( |
| 3763 | *this, Loc, FTD, Args->asArray(), |
| 3764 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 3765 | if (Inst.isInvalid()) |
| 3766 | return nullptr; |
| 3767 | |
| 3768 | ContextRAII SavedContext(*this, FD); |
| 3769 | MultiLevelTemplateArgumentList MArgs(*Args); |
| 3770 | |
| 3771 | return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); |
| 3772 | } |
| 3773 | |
Reid Kleckner | 61195e1 | 2017-01-05 01:08:22 +0000 | [diff] [blame] | 3774 | /// In the MS ABI, we need to instantiate default arguments of dllexported |
| 3775 | /// default constructors along with the constructor definition. This allows IR |
| 3776 | /// gen to emit a constructor closure which calls the default constructor with |
| 3777 | /// its default arguments. |
| 3778 | static void InstantiateDefaultCtorDefaultArgs(Sema &S, |
| 3779 | CXXConstructorDecl *Ctor) { |
| 3780 | assert(S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3781 | Ctor->isDefaultConstructor()); |
| 3782 | unsigned NumParams = Ctor->getNumParams(); |
| 3783 | if (NumParams == 0) |
| 3784 | return; |
| 3785 | DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); |
| 3786 | if (!Attr) |
| 3787 | return; |
| 3788 | for (unsigned I = 0; I != NumParams; ++I) { |
| 3789 | (void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, |
| 3790 | Ctor->getParamDecl(I)); |
| 3791 | S.DiscardCleanupsInEvaluationContext(); |
| 3792 | } |
| 3793 | } |
| 3794 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3795 | /// Instantiate the definition of the given function from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3796 | /// template. |
| 3797 | /// |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3798 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3799 | /// required. Note that this is not precisely a "point of instantiation" |
| 3800 | /// for the function, but it's close. |
| 3801 | /// |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3802 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3803 | /// function template specialization or member function of a class template |
| 3804 | /// specialization. |
| 3805 | /// |
| 3806 | /// \param Recursive if true, recursively instantiates any functions that |
| 3807 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3808 | /// |
| 3809 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3810 | /// instantiation where the body of the function is required. Complain if |
| 3811 | /// there is no such body. |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3812 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3813 | FunctionDecl *Function, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3814 | bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 3815 | bool DefinitionRequired, |
| 3816 | bool AtEndOfTU) { |
Richard Smith | cb18957 | 2017-10-28 01:15:00 +0000 | [diff] [blame] | 3817 | if (Function->isInvalidDecl() || Function->isDefined() || |
| 3818 | isa<CXXDeductionGuideDecl>(Function)) |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3819 | return; |
| 3820 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3821 | // Never instantiate an explicit specialization except if it is a class scope |
| 3822 | // explicit specialization. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3823 | TemplateSpecializationKind TSK = Function->getTemplateSpecializationKind(); |
| 3824 | if (TSK == TSK_ExplicitSpecialization && |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3825 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3826 | return; |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3827 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3828 | // Find the function body that we'll be substituting. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3829 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3830 | assert(PatternDecl && "instantiating a non-template"); |
| 3831 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3832 | const FunctionDecl *PatternDef = PatternDecl->getDefinition(); |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 3833 | Stmt *Pattern = nullptr; |
| 3834 | if (PatternDef) { |
| 3835 | Pattern = PatternDef->getBody(PatternDef); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3836 | PatternDecl = PatternDef; |
Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 3837 | if (PatternDef->willHaveBody()) |
| 3838 | PatternDef = nullptr; |
Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 3839 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3840 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3841 | // FIXME: We need to track the instantiation stack in order to know which |
| 3842 | // definitions should be visible within this instantiation. |
| 3843 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, |
| 3844 | Function->getInstantiatedFromMemberFunction(), |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3845 | PatternDecl, PatternDef, TSK, |
| 3846 | /*Complain*/DefinitionRequired)) { |
| 3847 | if (DefinitionRequired) |
| 3848 | Function->setInvalidDecl(); |
| 3849 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 3850 | // Try again at the end of the translation unit (at which point a |
| 3851 | // definition will be required). |
| 3852 | assert(!Recursive); |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 3853 | Function->setInstantiationIsPending(true); |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3854 | PendingInstantiations.push_back( |
| 3855 | std::make_pair(Function, PointOfInstantiation)); |
| 3856 | } else if (TSK == TSK_ImplicitInstantiation) { |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 3857 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3858 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3859 | Diag(PointOfInstantiation, diag::warn_func_template_missing) |
| 3860 | << Function; |
| 3861 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 3862 | if (getLangOpts().CPlusPlus11) |
| 3863 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) |
| 3864 | << Function; |
| 3865 | } |
| 3866 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3867 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3868 | return; |
| 3869 | } |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3870 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3871 | // Postpone late parsed template instantiations. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3872 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 3873 | !LateTemplateParser) { |
Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 3874 | Function->setInstantiationIsPending(true); |
Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 3875 | LateParsedInstantiations.push_back( |
| 3876 | std::make_pair(Function, PointOfInstantiation)); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3877 | return; |
| 3878 | } |
| 3879 | |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3880 | // If we're performing recursive template instantiation, create our own |
| 3881 | // queue of pending implicit instantiations that we will instantiate later, |
| 3882 | // while we're still within our own instantiation context. |
| 3883 | // This has to happen before LateTemplateParser below is called, so that |
| 3884 | // it marks vtables used in late parsed templates as used. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 3885 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 3886 | /*Enabled=*/Recursive); |
| 3887 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3888 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 3889 | // Call the LateTemplateParser callback if there is a need to late parse |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3890 | // a templated function definition. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3891 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3892 | LateTemplateParser) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3893 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 3894 | if (PatternDecl->isFromASTFile()) |
| 3895 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 3896 | |
Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 3897 | auto LPTIter = LateParsedTemplateMap.find(PatternDecl); |
| 3898 | assert(LPTIter != LateParsedTemplateMap.end() && |
| 3899 | "missing LateParsedTemplate"); |
| 3900 | LateTemplateParser(OpaqueParser, *LPTIter->second); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3901 | Pattern = PatternDecl->getBody(PatternDecl); |
| 3902 | } |
| 3903 | |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3904 | // Note, we should never try to instantiate a deleted function template. |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3905 | assert((Pattern || PatternDecl->isDefaulted() || |
| 3906 | PatternDecl->hasSkippedBody()) && |
Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 3907 | "unexpected kind of function template definition"); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3908 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3909 | // C++1y [temp.explicit]p10: |
| 3910 | // Except for inline functions, declarations with types deduced from their |
| 3911 | // initializer or return value, and class template specializations, other |
| 3912 | // explicit instantiation declarations have the effect of suppressing the |
| 3913 | // implicit instantiation of the entity to which they refer. |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3914 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3915 | !PatternDecl->isInlined() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3916 | !PatternDecl->getReturnType()->getContainedAutoType()) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3917 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | |
Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 3919 | if (PatternDecl->isInlined()) { |
| 3920 | // Function, and all later redeclarations of it (from imported modules, |
| 3921 | // for instance), are now implicitly inline. |
| 3922 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 3923 | D = D->getPreviousDecl()) { |
| 3924 | D->setImplicitlyInline(); |
| 3925 | if (D == Function) |
| 3926 | break; |
| 3927 | } |
| 3928 | } |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3929 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3930 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3931 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3932 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 3933 | PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 3934 | "instantiating function definition"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3935 | |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3936 | // The instantiation is visible here, even if it was first declared in an |
| 3937 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 3938 | Function->setVisibleDespiteOwningModule(); |
Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 3939 | |
Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 3940 | // Copy the inner loc start from the pattern. |
| 3941 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 3942 | |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3943 | EnterExpressionEvaluationContext EvalContext( |
| 3944 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 3945 | |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3946 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3947 | // recorded, unless we're actually a member function within a local |
| 3948 | // class, in which case we need to merge our results with the parent |
| 3949 | // scope (of the enclosing function). |
| 3950 | bool MergeWithParentScope = false; |
| 3951 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 3952 | MergeWithParentScope = Rec->isLocalClass(); |
| 3953 | |
| 3954 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3956 | if (PatternDecl->isDefaulted()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3957 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3958 | else { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 3959 | MultiLevelTemplateArgumentList TemplateArgs = |
| 3960 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
| 3961 | |
| 3962 | // Substitute into the qualifier; we can get a substitution failure here |
| 3963 | // through evil use of alias templates. |
| 3964 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 3965 | // of the) lexical context of the pattern? |
| 3966 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 3967 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3968 | ActOnStartOfFunctionDef(nullptr, Function); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3969 | |
| 3970 | // Enter the scope of this instantiation. We don't use |
| 3971 | // PushDeclContext because we don't have a scope. |
| 3972 | Sema::ContextRAII savedContext(*this, Function); |
| 3973 | |
Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3974 | if (addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 3975 | TemplateArgs)) |
| 3976 | return; |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3977 | |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 3978 | StmtResult Body; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3979 | if (PatternDecl->hasSkippedBody()) { |
| 3980 | ActOnSkippedFunctionBody(Function); |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 3981 | Body = nullptr; |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3982 | } else { |
Ilya Biryukov | 95f0d32 | 2017-12-28 13:05:46 +0000 | [diff] [blame] | 3983 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { |
| 3984 | // If this is a constructor, instantiate the member initializers. |
| 3985 | InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), |
| 3986 | TemplateArgs); |
| 3987 | |
| 3988 | // If this is an MS ABI dllexport default constructor, instantiate any |
| 3989 | // default arguments. |
| 3990 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3991 | Ctor->isDefaultConstructor()) { |
| 3992 | InstantiateDefaultCtorDefaultArgs(*this, Ctor); |
| 3993 | } |
| 3994 | } |
| 3995 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3996 | // Instantiate the function body. |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 3997 | Body = SubstStmt(Pattern, TemplateArgs); |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3998 | |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 3999 | if (Body.isInvalid()) |
| 4000 | Function->setInvalidDecl(); |
Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4001 | } |
Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4002 | // FIXME: finishing the function body while in an expression evaluation |
| 4003 | // context seems wrong. Investigate more. |
| 4004 | ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4005 | |
| 4006 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 4007 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4008 | if (auto *Listener = getASTMutationListener()) |
| 4009 | Listener->FunctionDefinitionInstantiated(Function); |
Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 4010 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4011 | savedContext.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 4014 | DeclGroupRef DG(Function); |
| 4015 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4017 | // This class may have local implicit instantiations that need to be |
| 4018 | // instantiation within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4019 | LocalInstantiations.perform(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4020 | Scope.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4021 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4024 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 4025 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 4026 | const TemplateArgumentList &TemplateArgList, |
| 4027 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 4028 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4029 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 4030 | LateInstantiatedAttrVec *LateAttrs, |
| 4031 | LocalInstantiationScope *StartingScope) { |
| 4032 | if (FromVar->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4033 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4034 | |
| 4035 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4036 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4037 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4038 | |
| 4039 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4040 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 4041 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4042 | // Instantiate the first declaration of the variable template: for a partial |
| 4043 | // specialization of a static data member template, the first declaration may |
| 4044 | // or may not be the declaration in the class; if it's in the class, we want |
| 4045 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 4046 | // we want to instantiate a definition. |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4047 | // |
| 4048 | // If we're instantiating an explicitly-specialized member template or member |
| 4049 | // partial specialization, don't do this. The member specialization completely |
| 4050 | // replaces the original declaration in this case. |
| 4051 | bool IsMemberSpec = false; |
| 4052 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 4053 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 4054 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 4055 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 4056 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 4057 | if (!IsMemberSpec) |
| 4058 | FromVar = FromVar->getFirstDecl(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4059 | |
Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 4060 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 4061 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 4062 | MultiLevelList); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4063 | |
| 4064 | // TODO: Set LateAttrs and StartingScope ... |
| 4065 | |
| 4066 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 4067 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 4068 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 4069 | } |
| 4070 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4071 | /// Instantiates a variable template specialization by completing it |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4072 | /// with appropriate type information and initializer. |
| 4073 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 4074 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 4075 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4076 | assert(PatternDecl->isThisDeclarationADefinition() && |
| 4077 | "don't have a definition to instantiate from"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4078 | |
| 4079 | // Do substitution on the type of the declaration |
| 4080 | TypeSourceInfo *DI = |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4081 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4082 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 4083 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4084 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4085 | |
| 4086 | // Update the type of this variable template specialization. |
| 4087 | VarSpec->setType(DI->getType()); |
| 4088 | |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4089 | // Convert the declaration into a definition now. |
| 4090 | VarSpec->setCompleteDefinition(); |
| 4091 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4092 | // Instantiate the initializer. |
| 4093 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 4094 | |
| 4095 | return VarSpec; |
| 4096 | } |
| 4097 | |
| 4098 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 4099 | /// Sets basic variable data and decides whether to postpone the |
| 4100 | /// variable instantiation. |
| 4101 | void Sema::BuildVariableInstantiation( |
| 4102 | VarDecl *NewVar, VarDecl *OldVar, |
| 4103 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4104 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 4105 | LocalInstantiationScope *StartingScope, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4106 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4107 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4108 | // If we are instantiating a local extern declaration, the |
| 4109 | // instantiation belongs lexically to the containing function. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4110 | // If we are instantiating a static data member defined |
| 4111 | // out-of-line, the instantiation will have the same lexical |
| 4112 | // context (which will be a namespace scope) as the template. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4113 | if (OldVar->isLocalExternDecl()) { |
| 4114 | NewVar->setLocalExternDecl(); |
| 4115 | NewVar->setLexicalDeclContext(Owner); |
| 4116 | } else if (OldVar->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4117 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 4118 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 4119 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 4120 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4121 | NewVar->setObjCForDecl(OldVar->isObjCForDecl()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4122 | NewVar->setConstexpr(OldVar->isConstexpr()); |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4123 | NewVar->setInitCapture(OldVar->isInitCapture()); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4124 | NewVar->setPreviousDeclInSameBlockScope( |
| 4125 | OldVar->isPreviousDeclInSameBlockScope()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4126 | NewVar->setAccess(OldVar->getAccess()); |
| 4127 | |
Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 4128 | if (!OldVar->isStaticDataMember()) { |
Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 4129 | if (OldVar->isUsed(false)) |
| 4130 | NewVar->setIsUsed(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4131 | NewVar->setReferenced(OldVar->isReferenced()); |
| 4132 | } |
| 4133 | |
| 4134 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 4135 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4136 | LookupResult Previous( |
| 4137 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 4138 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 4139 | : Sema::LookupOrdinaryName, |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4140 | NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 4141 | : forRedeclarationInCurContext()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4142 | |
Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 4143 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 4144 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 4145 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4146 | // We have a previous declaration. Use that one, so we merge with the |
| 4147 | // right type. |
| 4148 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 4149 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 4150 | Previous.addDecl(NewPrev); |
| 4151 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 4152 | OldVar->hasLinkage()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4153 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4154 | CheckVariableDeclaration(NewVar, Previous); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4155 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4156 | if (!InstantiatingVarTemplate) { |
| 4157 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 4158 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4159 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | if (!OldVar->isOutOfLine()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4163 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 4164 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 4165 | } |
| 4166 | |
| 4167 | // Link instantiations of static data members back to the template from |
| 4168 | // which they were instantiated. |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4169 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4170 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 4171 | TSK_ImplicitInstantiation); |
| 4172 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4173 | // Forward the mangling number from the template to the instantiated decl. |
| 4174 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4175 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4176 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4177 | // Delay instantiation of the initializer for variable templates or inline |
| 4178 | // static data members until a definition of the variable is needed. We need |
| 4179 | // it right away if the type contains 'auto'. |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4180 | if ((!isa<VarTemplateSpecializationDecl>(NewVar) && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4181 | !InstantiatingVarTemplate && |
Richard Smith | 93ee9ca | 2018-01-10 23:08:26 +0000 | [diff] [blame] | 4182 | !(OldVar->isInline() && OldVar->isThisDeclarationADefinition() && |
| 4183 | !NewVar->isThisDeclarationADefinition())) || |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 4184 | NewVar->getType()->isUndeducedType()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4185 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 4186 | |
| 4187 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 4188 | // will have been deferred. |
| 4189 | if (!NewVar->isInvalidDecl() && |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4190 | NewVar->getDeclContext()->isFunctionOrMethod() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4191 | OldVar->getType()->isDependentType()) |
| 4192 | DiagnoseUnusedDecl(NewVar); |
| 4193 | } |
| 4194 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4195 | /// Instantiate the initializer of a variable. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4196 | void Sema::InstantiateVariableInitializer( |
| 4197 | VarDecl *Var, VarDecl *OldVar, |
| 4198 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4199 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4200 | L->VariableDefinitionInstantiated(Var); |
| 4201 | |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4202 | // We propagate the 'inline' flag with the initializer, because it |
| 4203 | // would otherwise imply that the variable is a definition for a |
| 4204 | // non-static data member. |
| 4205 | if (OldVar->isInlineSpecified()) |
| 4206 | Var->setInlineSpecified(); |
| 4207 | else if (OldVar->isInline()) |
| 4208 | Var->setImplicitlyInline(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4209 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4210 | if (OldVar->getInit()) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 4211 | EnterExpressionEvaluationContext Evaluated( |
| 4212 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4213 | |
| 4214 | // Instantiate the initializer. |
Akira Hatanaka | b87faff | 2016-04-28 23:50:12 +0000 | [diff] [blame] | 4215 | ExprResult Init; |
| 4216 | |
| 4217 | { |
| 4218 | ContextRAII SwitchContext(*this, Var->getDeclContext()); |
| 4219 | Init = SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 4220 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 4221 | } |
| 4222 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4223 | if (!Init.isInvalid()) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4224 | Expr *InitExpr = Init.get(); |
| 4225 | |
Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 4226 | if (Var->hasAttr<DLLImportAttr>() && |
| 4227 | (!InitExpr || |
| 4228 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4229 | // Do not dynamically initialize dllimport variables. |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4230 | } else if (InitExpr) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4231 | bool DirectInit = OldVar->isDirectInit(); |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4232 | AddInitializerToDecl(Var, InitExpr, DirectInit); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4233 | } else |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4234 | ActOnUninitializedDecl(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4235 | } else { |
| 4236 | // FIXME: Not too happy about invalidating the declaration |
| 4237 | // because of a bogus initializer. |
| 4238 | Var->setInvalidDecl(); |
| 4239 | } |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4240 | } else { |
George Burgess IV | 18b28a8 | 2018-03-20 03:27:44 +0000 | [diff] [blame] | 4241 | // `inline` variables are a definition and declaration all in one; we won't |
| 4242 | // pick up an initializer from anywhere else. |
| 4243 | if (Var->isStaticDataMember() && !Var->isInline()) { |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4244 | if (!Var->isOutOfLine()) |
| 4245 | return; |
| 4246 | |
| 4247 | // If the declaration inside the class had an initializer, don't add |
| 4248 | // another one to the out-of-line definition. |
| 4249 | if (OldVar->getFirstDecl()->hasInit()) |
| 4250 | return; |
| 4251 | } |
| 4252 | |
| 4253 | // We'll add an initializer to a for-range declaration later. |
George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4254 | if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4255 | return; |
| 4256 | |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4257 | ActOnUninitializedDecl(Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4258 | } |
Artem Belevich | e9fa53a | 2018-06-06 22:37:25 +0000 | [diff] [blame] | 4259 | |
| 4260 | if (getLangOpts().CUDA) |
| 4261 | checkAllowedCUDAInitializer(Var); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4262 | } |
| 4263 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4264 | /// Instantiate the definition of the given variable from its |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4265 | /// template. |
| 4266 | /// |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4267 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4268 | /// required. Note that this is not precisely a "point of instantiation" |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4269 | /// for the variable, but it's close. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4270 | /// |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4271 | /// \param Var the already-instantiated declaration of a templated variable. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4272 | /// |
| 4273 | /// \param Recursive if true, recursively instantiates any functions that |
| 4274 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4275 | /// |
| 4276 | /// \param DefinitionRequired if true, then we are performing an explicit |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4277 | /// instantiation where a definition of the variable is required. Complain |
| 4278 | /// if there is no such definition. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4279 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 4280 | VarDecl *Var, bool Recursive, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4281 | bool DefinitionRequired, bool AtEndOfTU) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4282 | if (Var->isInvalidDecl()) |
| 4283 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4284 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4285 | VarTemplateSpecializationDecl *VarSpec = |
| 4286 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4287 | VarDecl *PatternDecl = nullptr, *Def = nullptr; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4288 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4289 | getTemplateInstantiationArgs(Var); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4291 | if (VarSpec) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4292 | // If this is a variable template specialization, make sure that it is |
| 4293 | // non-dependent, then find its instantiation pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4294 | bool InstantiationDependent = false; |
| 4295 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 4296 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 4297 | "Only instantiate variable template specializations that are " |
| 4298 | "not type-dependent"); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 4299 | (void)InstantiationDependent; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4300 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4301 | // Find the variable initialization that we'll be substituting. If the |
| 4302 | // pattern was instantiated from a member template, look back further to |
| 4303 | // find the real pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4304 | assert(VarSpec->getSpecializedTemplate() && |
| 4305 | "Specialization without specialized template?"); |
| 4306 | llvm::PointerUnion<VarTemplateDecl *, |
| 4307 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4308 | VarSpec->getSpecializedTemplateOrPartial(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4309 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4310 | VarTemplatePartialSpecializationDecl *Tmpl = |
| 4311 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); |
| 4312 | while (VarTemplatePartialSpecializationDecl *From = |
| 4313 | Tmpl->getInstantiatedFromMember()) { |
| 4314 | if (Tmpl->isMemberSpecialization()) |
| 4315 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4316 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4317 | Tmpl = From; |
| 4318 | } |
| 4319 | PatternDecl = Tmpl; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4320 | } else { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4321 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); |
| 4322 | while (VarTemplateDecl *From = |
| 4323 | Tmpl->getInstantiatedFromMemberTemplate()) { |
| 4324 | if (Tmpl->isMemberSpecialization()) |
| 4325 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4326 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4327 | Tmpl = From; |
| 4328 | } |
| 4329 | PatternDecl = Tmpl->getTemplatedDecl(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 4330 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4331 | |
| 4332 | // If this is a static data member template, there might be an |
| 4333 | // uninstantiated initializer on the declaration. If so, instantiate |
| 4334 | // it now. |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4335 | // |
| 4336 | // FIXME: This largely duplicates what we would do below. The difference |
| 4337 | // is that along this path we may instantiate an initializer from an |
| 4338 | // in-class declaration of the template and instantiate the definition |
| 4339 | // from a separate out-of-class definition. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4340 | if (PatternDecl->isStaticDataMember() && |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 4341 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4342 | !Var->hasInit()) { |
| 4343 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 4344 | // code here. |
| 4345 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4346 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4347 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4348 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4349 | "instantiating variable initializer"); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4350 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4351 | // The instantiation is visible here, even if it was first declared in an |
| 4352 | // unimported module. |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4353 | Var->setVisibleDespiteOwningModule(); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4354 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4355 | // If we're performing recursive template instantiation, create our own |
| 4356 | // queue of pending implicit instantiations that we will instantiate |
| 4357 | // later, while we're still within our own instantiation context. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4358 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4359 | /*Enabled=*/Recursive); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4360 | LocalInstantiationScope Local(*this); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4361 | LocalEagerInstantiationScope LocalInstantiations(*this); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4362 | |
| 4363 | // Enter the scope of this instantiation. We don't use |
| 4364 | // PushDeclContext because we don't have a scope. |
| 4365 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 4366 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 4367 | PreviousContext.pop(); |
| 4368 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4369 | // This variable may have local implicit instantiations that need to be |
| 4370 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4371 | LocalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4372 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4373 | GlobalInstantiations.perform(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | // Find actual definition |
| 4377 | Def = PatternDecl->getDefinition(getASTContext()); |
| 4378 | } else { |
| 4379 | // If this is a static data member, find its out-of-line definition. |
| 4380 | assert(Var->isStaticDataMember() && "not a static data member?"); |
| 4381 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); |
| 4382 | |
| 4383 | assert(PatternDecl && "data member was not instantiated from a template?"); |
| 4384 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4385 | Def = PatternDecl->getDefinition(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4388 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4389 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4390 | // If we don't have a definition of the variable template, we won't perform |
| 4391 | // any instantiation. Rather, we rely on the user to instantiate this |
| 4392 | // definition (or provide a specialization for it) in another translation |
| 4393 | // unit. |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4394 | if (!Def && !DefinitionRequired) { |
| 4395 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4396 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4397 | std::make_pair(Var, PointOfInstantiation)); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4398 | } else if (TSK == TSK_ImplicitInstantiation) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4399 | // Warn about missing definition at the end of translation unit. |
Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4400 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4401 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4402 | Diag(PointOfInstantiation, diag::warn_var_template_missing) |
| 4403 | << Var; |
| 4404 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4405 | if (getLangOpts().CPlusPlus11) |
| 4406 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; |
| 4407 | } |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4408 | return; |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4413 | // FIXME: We need to track the instantiation stack in order to know which |
| 4414 | // definitions should be visible within this instantiation. |
| 4415 | // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). |
| 4416 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, |
| 4417 | /*InstantiatedFromMember*/false, |
| 4418 | PatternDecl, Def, TSK, |
| 4419 | /*Complain*/DefinitionRequired)) |
| 4420 | return; |
| 4421 | |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4423 | // Never instantiate an explicit specialization. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 4424 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4425 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4426 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4427 | // C++11 [temp.explicit]p10: |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4428 | // Except for inline functions, const variables of literal types, variables |
| 4429 | // of reference types, [...] explicit instantiation declarations |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4430 | // have the effect of suppressing the implicit instantiation of the entity |
| 4431 | // to which they refer. |
Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4432 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| 4433 | !Var->isUsableInConstantExpressions(getASTContext())) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4434 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4436 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 4437 | struct PassToConsumerRAII { |
| 4438 | ASTConsumer &Consumer; |
| 4439 | VarDecl *Var; |
| 4440 | |
| 4441 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 4442 | : Consumer(Consumer), Var(Var) { } |
| 4443 | |
| 4444 | ~PassToConsumerRAII() { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4445 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4446 | } |
| 4447 | } PassToConsumerRAII(Consumer, Var); |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4448 | |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4449 | // If we already have a definition, we're done. |
| 4450 | if (VarDecl *Def = Var->getDefinition()) { |
| 4451 | // We may be explicitly instantiating something we've already implicitly |
| 4452 | // instantiated. |
| 4453 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 4454 | PointOfInstantiation); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4455 | return; |
Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4456 | } |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4458 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4459 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4460 | return; |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4461 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4462 | "instantiating variable definition"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4464 | // If we're performing recursive template instantiation, create our own |
| 4465 | // queue of pending implicit instantiations that we will instantiate later, |
| 4466 | // while we're still within our own instantiation context. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4467 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4468 | /*Enabled=*/Recursive); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4470 | // Enter the scope of this instantiation. We don't use |
| 4471 | // PushDeclContext because we don't have a scope. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4472 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4473 | LocalInstantiationScope Local(*this); |
John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 4474 | |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4475 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| 4476 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4477 | VarDecl *OldVar = Var; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4478 | if (Def->isStaticDataMember() && !Def->isOutOfLine()) { |
| 4479 | // We're instantiating an inline static data member whose definition was |
| 4480 | // provided inside the class. |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4481 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4482 | } else if (!VarSpec) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4483 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4484 | TemplateArgs)); |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4485 | } else if (Var->isStaticDataMember() && |
| 4486 | Var->getLexicalDeclContext()->isRecord()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4487 | // We need to instantiate the definition of a static data member template, |
| 4488 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 4489 | // declaration of the definition. |
| 4490 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 4491 | TemplateArgs); |
| 4492 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4493 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4494 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 4495 | if (Var) { |
| 4496 | llvm::PointerUnion<VarTemplateDecl *, |
| 4497 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4498 | VarSpec->getSpecializedTemplateOrPartial(); |
| 4499 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 4500 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 4501 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 4502 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 4503 | |
| 4504 | // Merge the definition with the declaration. |
| 4505 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4506 | LookupOrdinaryName, forRedeclarationInCurContext()); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4507 | R.addDecl(OldVar); |
| 4508 | MergeVarDecl(Var, R); |
| 4509 | |
| 4510 | // Attach the initializer. |
| 4511 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4512 | } |
| 4513 | } else |
| 4514 | // Complete the existing variable's definition with an appropriately |
| 4515 | // substituted type and initializer. |
| 4516 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4517 | |
| 4518 | PreviousContext.pop(); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4519 | |
| 4520 | if (Var) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4521 | PassToConsumerRAII.Var = Var; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4522 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 4523 | OldVar->getPointOfInstantiation()); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4524 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4525 | |
| 4526 | // This variable may have local implicit instantiations that need to be |
| 4527 | // instantiated within this scope. |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4528 | LocalInstantiations.perform(); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4529 | Local.Exit(); |
Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4530 | GlobalInstantiations.perform(); |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4531 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4532 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4533 | void |
| 4534 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4535 | const CXXConstructorDecl *Tmpl, |
| 4536 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4538 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4539 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4540 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4541 | // Instantiate all the initializers. |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4542 | for (const auto *Init : Tmpl->inits()) { |
Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4543 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4544 | // ones. |
| 4545 | if (!Init->isWritten()) |
| 4546 | continue; |
| 4547 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4548 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4549 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4550 | if (Init->isPackExpansion()) { |
| 4551 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4552 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4553 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4554 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4555 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4556 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4557 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4558 | Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4559 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4560 | BaseTL.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4561 | Unexpanded, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4562 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4563 | RetainExpansion, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4564 | NumExpansions)) { |
| 4565 | AnyErrors = true; |
| 4566 | New->setInvalidDecl(); |
| 4567 | continue; |
| 4568 | } |
| 4569 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4570 | |
| 4571 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4572 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4573 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4574 | |
| 4575 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4576 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4577 | /*CXXDirectInit=*/true); |
| 4578 | if (TempInit.isInvalid()) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4579 | AnyErrors = true; |
| 4580 | break; |
| 4581 | } |
| 4582 | |
| 4583 | // Instantiate the base type. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4584 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4585 | TemplateArgs, |
| 4586 | Init->getSourceLocation(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4587 | New->getDeclName()); |
| 4588 | if (!BaseTInfo) { |
| 4589 | AnyErrors = true; |
| 4590 | break; |
| 4591 | } |
| 4592 | |
| 4593 | // Build the initializer. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4594 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4595 | BaseTInfo, TempInit.get(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4596 | New->getParent(), |
| 4597 | SourceLocation()); |
| 4598 | if (NewInit.isInvalid()) { |
| 4599 | AnyErrors = true; |
| 4600 | break; |
| 4601 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4602 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4603 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4604 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4605 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4606 | continue; |
| 4607 | } |
| 4608 | |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4609 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4610 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4611 | /*CXXDirectInit=*/true); |
| 4612 | if (TempInit.isInvalid()) { |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4613 | AnyErrors = true; |
| 4614 | continue; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4615 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4616 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4617 | MemInitResult NewInit; |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4618 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4619 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4620 | TemplateArgs, |
| 4621 | Init->getSourceLocation(), |
| 4622 | New->getDeclName()); |
| 4623 | if (!TInfo) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4624 | AnyErrors = true; |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4625 | New->setInvalidDecl(); |
| 4626 | continue; |
| 4627 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4629 | if (Init->isBaseInitializer()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4630 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4631 | New->getParent(), EllipsisLoc); |
| 4632 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4633 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4634 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4635 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4636 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4637 | Init->getMemberLocation(), |
| 4638 | Init->getMember(), |
| 4639 | TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4640 | if (!Member) { |
| 4641 | AnyErrors = true; |
| 4642 | New->setInvalidDecl(); |
| 4643 | continue; |
| 4644 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4646 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4647 | Init->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4648 | } else if (Init->isIndirectMemberInitializer()) { |
| 4649 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4650 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4651 | Init->getMemberLocation(), |
| 4652 | Init->getIndirectMember(), TemplateArgs)); |
| 4653 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4654 | if (!IndirectMember) { |
| 4655 | AnyErrors = true; |
| 4656 | New->setInvalidDecl(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4657 | continue; |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4658 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4659 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4660 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4661 | Init->getSourceLocation()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4664 | if (NewInit.isInvalid()) { |
| 4665 | AnyErrors = true; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4666 | New->setInvalidDecl(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4667 | } else { |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4668 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4669 | } |
| 4670 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4671 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4672 | // Assign all the initializers to the new constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4673 | ActOnMemInitializers(New, |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4674 | /*FIXME: ColonLoc */ |
| 4675 | SourceLocation(), |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4676 | NewInits, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4677 | AnyErrors); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4680 | // TODO: this could be templated if the various decl types used the |
| 4681 | // same method name. |
| 4682 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4683 | ClassTemplateDecl *Instance) { |
| 4684 | Pattern = Pattern->getCanonicalDecl(); |
| 4685 | |
| 4686 | do { |
| 4687 | Instance = Instance->getCanonicalDecl(); |
| 4688 | if (Pattern == Instance) return true; |
| 4689 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4690 | } while (Instance); |
| 4691 | |
| 4692 | return false; |
| 4693 | } |
| 4694 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4695 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 4696 | FunctionTemplateDecl *Instance) { |
| 4697 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4699 | do { |
| 4700 | Instance = Instance->getCanonicalDecl(); |
| 4701 | if (Pattern == Instance) return true; |
| 4702 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4703 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4704 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4705 | return false; |
| 4706 | } |
| 4707 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4708 | static bool |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4709 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 4710 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4711 | Pattern |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4712 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 4713 | do { |
| 4714 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 4715 | Instance->getCanonicalDecl()); |
| 4716 | if (Pattern == Instance) |
| 4717 | return true; |
| 4718 | Instance = Instance->getInstantiatedFromMember(); |
| 4719 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4721 | return false; |
| 4722 | } |
| 4723 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4724 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 4725 | CXXRecordDecl *Instance) { |
| 4726 | Pattern = Pattern->getCanonicalDecl(); |
| 4727 | |
| 4728 | do { |
| 4729 | Instance = Instance->getCanonicalDecl(); |
| 4730 | if (Pattern == Instance) return true; |
| 4731 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 4732 | } while (Instance); |
| 4733 | |
| 4734 | return false; |
| 4735 | } |
| 4736 | |
| 4737 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 4738 | FunctionDecl *Instance) { |
| 4739 | Pattern = Pattern->getCanonicalDecl(); |
| 4740 | |
| 4741 | do { |
| 4742 | Instance = Instance->getCanonicalDecl(); |
| 4743 | if (Pattern == Instance) return true; |
| 4744 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 4745 | } while (Instance); |
| 4746 | |
| 4747 | return false; |
| 4748 | } |
| 4749 | |
| 4750 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 4751 | EnumDecl *Instance) { |
| 4752 | Pattern = Pattern->getCanonicalDecl(); |
| 4753 | |
| 4754 | do { |
| 4755 | Instance = Instance->getCanonicalDecl(); |
| 4756 | if (Pattern == Instance) return true; |
| 4757 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 4758 | } while (Instance); |
| 4759 | |
| 4760 | return false; |
| 4761 | } |
| 4762 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4763 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 4764 | UsingShadowDecl *Instance, |
| 4765 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4766 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 4767 | Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4768 | } |
| 4769 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4770 | static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4771 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4772 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4775 | template<typename T> |
| 4776 | static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, |
| 4777 | ASTContext &Ctx) { |
| 4778 | // An unresolved using declaration can instantiate to an unresolved using |
| 4779 | // declaration, or to a using declaration or a using declaration pack. |
| 4780 | // |
| 4781 | // Multiple declarations can claim to be instantiated from an unresolved |
| 4782 | // using declaration if it's a pack expansion. We want the UsingPackDecl |
| 4783 | // in that case, not the individual UsingDecls within the pack. |
| 4784 | bool OtherIsPackExpansion; |
| 4785 | NamedDecl *OtherFrom; |
| 4786 | if (auto *OtherUUD = dyn_cast<T>(Other)) { |
| 4787 | OtherIsPackExpansion = OtherUUD->isPackExpansion(); |
| 4788 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); |
| 4789 | } else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { |
| 4790 | OtherIsPackExpansion = true; |
| 4791 | OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); |
| 4792 | } else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { |
| 4793 | OtherIsPackExpansion = false; |
| 4794 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); |
| 4795 | } else { |
| 4796 | return false; |
| 4797 | } |
| 4798 | return Pattern->isPackExpansion() == OtherIsPackExpansion && |
| 4799 | declaresSameEntity(OtherFrom, Pattern); |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4802 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 4803 | VarDecl *Instance) { |
| 4804 | assert(Instance->isStaticDataMember()); |
| 4805 | |
| 4806 | Pattern = Pattern->getCanonicalDecl(); |
| 4807 | |
| 4808 | do { |
| 4809 | Instance = Instance->getCanonicalDecl(); |
| 4810 | if (Pattern == Instance) return true; |
| 4811 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 4812 | } while (Instance); |
| 4813 | |
| 4814 | return false; |
| 4815 | } |
| 4816 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4817 | // Other is the prospective instantiation |
| 4818 | // D is the prospective pattern |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4819 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4820 | if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) |
| 4821 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4822 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4823 | if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) |
| 4824 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4825 | |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 4826 | if (D->getKind() != Other->getKind()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4827 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4828 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4829 | if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4830 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4832 | if (auto *Function = dyn_cast<FunctionDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4833 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4834 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4835 | if (auto *Enum = dyn_cast<EnumDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4836 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4837 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4838 | if (auto *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4839 | if (Var->isStaticDataMember()) |
| 4840 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 4841 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4842 | if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4843 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 4844 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4845 | if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4846 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 4847 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4848 | if (auto *PartialSpec = |
| 4849 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4850 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 4851 | PartialSpec); |
| 4852 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4853 | if (auto *Field = dyn_cast<FieldDecl>(Other)) { |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4854 | if (!Field->getDeclName()) { |
| 4855 | // This is an unnamed field. |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4856 | return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 4857 | cast<FieldDecl>(D)); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4858 | } |
| 4859 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4860 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4861 | if (auto *Using = dyn_cast<UsingDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4862 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 4863 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4864 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4865 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 4866 | |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 4867 | return D->getDeclName() && |
| 4868 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | template<typename ForwardIterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4872 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4873 | NamedDecl *D, |
| 4874 | ForwardIterator first, |
| 4875 | ForwardIterator last) { |
| 4876 | for (; first != last; ++first) |
| 4877 | if (isInstantiationOf(Ctx, D, *first)) |
| 4878 | return cast<NamedDecl>(*first); |
| 4879 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4880 | return nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4883 | /// Finds the instantiation of the given declaration context |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4884 | /// within the current instantiation. |
| 4885 | /// |
| 4886 | /// \returns NULL if there was an error |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4887 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4888 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4889 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 4890 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs, true); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4891 | return cast_or_null<DeclContext>(ID); |
| 4892 | } else return DC; |
| 4893 | } |
| 4894 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4895 | /// Find the instantiation of the given declaration within the |
Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 4896 | /// current instantiation. |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4897 | /// |
| 4898 | /// This routine is intended to be used when \p D is a declaration |
| 4899 | /// referenced from within a template, that needs to mapped into the |
| 4900 | /// corresponding declaration within an instantiation. For example, |
| 4901 | /// given: |
| 4902 | /// |
| 4903 | /// \code |
| 4904 | /// template<typename T> |
| 4905 | /// struct X { |
| 4906 | /// enum Kind { |
| 4907 | /// KnownValue = sizeof(T) |
| 4908 | /// }; |
| 4909 | /// |
| 4910 | /// bool getKind() const { return KnownValue; } |
| 4911 | /// }; |
| 4912 | /// |
| 4913 | /// template struct X<int>; |
| 4914 | /// \endcode |
| 4915 | /// |
Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 4916 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the |
| 4917 | /// \p EnumConstantDecl for \p KnownValue (which refers to |
| 4918 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation |
| 4919 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs |
| 4920 | /// this mapping from within the instantiation of <tt>X<int></tt>. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4921 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 4922 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 4923 | bool FindingInstantiatedContext) { |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4924 | DeclContext *ParentDC = D->getDeclContext(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4925 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4926 | // parameters (p below) can have their ParentDC set to the translation-unit |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4927 | // - 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] | 4928 | // is Dependent or/and a FunctionOrMethod. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4929 | // For e.g. this code, during Template argument deduction tries to |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4930 | // 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] | 4931 | // the translation unit. |
| 4932 | // 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] | 4933 | // float baz(float(*)()) { return 0.0; } |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4934 | // Foo(baz); |
| 4935 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 4936 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 4937 | // For now: |
| 4938 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 4939 | // whose type is not instantiation dependent, do nothing to the decl |
| 4940 | // - otherwise find its instantiated decl. |
| 4941 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 4942 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 4943 | return D; |
Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 4944 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 4945 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 4946 | ((ParentDC->isFunctionOrMethod() || |
| 4947 | isa<OMPDeclareReductionDecl>(ParentDC)) && |
| 4948 | ParentDC->isDependentContext()) || |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4949 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4950 | // D is a local of some kind. Look into the map of local |
| 4951 | // declarations to their instantiations. |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 4952 | if (CurrentInstantiationScope) { |
| 4953 | if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 4954 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 4955 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4956 | |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 4957 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 4958 | assert(PackIdx != -1 && |
| 4959 | "found declaration pack but not pack expanding"); |
| 4960 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 4961 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 4962 | } |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 4965 | // If we're performing a partial substitution during template argument |
| 4966 | // deduction, we may not have values for template parameters yet. They |
| 4967 | // just map to themselves. |
| 4968 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 4969 | isa<TemplateTemplateParmDecl>(D)) |
| 4970 | return D; |
| 4971 | |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4972 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4973 | return nullptr; |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4974 | |
Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 4975 | // Normally this function only searches for already instantiated declaration |
| 4976 | // however we have to make an exclusion for local types used before |
| 4977 | // definition as in the code: |
| 4978 | // |
| 4979 | // template<typename T> void f1() { |
| 4980 | // void g1(struct x1); |
| 4981 | // struct x1 {}; |
| 4982 | // } |
| 4983 | // |
| 4984 | // In this case instantiation of the type of 'g1' requires definition of |
| 4985 | // 'x1', which is defined later. Error recovery may produce an enum used |
| 4986 | // before definition. In these cases we need to instantiate relevant |
| 4987 | // declarations here. |
| 4988 | bool NeedInstantiate = false; |
| 4989 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 4990 | NeedInstantiate = RD->isLocalClass(); |
| 4991 | else |
| 4992 | NeedInstantiate = isa<EnumDecl>(D); |
| 4993 | if (NeedInstantiate) { |
Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 4994 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 4995 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 4996 | return cast<TypeDecl>(Inst); |
| 4997 | } |
| 4998 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4999 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 5000 | // been found yet. Lazily instantiate it and return it now. |
| 5001 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5002 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5003 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5004 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5005 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5006 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5007 | return cast<LabelDecl>(Inst); |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5008 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5009 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5010 | // For variable template specializations, update those that are still |
| 5011 | // type-dependent. |
| 5012 | if (VarTemplateSpecializationDecl *VarSpec = |
| 5013 | dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 5014 | bool InstantiationDependent = false; |
| 5015 | const TemplateArgumentListInfo &VarTemplateArgs = |
| 5016 | VarSpec->getTemplateArgsInfo(); |
| 5017 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
| 5018 | VarTemplateArgs, InstantiationDependent)) |
| 5019 | D = cast<NamedDecl>( |
| 5020 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); |
| 5021 | return D; |
| 5022 | } |
| 5023 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5024 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 5025 | if (!Record->isDependentContext()) |
| 5026 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5027 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5028 | // Determine whether this record is the "templated" declaration describing |
| 5029 | // a class template or class template partial specialization. |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5030 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5031 | if (ClassTemplate) |
| 5032 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 5033 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5034 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 5035 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5036 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5037 | // Walk the current context to find either the record or an instantiation of |
| 5038 | // it. |
| 5039 | DeclContext *DC = CurContext; |
| 5040 | while (!DC->isFileContext()) { |
| 5041 | // If we're performing substitution while we're inside the template |
| 5042 | // definition, we'll find our own context. We're done. |
| 5043 | if (DC->Equals(Record)) |
| 5044 | return Record; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5045 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5046 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 5047 | // Check whether we're in the process of instantiating a class template |
| 5048 | // specialization of the template we're mapping. |
| 5049 | if (ClassTemplateSpecializationDecl *InstSpec |
| 5050 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 5051 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 5052 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 5053 | return InstRecord; |
| 5054 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5056 | // Check whether we're in the process of instantiating a member class. |
| 5057 | if (isInstantiationOf(Record, InstRecord)) |
| 5058 | return InstRecord; |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5059 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5060 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5061 | // Move to the outer template scope. |
| 5062 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 5063 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 5064 | DC = FD->getLexicalDeclContext(); |
| 5065 | continue; |
| 5066 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5067 | // An implicit deduction guide acts as if it's within the class template |
| 5068 | // specialization described by its name and first N template params. |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5069 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FD); |
| 5070 | if (Guide && Guide->isImplicit()) { |
| 5071 | TemplateDecl *TD = Guide->getDeducedTemplate(); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5072 | // Convert the arguments to an "as-written" list. |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5073 | TemplateArgumentListInfo Args(Loc, Loc); |
Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5074 | for (TemplateArgument Arg : TemplateArgs.getInnermost().take_front( |
| 5075 | TD->getTemplateParameters()->size())) { |
| 5076 | ArrayRef<TemplateArgument> Unpacked(Arg); |
| 5077 | if (Arg.getKind() == TemplateArgument::Pack) |
| 5078 | Unpacked = Arg.pack_elements(); |
| 5079 | for (TemplateArgument UnpackedArg : Unpacked) |
| 5080 | Args.addArgument( |
| 5081 | getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); |
| 5082 | } |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5083 | QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); |
| 5084 | if (T.isNull()) |
| 5085 | return nullptr; |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5086 | auto *SubstRecord = T->getAsCXXRecordDecl(); |
| 5087 | assert(SubstRecord && "class template id not a class type?"); |
| 5088 | // Check that this template-id names the primary template and not a |
| 5089 | // partial or explicit specialization. (In the latter cases, it's |
| 5090 | // meaningless to attempt to find an instantiation of D within the |
| 5091 | // specialization.) |
| 5092 | // FIXME: The standard doesn't say what should happen here. |
Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5093 | if (FindingInstantiatedContext && |
| 5094 | usesPartialOrExplicitSpecialization( |
| 5095 | Loc, cast<ClassTemplateSpecializationDecl>(SubstRecord))) { |
Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5096 | Diag(Loc, diag::err_specialization_not_primary_template) |
| 5097 | << T << (SubstRecord->getTemplateSpecializationKind() == |
| 5098 | TSK_ExplicitSpecialization); |
| 5099 | return nullptr; |
| 5100 | } |
| 5101 | DC = SubstRecord; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5102 | continue; |
| 5103 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5104 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5106 | DC = DC->getParent(); |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5107 | } |
Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5109 | // Fall through to deal with other dependent record types (e.g., |
| 5110 | // anonymous unions in class templates). |
| 5111 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5112 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5113 | if (!ParentDC->isDependentContext()) |
| 5114 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5115 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5116 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | if (!ParentDC) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5118 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5119 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5120 | if (ParentDC != D->getDeclContext()) { |
| 5121 | // We performed some kind of instantiation in the parent context, |
| 5122 | // so now we need to look into the instantiated parent context to |
| 5123 | // find the instantiation of the declaration D. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5124 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5125 | // If our context used to be dependent, we may need to instantiate |
| 5126 | // it before performing lookup into that context. |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5127 | bool IsBeingInstantiated = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5128 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5129 | if (!Spec->isDependentContext()) { |
| 5130 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5131 | const RecordType *Tag = T->getAs<RecordType>(); |
| 5132 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5133 | if (Tag->isBeingDefined()) |
| 5134 | IsBeingInstantiated = true; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5135 | if (!Tag->isBeingDefined() && |
| 5136 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5137 | return nullptr; |
Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 5138 | |
| 5139 | ParentDC = Tag->getDecl(); |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5140 | } |
| 5141 | } |
| 5142 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5143 | NamedDecl *Result = nullptr; |
Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5144 | // FIXME: If the name is a dependent name, this lookup won't necessarily |
| 5145 | // find it. Does that ever matter? |
Akira Hatanaka | 59e3b43 | 2017-01-31 19:53:32 +0000 | [diff] [blame] | 5146 | if (auto Name = D->getDeclName()) { |
| 5147 | DeclarationNameInfo NameInfo(Name, D->getLocation()); |
| 5148 | Name = SubstDeclarationNameInfo(NameInfo, TemplateArgs).getName(); |
| 5149 | if (!Name) |
| 5150 | return nullptr; |
| 5151 | DeclContext::lookup_result Found = ParentDC->lookup(Name); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 5152 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5153 | } else { |
| 5154 | // Since we don't have a name for the entity we're looking for, |
| 5155 | // our only option is to walk through all of the declarations to |
| 5156 | // find that name. This will occur in a few cases: |
| 5157 | // |
| 5158 | // - anonymous struct/union within a template |
| 5159 | // - unnamed class/struct/union/enum within a template |
| 5160 | // |
| 5161 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5162 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5163 | ParentDC->decls_begin(), |
| 5164 | ParentDC->decls_end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5166 | |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5167 | if (!Result) { |
| 5168 | if (isa<UsingShadowDecl>(D)) { |
| 5169 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 5170 | } else if (Diags.hasErrorOccurred()) { |
| 5171 | // We've already complained about something, so most likely this |
| 5172 | // declaration failed to instantiate. There's no point in complaining |
| 5173 | // further, since this is normal in invalid code. |
| 5174 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5175 | // The class in which this member exists is currently being |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5176 | // instantiated, and we haven't gotten around to instantiating this |
| 5177 | // member yet. This can happen when the code uses forward declarations |
| 5178 | // of member classes, and introduces ordering dependencies via |
| 5179 | // template instantiation. |
| 5180 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 5181 | << D->getDeclName() |
| 5182 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 5183 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 5184 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 5185 | // This enumeration constant was found when the template was defined, |
| 5186 | // but can't be found in the instantiation. This can happen if an |
| 5187 | // unscoped enumeration member is explicitly specialized. |
| 5188 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 5189 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 5190 | TemplateArgs)); |
| 5191 | assert(Spec->getTemplateSpecializationKind() == |
| 5192 | TSK_ExplicitSpecialization); |
| 5193 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 5194 | << D->getDeclName() |
| 5195 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 5196 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 5197 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5198 | } else { |
| 5199 | // We should have found something, but didn't. |
| 5200 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 5201 | } |
| 5202 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5203 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5204 | D = Result; |
| 5205 | } |
| 5206 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5207 | return D; |
| 5208 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5209 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5210 | /// Performs template instantiation for all implicit template |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5211 | /// instantiations we have seen until this point. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 5212 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5213 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5214 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5215 | PendingImplicitInstantiation Inst; |
| 5216 | |
| 5217 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5218 | Inst = PendingInstantiations.front(); |
| 5219 | PendingInstantiations.pop_front(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5220 | } else { |
| 5221 | Inst = PendingLocalImplicitInstantiations.front(); |
| 5222 | PendingLocalImplicitInstantiations.pop_front(); |
| 5223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5224 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5225 | // Instantiate function definitions |
| 5226 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5227 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 5228 | TSK_ExplicitInstantiationDefinition; |
Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 5229 | if (Function->isMultiVersion()) { |
| 5230 | getASTContext().forEachMultiversionedFunctionVersion( |
| 5231 | Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) { |
| 5232 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true, |
| 5233 | DefinitionRequired, true); |
| 5234 | if (CurFD->isDefined()) |
| 5235 | CurFD->setInstantiationIsPending(false); |
| 5236 | }); |
| 5237 | } else { |
| 5238 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true, |
| 5239 | DefinitionRequired, true); |
| 5240 | if (Function->isDefined()) |
| 5241 | Function->setInstantiationIsPending(false); |
| 5242 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5243 | continue; |
| 5244 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5245 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5246 | // Instantiate variable definitions |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5247 | VarDecl *Var = cast<VarDecl>(Inst.first); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5248 | |
| 5249 | assert((Var->isStaticDataMember() || |
| 5250 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 5251 | "Not a static data member, nor a variable template" |
| 5252 | " specialization?"); |
Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 5253 | |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5254 | // Don't try to instantiate declarations if the most recent redeclaration |
| 5255 | // is invalid. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5256 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5257 | continue; |
| 5258 | |
| 5259 | // Check if the most recent declaration has changed the specialization kind |
| 5260 | // and removed the need for implicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5261 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5262 | case TSK_Undeclared: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5263 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5264 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5265 | case TSK_ExplicitSpecialization: |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5266 | continue; // No longer need to instantiate this type. |
| 5267 | case TSK_ExplicitInstantiationDefinition: |
| 5268 | // We only need an instantiation if the pending instantiation *is* the |
| 5269 | // explicit instantiation. |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5270 | if (Var != Var->getMostRecentDecl()) |
| 5271 | continue; |
| 5272 | break; |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5273 | case TSK_ImplicitInstantiation: |
| 5274 | break; |
| 5275 | } |
| 5276 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 5277 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5278 | "instantiating variable definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5279 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 5280 | TSK_ExplicitInstantiationDefinition; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5281 | |
| 5282 | // Instantiate static data member definitions or variable template |
| 5283 | // specializations. |
| 5284 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 5285 | DefinitionRequired, true); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5286 | } |
| 5287 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5288 | |
| 5289 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 5290 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 5291 | for (auto DD : Pattern->ddiags()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5292 | switch (DD->getKind()) { |
| 5293 | case DependentDiagnostic::Access: |
| 5294 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 5295 | break; |
| 5296 | } |
| 5297 | } |
| 5298 | } |