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" |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
| 24 | #include "clang/Sema/Template.h" |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 28 | static bool isDeclWithinFunction(const Decl *D) { |
| 29 | const DeclContext *DC = D->getDeclContext(); |
| 30 | if (DC->isFunctionOrMethod()) |
| 31 | return true; |
| 32 | |
| 33 | if (DC->isRecord()) |
| 34 | return cast<CXXRecordDecl>(DC)->isLocalClass(); |
| 35 | |
| 36 | return false; |
| 37 | } |
| 38 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 39 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 40 | DeclaratorDecl *NewDecl) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 41 | if (!OldDecl->getQualifierLoc()) |
| 42 | return false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 44 | NestedNameSpecifierLoc NewQualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 45 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 46 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 48 | if (!NewQualifierLoc) |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 49 | return true; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 51 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 56 | TagDecl *NewDecl) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 57 | if (!OldDecl->getQualifierLoc()) |
| 58 | return false; |
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 | NestedNameSpecifierLoc NewQualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 61 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 62 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 63 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 64 | if (!NewQualifierLoc) |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 65 | return true; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 67 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 71 | // Include attribute instantiation code. |
| 72 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 73 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 74 | static void instantiateDependentAlignedAttr( |
| 75 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 76 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 77 | if (Aligned->isAlignmentExpr()) { |
| 78 | // The alignment expression is a constant expression. |
| 79 | EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated); |
| 80 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 81 | if (!Result.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 82 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 83 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 84 | } else { |
| 85 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 86 | TemplateArgs, Aligned->getLocation(), |
| 87 | DeclarationName()); |
| 88 | if (Result) |
| 89 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 90 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static void instantiateDependentAlignedAttr( |
| 95 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 96 | const AlignedAttr *Aligned, Decl *New) { |
| 97 | if (!Aligned->isPackExpansion()) { |
| 98 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 103 | if (Aligned->isAlignmentExpr()) |
| 104 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 105 | Unexpanded); |
| 106 | else |
| 107 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 108 | Unexpanded); |
| 109 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 110 | |
| 111 | // Determine whether we can expand this attribute pack yet. |
| 112 | bool Expand = true, RetainExpansion = false; |
| 113 | Optional<unsigned> NumExpansions; |
| 114 | // FIXME: Use the actual location of the ellipsis. |
| 115 | SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 116 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 117 | Unexpanded, TemplateArgs, Expand, |
| 118 | RetainExpansion, NumExpansions)) |
| 119 | return; |
| 120 | |
| 121 | if (!Expand) { |
| 122 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 123 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 124 | } else { |
| 125 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 126 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 127 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 132 | static void instantiateDependentEnableIfAttr( |
| 133 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 134 | const EnableIfAttr *A, const Decl *Tmpl, Decl *New) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 135 | Expr *Cond = nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 136 | { |
| 137 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 138 | ExprResult Result = S.SubstExpr(A->getCond(), TemplateArgs); |
| 139 | if (Result.isInvalid()) |
| 140 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 141 | Cond = Result.getAs<Expr>(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 142 | } |
| 143 | if (A->getCond()->isTypeDependent() && !Cond->isTypeDependent()) { |
| 144 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 145 | if (Converted.isInvalid()) |
| 146 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 147 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 151 | if (A->getCond()->isValueDependent() && !Cond->isValueDependent() && |
| 152 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(Tmpl), |
| 153 | Diags)) { |
| 154 | S.Diag(A->getLocation(), diag::err_enable_if_never_constant_expr); |
| 155 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 156 | S.Diag(Diags[I].first, Diags[I].second); |
| 157 | return; |
| 158 | } |
| 159 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 160 | EnableIfAttr *EIA = new (S.getASTContext()) |
| 161 | EnableIfAttr(A->getLocation(), S.getASTContext(), Cond, |
| 162 | A->getMessage(), |
| 163 | A->getSpellingListIndex()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 164 | New->addAttr(EIA); |
| 165 | } |
| 166 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 167 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 168 | const Decl *Tmpl, Decl *New, |
| 169 | LateInstantiatedAttrVec *LateAttrs, |
| 170 | LocalInstantiationScope *OuterMostScope) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 171 | for (const auto *TmplAttr : Tmpl->attrs()) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 172 | // FIXME: This should be generalized to more than just the AlignedAttr. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 173 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 174 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 175 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 176 | continue; |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 179 | const EnableIfAttr *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr); |
| 180 | if (EnableIf && EnableIf->getCond()->isValueDependent()) { |
| 181 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
| 182 | New); |
| 183 | continue; |
| 184 | } |
| 185 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 186 | assert(!TmplAttr->isPackExpansion()); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 187 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 188 | // Late parsed attributes must be instantiated and attached after the |
| 189 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 190 | LocalInstantiationScope *Saved = nullptr; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 191 | if (CurrentInstantiationScope) |
| 192 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 193 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 194 | } else { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 195 | // Allow 'this' within late-parsed attributes. |
| 196 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 197 | CXXRecordDecl *ThisContext = |
| 198 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| 199 | CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0, |
| 200 | ND && ND->isCXXInstanceMember()); |
| 201 | |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 202 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 203 | *this, TemplateArgs); |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 204 | if (NewAttr) |
| 205 | New->addAttr(NewAttr); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 206 | } |
Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 210 | Decl * |
| 211 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 212 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | Decl * |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 216 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 217 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 218 | D->getIdentifier()); |
| 219 | Owner->addDecl(Inst); |
| 220 | return Inst; |
| 221 | } |
| 222 | |
| 223 | Decl * |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 224 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 225 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 226 | } |
| 227 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 228 | Decl * |
| 229 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 230 | NamespaceAliasDecl *Inst |
| 231 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 232 | D->getNamespaceLoc(), |
| 233 | D->getAliasLoc(), |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 234 | D->getIdentifier(), |
| 235 | D->getQualifierLoc(), |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 236 | D->getTargetNameLoc(), |
| 237 | D->getNamespace()); |
| 238 | Owner->addDecl(Inst); |
| 239 | return Inst; |
| 240 | } |
| 241 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 242 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 243 | bool IsTypeAlias) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 244 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 245 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 246 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 247 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 248 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 249 | D->getLocation(), D->getDeclName()); |
| 250 | if (!DI) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 251 | Invalid = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 252 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 253 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 254 | } else { |
| 255 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 256 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 258 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 259 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 260 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 261 | // semantics. See LWG issue 2141 for more information on the bug. |
| 262 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 263 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 264 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 265 | DT->isReferenceType() && |
| 266 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 267 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 268 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
| 269 | SemaRef.getSourceManager().isInSystemHeader(D->getLocStart())) |
| 270 | // Fold it to the (non-reference) type which g++ would have produced. |
| 271 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 272 | DI->getType().getNonReferenceType()); |
| 273 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 274 | // Create the new typedef |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 275 | TypedefNameDecl *Typedef; |
| 276 | if (IsTypeAlias) |
| 277 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 278 | D->getLocation(), D->getIdentifier(), DI); |
| 279 | else |
| 280 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 281 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 282 | if (Invalid) |
| 283 | Typedef->setInvalidDecl(); |
| 284 | |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 285 | // If the old typedef was the name for linkage purposes of an anonymous |
| 286 | // tag decl, re-establish that relationship for the new typedef. |
| 287 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 288 | TagDecl *oldTag = oldTagType->getDecl(); |
Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 289 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 290 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 291 | assert(!newTag->hasNameForLinkage()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 292 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 293 | } |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 294 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 296 | if (TypedefNameDecl *Prev = D->getPreviousDecl()) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 297 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 298 | TemplateArgs); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 299 | if (!InstPrev) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 300 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 301 | |
Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 302 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 303 | |
| 304 | // If the typedef types are not identical, reject them. |
| 305 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 306 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 307 | Typedef->setPreviousDecl(InstPrevTypedef); |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 308 | } |
| 309 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 310 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 311 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 312 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 314 | return Typedef; |
| 315 | } |
| 316 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 317 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 318 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
| 319 | Owner->addDecl(Typedef); |
| 320 | return Typedef; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 324 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
| 325 | Owner->addDecl(Typedef); |
| 326 | return Typedef; |
| 327 | } |
| 328 | |
| 329 | Decl * |
| 330 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 331 | // Create a local instantiation scope for this type alias template, which |
| 332 | // will contain the instantiations of the template parameters. |
| 333 | LocalInstantiationScope Scope(SemaRef); |
| 334 | |
| 335 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 336 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 337 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 338 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 339 | |
| 340 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 341 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 342 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 343 | if (Pattern->getPreviousDecl()) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 344 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 345 | if (!Found.empty()) { |
| 346 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 351 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 352 | if (!AliasInst) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 353 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 354 | |
| 355 | TypeAliasTemplateDecl *Inst |
| 356 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 357 | D->getDeclName(), InstParams, AliasInst); |
| 358 | if (PrevAliasTemplate) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 359 | Inst->setPreviousDecl(PrevAliasTemplate); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 360 | |
| 361 | Inst->setAccess(D->getAccess()); |
| 362 | |
| 363 | if (!PrevAliasTemplate) |
| 364 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 365 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 366 | Owner->addDecl(Inst); |
| 367 | |
| 368 | return Inst; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 371 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 372 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 375 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
| 376 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 378 | // If this is the variable for an anonymous struct or union, |
| 379 | // instantiate the anonymous struct/union type first. |
| 380 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 381 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 382 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 383 | return nullptr; |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 384 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 385 | // Do substitution on the type of the declaration |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 386 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), |
John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 387 | TemplateArgs, |
| 388 | D->getTypeSpecStartLoc(), |
| 389 | D->getDeclName()); |
| 390 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 391 | return nullptr; |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 393 | if (DI->getType()->isFunctionType()) { |
| 394 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 395 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 396 | return nullptr; |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 397 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 398 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 399 | DeclContext *DC = Owner; |
| 400 | if (D->isLocalExternDecl()) |
| 401 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 402 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 403 | // Build the instantiated declaration. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 404 | VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 405 | D->getLocation(), D->getIdentifier(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 406 | DI->getType(), DI, D->getStorageClass()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 408 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 409 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 410 | SemaRef.inferObjCARCLifetime(Var)) |
| 411 | Var->setInvalidDecl(); |
| 412 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 413 | // Substitute the nested name specifier, if any. |
| 414 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 415 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 417 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 418 | StartingScope, InstantiatingVarTemplate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 419 | |
| 420 | if (D->isNRVOVariable()) { |
| 421 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
| 422 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, false)) |
| 423 | Var->setNRVOVariable(true); |
| 424 | } |
| 425 | |
Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 426 | Var->setImplicit(D->isImplicit()); |
| 427 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 428 | return Var; |
| 429 | } |
| 430 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 431 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 432 | AccessSpecDecl* AD |
| 433 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 434 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 435 | Owner->addHiddenDecl(AD); |
| 436 | return AD; |
| 437 | } |
| 438 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 439 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 440 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 441 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 442 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 443 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 444 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 445 | D->getLocation(), D->getDeclName()); |
| 446 | if (!DI) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 447 | DI = D->getTypeSourceInfo(); |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 448 | Invalid = true; |
| 449 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 450 | // C++ [temp.arg.type]p3: |
| 451 | // If a declaration acquires a function type through a type |
| 452 | // dependent on a template-parameter and this causes a |
| 453 | // declaration that does not use the syntactic form of a |
| 454 | // function declarator to have function type, the program is |
| 455 | // ill-formed. |
| 456 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 457 | << DI->getType(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 458 | Invalid = true; |
| 459 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 460 | } else { |
| 461 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | Expr *BitWidth = D->getBitWidth(); |
| 465 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 466 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 467 | else if (BitWidth) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 468 | // The bit-width expression is a constant expression. |
| 469 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 470 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 472 | ExprResult InstantiatedBitWidth |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 473 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 474 | if (InstantiatedBitWidth.isInvalid()) { |
| 475 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 476 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 477 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 478 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 479 | } |
| 480 | |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 481 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 482 | DI->getType(), DI, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | cast<RecordDecl>(Owner), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 484 | D->getLocation(), |
| 485 | D->isMutable(), |
| 486 | BitWidth, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 487 | D->getInClassInitStyle(), |
Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 488 | D->getInnerLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 489 | D->getAccess(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 490 | nullptr); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 491 | if (!Field) { |
| 492 | cast<Decl>(Owner)->setInvalidDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 493 | return nullptr; |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 494 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 496 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 497 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 498 | if (Field->hasAttrs()) |
| 499 | SemaRef.CheckAlignasUnderalignment(Field); |
| 500 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 501 | if (Invalid) |
| 502 | Field->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 504 | if (!Field->getDeclName()) { |
| 505 | // Keep track of where this decl came from. |
| 506 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 507 | } |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 508 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 509 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 510 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 511 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 512 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 514 | Field->setImplicit(D->isImplicit()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 515 | Field->setAccess(D->getAccess()); |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 516 | Owner->addDecl(Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 517 | |
| 518 | return Field; |
| 519 | } |
| 520 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 521 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 522 | bool Invalid = false; |
| 523 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 524 | |
| 525 | if (DI->getType()->isVariablyModifiedType()) { |
| 526 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 527 | << D; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 528 | Invalid = true; |
| 529 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 530 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 531 | D->getLocation(), D->getDeclName()); |
| 532 | if (!DI) { |
| 533 | DI = D->getTypeSourceInfo(); |
| 534 | Invalid = true; |
| 535 | } else if (DI->getType()->isFunctionType()) { |
| 536 | // C++ [temp.arg.type]p3: |
| 537 | // If a declaration acquires a function type through a type |
| 538 | // dependent on a template-parameter and this causes a |
| 539 | // declaration that does not use the syntactic form of a |
| 540 | // function declarator to have function type, the program is |
| 541 | // ill-formed. |
| 542 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 543 | << DI->getType(); |
| 544 | Invalid = true; |
| 545 | } |
| 546 | } else { |
| 547 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 548 | } |
| 549 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 550 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 551 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
| 552 | DI, D->getLocStart(), D->getGetterId(), D->getSetterId()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 553 | |
| 554 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 555 | StartingScope); |
| 556 | |
| 557 | if (Invalid) |
| 558 | Property->setInvalidDecl(); |
| 559 | |
| 560 | Property->setAccess(D->getAccess()); |
| 561 | Owner->addDecl(Property); |
| 562 | |
| 563 | return Property; |
| 564 | } |
| 565 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 566 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 567 | NamedDecl **NamedChain = |
| 568 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 569 | |
| 570 | int i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 571 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 572 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 573 | TemplateArgs); |
| 574 | if (!Next) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 575 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 577 | NamedChain[i++] = Next; |
| 578 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 579 | |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 580 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 581 | IndirectFieldDecl* IndirectField |
| 582 | = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 583 | D->getIdentifier(), T, |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 584 | NamedChain, D->getChainingSize()); |
| 585 | |
| 586 | |
| 587 | IndirectField->setImplicit(D->isImplicit()); |
| 588 | IndirectField->setAccess(D->getAccess()); |
| 589 | Owner->addDecl(IndirectField); |
| 590 | return IndirectField; |
| 591 | } |
| 592 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 593 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 594 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 595 | // parameters into the pattern type and checking the result. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 596 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 597 | TypeSourceInfo *InstTy; |
| 598 | // If this is an unsupported friend, don't bother substituting template |
| 599 | // arguments into it. The actual type referred to won't be used by any |
| 600 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 601 | // same info for the instantiated friend. |
| 602 | if (D->isUnsupportedFriend()) { |
| 603 | InstTy = Ty; |
| 604 | } else { |
| 605 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 606 | D->getLocation(), DeclarationName()); |
| 607 | } |
| 608 | if (!InstTy) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 609 | return nullptr; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 610 | |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 611 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(), |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 612 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 613 | if (!FD) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 614 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 616 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 617 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 618 | Owner->addDecl(FD); |
| 619 | return FD; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 622 | NamedDecl *ND = D->getFriendDecl(); |
| 623 | assert(ND && "friend decl must be a decl or a type!"); |
| 624 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 625 | // All of the Visit implementations for the various potential friend |
| 626 | // declarations have to be carefully written to work for friend |
| 627 | // objects, with the most important detail being that the target |
| 628 | // decl should almost certainly not be placed in Owner. |
| 629 | Decl *NewND = Visit(ND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 630 | if (!NewND) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 632 | FriendDecl *FD = |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 633 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 634 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 635 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 636 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 637 | Owner->addDecl(FD); |
| 638 | return FD; |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 641 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 642 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 644 | // The expression in a static assertion is a constant expression. |
| 645 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 646 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 648 | ExprResult InstantiatedAssertExpr |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 649 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 650 | if (InstantiatedAssertExpr.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 651 | return nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 652 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 653 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 654 | InstantiatedAssertExpr.get(), |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 655 | D->getMessage(), |
| 656 | D->getRParenLoc(), |
| 657 | D->isFailed()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 661 | EnumDecl *PrevDecl = nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 662 | if (D->getPreviousDecl()) { |
| 663 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 664 | D->getPreviousDecl(), |
| 665 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 666 | if (!Prev) return nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 667 | PrevDecl = cast<EnumDecl>(Prev); |
| 668 | } |
| 669 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 670 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 671 | D->getLocation(), D->getIdentifier(), |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 672 | PrevDecl, D->isScoped(), |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 673 | D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 674 | if (D->isFixed()) { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 675 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 676 | // If we have type source information for the underlying type, it means it |
| 677 | // has been explicitly set by the user. Perform substitution on it before |
| 678 | // moving on. |
| 679 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 680 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 681 | DeclarationName()); |
| 682 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 683 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 684 | else |
| 685 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 686 | } else { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 687 | assert(!D->getIntegerType()->isDependentType() |
| 688 | && "Dependent type without type source info"); |
| 689 | Enum->setIntegerType(D->getIntegerType()); |
| 690 | } |
| 691 | } |
| 692 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 693 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 694 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 695 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 696 | Enum->setAccess(D->getAccess()); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 697 | // Forward the mangling number from the template to the instantiated decl. |
| 698 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 699 | if (SubstQualifier(D, Enum)) return nullptr; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 700 | Owner->addDecl(Enum); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 701 | |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 702 | EnumDecl *Def = D->getDefinition(); |
| 703 | if (Def && Def != D) { |
| 704 | // If this is an out-of-line definition of an enum member template, check |
| 705 | // that the underlying types match in the instantiation of both |
| 706 | // declarations. |
| 707 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 708 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 709 | QualType DefnUnderlying = |
| 710 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 711 | UnderlyingLoc, DeclarationName()); |
| 712 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
| 713 | DefnUnderlying, Enum); |
| 714 | } |
| 715 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 716 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 717 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 718 | // specialization causes the implicit instantiation of the declarations, but |
| 719 | // not the definitions of scoped member enumerations. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 720 | // |
| 721 | // DR1484 clarifies that enumeration definitions inside of a template |
| 722 | // declaration aren't considered entities that can be separately instantiated |
| 723 | // from the rest of the entity they are declared inside of. |
| 724 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 725 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 726 | InstantiateEnumDefinition(Enum, Def); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 727 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 728 | |
| 729 | return Enum; |
| 730 | } |
| 731 | |
| 732 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 733 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 734 | Enum->startDefinition(); |
| 735 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 736 | // Update the location to refer to the definition. |
| 737 | Enum->setLocation(Pattern->getLocation()); |
| 738 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 739 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 740 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 741 | EnumConstantDecl *LastEnumConst = nullptr; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 742 | for (auto *EC : Pattern->enumerators()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 743 | // The specified value for the enumerator. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 744 | ExprResult Value((Expr *)nullptr); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 745 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 746 | // The enumerator's value expression is a constant expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 748 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 750 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 751 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 752 | |
| 753 | // Drop the initial value and continue. |
| 754 | bool isInvalid = false; |
| 755 | if (Value.isInvalid()) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 756 | Value = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 757 | isInvalid = true; |
| 758 | } |
| 759 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | EnumConstantDecl *EnumConst |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 761 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 762 | EC->getLocation(), EC->getIdentifier(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 763 | Value.get()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 764 | |
| 765 | if (isInvalid) { |
| 766 | if (EnumConst) |
| 767 | EnumConst->setInvalidDecl(); |
| 768 | Enum->setInvalidDecl(); |
| 769 | } |
| 770 | |
| 771 | if (EnumConst) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 772 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 773 | |
John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 774 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 775 | Enum->addDecl(EnumConst); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 776 | Enumerators.push_back(EnumConst); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 777 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 778 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 779 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 780 | !Enum->isScoped()) { |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 781 | // If the enumeration is within a function or method, record the enum |
| 782 | // constant as a local. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 783 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 784 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 788 | // FIXME: Fixup LBraceLoc |
| 789 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), |
| 790 | Enum->getRBraceLoc(), Enum, |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 791 | Enumerators, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 792 | nullptr, nullptr); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 795 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 796 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 797 | } |
| 798 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 799 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 800 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 801 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 802 | // Create a local instantiation scope for this class template, which |
| 803 | // will contain the instantiations of the template parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 804 | LocalInstantiationScope Scope(SemaRef); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 805 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 806 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 808 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 809 | |
| 810 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 811 | |
| 812 | // Instantiate the qualifier. We have to do this first in case |
| 813 | // we're a friend declaration, because if we are then we need to put |
| 814 | // the new declaration in the appropriate context. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 815 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 816 | if (QualifierLoc) { |
| 817 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 818 | TemplateArgs); |
| 819 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 820 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 823 | CXXRecordDecl *PrevDecl = nullptr; |
| 824 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 826 | if (!isFriend && Pattern->getPreviousDecl()) { |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 827 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 828 | if (!Found.empty()) { |
| 829 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 830 | if (PrevClassTemplate) |
| 831 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 832 | } |
| 833 | } |
| 834 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 835 | // If this isn't a friend, then it's a member template, in which |
| 836 | // case we just want to build the instantiation in the |
| 837 | // specialization. If it is a friend, we want to build it in |
| 838 | // the appropriate context. |
| 839 | DeclContext *DC = Owner; |
| 840 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 841 | if (QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 842 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 843 | SS.Adopt(QualifierLoc); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 844 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 845 | if (!DC) return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 846 | } else { |
| 847 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 848 | Pattern->getDeclContext(), |
| 849 | TemplateArgs); |
| 850 | } |
| 851 | |
| 852 | // Look for a previous declaration of the template in the owning |
| 853 | // context. |
| 854 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| 855 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 856 | SemaRef.LookupQualifiedName(R, DC); |
| 857 | |
| 858 | if (R.isSingleResult()) { |
| 859 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 860 | if (PrevClassTemplate) |
| 861 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 862 | } |
| 863 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 864 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 865 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 866 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 867 | << QualifierLoc.getSourceRange(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 868 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 871 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 872 | if (PrevClassTemplate) { |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 873 | bool Complain = true; |
| 874 | |
| 875 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 876 | // template for struct std::tr1::__detail::_Map_base, where the |
| 877 | // template parameters of the friend declaration don't match the |
| 878 | // template parameters of the original declaration. In this one |
| 879 | // case, we don't complain about the ill-formed friend |
| 880 | // declaration. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 881 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 882 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 883 | DC->isNamespace() && |
| 884 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 885 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 886 | DeclContext *DCParent = DC->getParent(); |
| 887 | if (DCParent->isNamespace() && |
| 888 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 889 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 890 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 891 | Complain = false; |
| 892 | } |
| 893 | } |
| 894 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 895 | TemplateParameterList *PrevParams |
| 896 | = PrevClassTemplate->getTemplateParameters(); |
| 897 | |
| 898 | // Make sure the parameter lists match. |
| 899 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 900 | Complain, |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 901 | Sema::TPL_TemplateMatch)) { |
| 902 | if (Complain) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 903 | return nullptr; |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 904 | |
| 905 | AdoptedPreviousTemplateParams = true; |
| 906 | InstParams = PrevParams; |
| 907 | } |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 908 | |
| 909 | // Do some additional validation, then merge default arguments |
| 910 | // from the existing declarations. |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 911 | if (!AdoptedPreviousTemplateParams && |
| 912 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 913 | Sema::TPC_ClassTemplate)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 914 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 918 | CXXRecordDecl *RecordInst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 919 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 920 | Pattern->getLocStart(), Pattern->getLocation(), |
| 921 | Pattern->getIdentifier(), PrevDecl, |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 922 | /*DelayTypeCreation=*/true); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 924 | if (QualifierLoc) |
| 925 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 926 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 927 | ClassTemplateDecl *Inst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 928 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| 929 | D->getIdentifier(), InstParams, RecordInst, |
| 930 | PrevClassTemplate); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 931 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 932 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 933 | if (isFriend) { |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 934 | if (PrevClassTemplate) |
| 935 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 936 | else |
| 937 | Inst->setAccess(D->getAccess()); |
| 938 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 939 | Inst->setObjectOfFriendDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 940 | // TODO: do we want to track the instantiation progeny of this |
| 941 | // friend target decl? |
| 942 | } else { |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 943 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 944 | if (!PrevClassTemplate) |
| 945 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 946 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 948 | // Trigger creation of the type for the instantiation. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 949 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 950 | Inst->getInjectedClassNameSpecialization()); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 952 | // Finish handling of friends. |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 953 | if (isFriend) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 954 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 955 | Inst->setLexicalDeclContext(Owner); |
| 956 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 957 | return Inst; |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 958 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 959 | |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 960 | if (D->isOutOfLine()) { |
| 961 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 962 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 963 | } |
| 964 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 965 | Owner->addDecl(Inst); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 966 | |
| 967 | if (!PrevClassTemplate) { |
| 968 | // Queue up any out-of-line partial specializations of this member |
| 969 | // class template; the client will force their instantiation once |
| 970 | // the enclosing class has been instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 971 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 972 | D->getPartialSpecializations(PartialSpecs); |
| 973 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 974 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 975 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 976 | } |
| 977 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 978 | return Inst; |
| 979 | } |
| 980 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 981 | Decl * |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 982 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 983 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 984 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 986 | // Lookup the already-instantiated declaration in the instantiation |
| 987 | // of the class template and return that. |
| 988 | DeclContext::lookup_result Found |
| 989 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 990 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 991 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 993 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 994 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 995 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 996 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 998 | if (ClassTemplatePartialSpecializationDecl *Result |
| 999 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1000 | return Result; |
| 1001 | |
| 1002 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1005 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1006 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1007 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Create a local instantiation scope for this variable template, which |
| 1010 | // will contain the instantiations of the template parameters. |
| 1011 | LocalInstantiationScope Scope(SemaRef); |
| 1012 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1013 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1014 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1015 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1016 | |
| 1017 | VarDecl *Pattern = D->getTemplatedDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1018 | VarTemplateDecl *PrevVarTemplate = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1019 | |
| 1020 | if (Pattern->getPreviousDecl()) { |
| 1021 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1022 | if (!Found.empty()) |
| 1023 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1024 | } |
| 1025 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1026 | VarDecl *VarInst = |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1027 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1028 | /*InstantiatingVarTemplate=*/true)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1029 | |
| 1030 | DeclContext *DC = Owner; |
| 1031 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1032 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1033 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1034 | VarInst); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1035 | VarInst->setDescribedVarTemplate(Inst); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1036 | Inst->setPreviousDecl(PrevVarTemplate); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1037 | |
| 1038 | Inst->setAccess(D->getAccess()); |
| 1039 | if (!PrevVarTemplate) |
| 1040 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1041 | |
| 1042 | if (D->isOutOfLine()) { |
| 1043 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1044 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1045 | } |
| 1046 | |
| 1047 | Owner->addDecl(Inst); |
| 1048 | |
| 1049 | if (!PrevVarTemplate) { |
| 1050 | // Queue up any out-of-line partial specializations of this member |
| 1051 | // variable template; the client will force their instantiation once |
| 1052 | // the enclosing class has been instantiated. |
| 1053 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1054 | D->getPartialSpecializations(PartialSpecs); |
| 1055 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1056 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1057 | OutOfLineVarPartialSpecs.push_back( |
| 1058 | std::make_pair(Inst, PartialSpecs[I])); |
| 1059 | } |
| 1060 | |
| 1061 | return Inst; |
| 1062 | } |
| 1063 | |
| 1064 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1065 | VarTemplatePartialSpecializationDecl *D) { |
| 1066 | assert(D->isStaticDataMember() && |
| 1067 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1068 | |
| 1069 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1070 | |
| 1071 | // Lookup the already-instantiated declaration and return that. |
| 1072 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1073 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1074 | |
| 1075 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1076 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1077 | |
| 1078 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1079 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1080 | return Result; |
| 1081 | |
| 1082 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1083 | } |
| 1084 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1085 | Decl * |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1086 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1087 | // Create a local instantiation scope for this function template, which |
| 1088 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1089 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1090 | // itself. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1091 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1093 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1094 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1096 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1097 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1098 | FunctionDecl *Instantiated = nullptr; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1099 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1100 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1101 | InstParams)); |
| 1102 | else |
| 1103 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1104 | D->getTemplatedDecl(), |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1105 | InstParams)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1107 | if (!Instantiated) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1108 | return nullptr; |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1109 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | // Link the instantiated function template declaration to the function |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1111 | // template from which it was instantiated. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1112 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1113 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1114 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1115 | assert(InstTemplate && |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1116 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1117 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1118 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1119 | |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1120 | // Link the instantiation back to the pattern *unless* this is a |
| 1121 | // non-definition friend declaration. |
| 1122 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1123 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1124 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1125 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1126 | // Make declarations visible in the appropriate context. |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1127 | if (!isFriend) { |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1128 | Owner->addDecl(InstTemplate); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1129 | } else if (InstTemplate->getDeclContext()->isRecord() && |
| 1130 | !D->getPreviousDecl()) { |
| 1131 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1132 | } |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1134 | return InstTemplate; |
| 1135 | } |
| 1136 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1137 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1138 | CXXRecordDecl *PrevDecl = nullptr; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1139 | if (D->isInjectedClassName()) |
| 1140 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1141 | else if (D->getPreviousDecl()) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1142 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1143 | D->getPreviousDecl(), |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1144 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1145 | if (!Prev) return nullptr; |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1146 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1147 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1148 | |
| 1149 | CXXRecordDecl *Record |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1151 | D->getLocStart(), D->getLocation(), |
| 1152 | D->getIdentifier(), PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Substitute the nested name specifier, if any. |
| 1155 | if (SubstQualifier(D, Record)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1156 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1158 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1159 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1160 | // the tag decls introduced by friend class declarations don't have an access |
| 1161 | // specifier. Remove once this area of the code gets sorted out. |
| 1162 | if (D->getAccess() != AS_none) |
| 1163 | Record->setAccess(D->getAccess()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1164 | if (!D->isInjectedClassName()) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1165 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1166 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1167 | // If the original function was part of a friend declaration, |
| 1168 | // inherit its namespace state. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1169 | if (D->getFriendObjectKind()) |
| 1170 | Record->setObjectOfFriendDecl(); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1172 | // Make sure that anonymous structs and unions are recorded. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1173 | if (D->isAnonymousStructOrUnion()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1174 | Record->setAnonymousStructOrUnion(true); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1175 | |
| 1176 | if (D->isLocalClass()) |
| 1177 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1178 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1179 | // Forward the mangling number from the template to the instantiated decl. |
| 1180 | SemaRef.Context.setManglingNumber(Record, |
| 1181 | SemaRef.Context.getManglingNumber(D)); |
| 1182 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1183 | Owner->addDecl(Record); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1184 | |
| 1185 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1186 | // of the instantiation of their enclosing entity. |
| 1187 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1188 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1189 | TSK_ImplicitInstantiation, |
| 1190 | /*Complain=*/true); |
| 1191 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1192 | TSK_ImplicitInstantiation); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1193 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1194 | return Record; |
| 1195 | } |
| 1196 | |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1197 | /// \brief Adjust the given function type for an instantiation of the |
| 1198 | /// given declaration, to cope with modifications to the function's type that |
| 1199 | /// aren't reflected in the type-source information. |
| 1200 | /// |
| 1201 | /// \param D The declaration we're instantiating. |
| 1202 | /// \param TInfo The already-instantiated type. |
| 1203 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1204 | FunctionDecl *D, |
| 1205 | TypeSourceInfo *TInfo) { |
Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1206 | const FunctionProtoType *OrigFunc |
| 1207 | = D->getType()->castAs<FunctionProtoType>(); |
| 1208 | const FunctionProtoType *NewFunc |
| 1209 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1210 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1211 | return TInfo->getType(); |
| 1212 | |
| 1213 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1214 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1215 | return Context.getFunctionType(NewFunc->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1216 | NewFunc->getParamTypes(), NewEPI); |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1219 | /// Normal class members are of more specific types and therefore |
| 1220 | /// don't make it here. This function serves two purposes: |
| 1221 | /// 1) instantiating function templates |
| 1222 | /// 2) substituting friend declarations |
Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1223 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1224 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1225 | // Check whether there is already a function template specialization for |
| 1226 | // this declaration. |
| 1227 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1228 | if (FunctionTemplate && !TemplateParams) { |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1229 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1231 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1232 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1233 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1235 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1236 | if (SpecFunc) |
| 1237 | return SpecFunc; |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1238 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1240 | bool isFriend; |
| 1241 | if (FunctionTemplate) |
| 1242 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1243 | else |
| 1244 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1245 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1246 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1247 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1248 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1249 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1250 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1252 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1253 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1254 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1255 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1256 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1258 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1259 | if (QualifierLoc) { |
| 1260 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1261 | TemplateArgs); |
| 1262 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1263 | return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1266 | // If we're instantiating a local function declaration, put the result |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1267 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1268 | // context. |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1269 | DeclContext *DC; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1270 | if (D->isLocalExternDecl()) { |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1271 | DC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1272 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1273 | } else if (isFriend && QualifierLoc) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1274 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1275 | SS.Adopt(QualifierLoc); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1276 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1277 | if (!DC) return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1278 | } else { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1279 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1280 | TemplateArgs); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1281 | } |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1282 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1283 | FunctionDecl *Function = |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1284 | FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
Abramo Bagnara | 0d4fce1 | 2012-10-04 21:40:42 +0000 | [diff] [blame] | 1285 | D->getNameInfo(), T, TInfo, |
Rafael Espindola | 9dd86de | 2013-04-16 02:29:15 +0000 | [diff] [blame] | 1286 | D->getCanonicalDecl()->getStorageClass(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1287 | D->isInlineSpecified(), D->hasWrittenPrototype(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1288 | D->isConstexpr()); |
Enea Zaffanella | 25723ce | 2013-07-19 18:02:36 +0000 | [diff] [blame] | 1289 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1290 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1291 | if (D->isInlined()) |
| 1292 | Function->setImplicitlyInline(); |
| 1293 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1294 | if (QualifierLoc) |
| 1295 | Function->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1296 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1297 | if (D->isLocalExternDecl()) |
| 1298 | Function->setLocalExternDecl(); |
| 1299 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1300 | DeclContext *LexicalDC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1301 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1302 | assert(D->getDeclContext()->isFileContext()); |
| 1303 | LexicalDC = D->getDeclContext(); |
| 1304 | } |
| 1305 | |
| 1306 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1308 | // Attach the parameters |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1309 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1310 | if (Params[P]) |
| 1311 | Params[P]->setOwningFunction(Function); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1312 | Function->setParams(Params); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | 1cd6ea0 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1314 | SourceLocation InstantiateAtPOI; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1315 | if (TemplateParams) { |
| 1316 | // Our resulting instantiation is actually a function template, since we |
| 1317 | // are substituting only the outer template parameters. For example, given |
| 1318 | // |
| 1319 | // template<typename T> |
| 1320 | // struct X { |
| 1321 | // template<typename U> friend void f(T, U); |
| 1322 | // }; |
| 1323 | // |
| 1324 | // X<int> x; |
| 1325 | // |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1326 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1327 | // which means substituting int for T, but leaving "f" as a friend function |
| 1328 | // template. |
| 1329 | // Build the function template itself. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1330 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1331 | Function->getLocation(), |
| 1332 | Function->getDeclName(), |
| 1333 | TemplateParams, Function); |
| 1334 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1335 | |
| 1336 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1337 | |
| 1338 | if (isFriend && D->isThisDeclarationADefinition()) { |
| 1339 | // TODO: should we remember this connection regardless of whether |
| 1340 | // the friend declaration provided a body? |
| 1341 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1342 | D->getDescribedFunctionTemplate()); |
| 1343 | } |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1344 | } else if (FunctionTemplate) { |
| 1345 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1346 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1347 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1348 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1349 | Innermost.begin(), |
| 1350 | Innermost.size()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1351 | /*InsertPos=*/nullptr); |
Chandler Carruth | 48b2831 | 2011-08-18 09:09:59 +0000 | [diff] [blame] | 1352 | } else if (isFriend) { |
| 1353 | // Note, we need this connection even if the friend doesn't have a body. |
| 1354 | // Its body may exist but not have been attached yet due to deferred |
| 1355 | // parsing. |
| 1356 | // FIXME: It might be cleaner to set this when attaching the body to the |
| 1357 | // friend function declaration, however that would require finding all the |
| 1358 | // instantiations and modifying them. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1359 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1360 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1361 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1362 | if (InitFunctionInstantiation(Function, D)) |
| 1363 | Function->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1365 | bool isExplicitSpecialization = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1366 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1367 | LookupResult Previous( |
| 1368 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1369 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1370 | : Sema::LookupOrdinaryName, |
| 1371 | Sema::ForRedeclaration); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1372 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1373 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1374 | = D->getDependentSpecializationInfo()) { |
| 1375 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1376 | |
| 1377 | // This needs to be set now for future sanity. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1378 | Function->setObjectOfFriendDecl(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1379 | |
| 1380 | // Instantiate the explicit template arguments. |
| 1381 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1382 | Info->getRAngleLoc()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1383 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1384 | ExplicitArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1385 | return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Map the candidate templates to their instantiations. |
| 1388 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1389 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1390 | Info->getTemplate(I), |
| 1391 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1392 | if (!Temp) return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1393 | |
| 1394 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1395 | } |
| 1396 | |
| 1397 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1398 | &ExplicitArgs, |
| 1399 | Previous)) |
| 1400 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1401 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1402 | isExplicitSpecialization = true; |
| 1403 | |
| 1404 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1405 | // Look only into the namespace where the friend would be declared to |
| 1406 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1407 | // as described in ActOnFriendFunctionDecl. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1408 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1410 | // In C++, the previous declaration we find might be a tag type |
| 1411 | // (class or enum). In this case, the new declaration will hide the |
| 1412 | // tag type. Note that this does does not apply if we're declaring a |
| 1413 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1414 | if (Previous.isSingleTagDecl()) |
| 1415 | Previous.clear(); |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1416 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1417 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1418 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1419 | isExplicitSpecialization); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1420 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1421 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1422 | ? cast<NamedDecl>(FunctionTemplate) |
| 1423 | : Function); |
| 1424 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1425 | // If the original function was part of a friend declaration, |
| 1426 | // inherit its namespace state and add it to the owner. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1427 | if (isFriend) { |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1428 | PrincipalDecl->setObjectOfFriendDecl(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1429 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1430 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1431 | bool QueuedInstantiation = false; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1432 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1433 | // C++11 [temp.friend]p4 (DR329): |
| 1434 | // When a function is defined in a friend function declaration in a class |
| 1435 | // template, the function is instantiated when the function is odr-used. |
| 1436 | // The same restrictions on multiple declarations and definitions that |
| 1437 | // apply to non-template function declarations and definitions also apply |
| 1438 | // to these implicit definitions. |
| 1439 | if (D->isThisDeclarationADefinition()) { |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1440 | // Check for a function body. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1441 | const FunctionDecl *Definition = nullptr; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1442 | if (Function->isDefined(Definition) && |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1443 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1444 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1445 | << Function->getDeclName(); |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1446 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1447 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1448 | // Check for redefinitions due to other instantiations of this or |
| 1449 | // a similar friend function. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1450 | else for (auto R : Function->redecls()) { |
| 1451 | if (R == Function) |
Gabor Greif | 122f1eb | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1452 | continue; |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1453 | |
| 1454 | // If some prior declaration of this function has been used, we need |
| 1455 | // to instantiate its definition. |
| 1456 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1457 | if (MemberSpecializationInfo *MSInfo = |
| 1458 | Function->getMemberSpecializationInfo()) { |
| 1459 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1460 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1461 | MSInfo->setPointOfInstantiation(Loc); |
| 1462 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1463 | std::make_pair(Function, Loc)); |
| 1464 | QueuedInstantiation = true; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // If some prior declaration of this function was a friend with an |
| 1470 | // uninstantiated definition, reject it. |
| 1471 | if (R->getFriendObjectKind()) { |
| 1472 | if (const FunctionDecl *RPattern = |
| 1473 | R->getTemplateInstantiationPattern()) { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1474 | if (RPattern->isDefined(RPattern)) { |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1475 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1476 | << Function->getDeclName(); |
Gabor Greif | ae849e4 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1477 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1478 | break; |
| 1479 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1480 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1486 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1487 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1488 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1489 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1490 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1491 | PrincipalDecl->setNonMemberOperator(); |
| 1492 | |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1493 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1494 | return Function; |
| 1495 | } |
| 1496 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1497 | Decl * |
| 1498 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1499 | TemplateParameterList *TemplateParams, |
| 1500 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1501 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1502 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | // We are creating a function template specialization from a function |
| 1504 | // template. Check whether there is already a function template |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1505 | // specialization for this particular set of template arguments. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1506 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1508 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1509 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1510 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1512 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1513 | if (SpecFunc) |
| 1514 | return SpecFunc; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1517 | bool isFriend; |
| 1518 | if (FunctionTemplate) |
| 1519 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1520 | else |
| 1521 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1522 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1523 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1524 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1525 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1526 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1527 | |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1528 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1529 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1530 | unsigned NumTempParamLists = 0; |
| 1531 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 1532 | TempParamLists.set_size(NumTempParamLists); |
| 1533 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1534 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1535 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1536 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1537 | return nullptr; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1538 | TempParamLists[I] = InstParams; |
| 1539 | } |
| 1540 | } |
| 1541 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1542 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1543 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1544 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1545 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1546 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1548 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1549 | if (QualifierLoc) { |
| 1550 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1551 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1552 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1553 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | DeclContext *DC = Owner; |
| 1557 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1558 | if (QualifierLoc) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1559 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1560 | SS.Adopt(QualifierLoc); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1561 | DC = SemaRef.computeDeclContext(SS); |
John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1562 | |
| 1563 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1564 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1565 | } else { |
| 1566 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1567 | D->getDeclContext(), |
| 1568 | TemplateArgs); |
| 1569 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1570 | if (!DC) return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1573 | // Build the instantiated method declaration. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1574 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1575 | CXXMethodDecl *Method = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1577 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1578 | DeclarationNameInfo NameInfo |
| 1579 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1580 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1582 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | Constructor->isExplicit(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1584 | Constructor->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1585 | false, Constructor->isConstexpr()); |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1586 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1587 | // Claim that the instantiation of a constructor or constructor template |
| 1588 | // inherits the same constructor that the template does. |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1589 | if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>( |
| 1590 | Constructor->getInheritedConstructor())) { |
| 1591 | // If we're instantiating a specialization of a function template, our |
| 1592 | // "inherited constructor" will actually itself be a function template. |
| 1593 | // Instantiate a declaration of it, too. |
| 1594 | if (FunctionTemplate) { |
| 1595 | assert(!TemplateParams && Inh->getDescribedFunctionTemplate() && |
| 1596 | !Inh->getParent()->isDependentContext() && |
| 1597 | "inheriting constructor template in dependent context?"); |
| 1598 | Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(), |
| 1599 | Inh); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 1600 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1601 | return nullptr; |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1602 | Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext()); |
| 1603 | LocalInstantiationScope LocalScope(SemaRef); |
| 1604 | |
| 1605 | // Use the same template arguments that we deduced for the inheriting |
| 1606 | // constructor. There's no way they could be deduced differently. |
| 1607 | MultiLevelTemplateArgumentList InheritedArgs; |
| 1608 | InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost()); |
| 1609 | Inh = cast_or_null<CXXConstructorDecl>( |
| 1610 | SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs)); |
| 1611 | if (!Inh) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1612 | return nullptr; |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1613 | } |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1614 | cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh); |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1615 | } |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1616 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1617 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1618 | StartLoc, NameInfo, T, TInfo, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1619 | Destructor->isInlineSpecified(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1620 | false); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1621 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1622 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1623 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1624 | Conversion->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1625 | Conversion->isExplicit(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1626 | Conversion->isConstexpr(), |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1627 | Conversion->getLocEnd()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1628 | } else { |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1629 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1630 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1631 | StartLoc, NameInfo, T, TInfo, |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1632 | SC, D->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1633 | D->isConstexpr(), D->getLocEnd()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1634 | } |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1635 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1636 | if (D->isInlined()) |
| 1637 | Method->setImplicitlyInline(); |
| 1638 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1639 | if (QualifierLoc) |
| 1640 | Method->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1641 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1642 | if (TemplateParams) { |
| 1643 | // Our resulting instantiation is actually a function template, since we |
| 1644 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | // |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1646 | // template<typename T> |
| 1647 | // struct X { |
| 1648 | // template<typename U> void f(T, U); |
| 1649 | // }; |
| 1650 | // |
| 1651 | // X<int> x; |
| 1652 | // |
| 1653 | // We are instantiating the member template "f" within X<int>, which means |
| 1654 | // substituting int for T, but leaving "f" as a member function template. |
| 1655 | // Build the function template itself. |
| 1656 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1657 | Method->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | Method->getDeclName(), |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1659 | TemplateParams, Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1660 | if (isFriend) { |
| 1661 | FunctionTemplate->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1662 | FunctionTemplate->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1663 | } else if (D->isOutOfLine()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1665 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1666 | } else if (FunctionTemplate) { |
| 1667 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1668 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1669 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1670 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1671 | Innermost.begin(), |
| 1672 | Innermost.size()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1673 | /*InsertPos=*/nullptr); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1674 | } else if (!isFriend) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1675 | // Record that this is an instantiation of a member function. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1676 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1677 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1678 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | // If we are instantiating a member function defined |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1680 | // out-of-line, the instantiation will have the same lexical |
| 1681 | // context (which will be a namespace scope) as the template. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1682 | if (isFriend) { |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1683 | if (NumTempParamLists) |
| 1684 | Method->setTemplateParameterListsInfo(SemaRef.Context, |
| 1685 | NumTempParamLists, |
| 1686 | TempParamLists.data()); |
| 1687 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1688 | Method->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1689 | Method->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1690 | } else if (D->isOutOfLine()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1691 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1693 | // Attach the parameters |
| 1694 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1695 | Params[P]->setOwningFunction(Method); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1696 | Method->setParams(Params); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1697 | |
| 1698 | if (InitMethodInstantiation(Method, D)) |
| 1699 | Method->setInvalidDecl(); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1700 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1701 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 1702 | Sema::ForRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1704 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1705 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1707 | // In C++, the previous declaration we find might be a tag type |
| 1708 | // (class or enum). In this case, the new declaration will hide the |
| 1709 | // tag type. Note that this does does not apply if we're declaring a |
| 1710 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1711 | if (Previous.isSingleTagDecl()) |
| 1712 | Previous.clear(); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1713 | } |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1714 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1715 | if (!IsClassScopeSpecialization) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1716 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, false); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1718 | if (D->isPure()) |
| 1719 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1720 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1721 | // Propagate access. For a non-friend declaration, the access is |
| 1722 | // whatever we're propagating from. For a friend, it should be the |
| 1723 | // previous declaration we just found. |
| 1724 | if (isFriend && Method->getPreviousDecl()) |
| 1725 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
| 1726 | else |
| 1727 | Method->setAccess(D->getAccess()); |
| 1728 | if (FunctionTemplate) |
| 1729 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1730 | |
Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 1731 | SemaRef.CheckOverrideControl(Method); |
| 1732 | |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1733 | // 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] | 1734 | if (D->isExplicitlyDefaulted()) |
| 1735 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1736 | if (D->isDeletedAsWritten()) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1737 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1738 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1739 | // If there's a function template, let our caller handle it. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1740 | if (FunctionTemplate) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1741 | // do nothing |
| 1742 | |
| 1743 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1744 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1745 | // do nothing |
| 1746 | |
| 1747 | // Otherwise, check access to friends and make them visible. |
| 1748 | } else if (isFriend) { |
| 1749 | // We only need to re-check access for methods which we didn't |
| 1750 | // manage to match during parsing. |
| 1751 | if (!D->getPreviousDecl()) |
| 1752 | SemaRef.CheckFriendAccess(Method); |
| 1753 | |
| 1754 | Record->makeDeclVisibleInContext(Method); |
| 1755 | |
| 1756 | // Otherwise, add the declaration. We don't need to do this for |
| 1757 | // class-scope specializations because we'll have matched them with |
| 1758 | // the appropriate template. |
| 1759 | } else if (!IsClassScopeSpecialization) { |
| 1760 | Owner->addDecl(Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1761 | } |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1763 | return Method; |
| 1764 | } |
| 1765 | |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1766 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1767 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1770 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1771 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1774 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1775 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 1778 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1779 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 1780 | /*ExpectParameterPack=*/ false); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1783 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1784 | TemplateTypeParmDecl *D) { |
| 1785 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1786 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1788 | TemplateTypeParmDecl *Inst = |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 1789 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, |
| 1790 | D->getLocStart(), D->getLocation(), |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1791 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1792 | D->getIndex(), D->getIdentifier(), |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1793 | D->wasDeclaredWithTypename(), |
| 1794 | D->isParameterPack()); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1795 | Inst->setAccess(AS_public); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1796 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 1797 | if (D->hasDefaultArgument()) { |
| 1798 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1799 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 1800 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 1801 | if (InstantiatedDefaultArg) |
| 1802 | Inst->setDefaultArgument(InstantiatedDefaultArg, false); |
| 1803 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1806 | // scope. |
| 1807 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1808 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1809 | return Inst; |
| 1810 | } |
| 1811 | |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1812 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1813 | NonTypeTemplateParmDecl *D) { |
| 1814 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1815 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1816 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 1817 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1818 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1819 | TypeSourceInfo *DI; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1820 | QualType T; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1821 | bool Invalid = false; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1824 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1825 | // expansion of types. Substitute into each of the expanded types. |
| 1826 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 1827 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 1828 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| 1829 | TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), |
| 1830 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1831 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1832 | D->getDeclName()); |
| 1833 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1834 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1836 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1837 | QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(), |
| 1838 | D->getLocation()); |
| 1839 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1840 | return nullptr; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1841 | ExpandedParameterPackTypes.push_back(NewT); |
| 1842 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1844 | IsExpandedParameterPack = true; |
| 1845 | DI = D->getTypeSourceInfo(); |
| 1846 | T = DI->getType(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1847 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1848 | // The non-type template parameter pack's type is a pack expansion of types. |
| 1849 | // Determine whether we need to expand this parameter pack into separate |
| 1850 | // types. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1851 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1852 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1853 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1854 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1856 | // Determine whether the set of unexpanded parameter packs can and should |
| 1857 | // be expanded. |
| 1858 | bool Expand = true; |
| 1859 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1860 | Optional<unsigned> OrigNumExpansions |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1861 | = Expansion.getTypePtr()->getNumExpansions(); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1862 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1863 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 1864 | Pattern.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 1865 | Unexpanded, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1866 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1867 | Expand, RetainExpansion, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1868 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1869 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1871 | if (Expand) { |
| 1872 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1873 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 1874 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1875 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1876 | D->getDeclName()); |
| 1877 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1878 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1880 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1881 | QualType NewT = SemaRef.CheckNonTypeTemplateParameterType( |
| 1882 | NewDI->getType(), |
| 1883 | D->getLocation()); |
| 1884 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1885 | return nullptr; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1886 | ExpandedParameterPackTypes.push_back(NewT); |
| 1887 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1888 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1889 | // Note that we have an expanded parameter pack. The "type" of this |
| 1890 | // expanded parameter pack is the original expansion type, but callers |
| 1891 | // will end up using the expanded parameter pack types for type-checking. |
| 1892 | IsExpandedParameterPack = true; |
| 1893 | DI = D->getTypeSourceInfo(); |
| 1894 | T = DI->getType(); |
| 1895 | } else { |
| 1896 | // We cannot fully expand the pack expansion now, so substitute into the |
| 1897 | // pattern and create a new pack expansion type. |
| 1898 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1899 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1900 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1901 | D->getDeclName()); |
| 1902 | if (!NewPattern) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1903 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1905 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 1906 | NumExpansions); |
| 1907 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1908 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1909 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1910 | T = DI->getType(); |
| 1911 | } |
| 1912 | } else { |
| 1913 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1914 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1915 | D->getLocation(), D->getDeclName()); |
| 1916 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1917 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1919 | // Check that this type is acceptable for a non-type template parameter. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1920 | T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1921 | D->getLocation()); |
| 1922 | if (T.isNull()) { |
| 1923 | T = SemaRef.Context.IntTy; |
| 1924 | Invalid = true; |
| 1925 | } |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1926 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1928 | NonTypeTemplateParmDecl *Param; |
| 1929 | if (IsExpandedParameterPack) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1930 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1931 | D->getInnerLocStart(), |
| 1932 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1933 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1934 | D->getPosition(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1935 | D->getIdentifier(), T, |
| 1936 | DI, |
| 1937 | ExpandedParameterPackTypes.data(), |
| 1938 | ExpandedParameterPackTypes.size(), |
| 1939 | ExpandedParameterPackTypesAsWritten.data()); |
| 1940 | else |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1941 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1942 | D->getInnerLocStart(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1943 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1944 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1945 | D->getPosition(), |
| 1946 | D->getIdentifier(), T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1947 | D->isParameterPack(), DI); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1949 | Param->setAccess(AS_public); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1950 | if (Invalid) |
| 1951 | Param->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1952 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 1953 | if (D->hasDefaultArgument()) { |
| 1954 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 1955 | if (!Value.isInvalid()) |
| 1956 | Param->setDefaultArgument(Value.get(), false); |
| 1957 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1958 | |
| 1959 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1960 | // scope. |
| 1961 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1962 | return Param; |
| 1963 | } |
| 1964 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1965 | static void collectUnexpandedParameterPacks( |
| 1966 | Sema &S, |
| 1967 | TemplateParameterList *Params, |
| 1968 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 1969 | for (TemplateParameterList::const_iterator I = Params->begin(), |
| 1970 | E = Params->end(); I != E; ++I) { |
| 1971 | if ((*I)->isTemplateParameterPack()) |
| 1972 | continue; |
| 1973 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I)) |
| 1974 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 1975 | Unexpanded); |
| 1976 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I)) |
| 1977 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 1978 | Unexpanded); |
| 1979 | } |
| 1980 | } |
| 1981 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1982 | Decl * |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1983 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 1984 | TemplateTemplateParmDecl *D) { |
| 1985 | // Instantiate the template parameter list of the template template parameter. |
| 1986 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1987 | TemplateParameterList *InstParams; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1988 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 1989 | |
| 1990 | bool IsExpandedParameterPack = false; |
| 1991 | |
| 1992 | if (D->isExpandedParameterPack()) { |
| 1993 | // The template template parameter pack is an already-expanded pack |
| 1994 | // expansion of template parameters. Substitute into each of the expanded |
| 1995 | // parameters. |
| 1996 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 1997 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 1998 | I != N; ++I) { |
| 1999 | LocalInstantiationScope Scope(SemaRef); |
| 2000 | TemplateParameterList *Expansion = |
| 2001 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2002 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2003 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2004 | ExpandedParams.push_back(Expansion); |
| 2005 | } |
| 2006 | |
| 2007 | IsExpandedParameterPack = true; |
| 2008 | InstParams = TempParams; |
| 2009 | } else if (D->isPackExpansion()) { |
| 2010 | // The template template parameter pack expands to a pack of template |
| 2011 | // template parameters. Determine whether we need to expand this parameter |
| 2012 | // pack into separate parameters. |
| 2013 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2014 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2015 | Unexpanded); |
| 2016 | |
| 2017 | // Determine whether the set of unexpanded parameter packs can and should |
| 2018 | // be expanded. |
| 2019 | bool Expand = true; |
| 2020 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2021 | Optional<unsigned> NumExpansions; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2022 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2023 | TempParams->getSourceRange(), |
| 2024 | Unexpanded, |
| 2025 | TemplateArgs, |
| 2026 | Expand, RetainExpansion, |
| 2027 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2028 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2029 | |
| 2030 | if (Expand) { |
| 2031 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2032 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2033 | LocalInstantiationScope Scope(SemaRef); |
| 2034 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2035 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2036 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2037 | ExpandedParams.push_back(Expansion); |
| 2038 | } |
| 2039 | |
| 2040 | // Note that we have an expanded parameter pack. The "type" of this |
| 2041 | // expanded parameter pack is the original expansion type, but callers |
| 2042 | // will end up using the expanded parameter pack types for type-checking. |
| 2043 | IsExpandedParameterPack = true; |
| 2044 | InstParams = TempParams; |
| 2045 | } else { |
| 2046 | // We cannot fully expand the pack expansion now, so just substitute |
| 2047 | // into the pattern. |
| 2048 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2049 | |
| 2050 | LocalInstantiationScope Scope(SemaRef); |
| 2051 | InstParams = SubstTemplateParams(TempParams); |
| 2052 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2053 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } else { |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2056 | // Perform the actual substitution of template parameters within a new, |
| 2057 | // local instantiation scope. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2058 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2059 | InstParams = SubstTemplateParams(TempParams); |
| 2060 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2061 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2064 | // Build the template template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2065 | TemplateTemplateParmDecl *Param; |
| 2066 | if (IsExpandedParameterPack) |
| 2067 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 2068 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2069 | D->getDepth() - TemplateArgs.getNumLevels(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2070 | D->getPosition(), |
| 2071 | D->getIdentifier(), InstParams, |
| 2072 | ExpandedParams); |
| 2073 | else |
| 2074 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 2075 | D->getLocation(), |
| 2076 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 2077 | D->getPosition(), |
| 2078 | D->isParameterPack(), |
| 2079 | D->getIdentifier(), InstParams); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2080 | if (D->hasDefaultArgument()) { |
| 2081 | NestedNameSpecifierLoc QualifierLoc = |
| 2082 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2083 | QualifierLoc = |
| 2084 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2085 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2086 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2087 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2088 | if (!TName.isNull()) |
| 2089 | Param->setDefaultArgument( |
| 2090 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2091 | D->getDefaultArgument().getTemplateQualifierLoc(), |
| 2092 | D->getDefaultArgument().getTemplateNameLoc()), |
| 2093 | false); |
| 2094 | } |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2095 | Param->setAccess(AS_public); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2096 | |
| 2097 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2098 | // scope. |
| 2099 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2100 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2101 | return Param; |
| 2102 | } |
| 2103 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2104 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2105 | // Using directives are never dependent (and never contain any types or |
| 2106 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2108 | UsingDirectiveDecl *Inst |
| 2109 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2110 | D->getNamespaceKeyLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2111 | D->getQualifierLoc(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2112 | D->getIdentLocation(), |
| 2113 | D->getNominatedNamespace(), |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2114 | D->getCommonAncestor()); |
Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Add the using directive to its declaration context |
| 2117 | // only if this is not a function or method. |
| 2118 | if (!Owner->isFunctionOrMethod()) |
| 2119 | Owner->addDecl(Inst); |
| 2120 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2121 | return Inst; |
| 2122 | } |
| 2123 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2124 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2125 | |
| 2126 | // The nested name specifier may be dependent, for example |
| 2127 | // template <typename T> struct t { |
| 2128 | // struct s1 { T f1(); }; |
| 2129 | // struct s2 : s1 { using s1::f1; }; |
| 2130 | // }; |
| 2131 | // template struct t<int>; |
| 2132 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2133 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2134 | NestedNameSpecifierLoc QualifierLoc |
| 2135 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2136 | TemplateArgs); |
| 2137 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2138 | return nullptr; |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2139 | |
| 2140 | // The name info is non-dependent, so no transformation |
| 2141 | // is required. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2142 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2143 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2144 | // We only need to do redeclaration lookups if we're in a class |
| 2145 | // scope (in fact, it's not really even possible in non-class |
| 2146 | // scopes). |
| 2147 | bool CheckRedeclaration = Owner->isRecord(); |
| 2148 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2149 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 2150 | Sema::ForRedeclaration); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2151 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2152 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2153 | D->getUsingLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2154 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2155 | NameInfo, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2156 | D->hasTypename()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2157 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2158 | CXXScopeSpec SS; |
| 2159 | SS.Adopt(QualifierLoc); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2160 | if (CheckRedeclaration) { |
| 2161 | Prev.setHideTags(false); |
| 2162 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2163 | |
| 2164 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2165 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2166 | D->hasTypename(), SS, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2167 | D->getLocation(), Prev)) |
| 2168 | NewUD->setInvalidDecl(); |
| 2169 | |
| 2170 | } |
| 2171 | |
| 2172 | if (!NewUD->isInvalidDecl() && |
Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 2173 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS, NameInfo, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2174 | D->getLocation())) |
| 2175 | NewUD->setInvalidDecl(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2176 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2177 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2178 | NewUD->setAccess(D->getAccess()); |
| 2179 | Owner->addDecl(NewUD); |
| 2180 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2181 | // Don't process the shadow decls for an invalid decl. |
| 2182 | if (NewUD->isInvalidDecl()) |
| 2183 | return NewUD; |
| 2184 | |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2185 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2186 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2187 | return NewUD; |
| 2188 | } |
| 2189 | |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2190 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2191 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2192 | // Process the shadow decls. |
Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2193 | for (auto *Shadow : D->shadows()) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2194 | NamedDecl *InstTarget = |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2195 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| 2196 | Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2197 | if (!InstTarget) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2198 | return nullptr; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2199 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2200 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2201 | if (CheckRedeclaration) { |
| 2202 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2203 | continue; |
| 2204 | } else if (UsingShadowDecl *OldPrev = Shadow->getPreviousDecl()) { |
| 2205 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2206 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2207 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2208 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2209 | UsingShadowDecl *InstShadow = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2210 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2211 | PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2212 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2213 | |
| 2214 | if (isFunctionScope) |
| 2215 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2216 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2217 | |
| 2218 | return NewUD; |
| 2219 | } |
| 2220 | |
| 2221 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2222 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2223 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2226 | Decl * TemplateDeclInstantiator |
| 2227 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2228 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2229 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2230 | TemplateArgs); |
| 2231 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2232 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2234 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2235 | SS.Adopt(QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2237 | // Since NameInfo refers to a typename, it cannot be a C++ special name. |
Benjamin Kramer | d81108f | 2012-11-14 15:08:31 +0000 | [diff] [blame] | 2238 | // Hence, no transformation is required for it. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2239 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | NamedDecl *UD = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2241 | SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(), |
| 2242 | D->getUsingLoc(), SS, NameInfo, nullptr, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2243 | /*instantiation*/ true, |
| 2244 | /*typename*/ true, D->getTypenameLoc()); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2245 | if (UD) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2246 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2247 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2248 | return UD; |
| 2249 | } |
| 2250 | |
| 2251 | Decl * TemplateDeclInstantiator |
| 2252 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2253 | NestedNameSpecifierLoc QualifierLoc |
| 2254 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs); |
| 2255 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2256 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2257 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2258 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2259 | SS.Adopt(QualifierLoc); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2260 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2261 | DeclarationNameInfo NameInfo |
| 2262 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2263 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2264 | NamedDecl *UD = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2265 | SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(), |
| 2266 | D->getUsingLoc(), SS, NameInfo, nullptr, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2267 | /*instantiation*/ true, |
| 2268 | /*typename*/ false, SourceLocation()); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2269 | if (UD) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2270 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2271 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2272 | return UD; |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2275 | |
| 2276 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
| 2277 | ClassScopeFunctionSpecializationDecl *Decl) { |
| 2278 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2279 | CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2280 | nullptr, true)); |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2281 | |
| 2282 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2283 | Sema::ForRedeclaration); |
| 2284 | |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2285 | TemplateArgumentListInfo TemplateArgs; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2286 | TemplateArgumentListInfo *TemplateArgsPtr = nullptr; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2287 | if (Decl->hasExplicitTemplateArgs()) { |
| 2288 | TemplateArgs = Decl->templateArgs(); |
| 2289 | TemplateArgsPtr = &TemplateArgs; |
| 2290 | } |
| 2291 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2292 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2293 | if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr, |
| 2294 | Previous)) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2295 | NewFD->setInvalidDecl(); |
| 2296 | return NewFD; |
| 2297 | } |
| 2298 | |
| 2299 | // Associate the specialization with the pattern. |
| 2300 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2301 | assert(Specialization && "Class scope Specialization is null"); |
| 2302 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2303 | |
| 2304 | return NewFD; |
| 2305 | } |
| 2306 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2307 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2308 | OMPThreadPrivateDecl *D) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2309 | SmallVector<Expr *, 5> Vars; |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2310 | for (auto *I : D->varlists()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2311 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2312 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2313 | Vars.push_back(Var); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | OMPThreadPrivateDecl *TD = |
| 2317 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2318 | |
Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2319 | TD->setAccess(AS_public); |
| 2320 | Owner->addDecl(TD); |
| 2321 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2322 | return TD; |
| 2323 | } |
| 2324 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2325 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2326 | return VisitFunctionDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2330 | return VisitCXXMethodDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 2334 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 2335 | } |
| 2336 | |
| 2337 | Decl * |
| 2338 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 2339 | ClassTemplateSpecializationDecl *D) { |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2340 | // As a MS extension, we permit class-scope explicit specialization |
| 2341 | // of member class templates. |
| 2342 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 2343 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 2344 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 2345 | "can only instantiate an explicit specialization " |
| 2346 | "for a member class template"); |
| 2347 | |
| 2348 | // Lookup the already-instantiated declaration in the instantiation |
| 2349 | // of the class template. FIXME: Diagnose or assert if this fails? |
| 2350 | DeclContext::lookup_result Found |
| 2351 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 2352 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2353 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2354 | ClassTemplateDecl *InstClassTemplate |
| 2355 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 2356 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2357 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2358 | |
| 2359 | // Substitute into the template arguments of the class template explicit |
| 2360 | // specialization. |
| 2361 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 2362 | castAs<TemplateSpecializationTypeLoc>(); |
| 2363 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 2364 | Loc.getRAngleLoc()); |
| 2365 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 2366 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 2367 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 2368 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 2369 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2370 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2371 | |
| 2372 | // Check that the template argument list is well-formed for this |
| 2373 | // class template. |
| 2374 | SmallVector<TemplateArgument, 4> Converted; |
| 2375 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 2376 | D->getLocation(), |
| 2377 | InstTemplateArgs, |
| 2378 | false, |
| 2379 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2380 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2381 | |
| 2382 | // Figure out where to insert this class template explicit specialization |
| 2383 | // in the member template's set of class template explicit specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2384 | void *InsertPos = nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2385 | ClassTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2386 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2387 | |
| 2388 | // Check whether we've already seen a conflicting instantiation of this |
| 2389 | // declaration (for instance, if there was a prior implicit instantiation). |
| 2390 | bool Ignored; |
| 2391 | if (PrevDecl && |
| 2392 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 2393 | D->getSpecializationKind(), |
| 2394 | PrevDecl, |
| 2395 | PrevDecl->getSpecializationKind(), |
| 2396 | PrevDecl->getPointOfInstantiation(), |
| 2397 | Ignored)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2398 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2399 | |
| 2400 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 2401 | // This happens in cases like: |
| 2402 | // |
| 2403 | // template<typename T, typename U> |
| 2404 | // struct Outer { |
| 2405 | // template<typename X> struct Inner; |
| 2406 | // template<> struct Inner<T> {}; |
| 2407 | // template<> struct Inner<U> {}; |
| 2408 | // }; |
| 2409 | // |
| 2410 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 2411 | // // have the same signature. |
| 2412 | if (PrevDecl && PrevDecl->getDefinition() && |
| 2413 | D->isThisDeclarationADefinition()) { |
| 2414 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 2415 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 2416 | diag::note_previous_definition); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2417 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | // Create the class template partial specialization declaration. |
| 2421 | ClassTemplateSpecializationDecl *InstD |
| 2422 | = ClassTemplateSpecializationDecl::Create(SemaRef.Context, |
| 2423 | D->getTagKind(), |
| 2424 | Owner, |
| 2425 | D->getLocStart(), |
| 2426 | D->getLocation(), |
| 2427 | InstClassTemplate, |
| 2428 | Converted.data(), |
| 2429 | Converted.size(), |
| 2430 | PrevDecl); |
| 2431 | |
| 2432 | // Add this partial specialization to the set of class template partial |
| 2433 | // specializations. |
| 2434 | if (!PrevDecl) |
| 2435 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 2436 | |
| 2437 | // Substitute the nested name specifier, if any. |
| 2438 | if (SubstQualifier(D, InstD)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2439 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2440 | |
| 2441 | // Build the canonical type that describes the converted template |
| 2442 | // arguments of the class template explicit specialization. |
| 2443 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 2444 | TemplateName(InstClassTemplate), Converted.data(), Converted.size(), |
| 2445 | SemaRef.Context.getRecordType(InstD)); |
| 2446 | |
| 2447 | // Build the fully-sugared type for this class template |
| 2448 | // specialization as the user wrote in the specialization |
| 2449 | // itself. This means that we'll pretty-print the type retrieved |
| 2450 | // from the specialization's declaration the way that the user |
| 2451 | // actually wrote the specialization, rather than formatting the |
| 2452 | // name based on the "canonical" representation used to store the |
| 2453 | // template arguments in the specialization. |
| 2454 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2455 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 2456 | CanonType); |
| 2457 | |
| 2458 | InstD->setAccess(D->getAccess()); |
| 2459 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 2460 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 2461 | InstD->setTypeAsWritten(WrittenTy); |
| 2462 | InstD->setExternLoc(D->getExternLoc()); |
| 2463 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 2464 | |
| 2465 | Owner->addDecl(InstD); |
| 2466 | |
| 2467 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 2468 | // We don't have support for lazy instantiation of an explicit specialization |
| 2469 | // yet, and MSVC eagerly instantiates in this case. |
| 2470 | if (D->isThisDeclarationADefinition() && |
| 2471 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 2472 | TSK_ImplicitInstantiation, |
| 2473 | /*Complain=*/true)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2474 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2475 | |
| 2476 | return InstD; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2479 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 2480 | VarTemplateSpecializationDecl *D) { |
| 2481 | |
| 2482 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 2483 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 2484 | assert(VarTemplate && |
| 2485 | "A template specialization without specialized template?"); |
| 2486 | |
| 2487 | // Substitute the current template arguments. |
| 2488 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 2489 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 2490 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 2491 | |
| 2492 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 2493 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2494 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2495 | |
| 2496 | // Check that the template argument list is well-formed for this template. |
| 2497 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2498 | if (SemaRef.CheckTemplateArgumentList( |
| 2499 | VarTemplate, VarTemplate->getLocStart(), |
| 2500 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2501 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2502 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Find the variable template specialization declaration that |
| 2505 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2506 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2507 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2508 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2509 | // If we already have a variable template specialization, return it. |
| 2510 | return VarSpec; |
| 2511 | |
| 2512 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, |
| 2513 | VarTemplateArgsInfo, Converted); |
| 2514 | } |
| 2515 | |
| 2516 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 2517 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 2518 | const TemplateArgumentListInfo &TemplateArgsInfo, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 2519 | ArrayRef<TemplateArgument> Converted) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2520 | |
| 2521 | // If this is the variable for an anonymous struct or union, |
| 2522 | // instantiate the anonymous struct/union type first. |
| 2523 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 2524 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 2525 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2526 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2527 | |
| 2528 | // Do substitution on the type of the declaration |
| 2529 | TypeSourceInfo *DI = |
| 2530 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 2531 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 2532 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2533 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2534 | |
| 2535 | if (DI->getType()->isFunctionType()) { |
| 2536 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 2537 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2538 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | // Build the instantiated declaration |
| 2542 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 2543 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2544 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(), |
| 2545 | Converted.size()); |
| 2546 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2547 | if (InsertPos) |
| 2548 | VarTemplate->AddSpecialization(Var, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2549 | |
| 2550 | // Substitute the nested name specifier, if any. |
| 2551 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2552 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2553 | |
| 2554 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2555 | Owner, StartingScope); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2556 | |
| 2557 | return Var; |
| 2558 | } |
| 2559 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2560 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 2561 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 2562 | } |
| 2563 | |
| 2564 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 2565 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 2566 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 2567 | DiagnosticsEngine::Error, |
| 2568 | "cannot instantiate %0 yet"); |
| 2569 | SemaRef.Diag(D->getLocation(), DiagID) |
| 2570 | << D->getDeclKindName(); |
| 2571 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2572 | return nullptr; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 2576 | llvm_unreachable("Unexpected decl"); |
| 2577 | } |
| 2578 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2579 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2580 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 2581 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2582 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2583 | return nullptr; |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2584 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 2585 | return Instantiator.Visit(D); |
| 2586 | } |
| 2587 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2588 | /// \brief Instantiates a nested template parameter list in the current |
| 2589 | /// instantiation context. |
| 2590 | /// |
| 2591 | /// \param L The parameter list to instantiate |
| 2592 | /// |
| 2593 | /// \returns NULL if there was an error |
| 2594 | TemplateParameterList * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2595 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2596 | // Get errors for all the parameters before bailing out. |
| 2597 | bool Invalid = false; |
| 2598 | |
| 2599 | unsigned N = L->size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2600 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2601 | ParamVector Params; |
| 2602 | Params.reserve(N); |
| 2603 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 2604 | PI != PE; ++PI) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2605 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2606 | Params.push_back(D); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 2607 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | // Clean up if we had an error. |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 2611 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2612 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2613 | |
| 2614 | TemplateParameterList *InstL |
| 2615 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 2616 | L->getLAngleLoc(), &Params.front(), N, |
| 2617 | L->getRAngleLoc()); |
| 2618 | return InstL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | } |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2620 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2621 | /// \brief Instantiate the declaration of a class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2622 | /// specialization. |
| 2623 | /// |
| 2624 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 2625 | // specialized by the instantiation of \p PartialSpec. |
| 2626 | /// |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2627 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2628 | /// specialization that we are instantiating. |
| 2629 | /// |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2630 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 2631 | /// NULL to indicate an error. |
| 2632 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2633 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 2634 | ClassTemplateDecl *ClassTemplate, |
| 2635 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2636 | // Create a local instantiation scope for this class template partial |
| 2637 | // specialization, which will contain the instantiations of the template |
| 2638 | // parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2639 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2640 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2641 | // Substitute into the template parameters of the class template partial |
| 2642 | // specialization. |
| 2643 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 2644 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2645 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2646 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2647 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2648 | // Substitute into the template arguments of the class template partial |
| 2649 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2650 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 2651 | = PartialSpec->getTemplateArgsAsWritten(); |
| 2652 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 2653 | TemplArgInfo->RAngleLoc); |
| 2654 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 2655 | TemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2656 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2657 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2658 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2659 | // Check that the template argument list is well-formed for this |
| 2660 | // class template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2661 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2662 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2663 | PartialSpec->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2664 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2665 | false, |
| 2666 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2667 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Figure out where to insert this class template partial specialization |
| 2670 | // in the member template's set of class template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2671 | void *InsertPos = nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2672 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2673 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2675 | // Build the canonical type that describes the converted template |
| 2676 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2677 | QualType CanonType |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2678 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2679 | Converted.data(), |
| 2680 | Converted.size()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2681 | |
| 2682 | // Build the fully-sugared type for this class template |
| 2683 | // specialization as the user wrote in the specialization |
| 2684 | // itself. This means that we'll pretty-print the type retrieved |
| 2685 | // from the specialization's declaration the way that the user |
| 2686 | // actually wrote the specialization, rather than formatting the |
| 2687 | // name based on the "canonical" representation used to store the |
| 2688 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2689 | TypeSourceInfo *WrittenTy |
| 2690 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2691 | TemplateName(ClassTemplate), |
| 2692 | PartialSpec->getLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2693 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2694 | CanonType); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2695 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2696 | if (PrevDecl) { |
| 2697 | // We've already seen a partial specialization with the same template |
| 2698 | // parameters and template arguments. This can happen, for example, when |
| 2699 | // substituting the outer template arguments ends up causing two |
| 2700 | // class template partial specializations of a member class template |
| 2701 | // to have identical forms, e.g., |
| 2702 | // |
| 2703 | // template<typename T, typename U> |
| 2704 | // struct Outer { |
| 2705 | // template<typename X, typename Y> struct Inner; |
| 2706 | // template<typename Y> struct Inner<T, Y>; |
| 2707 | // template<typename Y> struct Inner<U, Y>; |
| 2708 | // }; |
| 2709 | // |
| 2710 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2711 | // // have the same signature. |
| 2712 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2713 | << WrittenTy->getType(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2714 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 2715 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2716 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2717 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2718 | |
| 2719 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2720 | // Create the class template partial specialization declaration. |
| 2721 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2722 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2723 | PartialSpec->getTagKind(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2724 | Owner, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2725 | PartialSpec->getLocStart(), |
| 2726 | PartialSpec->getLocation(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2727 | InstParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2728 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2729 | Converted.data(), |
| 2730 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2731 | InstTemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2732 | CanonType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2733 | nullptr); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2734 | // Substitute the nested name specifier, if any. |
| 2735 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2736 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2738 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2739 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2741 | // Add this partial specialization to the set of class template partial |
| 2742 | // specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2743 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 2744 | /*InsertPos=*/nullptr); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2745 | return InstPartialSpec; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2748 | /// \brief Instantiate the declaration of a variable template partial |
| 2749 | /// specialization. |
| 2750 | /// |
| 2751 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 2752 | /// specialized by the instantiation of \p PartialSpec. |
| 2753 | /// |
| 2754 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 2755 | /// specialization that we are instantiating. |
| 2756 | /// |
| 2757 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 2758 | /// NULL to indicate an error. |
| 2759 | VarTemplatePartialSpecializationDecl * |
| 2760 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 2761 | VarTemplateDecl *VarTemplate, |
| 2762 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 2763 | // Create a local instantiation scope for this variable template partial |
| 2764 | // specialization, which will contain the instantiations of the template |
| 2765 | // parameters. |
| 2766 | LocalInstantiationScope Scope(SemaRef); |
| 2767 | |
| 2768 | // Substitute into the template parameters of the variable template partial |
| 2769 | // specialization. |
| 2770 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 2771 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2772 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2773 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2774 | |
| 2775 | // Substitute into the template arguments of the variable template partial |
| 2776 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2777 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 2778 | = PartialSpec->getTemplateArgsAsWritten(); |
| 2779 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 2780 | TemplArgInfo->RAngleLoc); |
| 2781 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 2782 | TemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2783 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2784 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2785 | |
| 2786 | // Check that the template argument list is well-formed for this |
| 2787 | // class template. |
| 2788 | SmallVector<TemplateArgument, 4> Converted; |
| 2789 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 2790 | InstTemplateArgs, false, Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2791 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Figure out where to insert this variable template partial specialization |
| 2794 | // in the member template's set of variable template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2795 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2796 | VarTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2797 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2798 | |
| 2799 | // Build the canonical type that describes the converted template |
| 2800 | // arguments of the variable template partial specialization. |
| 2801 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 2802 | TemplateName(VarTemplate), Converted.data(), Converted.size()); |
| 2803 | |
| 2804 | // Build the fully-sugared type for this variable template |
| 2805 | // specialization as the user wrote in the specialization |
| 2806 | // itself. This means that we'll pretty-print the type retrieved |
| 2807 | // from the specialization's declaration the way that the user |
| 2808 | // actually wrote the specialization, rather than formatting the |
| 2809 | // name based on the "canonical" representation used to store the |
| 2810 | // template arguments in the specialization. |
| 2811 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2812 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 2813 | CanonType); |
| 2814 | |
| 2815 | if (PrevDecl) { |
| 2816 | // We've already seen a partial specialization with the same template |
| 2817 | // parameters and template arguments. This can happen, for example, when |
| 2818 | // substituting the outer template arguments ends up causing two |
| 2819 | // variable template partial specializations of a member variable template |
| 2820 | // to have identical forms, e.g., |
| 2821 | // |
| 2822 | // template<typename T, typename U> |
| 2823 | // struct Outer { |
| 2824 | // template<typename X, typename Y> pair<X,Y> p; |
| 2825 | // template<typename Y> pair<T, Y> p; |
| 2826 | // template<typename Y> pair<U, Y> p; |
| 2827 | // }; |
| 2828 | // |
| 2829 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2830 | // // have the same signature. |
| 2831 | SemaRef.Diag(PartialSpec->getLocation(), |
| 2832 | diag::err_var_partial_spec_redeclared) |
| 2833 | << WrittenTy->getType(); |
| 2834 | SemaRef.Diag(PrevDecl->getLocation(), |
| 2835 | diag::note_var_prev_partial_spec_here); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2836 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | // Do substitution on the type of the declaration |
| 2840 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 2841 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 2842 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 2843 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2844 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2845 | |
| 2846 | if (DI->getType()->isFunctionType()) { |
| 2847 | SemaRef.Diag(PartialSpec->getLocation(), |
| 2848 | diag::err_variable_instantiates_to_function) |
| 2849 | << PartialSpec->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2850 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | // Create the variable template partial specialization declaration. |
| 2854 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 2855 | VarTemplatePartialSpecializationDecl::Create( |
| 2856 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 2857 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
| 2858 | DI, PartialSpec->getStorageClass(), Converted.data(), |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2859 | Converted.size(), InstTemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2860 | |
| 2861 | // Substitute the nested name specifier, if any. |
| 2862 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2863 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2864 | |
| 2865 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 2866 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 2867 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2868 | // Add this partial specialization to the set of variable template partial |
| 2869 | // specializations. The instantiation of the initializer is not necessary. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2870 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2871 | |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2872 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2873 | LateAttrs, Owner, StartingScope); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2874 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2875 | return InstPartialSpec; |
| 2876 | } |
| 2877 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2878 | TypeSourceInfo* |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2879 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2880 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2881 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 2882 | assert(OldTInfo && "substituting function without type source info"); |
| 2883 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2884 | |
| 2885 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2886 | unsigned ThisTypeQuals = 0; |
| 2887 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 2888 | ThisContext = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2889 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2890 | } |
| 2891 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 2892 | TypeSourceInfo *NewTInfo |
| 2893 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 2894 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2895 | D->getDeclName(), |
| 2896 | ThisContext, ThisTypeQuals); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2897 | if (!NewTInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2898 | return nullptr; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2899 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2900 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 2901 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 2902 | if (NewTInfo != OldTInfo) { |
| 2903 | // Get parameters from the new type info. |
Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2904 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2905 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2906 | unsigned NewIdx = 0; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2907 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2908 | OldIdx != NumOldParams; ++OldIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2909 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2910 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 2911 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2912 | Optional<unsigned> NumArgumentsInExpansion; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2913 | if (OldParam->isParameterPack()) |
| 2914 | NumArgumentsInExpansion = |
| 2915 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 2916 | TemplateArgs); |
| 2917 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2918 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2919 | // instantiated to a (still-dependent) parameter pack. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2920 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2921 | Params.push_back(NewParam); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2922 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 2923 | } else { |
| 2924 | // Parameter pack expansion: make the instantiation an argument pack. |
| 2925 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 2926 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2927 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2928 | Params.push_back(NewParam); |
| 2929 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 2930 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2931 | } |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2932 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2933 | } else { |
| 2934 | // The function type itself was not dependent and therefore no |
| 2935 | // substitution occurred. However, we still need to instantiate |
| 2936 | // the function parameters themselves. |
| 2937 | const FunctionProtoType *OldProto = |
| 2938 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2939 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 2940 | ++i) { |
| 2941 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2942 | if (!OldParam) { |
| 2943 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2944 | D, D->getLocation(), OldProto->getParamType(i))); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2945 | continue; |
| 2946 | } |
| 2947 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2948 | ParmVarDecl *Parm = |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2949 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2950 | if (!Parm) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2951 | return nullptr; |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2952 | Params.push_back(Parm); |
| 2953 | } |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2954 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2955 | } else { |
| 2956 | // If the type of this function, after ignoring parentheses, is not |
| 2957 | // *directly* a function type, then we're instantiating a function that |
| 2958 | // was declared via a typedef or with attributes, e.g., |
| 2959 | // |
| 2960 | // typedef int functype(int, int); |
| 2961 | // functype func; |
| 2962 | // int __cdecl meth(int, int); |
| 2963 | // |
| 2964 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 2965 | // synthesized in the method declaration. |
| 2966 | SmallVector<QualType, 4> ParamTypes; |
| 2967 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), |
| 2968 | D->getNumParams(), TemplateArgs, ParamTypes, |
| 2969 | &Params)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2970 | return nullptr; |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2971 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2972 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2973 | return NewTInfo; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2976 | /// Introduce the instantiated function parameters into the local |
| 2977 | /// instantiation scope, and set the parameter names to those used |
| 2978 | /// in the template. |
| 2979 | static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
| 2980 | const FunctionDecl *PatternDecl, |
| 2981 | LocalInstantiationScope &Scope, |
| 2982 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2983 | unsigned FParamIdx = 0; |
| 2984 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 2985 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 2986 | if (!PatternParam->isParameterPack()) { |
| 2987 | // Simple case: not a parameter pack. |
| 2988 | assert(FParamIdx < Function->getNumParams()); |
| 2989 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 2990 | // If the parameter's type is not dependent, update it to match the type |
| 2991 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 2992 | // the pattern's type here. If the type is dependent, they can't differ, |
| 2993 | // per core issue 1668. |
| 2994 | // FIXME: Updating the type to work around this is at best fragile. |
| 2995 | if (!PatternDecl->getType()->isDependentType()) |
| 2996 | FunctionParam->setType(PatternParam->getType()); |
| 2997 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2998 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2999 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3000 | ++FParamIdx; |
| 3001 | continue; |
| 3002 | } |
| 3003 | |
| 3004 | // Expand the parameter pack. |
| 3005 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3006 | Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3007 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3008 | assert(NumArgumentsInExpansion && |
| 3009 | "should only be called when all template arguments are known"); |
| 3010 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3011 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3012 | if (!PatternDecl->getType()->isDependentType()) |
| 3013 | FunctionParam->setType(PatternParam->getType()); |
| 3014 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3015 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 3016 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3017 | ++FParamIdx; |
| 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New, |
| 3023 | const FunctionProtoType *Proto, |
| 3024 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3025 | assert(Proto->getExceptionSpecType() != EST_Uninstantiated); |
| 3026 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3027 | // C++11 [expr.prim.general]p3: |
| 3028 | // If a declaration declares a member function or member function |
| 3029 | // template of a class X, the expression this is a prvalue of type |
| 3030 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 3031 | // and the end of the function-definition, member-declarator, or |
| 3032 | // declarator. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3033 | CXXRecordDecl *ThisContext = nullptr; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3034 | unsigned ThisTypeQuals = 0; |
| 3035 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) { |
| 3036 | ThisContext = Method->getParent(); |
| 3037 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 3038 | } |
| 3039 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3040 | SemaRef.getLangOpts().CPlusPlus11); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3041 | |
| 3042 | // The function has an exception specification or a "noreturn" |
| 3043 | // attribute. Substitute into each of the exception types. |
| 3044 | SmallVector<QualType, 4> Exceptions; |
| 3045 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 3046 | // FIXME: Poor location information! |
| 3047 | if (const PackExpansionType *PackExpansion |
| 3048 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { |
| 3049 | // We have a pack expansion. Instantiate it. |
| 3050 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 3051 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), |
| 3052 | Unexpanded); |
| 3053 | assert(!Unexpanded.empty() && |
| 3054 | "Pack expansion without parameter packs?"); |
| 3055 | |
| 3056 | bool Expand = false; |
| 3057 | bool RetainExpansion = false; |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3058 | Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3059 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), |
| 3060 | SourceRange(), |
| 3061 | Unexpanded, |
| 3062 | TemplateArgs, |
| 3063 | Expand, |
| 3064 | RetainExpansion, |
| 3065 | NumExpansions)) |
| 3066 | break; |
| 3067 | |
| 3068 | if (!Expand) { |
| 3069 | // We can't expand this pack expansion into separate arguments yet; |
| 3070 | // just substitute into the pattern and create a new pack expansion |
| 3071 | // type. |
| 3072 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 3073 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 3074 | TemplateArgs, |
| 3075 | New->getLocation(), New->getDeclName()); |
| 3076 | if (T.isNull()) |
| 3077 | break; |
| 3078 | |
| 3079 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); |
| 3080 | Exceptions.push_back(T); |
| 3081 | continue; |
| 3082 | } |
| 3083 | |
| 3084 | // Substitute into the pack expansion pattern for each template |
| 3085 | bool Invalid = false; |
| 3086 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { |
| 3087 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); |
| 3088 | |
| 3089 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 3090 | TemplateArgs, |
| 3091 | New->getLocation(), New->getDeclName()); |
| 3092 | if (T.isNull()) { |
| 3093 | Invalid = true; |
| 3094 | break; |
| 3095 | } |
| 3096 | |
| 3097 | Exceptions.push_back(T); |
| 3098 | } |
| 3099 | |
| 3100 | if (Invalid) |
| 3101 | break; |
| 3102 | |
| 3103 | continue; |
| 3104 | } |
| 3105 | |
| 3106 | QualType T |
| 3107 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 3108 | New->getLocation(), New->getDeclName()); |
| 3109 | if (T.isNull() || |
| 3110 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 3111 | continue; |
| 3112 | |
| 3113 | Exceptions.push_back(T); |
| 3114 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3115 | Expr *NoexceptExpr = nullptr; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3116 | if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) { |
| 3117 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 3118 | Sema::ConstantEvaluated); |
| 3119 | ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs); |
| 3120 | if (E.isUsable()) |
| 3121 | E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart()); |
| 3122 | |
| 3123 | if (E.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3124 | NoexceptExpr = E.get(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3125 | if (!NoexceptExpr->isTypeDependent() && |
| 3126 | !NoexceptExpr->isValueDependent()) |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3127 | NoexceptExpr |
| 3128 | = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3129 | nullptr, diag::err_noexcept_needs_constant_expression, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3130 | /*AllowFold*/ false).get(); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3134 | FunctionProtoType::ExceptionSpecInfo ESI; |
| 3135 | ESI.Type = Proto->getExceptionSpecType(); |
| 3136 | ESI.Exceptions = Exceptions; |
| 3137 | ESI.NoexceptExpr = NoexceptExpr; |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3138 | |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3139 | SemaRef.UpdateExceptionSpec(New, ESI); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3143 | FunctionDecl *Decl) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3144 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3145 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | |
| 3148 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3149 | InstantiatingTemplate::ExceptionSpecification()); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3150 | if (Inst.isInvalid()) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3151 | // We hit the instantiation depth limit. Clear the exception specification |
| 3152 | // so that our callers don't have to cope with EST_Uninstantiated. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3153 | UpdateExceptionSpec(Decl, EST_None); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3154 | return; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3155 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3156 | |
| 3157 | // Enter the scope of this instantiation. We don't use |
| 3158 | // PushDeclContext because we don't have a scope. |
| 3159 | Sema::ContextRAII savedContext(*this, Decl); |
| 3160 | LocalInstantiationScope Scope(*this); |
| 3161 | |
| 3162 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3163 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3164 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3165 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
| 3166 | addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3167 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3168 | ::InstantiateExceptionSpec(*this, Decl, |
| 3169 | Template->getType()->castAs<FunctionProtoType>(), |
| 3170 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3174 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3175 | /// |
| 3176 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | bool |
| 3178 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3179 | FunctionDecl *Tmpl) { |
David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3180 | if (Tmpl->isDeleted()) |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3181 | New->setDeletedAsWritten(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3183 | // Forward the mangling number from the template to the instantiated decl. |
| 3184 | SemaRef.Context.setManglingNumber(New, |
| 3185 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3186 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3187 | // If we are performing substituting explicitly-specified template arguments |
| 3188 | // or deduced template arguments into a function template and we reach this |
| 3189 | // 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] | 3190 | // to keeping the new function template specialization. We therefore |
| 3191 | // convert the active template instantiation for the function template |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3192 | // into a template instantiation for this specific function template |
| 3193 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3194 | // further errors in the declaration itself. |
| 3195 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 3196 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 3197 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3198 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3200 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3202 | "Deduction from the wrong function template?"); |
Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3203 | (void) FunTmpl; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3204 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3205 | ActiveInst.Entity = New; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3209 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3210 | assert(Proto && "Function template without prototype?"); |
| 3211 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3212 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3213 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3214 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3215 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3216 | // exception specification. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3217 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3218 | EPI.ExceptionSpec.Type != EST_None && |
| 3219 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
| 3220 | EPI.ExceptionSpec.Type != EST_BasicNoexcept) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3221 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3222 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3223 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3224 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3225 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3226 | NewEST = EST_Unevaluated; |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3227 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3228 | // Mark the function has having an uninstantiated exception specification. |
| 3229 | const FunctionProtoType *NewProto |
| 3230 | = New->getType()->getAs<FunctionProtoType>(); |
| 3231 | assert(NewProto && "Template instantiation without function prototype?"); |
| 3232 | EPI = NewProto->getExtProtoInfo(); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame^] | 3233 | EPI.ExceptionSpec.Type = NewEST; |
| 3234 | EPI.ExceptionSpec.SourceDecl = New; |
| 3235 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3236 | New->setType(SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3237 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3238 | } else { |
| 3239 | ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs); |
| 3240 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3243 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3244 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3245 | Tmpl->isDefined(Definition); |
| 3246 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3247 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 3248 | LateAttrs, StartingScope); |
Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3249 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3250 | return false; |
| 3251 | } |
| 3252 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3253 | /// \brief Initializes common fields of an instantiated method |
| 3254 | /// declaration (New) from the corresponding fields of its template |
| 3255 | /// (Tmpl). |
| 3256 | /// |
| 3257 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | bool |
| 3259 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3260 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3261 | if (InitFunctionInstantiation(New, Tmpl)) |
| 3262 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3264 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3265 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3266 | New->setVirtualAsWritten(true); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3267 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3268 | // FIXME: New needs a pointer to Tmpl |
| 3269 | return false; |
| 3270 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3271 | |
| 3272 | /// \brief Instantiate the definition of the given function from its |
| 3273 | /// template. |
| 3274 | /// |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3275 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3276 | /// required. Note that this is not precisely a "point of instantiation" |
| 3277 | /// for the function, but it's close. |
| 3278 | /// |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3279 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3280 | /// function template specialization or member function of a class template |
| 3281 | /// specialization. |
| 3282 | /// |
| 3283 | /// \param Recursive if true, recursively instantiates any functions that |
| 3284 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3285 | /// |
| 3286 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3287 | /// instantiation where the body of the function is required. Complain if |
| 3288 | /// there is no such body. |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3289 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3290 | FunctionDecl *Function, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3291 | bool Recursive, |
| 3292 | bool DefinitionRequired) { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3293 | if (Function->isInvalidDecl() || Function->isDefined()) |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3296 | // Never instantiate an explicit specialization except if it is a class scope |
| 3297 | // explicit specialization. |
| 3298 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 3299 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3300 | return; |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3301 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3302 | // Find the function body that we'll be substituting. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3303 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3304 | assert(PatternDecl && "instantiating a non-template"); |
| 3305 | |
| 3306 | Stmt *Pattern = PatternDecl->getBody(PatternDecl); |
| 3307 | assert(PatternDecl && "template definition is not a template"); |
| 3308 | if (!Pattern) { |
| 3309 | // Try to find a defaulted definition |
| 3310 | PatternDecl->isDefined(PatternDecl); |
Alexis Hunt | 92a0adf | 2011-05-25 22:02:25 +0000 | [diff] [blame] | 3311 | } |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3312 | assert(PatternDecl && "template definition is not a template"); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3313 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3314 | // Postpone late parsed template instantiations. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3315 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 3316 | !LateTemplateParser) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3317 | PendingInstantiations.push_back( |
| 3318 | std::make_pair(Function, PointOfInstantiation)); |
| 3319 | return; |
| 3320 | } |
| 3321 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 3322 | // Call the LateTemplateParser callback if there is a need to late parse |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3323 | // a templated function definition. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3324 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3325 | LateTemplateParser) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3326 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 3327 | if (PatternDecl->isFromASTFile()) |
| 3328 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 3329 | |
| 3330 | LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl); |
| 3331 | assert(LPT && "missing LateParsedTemplate"); |
| 3332 | LateTemplateParser(OpaqueParser, *LPT); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3333 | Pattern = PatternDecl->getBody(PatternDecl); |
| 3334 | } |
| 3335 | |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3336 | if (!Pattern && !PatternDecl->isDefaulted()) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3337 | if (DefinitionRequired) { |
| 3338 | if (Function->getPrimaryTemplate()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3339 | Diag(PointOfInstantiation, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3340 | diag::err_explicit_instantiation_undefined_func_template) |
| 3341 | << Function->getPrimaryTemplate(); |
| 3342 | else |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3343 | Diag(PointOfInstantiation, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3344 | diag::err_explicit_instantiation_undefined_member) |
| 3345 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3346 | |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3347 | if (PatternDecl) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3348 | Diag(PatternDecl->getLocation(), |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3349 | diag::note_explicit_instantiation_here); |
Douglas Gregor | fd7224f | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 3350 | Function->setInvalidDecl(); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3351 | } else if (Function->getTemplateSpecializationKind() |
| 3352 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3353 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3354 | std::make_pair(Function, PointOfInstantiation)); |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3355 | } |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3356 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3357 | return; |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3358 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3359 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3360 | // C++1y [temp.explicit]p10: |
| 3361 | // Except for inline functions, declarations with types deduced from their |
| 3362 | // initializer or return value, and class template specializations, other |
| 3363 | // explicit instantiation declarations have the effect of suppressing the |
| 3364 | // implicit instantiation of the entity to which they refer. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3365 | if (Function->getTemplateSpecializationKind() == |
| 3366 | TSK_ExplicitInstantiationDeclaration && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3367 | !PatternDecl->isInlined() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3368 | !PatternDecl->getReturnType()->getContainedAutoType()) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3369 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 3371 | if (PatternDecl->isInlined()) { |
| 3372 | // Function, and all later redeclarations of it (from imported modules, |
| 3373 | // for instance), are now implicitly inline. |
| 3374 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 3375 | D = D->getPreviousDecl()) { |
| 3376 | D->setImplicitlyInline(); |
| 3377 | if (D == Function) |
| 3378 | break; |
| 3379 | } |
| 3380 | } |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3381 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3382 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3383 | if (Inst.isInvalid()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3384 | return; |
| 3385 | |
Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 3386 | // Copy the inner loc start from the pattern. |
| 3387 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 3388 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3389 | // If we're performing recursive template instantiation, create our own |
| 3390 | // queue of pending implicit instantiations that we will instantiate later, |
| 3391 | // while we're still within our own instantiation context. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3392 | SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3393 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 3394 | SavePendingLocalImplicitInstantiationsRAII |
| 3395 | SavedPendingLocalImplicitInstantiations(*this); |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3396 | if (Recursive) { |
| 3397 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3398 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3399 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3401 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3402 | Sema::PotentiallyEvaluated); |
Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3404 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3405 | // recorded, unless we're actually a member function within a local |
| 3406 | // class, in which case we need to merge our results with the parent |
| 3407 | // scope (of the enclosing function). |
| 3408 | bool MergeWithParentScope = false; |
| 3409 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 3410 | MergeWithParentScope = Rec->isLocalClass(); |
| 3411 | |
| 3412 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3414 | if (PatternDecl->isDefaulted()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3415 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3416 | else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3417 | ActOnStartOfFunctionDef(nullptr, Function); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Enter the scope of this instantiation. We don't use |
| 3420 | // PushDeclContext because we don't have a scope. |
| 3421 | Sema::ContextRAII savedContext(*this, Function); |
| 3422 | |
| 3423 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3424 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3425 | |
| 3426 | addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 3427 | TemplateArgs); |
| 3428 | |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3429 | // If this is a constructor, instantiate the member initializers. |
| 3430 | if (const CXXConstructorDecl *Ctor = |
| 3431 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 3432 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 3433 | TemplateArgs); |
| 3434 | } |
| 3435 | |
| 3436 | // Instantiate the function body. |
| 3437 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); |
| 3438 | |
| 3439 | if (Body.isInvalid()) |
| 3440 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3441 | |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3442 | ActOnFinishFunctionBody(Function, Body.get(), |
| 3443 | /*IsInstantiation=*/true); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3444 | |
| 3445 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 3446 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 3447 | if (auto *Listener = getASTMutationListener()) |
| 3448 | Listener->FunctionDefinitionInstantiated(Function); |
Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 3449 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3450 | savedContext.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 3453 | DeclGroupRef DG(Function); |
| 3454 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3456 | // This class may have local implicit instantiations that need to be |
| 3457 | // instantiation within this scope. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3458 | PerformPendingInstantiations(/*LocalOnly=*/true); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3459 | Scope.Exit(); |
| 3460 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3461 | if (Recursive) { |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3462 | // Define any pending vtables. |
| 3463 | DefineUsedVTables(); |
| 3464 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3465 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | // instantiation of this template. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3467 | PerformPendingInstantiations(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3469 | // Restore the set of pending vtables. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3470 | assert(VTableUses.empty() && |
| 3471 | "VTableUses should be empty before it is discarded."); |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3472 | VTableUses.swap(SavedVTableUses); |
| 3473 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3474 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3475 | assert(PendingInstantiations.empty() && |
| 3476 | "PendingInstantiations should be empty before it is discarded."); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3477 | PendingInstantiations.swap(SavedPendingInstantiations); |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3478 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3481 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 3482 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 3483 | const TemplateArgumentList &TemplateArgList, |
| 3484 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 3485 | SmallVectorImpl<TemplateArgument> &Converted, |
| 3486 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 3487 | LateInstantiatedAttrVec *LateAttrs, |
| 3488 | LocalInstantiationScope *StartingScope) { |
| 3489 | if (FromVar->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3490 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3491 | |
| 3492 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3493 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3494 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3495 | |
| 3496 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3497 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 3498 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3499 | // Instantiate the first declaration of the variable template: for a partial |
| 3500 | // specialization of a static data member template, the first declaration may |
| 3501 | // or may not be the declaration in the class; if it's in the class, we want |
| 3502 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 3503 | // we want to instantiate a definition. |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3504 | // |
| 3505 | // If we're instantiating an explicitly-specialized member template or member |
| 3506 | // partial specialization, don't do this. The member specialization completely |
| 3507 | // replaces the original declaration in this case. |
| 3508 | bool IsMemberSpec = false; |
| 3509 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 3510 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 3511 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 3512 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 3513 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 3514 | if (!IsMemberSpec) |
| 3515 | FromVar = FromVar->getFirstDecl(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3516 | |
Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 3517 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 3518 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 3519 | MultiLevelList); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3520 | |
| 3521 | // TODO: Set LateAttrs and StartingScope ... |
| 3522 | |
| 3523 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 3524 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 3525 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 3526 | } |
| 3527 | |
| 3528 | /// \brief Instantiates a variable template specialization by completing it |
| 3529 | /// with appropriate type information and initializer. |
| 3530 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 3531 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 3532 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3533 | |
| 3534 | // Do substitution on the type of the declaration |
| 3535 | TypeSourceInfo *DI = |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3536 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3537 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 3538 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3539 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3540 | |
| 3541 | // Update the type of this variable template specialization. |
| 3542 | VarSpec->setType(DI->getType()); |
| 3543 | |
| 3544 | // Instantiate the initializer. |
| 3545 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 3546 | |
| 3547 | return VarSpec; |
| 3548 | } |
| 3549 | |
| 3550 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 3551 | /// Sets basic variable data and decides whether to postpone the |
| 3552 | /// variable instantiation. |
| 3553 | void Sema::BuildVariableInstantiation( |
| 3554 | VarDecl *NewVar, VarDecl *OldVar, |
| 3555 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3556 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 3557 | LocalInstantiationScope *StartingScope, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3558 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3559 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3560 | // If we are instantiating a local extern declaration, the |
| 3561 | // instantiation belongs lexically to the containing function. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3562 | // If we are instantiating a static data member defined |
| 3563 | // out-of-line, the instantiation will have the same lexical |
| 3564 | // context (which will be a namespace scope) as the template. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3565 | if (OldVar->isLocalExternDecl()) { |
| 3566 | NewVar->setLocalExternDecl(); |
| 3567 | NewVar->setLexicalDeclContext(Owner); |
| 3568 | } else if (OldVar->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3569 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 3570 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 3571 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 3572 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
| 3573 | NewVar->setConstexpr(OldVar->isConstexpr()); |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 3574 | NewVar->setInitCapture(OldVar->isInitCapture()); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3575 | NewVar->setPreviousDeclInSameBlockScope( |
| 3576 | OldVar->isPreviousDeclInSameBlockScope()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3577 | NewVar->setAccess(OldVar->getAccess()); |
| 3578 | |
Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 3579 | if (!OldVar->isStaticDataMember()) { |
Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3580 | if (OldVar->isUsed(false)) |
| 3581 | NewVar->setIsUsed(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3582 | NewVar->setReferenced(OldVar->isReferenced()); |
| 3583 | } |
| 3584 | |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3585 | // See if the old variable had a type-specifier that defined an anonymous tag. |
| 3586 | // If it did, mark the new variable as being the declarator for the new |
| 3587 | // anonymous tag. |
| 3588 | if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) { |
| 3589 | TagDecl *OldTag = OldTagType->getDecl(); |
| 3590 | if (OldTag->getDeclaratorForAnonDecl() == OldVar) { |
| 3591 | TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl(); |
| 3592 | assert(!NewTag->hasNameForLinkage() && |
| 3593 | !NewTag->hasDeclaratorForAnonDecl()); |
| 3594 | NewTag->setDeclaratorForAnonDecl(NewVar); |
| 3595 | } |
| 3596 | } |
| 3597 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3598 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 3599 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3600 | LookupResult Previous( |
| 3601 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 3602 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 3603 | : Sema::LookupOrdinaryName, |
| 3604 | Sema::ForRedeclaration); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3605 | |
Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 3606 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 3607 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 3608 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3609 | // We have a previous declaration. Use that one, so we merge with the |
| 3610 | // right type. |
| 3611 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 3612 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 3613 | Previous.addDecl(NewPrev); |
| 3614 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 3615 | OldVar->hasLinkage()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3616 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3617 | CheckVariableDeclaration(NewVar, Previous); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3618 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3619 | if (!InstantiatingVarTemplate) { |
| 3620 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 3621 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3622 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | if (!OldVar->isOutOfLine()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3626 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 3627 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 3628 | } |
| 3629 | |
| 3630 | // Link instantiations of static data members back to the template from |
| 3631 | // which they were instantiated. |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3632 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3633 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 3634 | TSK_ImplicitInstantiation); |
| 3635 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3636 | // Forward the mangling number from the template to the instantiated decl. |
| 3637 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3638 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3639 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3640 | // Delay instantiation of the initializer for variable templates until a |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 3641 | // definition of the variable is needed. We need it right away if the type |
| 3642 | // contains 'auto'. |
| 3643 | if ((!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 3644 | !InstantiatingVarTemplate) || |
| 3645 | NewVar->getType()->isUndeducedType()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3646 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 3647 | |
| 3648 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 3649 | // will have been deferred. |
| 3650 | if (!NewVar->isInvalidDecl() && |
| 3651 | NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() && |
| 3652 | OldVar->getType()->isDependentType()) |
| 3653 | DiagnoseUnusedDecl(NewVar); |
| 3654 | } |
| 3655 | |
| 3656 | /// \brief Instantiate the initializer of a variable. |
| 3657 | void Sema::InstantiateVariableInitializer( |
| 3658 | VarDecl *Var, VarDecl *OldVar, |
| 3659 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3660 | |
| 3661 | if (Var->getAnyInitializer()) |
| 3662 | // We already have an initializer in the class. |
| 3663 | return; |
| 3664 | |
| 3665 | if (OldVar->getInit()) { |
| 3666 | if (Var->isStaticDataMember() && !OldVar->isOutOfLine()) |
| 3667 | PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar); |
| 3668 | else |
| 3669 | PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar); |
| 3670 | |
| 3671 | // Instantiate the initializer. |
| 3672 | ExprResult Init = |
| 3673 | SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 3674 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 3675 | if (!Init.isInvalid()) { |
| 3676 | bool TypeMayContainAuto = true; |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3677 | Expr *InitExpr = Init.get(); |
| 3678 | |
Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 3679 | if (Var->hasAttr<DLLImportAttr>() && |
| 3680 | (!InitExpr || |
| 3681 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3682 | // Do not dynamically initialize dllimport variables. |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3683 | } else if (InitExpr) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3684 | bool DirectInit = OldVar->isDirectInit(); |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3685 | AddInitializerToDecl(Var, InitExpr, DirectInit, TypeMayContainAuto); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3686 | } else |
| 3687 | ActOnUninitializedDecl(Var, TypeMayContainAuto); |
| 3688 | } else { |
| 3689 | // FIXME: Not too happy about invalidating the declaration |
| 3690 | // because of a bogus initializer. |
| 3691 | Var->setInvalidDecl(); |
| 3692 | } |
| 3693 | |
| 3694 | PopExpressionEvaluationContext(); |
| 3695 | } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) && |
| 3696 | !Var->isCXXForRangeDecl()) |
| 3697 | ActOnUninitializedDecl(Var, false); |
| 3698 | } |
| 3699 | |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3700 | /// \brief Instantiate the definition of the given variable from its |
| 3701 | /// template. |
| 3702 | /// |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3703 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3704 | /// required. Note that this is not precisely a "point of instantiation" |
| 3705 | /// for the function, but it's close. |
| 3706 | /// |
| 3707 | /// \param Var the already-instantiated declaration of a static member |
| 3708 | /// variable of a class template specialization. |
| 3709 | /// |
| 3710 | /// \param Recursive if true, recursively instantiates any functions that |
| 3711 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3712 | /// |
| 3713 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3714 | /// instantiation where an out-of-line definition of the member variable |
| 3715 | /// is required. Complain if there is no such definition. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3716 | void Sema::InstantiateStaticDataMemberDefinition( |
| 3717 | SourceLocation PointOfInstantiation, |
| 3718 | VarDecl *Var, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3719 | bool Recursive, |
| 3720 | bool DefinitionRequired) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3721 | InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive, |
| 3722 | DefinitionRequired); |
| 3723 | } |
| 3724 | |
| 3725 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 3726 | VarDecl *Var, bool Recursive, |
| 3727 | bool DefinitionRequired) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3728 | if (Var->isInvalidDecl()) |
| 3729 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3731 | VarTemplateSpecializationDecl *VarSpec = |
| 3732 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3733 | VarDecl *PatternDecl = nullptr, *Def = nullptr; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3734 | MultiLevelTemplateArgumentList TemplateArgs = |
| 3735 | getTemplateInstantiationArgs(Var); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3737 | if (VarSpec) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3738 | // If this is a variable template specialization, make sure that it is |
| 3739 | // non-dependent, then find its instantiation pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3740 | bool InstantiationDependent = false; |
| 3741 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3742 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 3743 | "Only instantiate variable template specializations that are " |
| 3744 | "not type-dependent"); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 3745 | (void)InstantiationDependent; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3746 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3747 | // Find the variable initialization that we'll be substituting. If the |
| 3748 | // pattern was instantiated from a member template, look back further to |
| 3749 | // find the real pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3750 | assert(VarSpec->getSpecializedTemplate() && |
| 3751 | "Specialization without specialized template?"); |
| 3752 | llvm::PointerUnion<VarTemplateDecl *, |
| 3753 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 3754 | VarSpec->getSpecializedTemplateOrPartial(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3755 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3756 | VarTemplatePartialSpecializationDecl *Tmpl = |
| 3757 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); |
| 3758 | while (VarTemplatePartialSpecializationDecl *From = |
| 3759 | Tmpl->getInstantiatedFromMember()) { |
| 3760 | if (Tmpl->isMemberSpecialization()) |
| 3761 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3762 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3763 | Tmpl = From; |
| 3764 | } |
| 3765 | PatternDecl = Tmpl; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3766 | } else { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3767 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); |
| 3768 | while (VarTemplateDecl *From = |
| 3769 | Tmpl->getInstantiatedFromMemberTemplate()) { |
| 3770 | if (Tmpl->isMemberSpecialization()) |
| 3771 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3772 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3773 | Tmpl = From; |
| 3774 | } |
| 3775 | PatternDecl = Tmpl->getTemplatedDecl(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3776 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3777 | |
| 3778 | // If this is a static data member template, there might be an |
| 3779 | // uninstantiated initializer on the declaration. If so, instantiate |
| 3780 | // it now. |
| 3781 | if (PatternDecl->isStaticDataMember() && |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3782 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3783 | !Var->hasInit()) { |
| 3784 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 3785 | // code here. |
| 3786 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3787 | if (Inst.isInvalid()) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | |
| 3790 | // If we're performing recursive template instantiation, create our own |
| 3791 | // queue of pending implicit instantiations that we will instantiate |
| 3792 | // later, while we're still within our own instantiation context. |
| 3793 | SmallVector<VTableUse, 16> SavedVTableUses; |
| 3794 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
| 3795 | if (Recursive) { |
| 3796 | VTableUses.swap(SavedVTableUses); |
| 3797 | PendingInstantiations.swap(SavedPendingInstantiations); |
| 3798 | } |
| 3799 | |
| 3800 | LocalInstantiationScope Local(*this); |
| 3801 | |
| 3802 | // Enter the scope of this instantiation. We don't use |
| 3803 | // PushDeclContext because we don't have a scope. |
| 3804 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 3805 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 3806 | PreviousContext.pop(); |
| 3807 | |
| 3808 | // FIXME: Need to inform the ASTConsumer that we instantiated the |
| 3809 | // initializer? |
| 3810 | |
| 3811 | // This variable may have local implicit instantiations that need to be |
| 3812 | // instantiated within this scope. |
| 3813 | PerformPendingInstantiations(/*LocalOnly=*/true); |
| 3814 | |
| 3815 | Local.Exit(); |
| 3816 | |
| 3817 | if (Recursive) { |
| 3818 | // Define any newly required vtables. |
| 3819 | DefineUsedVTables(); |
| 3820 | |
| 3821 | // Instantiate any pending implicit instantiations found during the |
| 3822 | // instantiation of this template. |
| 3823 | PerformPendingInstantiations(); |
| 3824 | |
| 3825 | // Restore the set of pending vtables. |
| 3826 | assert(VTableUses.empty() && |
| 3827 | "VTableUses should be empty before it is discarded."); |
| 3828 | VTableUses.swap(SavedVTableUses); |
| 3829 | |
| 3830 | // Restore the set of pending implicit instantiations. |
| 3831 | assert(PendingInstantiations.empty() && |
| 3832 | "PendingInstantiations should be empty before it is discarded."); |
| 3833 | PendingInstantiations.swap(SavedPendingInstantiations); |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | // Find actual definition |
| 3838 | Def = PatternDecl->getDefinition(getASTContext()); |
| 3839 | } else { |
| 3840 | // If this is a static data member, find its out-of-line definition. |
| 3841 | assert(Var->isStaticDataMember() && "not a static data member?"); |
| 3842 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); |
| 3843 | |
| 3844 | assert(PatternDecl && "data member was not instantiated from a template?"); |
| 3845 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); |
| 3846 | Def = PatternDecl->getOutOfLineDefinition(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3849 | // If we don't have a definition of the variable template, we won't perform |
| 3850 | // any instantiation. Rather, we rely on the user to instantiate this |
| 3851 | // definition (or provide a specialization for it) in another translation |
| 3852 | // unit. |
| 3853 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3854 | if (DefinitionRequired) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3855 | if (VarSpec) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3856 | Diag(PointOfInstantiation, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3857 | diag::err_explicit_instantiation_undefined_var_template) << Var; |
| 3858 | else |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3859 | Diag(PointOfInstantiation, |
| 3860 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3861 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 3862 | Diag(PatternDecl->getLocation(), |
| 3863 | diag::note_explicit_instantiation_here); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3864 | if (VarSpec) |
| 3865 | Var->setInvalidDecl(); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3866 | } else if (Var->getTemplateSpecializationKind() |
| 3867 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3868 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3869 | std::make_pair(Var, PointOfInstantiation)); |
| 3870 | } |
| 3871 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3872 | return; |
| 3873 | } |
| 3874 | |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3875 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
| 3876 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3877 | // Never instantiate an explicit specialization. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3878 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3879 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3880 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3881 | // C++11 [temp.explicit]p10: |
| 3882 | // Except for inline functions, [...] explicit instantiation declarations |
| 3883 | // have the effect of suppressing the implicit instantiation of the entity |
| 3884 | // to which they refer. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3885 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3886 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3888 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 3889 | struct PassToConsumerRAII { |
| 3890 | ASTConsumer &Consumer; |
| 3891 | VarDecl *Var; |
| 3892 | |
| 3893 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 3894 | : Consumer(Consumer), Var(Var) { } |
| 3895 | |
| 3896 | ~PassToConsumerRAII() { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3897 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3898 | } |
| 3899 | } PassToConsumerRAII(Consumer, Var); |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3900 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3901 | // If we already have a definition, we're done. |
| 3902 | if (VarDecl *Def = Var->getDefinition()) { |
| 3903 | // We may be explicitly instantiating something we've already implicitly |
| 3904 | // instantiated. |
| 3905 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 3906 | PointOfInstantiation); |
| 3907 | return; |
Nick Lewycky | a995a47 | 2012-04-04 02:38:36 +0000 | [diff] [blame] | 3908 | } |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 3909 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3910 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3911 | if (Inst.isInvalid()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3912 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3914 | // If we're performing recursive template instantiation, create our own |
| 3915 | // queue of pending implicit instantiations that we will instantiate later, |
| 3916 | // while we're still within our own instantiation context. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3917 | SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3918 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 3919 | SavePendingLocalImplicitInstantiationsRAII |
| 3920 | SavedPendingLocalImplicitInstantiations(*this); |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3921 | if (Recursive) { |
| 3922 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3923 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3924 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3926 | // Enter the scope of this instantiation. We don't use |
| 3927 | // PushDeclContext because we don't have a scope. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3928 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3929 | LocalInstantiationScope Local(*this); |
John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 3930 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3931 | VarDecl *OldVar = Var; |
| 3932 | if (!VarSpec) |
| 3933 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3934 | TemplateArgs)); |
| 3935 | else if (Var->isStaticDataMember() && |
| 3936 | Var->getLexicalDeclContext()->isRecord()) { |
| 3937 | // We need to instantiate the definition of a static data member template, |
| 3938 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 3939 | // declaration of the definition. |
| 3940 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 3941 | TemplateArgs); |
| 3942 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3943 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3944 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 3945 | if (Var) { |
| 3946 | llvm::PointerUnion<VarTemplateDecl *, |
| 3947 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 3948 | VarSpec->getSpecializedTemplateOrPartial(); |
| 3949 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 3950 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 3951 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 3952 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 3953 | |
| 3954 | // Merge the definition with the declaration. |
| 3955 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
| 3956 | LookupOrdinaryName, ForRedeclaration); |
| 3957 | R.addDecl(OldVar); |
| 3958 | MergeVarDecl(Var, R); |
| 3959 | |
| 3960 | // Attach the initializer. |
| 3961 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 3962 | } |
| 3963 | } else |
| 3964 | // Complete the existing variable's definition with an appropriately |
| 3965 | // substituted type and initializer. |
| 3966 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3967 | |
| 3968 | PreviousContext.pop(); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3969 | |
| 3970 | if (Var) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3971 | PassToConsumerRAII.Var = Var; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3972 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 3973 | OldVar->getPointOfInstantiation()); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3974 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3975 | |
| 3976 | // This variable may have local implicit instantiations that need to be |
| 3977 | // instantiated within this scope. |
| 3978 | PerformPendingInstantiations(/*LocalOnly=*/true); |
| 3979 | |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3980 | Local.Exit(); |
| 3981 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3982 | if (Recursive) { |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3983 | // Define any newly required vtables. |
| 3984 | DefineUsedVTables(); |
| 3985 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3986 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | // instantiation of this template. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3988 | PerformPendingInstantiations(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3990 | // Restore the set of pending vtables. |
| 3991 | assert(VTableUses.empty() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3992 | "VTableUses should be empty before it is discarded."); |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3993 | VTableUses.swap(SavedVTableUses); |
| 3994 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3995 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3996 | assert(PendingInstantiations.empty() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3997 | "PendingInstantiations should be empty before it is discarded."); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3998 | PendingInstantiations.swap(SavedPendingInstantiations); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4000 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4001 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4002 | void |
| 4003 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4004 | const CXXConstructorDecl *Tmpl, |
| 4005 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4007 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4008 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4009 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4010 | // Instantiate all the initializers. |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4011 | for (const auto *Init : Tmpl->inits()) { |
Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4012 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4013 | // ones. |
| 4014 | if (!Init->isWritten()) |
| 4015 | continue; |
| 4016 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4017 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4018 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4019 | if (Init->isPackExpansion()) { |
| 4020 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4021 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4022 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4023 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4024 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4025 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4026 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4027 | Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4028 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4029 | BaseTL.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4030 | Unexpanded, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4031 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4032 | RetainExpansion, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4033 | NumExpansions)) { |
| 4034 | AnyErrors = true; |
| 4035 | New->setInvalidDecl(); |
| 4036 | continue; |
| 4037 | } |
| 4038 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4039 | |
| 4040 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4041 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4042 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4043 | |
| 4044 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4045 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4046 | /*CXXDirectInit=*/true); |
| 4047 | if (TempInit.isInvalid()) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4048 | AnyErrors = true; |
| 4049 | break; |
| 4050 | } |
| 4051 | |
| 4052 | // Instantiate the base type. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4053 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4054 | TemplateArgs, |
| 4055 | Init->getSourceLocation(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4056 | New->getDeclName()); |
| 4057 | if (!BaseTInfo) { |
| 4058 | AnyErrors = true; |
| 4059 | break; |
| 4060 | } |
| 4061 | |
| 4062 | // Build the initializer. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4063 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4064 | BaseTInfo, TempInit.get(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4065 | New->getParent(), |
| 4066 | SourceLocation()); |
| 4067 | if (NewInit.isInvalid()) { |
| 4068 | AnyErrors = true; |
| 4069 | break; |
| 4070 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4071 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4072 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4073 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4074 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4075 | continue; |
| 4076 | } |
| 4077 | |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4078 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4079 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4080 | /*CXXDirectInit=*/true); |
| 4081 | if (TempInit.isInvalid()) { |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4082 | AnyErrors = true; |
| 4083 | continue; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4084 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4085 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4086 | MemInitResult NewInit; |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4087 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4088 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4089 | TemplateArgs, |
| 4090 | Init->getSourceLocation(), |
| 4091 | New->getDeclName()); |
| 4092 | if (!TInfo) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4093 | AnyErrors = true; |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4094 | New->setInvalidDecl(); |
| 4095 | continue; |
| 4096 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4098 | if (Init->isBaseInitializer()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4099 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4100 | New->getParent(), EllipsisLoc); |
| 4101 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4102 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4103 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4104 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4105 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4106 | Init->getMemberLocation(), |
| 4107 | Init->getMember(), |
| 4108 | TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4109 | if (!Member) { |
| 4110 | AnyErrors = true; |
| 4111 | New->setInvalidDecl(); |
| 4112 | continue; |
| 4113 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4115 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4116 | Init->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4117 | } else if (Init->isIndirectMemberInitializer()) { |
| 4118 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4119 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4120 | Init->getMemberLocation(), |
| 4121 | Init->getIndirectMember(), TemplateArgs)); |
| 4122 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4123 | if (!IndirectMember) { |
| 4124 | AnyErrors = true; |
| 4125 | New->setInvalidDecl(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4126 | continue; |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4127 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4128 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4129 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4130 | Init->getSourceLocation()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4133 | if (NewInit.isInvalid()) { |
| 4134 | AnyErrors = true; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4135 | New->setInvalidDecl(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4136 | } else { |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4137 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4138 | } |
| 4139 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4141 | // Assign all the initializers to the new constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4142 | ActOnMemInitializers(New, |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4143 | /*FIXME: ColonLoc */ |
| 4144 | SourceLocation(), |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4145 | NewInits, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4146 | AnyErrors); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4149 | // TODO: this could be templated if the various decl types used the |
| 4150 | // same method name. |
| 4151 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4152 | ClassTemplateDecl *Instance) { |
| 4153 | Pattern = Pattern->getCanonicalDecl(); |
| 4154 | |
| 4155 | do { |
| 4156 | Instance = Instance->getCanonicalDecl(); |
| 4157 | if (Pattern == Instance) return true; |
| 4158 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4159 | } while (Instance); |
| 4160 | |
| 4161 | return false; |
| 4162 | } |
| 4163 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4164 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 4165 | FunctionTemplateDecl *Instance) { |
| 4166 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4167 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4168 | do { |
| 4169 | Instance = Instance->getCanonicalDecl(); |
| 4170 | if (Pattern == Instance) return true; |
| 4171 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4172 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4173 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4174 | return false; |
| 4175 | } |
| 4176 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4177 | static bool |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4178 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 4179 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4180 | Pattern |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4181 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 4182 | do { |
| 4183 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 4184 | Instance->getCanonicalDecl()); |
| 4185 | if (Pattern == Instance) |
| 4186 | return true; |
| 4187 | Instance = Instance->getInstantiatedFromMember(); |
| 4188 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4189 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4190 | return false; |
| 4191 | } |
| 4192 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4193 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 4194 | CXXRecordDecl *Instance) { |
| 4195 | Pattern = Pattern->getCanonicalDecl(); |
| 4196 | |
| 4197 | do { |
| 4198 | Instance = Instance->getCanonicalDecl(); |
| 4199 | if (Pattern == Instance) return true; |
| 4200 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 4201 | } while (Instance); |
| 4202 | |
| 4203 | return false; |
| 4204 | } |
| 4205 | |
| 4206 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 4207 | FunctionDecl *Instance) { |
| 4208 | Pattern = Pattern->getCanonicalDecl(); |
| 4209 | |
| 4210 | do { |
| 4211 | Instance = Instance->getCanonicalDecl(); |
| 4212 | if (Pattern == Instance) return true; |
| 4213 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 4214 | } while (Instance); |
| 4215 | |
| 4216 | return false; |
| 4217 | } |
| 4218 | |
| 4219 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 4220 | EnumDecl *Instance) { |
| 4221 | Pattern = Pattern->getCanonicalDecl(); |
| 4222 | |
| 4223 | do { |
| 4224 | Instance = Instance->getCanonicalDecl(); |
| 4225 | if (Pattern == Instance) return true; |
| 4226 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 4227 | } while (Instance); |
| 4228 | |
| 4229 | return false; |
| 4230 | } |
| 4231 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4232 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 4233 | UsingShadowDecl *Instance, |
| 4234 | ASTContext &C) { |
| 4235 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 4236 | } |
| 4237 | |
| 4238 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 4239 | UsingDecl *Instance, |
| 4240 | ASTContext &C) { |
| 4241 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 4242 | } |
| 4243 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4244 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 4245 | UsingDecl *Instance, |
| 4246 | ASTContext &C) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4247 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4251 | UsingDecl *Instance, |
| 4252 | ASTContext &C) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4253 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4256 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 4257 | VarDecl *Instance) { |
| 4258 | assert(Instance->isStaticDataMember()); |
| 4259 | |
| 4260 | Pattern = Pattern->getCanonicalDecl(); |
| 4261 | |
| 4262 | do { |
| 4263 | Instance = Instance->getCanonicalDecl(); |
| 4264 | if (Pattern == Instance) return true; |
| 4265 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 4266 | } while (Instance); |
| 4267 | |
| 4268 | return false; |
| 4269 | } |
| 4270 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4271 | // Other is the prospective instantiation |
| 4272 | // D is the prospective pattern |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4273 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4274 | if (D->getKind() != Other->getKind()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4275 | if (UnresolvedUsingTypenameDecl *UUD |
| 4276 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 4277 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 4278 | return isInstantiationOf(UUD, UD, Ctx); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | if (UnresolvedUsingValueDecl *UUD |
| 4283 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4284 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 4285 | return isInstantiationOf(UUD, UD, Ctx); |
| 4286 | } |
| 4287 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4288 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4289 | return false; |
| 4290 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4291 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4292 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 4293 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4294 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4295 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 4296 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4297 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4298 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 4299 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4300 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4301 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4302 | if (Var->isStaticDataMember()) |
| 4303 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 4304 | |
| 4305 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 4306 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 4307 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4308 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 4309 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 4310 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4311 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4312 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 4313 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 4314 | PartialSpec); |
| 4315 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4316 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 4317 | if (!Field->getDeclName()) { |
| 4318 | // This is an unnamed field. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4320 | cast<FieldDecl>(D); |
| 4321 | } |
| 4322 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4323 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4324 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 4325 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 4326 | |
| 4327 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 4328 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 4329 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4330 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 4331 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 4332 | } |
| 4333 | |
| 4334 | template<typename ForwardIterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4336 | NamedDecl *D, |
| 4337 | ForwardIterator first, |
| 4338 | ForwardIterator last) { |
| 4339 | for (; first != last; ++first) |
| 4340 | if (isInstantiationOf(Ctx, D, *first)) |
| 4341 | return cast<NamedDecl>(*first); |
| 4342 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4343 | return nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4346 | /// \brief Finds the instantiation of the given declaration context |
| 4347 | /// within the current instantiation. |
| 4348 | /// |
| 4349 | /// \returns NULL if there was an error |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4350 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4351 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4352 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4353 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4354 | return cast_or_null<DeclContext>(ID); |
| 4355 | } else return DC; |
| 4356 | } |
| 4357 | |
Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 4358 | /// \brief Find the instantiation of the given declaration within the |
| 4359 | /// current instantiation. |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4360 | /// |
| 4361 | /// This routine is intended to be used when \p D is a declaration |
| 4362 | /// referenced from within a template, that needs to mapped into the |
| 4363 | /// corresponding declaration within an instantiation. For example, |
| 4364 | /// given: |
| 4365 | /// |
| 4366 | /// \code |
| 4367 | /// template<typename T> |
| 4368 | /// struct X { |
| 4369 | /// enum Kind { |
| 4370 | /// KnownValue = sizeof(T) |
| 4371 | /// }; |
| 4372 | /// |
| 4373 | /// bool getKind() const { return KnownValue; } |
| 4374 | /// }; |
| 4375 | /// |
| 4376 | /// template struct X<int>; |
| 4377 | /// \endcode |
| 4378 | /// |
Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 4379 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the |
| 4380 | /// \p EnumConstantDecl for \p KnownValue (which refers to |
| 4381 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation |
| 4382 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs |
| 4383 | /// this mapping from within the instantiation of <tt>X<int></tt>. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4384 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4385 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4386 | DeclContext *ParentDC = D->getDeclContext(); |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4387 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
| 4388 | // parameters (p below) can have their ParentDC set to the translation-unit |
| 4389 | // - thus we can not consistently check if the ParentDC of such a parameter |
| 4390 | // is Dependent or/and a FunctionOrMethod. |
| 4391 | // For e.g. this code, during Template argument deduction tries to |
| 4392 | // find an instantiated decl for (T y) when the ParentDC for y is |
| 4393 | // the translation unit. |
| 4394 | // 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] | 4395 | // float baz(float(*)()) { return 0.0; } |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4396 | // Foo(baz); |
| 4397 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 4398 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 4399 | // For now: |
| 4400 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 4401 | // whose type is not instantiation dependent, do nothing to the decl |
| 4402 | // - otherwise find its instantiated decl. |
| 4403 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 4404 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 4405 | return D; |
Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 4406 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 4407 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4408 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) || |
| 4409 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4410 | // D is a local of some kind. Look into the map of local |
| 4411 | // declarations to their instantiations. |
Chris Lattner | 50c3c13 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 4412 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 4413 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
| 4414 | = CurrentInstantiationScope->findInstantiationOf(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4415 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4416 | if (Found) { |
| 4417 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 4418 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4419 | |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 4420 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 4421 | assert(PackIdx != -1 && "found declaration pack but not pack expanding"); |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4422 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 4423 | } |
| 4424 | |
Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 4425 | // If we're performing a partial substitution during template argument |
| 4426 | // deduction, we may not have values for template parameters yet. They |
| 4427 | // just map to themselves. |
| 4428 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 4429 | isa<TemplateTemplateParmDecl>(D)) |
| 4430 | return D; |
| 4431 | |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4432 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4433 | return nullptr; |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4434 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4435 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 4436 | // been found yet. Lazily instantiate it and return it now. |
| 4437 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4438 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4439 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 4440 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4441 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4442 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 4443 | return cast<LabelDecl>(Inst); |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4444 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4445 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4446 | // For variable template specializations, update those that are still |
| 4447 | // type-dependent. |
| 4448 | if (VarTemplateSpecializationDecl *VarSpec = |
| 4449 | dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 4450 | bool InstantiationDependent = false; |
| 4451 | const TemplateArgumentListInfo &VarTemplateArgs = |
| 4452 | VarSpec->getTemplateArgsInfo(); |
| 4453 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
| 4454 | VarTemplateArgs, InstantiationDependent)) |
| 4455 | D = cast<NamedDecl>( |
| 4456 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); |
| 4457 | return D; |
| 4458 | } |
| 4459 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4460 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 4461 | if (!Record->isDependentContext()) |
| 4462 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4463 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4464 | // Determine whether this record is the "templated" declaration describing |
| 4465 | // a class template or class template partial specialization. |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4466 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4467 | if (ClassTemplate) |
| 4468 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 4469 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4470 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 4471 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4472 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4473 | // Walk the current context to find either the record or an instantiation of |
| 4474 | // it. |
| 4475 | DeclContext *DC = CurContext; |
| 4476 | while (!DC->isFileContext()) { |
| 4477 | // If we're performing substitution while we're inside the template |
| 4478 | // definition, we'll find our own context. We're done. |
| 4479 | if (DC->Equals(Record)) |
| 4480 | return Record; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4482 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 4483 | // Check whether we're in the process of instantiating a class template |
| 4484 | // specialization of the template we're mapping. |
| 4485 | if (ClassTemplateSpecializationDecl *InstSpec |
| 4486 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 4487 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 4488 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 4489 | return InstRecord; |
| 4490 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4492 | // Check whether we're in the process of instantiating a member class. |
| 4493 | if (isInstantiationOf(Record, InstRecord)) |
| 4494 | return InstRecord; |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4495 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4496 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4497 | // Move to the outer template scope. |
| 4498 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 4499 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 4500 | DC = FD->getLexicalDeclContext(); |
| 4501 | continue; |
| 4502 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4503 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4505 | DC = DC->getParent(); |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4506 | } |
Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 4507 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4508 | // Fall through to deal with other dependent record types (e.g., |
| 4509 | // anonymous unions in class templates). |
| 4510 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4512 | if (!ParentDC->isDependentContext()) |
| 4513 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4514 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4515 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | if (!ParentDC) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4517 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4519 | if (ParentDC != D->getDeclContext()) { |
| 4520 | // We performed some kind of instantiation in the parent context, |
| 4521 | // so now we need to look into the instantiated parent context to |
| 4522 | // find the instantiation of the declaration D. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4523 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4524 | // If our context used to be dependent, we may need to instantiate |
| 4525 | // it before performing lookup into that context. |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4526 | bool IsBeingInstantiated = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4527 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4528 | if (!Spec->isDependentContext()) { |
| 4529 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4530 | const RecordType *Tag = T->getAs<RecordType>(); |
| 4531 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4532 | if (Tag->isBeingDefined()) |
| 4533 | IsBeingInstantiated = true; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4534 | if (!Tag->isBeingDefined() && |
| 4535 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4536 | return nullptr; |
Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 4537 | |
| 4538 | ParentDC = Tag->getDecl(); |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4542 | NamedDecl *Result = nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4543 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4544 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4545 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4546 | } else { |
| 4547 | // Since we don't have a name for the entity we're looking for, |
| 4548 | // our only option is to walk through all of the declarations to |
| 4549 | // find that name. This will occur in a few cases: |
| 4550 | // |
| 4551 | // - anonymous struct/union within a template |
| 4552 | // - unnamed class/struct/union/enum within a template |
| 4553 | // |
| 4554 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4555 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4556 | ParentDC->decls_begin(), |
| 4557 | ParentDC->decls_end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4558 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4560 | if (!Result) { |
| 4561 | if (isa<UsingShadowDecl>(D)) { |
| 4562 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 4563 | } else if (Diags.hasErrorOccurred()) { |
| 4564 | // We've already complained about something, so most likely this |
| 4565 | // declaration failed to instantiate. There's no point in complaining |
| 4566 | // further, since this is normal in invalid code. |
| 4567 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4568 | // The class in which this member exists is currently being |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4569 | // instantiated, and we haven't gotten around to instantiating this |
| 4570 | // member yet. This can happen when the code uses forward declarations |
| 4571 | // of member classes, and introduces ordering dependencies via |
| 4572 | // template instantiation. |
| 4573 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 4574 | << D->getDeclName() |
| 4575 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 4576 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 4577 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 4578 | // This enumeration constant was found when the template was defined, |
| 4579 | // but can't be found in the instantiation. This can happen if an |
| 4580 | // unscoped enumeration member is explicitly specialized. |
| 4581 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 4582 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 4583 | TemplateArgs)); |
| 4584 | assert(Spec->getTemplateSpecializationKind() == |
| 4585 | TSK_ExplicitSpecialization); |
| 4586 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 4587 | << D->getDeclName() |
| 4588 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 4589 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 4590 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4591 | } else { |
| 4592 | // We should have found something, but didn't. |
| 4593 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 4594 | } |
| 4595 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4597 | D = Result; |
| 4598 | } |
| 4599 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4600 | return D; |
| 4601 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4602 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4603 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4604 | /// instantiations we have seen until this point. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4605 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4606 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4607 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4608 | PendingImplicitInstantiation Inst; |
| 4609 | |
| 4610 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4611 | Inst = PendingInstantiations.front(); |
| 4612 | PendingInstantiations.pop_front(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4613 | } else { |
| 4614 | Inst = PendingLocalImplicitInstantiations.front(); |
| 4615 | PendingLocalImplicitInstantiations.pop_front(); |
| 4616 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4618 | // Instantiate function definitions |
| 4619 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4620 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), |
| 4621 | "instantiating function definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4622 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 4623 | TSK_ExplicitInstantiationDefinition; |
| 4624 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, |
| 4625 | DefinitionRequired); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4626 | continue; |
| 4627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4629 | // Instantiate variable definitions |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4630 | VarDecl *Var = cast<VarDecl>(Inst.first); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4631 | |
| 4632 | assert((Var->isStaticDataMember() || |
| 4633 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 4634 | "Not a static data member, nor a variable template" |
| 4635 | " specialization?"); |
Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 4636 | |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4637 | // Don't try to instantiate declarations if the most recent redeclaration |
| 4638 | // is invalid. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4639 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4640 | continue; |
| 4641 | |
| 4642 | // Check if the most recent declaration has changed the specialization kind |
| 4643 | // and removed the need for implicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4644 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4645 | case TSK_Undeclared: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4646 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4647 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4648 | case TSK_ExplicitSpecialization: |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4649 | continue; // No longer need to instantiate this type. |
| 4650 | case TSK_ExplicitInstantiationDefinition: |
| 4651 | // We only need an instantiation if the pending instantiation *is* the |
| 4652 | // explicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4653 | if (Var != Var->getMostRecentDecl()) continue; |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4654 | case TSK_ImplicitInstantiation: |
| 4655 | break; |
| 4656 | } |
| 4657 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4658 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(), |
| 4659 | "instantiating variable definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4660 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 4661 | TSK_ExplicitInstantiationDefinition; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4662 | |
| 4663 | // Instantiate static data member definitions or variable template |
| 4664 | // specializations. |
| 4665 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
| 4666 | DefinitionRequired); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4667 | } |
| 4668 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 4669 | |
| 4670 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 4671 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 4672 | for (auto DD : Pattern->ddiags()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 4673 | switch (DD->getKind()) { |
| 4674 | case DependentDiagnostic::Access: |
| 4675 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 4676 | break; |
| 4677 | } |
| 4678 | } |
| 4679 | } |