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 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 39 | template<typename DeclT> |
| 40 | static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, |
| 41 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 42 | if (!OldDecl->getQualifierLoc()) |
| 43 | return false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 44 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 45 | assert((NewDecl->getFriendObjectKind() || |
| 46 | !OldDecl->getLexicalDeclContext()->isDependentContext()) && |
| 47 | "non-friend with qualified name defined in dependent context"); |
| 48 | Sema::ContextRAII SavedContext( |
| 49 | SemaRef, |
| 50 | const_cast<DeclContext *>(NewDecl->getFriendObjectKind() |
| 51 | ? NewDecl->getLexicalDeclContext() |
| 52 | : OldDecl->getLexicalDeclContext())); |
| 53 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 54 | NestedNameSpecifierLoc NewQualifierLoc |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 55 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 56 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 58 | if (!NewQualifierLoc) |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 59 | return true; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 61 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 65 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 66 | DeclaratorDecl *NewDecl) { |
| 67 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 68 | } |
| 69 | |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 70 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 71 | TagDecl *NewDecl) { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 72 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 73 | } |
| 74 | |
DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 75 | // Include attribute instantiation code. |
| 76 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 77 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 78 | static void instantiateDependentAlignedAttr( |
| 79 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 80 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 81 | if (Aligned->isAlignmentExpr()) { |
| 82 | // The alignment expression is a constant expression. |
| 83 | EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated); |
| 84 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 85 | if (!Result.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 86 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 87 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 88 | } else { |
| 89 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 90 | TemplateArgs, Aligned->getLocation(), |
| 91 | DeclarationName()); |
| 92 | if (Result) |
| 93 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 94 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void instantiateDependentAlignedAttr( |
| 99 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 100 | const AlignedAttr *Aligned, Decl *New) { |
| 101 | if (!Aligned->isPackExpansion()) { |
| 102 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 107 | if (Aligned->isAlignmentExpr()) |
| 108 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 109 | Unexpanded); |
| 110 | else |
| 111 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 112 | Unexpanded); |
| 113 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 114 | |
| 115 | // Determine whether we can expand this attribute pack yet. |
| 116 | bool Expand = true, RetainExpansion = false; |
| 117 | Optional<unsigned> NumExpansions; |
| 118 | // FIXME: Use the actual location of the ellipsis. |
| 119 | SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 120 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 121 | Unexpanded, TemplateArgs, Expand, |
| 122 | RetainExpansion, NumExpansions)) |
| 123 | return; |
| 124 | |
| 125 | if (!Expand) { |
| 126 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 127 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 128 | } else { |
| 129 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 130 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 131 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 136 | static void instantiateDependentAssumeAlignedAttr( |
| 137 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 138 | const AssumeAlignedAttr *Aligned, Decl *New) { |
| 139 | // The alignment expression is a constant expression. |
| 140 | EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated); |
| 141 | |
| 142 | Expr *E, *OE = nullptr; |
| 143 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 144 | if (Result.isInvalid()) |
| 145 | return; |
| 146 | E = Result.getAs<Expr>(); |
| 147 | |
| 148 | if (Aligned->getOffset()) { |
| 149 | Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); |
| 150 | if (Result.isInvalid()) |
| 151 | return; |
| 152 | OE = Result.getAs<Expr>(); |
| 153 | } |
| 154 | |
| 155 | S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE, |
| 156 | Aligned->getSpellingListIndex()); |
| 157 | } |
| 158 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 159 | static void instantiateDependentAlignValueAttr( |
| 160 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 161 | const AlignValueAttr *Aligned, Decl *New) { |
| 162 | // The alignment expression is a constant expression. |
| 163 | EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated); |
| 164 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 165 | if (!Result.isInvalid()) |
| 166 | S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
| 167 | Aligned->getSpellingListIndex()); |
| 168 | } |
| 169 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 170 | static void instantiateDependentEnableIfAttr( |
| 171 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 172 | const EnableIfAttr *A, const Decl *Tmpl, Decl *New) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 173 | Expr *Cond = nullptr; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 174 | { |
| 175 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 176 | ExprResult Result = S.SubstExpr(A->getCond(), TemplateArgs); |
| 177 | if (Result.isInvalid()) |
| 178 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 179 | Cond = Result.getAs<Expr>(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 180 | } |
| 181 | if (A->getCond()->isTypeDependent() && !Cond->isTypeDependent()) { |
| 182 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 183 | if (Converted.isInvalid()) |
| 184 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 185 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 189 | if (A->getCond()->isValueDependent() && !Cond->isValueDependent() && |
| 190 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(Tmpl), |
| 191 | Diags)) { |
| 192 | S.Diag(A->getLocation(), diag::err_enable_if_never_constant_expr); |
| 193 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 194 | S.Diag(Diags[I].first, Diags[I].second); |
| 195 | return; |
| 196 | } |
| 197 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 198 | EnableIfAttr *EIA = new (S.getASTContext()) |
| 199 | EnableIfAttr(A->getLocation(), S.getASTContext(), Cond, |
| 200 | A->getMessage(), |
| 201 | A->getSpellingListIndex()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 202 | New->addAttr(EIA); |
| 203 | } |
| 204 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 205 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 206 | const Decl *Tmpl, Decl *New, |
| 207 | LateInstantiatedAttrVec *LateAttrs, |
| 208 | LocalInstantiationScope *OuterMostScope) { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 209 | for (const auto *TmplAttr : Tmpl->attrs()) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 210 | // FIXME: This should be generalized to more than just the AlignedAttr. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 211 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 212 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 213 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 214 | continue; |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 217 | const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr); |
| 218 | if (AssumeAligned) { |
| 219 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 220 | continue; |
| 221 | } |
| 222 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 223 | const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr); |
| 224 | if (AlignValue) { |
| 225 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 226 | continue; |
| 227 | } |
| 228 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 229 | const EnableIfAttr *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr); |
| 230 | if (EnableIf && EnableIf->getCond()->isValueDependent()) { |
| 231 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
| 232 | New); |
| 233 | continue; |
| 234 | } |
| 235 | |
Hans Wennborg | c2b7f7a | 2014-08-24 00:12:36 +0000 | [diff] [blame] | 236 | // Existing DLL attribute on the instantiation takes precedence. |
| 237 | if (TmplAttr->getKind() == attr::DLLExport || |
| 238 | TmplAttr->getKind() == attr::DLLImport) { |
| 239 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 240 | continue; |
| 241 | } |
| 242 | } |
| 243 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 244 | assert(!TmplAttr->isPackExpansion()); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 245 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 246 | // Late parsed attributes must be instantiated and attached after the |
| 247 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 248 | LocalInstantiationScope *Saved = nullptr; |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 249 | if (CurrentInstantiationScope) |
| 250 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 251 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 252 | } else { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 253 | // Allow 'this' within late-parsed attributes. |
| 254 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 255 | CXXRecordDecl *ThisContext = |
| 256 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| 257 | CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0, |
| 258 | ND && ND->isCXXInstanceMember()); |
| 259 | |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 260 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 261 | *this, TemplateArgs); |
Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 262 | if (NewAttr) |
| 263 | New->addAttr(NewAttr); |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 264 | } |
Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 268 | /// Get the previous declaration of a declaration for the purposes of template |
| 269 | /// instantiation. If this finds a previous declaration, then the previous |
| 270 | /// declaration of the instantiation of D should be an instantiation of the |
| 271 | /// result of this function. |
| 272 | template<typename DeclT> |
| 273 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 274 | DeclT *Result = D->getPreviousDecl(); |
| 275 | |
| 276 | // If the declaration is within a class, and the previous declaration was |
| 277 | // merged from a different definition of that class, then we don't have a |
| 278 | // previous declaration for the purpose of template instantiation. |
| 279 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 280 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 281 | return nullptr; |
| 282 | |
| 283 | return Result; |
| 284 | } |
| 285 | |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 286 | Decl * |
| 287 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 288 | llvm_unreachable("Translation units cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | Decl * |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 292 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 293 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 294 | D->getIdentifier()); |
| 295 | Owner->addDecl(Inst); |
| 296 | return Inst; |
| 297 | } |
| 298 | |
| 299 | Decl * |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 300 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 301 | llvm_unreachable("Namespaces cannot be instantiated"); |
Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 302 | } |
| 303 | |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 304 | Decl * |
| 305 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 306 | NamespaceAliasDecl *Inst |
| 307 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 308 | D->getNamespaceLoc(), |
| 309 | D->getAliasLoc(), |
Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 310 | D->getIdentifier(), |
| 311 | D->getQualifierLoc(), |
John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 312 | D->getTargetNameLoc(), |
| 313 | D->getNamespace()); |
| 314 | Owner->addDecl(Inst); |
| 315 | return Inst; |
| 316 | } |
| 317 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 318 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 319 | bool IsTypeAlias) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 320 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 321 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 322 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 323 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 324 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 325 | D->getLocation(), D->getDeclName()); |
| 326 | if (!DI) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 327 | Invalid = true; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 328 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 329 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 330 | } else { |
| 331 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 332 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 334 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 335 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 336 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 337 | // semantics. See LWG issue 2141 for more information on the bug. |
| 338 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 339 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 340 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 341 | DT->isReferenceType() && |
| 342 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 343 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 344 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
| 345 | SemaRef.getSourceManager().isInSystemHeader(D->getLocStart())) |
| 346 | // Fold it to the (non-reference) type which g++ would have produced. |
| 347 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 348 | DI->getType().getNonReferenceType()); |
| 349 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 350 | // Create the new typedef |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 351 | TypedefNameDecl *Typedef; |
| 352 | if (IsTypeAlias) |
| 353 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 354 | D->getLocation(), D->getIdentifier(), DI); |
| 355 | else |
| 356 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 357 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 358 | if (Invalid) |
| 359 | Typedef->setInvalidDecl(); |
| 360 | |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 361 | // If the old typedef was the name for linkage purposes of an anonymous |
| 362 | // tag decl, re-establish that relationship for the new typedef. |
| 363 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 364 | TagDecl *oldTag = oldTagType->getDecl(); |
Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 365 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 366 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 367 | assert(!newTag->hasNameForLinkage()); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 368 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 369 | } |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 370 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 371 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 372 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 373 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 374 | TemplateArgs); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 375 | if (!InstPrev) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 376 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 377 | |
Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 378 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 379 | |
| 380 | // If the typedef types are not identical, reject them. |
| 381 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 382 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 383 | Typedef->setPreviousDecl(InstPrevTypedef); |
John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 386 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 387 | |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 388 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 390 | return Typedef; |
| 391 | } |
| 392 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 393 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 394 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 395 | if (Typedef) |
| 396 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 397 | return Typedef; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 401 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 402 | if (Typedef) |
| 403 | Owner->addDecl(Typedef); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 404 | return Typedef; |
| 405 | } |
| 406 | |
| 407 | Decl * |
| 408 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 409 | // Create a local instantiation scope for this type alias template, which |
| 410 | // will contain the instantiations of the template parameters. |
| 411 | LocalInstantiationScope Scope(SemaRef); |
| 412 | |
| 413 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 414 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 415 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 416 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 417 | |
| 418 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 419 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 420 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 421 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 422 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 423 | if (!Found.empty()) { |
| 424 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 429 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 430 | if (!AliasInst) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 431 | return nullptr; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 432 | |
| 433 | TypeAliasTemplateDecl *Inst |
| 434 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 435 | D->getDeclName(), InstParams, AliasInst); |
Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 436 | AliasInst->setDescribedAliasTemplate(Inst); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 437 | if (PrevAliasTemplate) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 438 | Inst->setPreviousDecl(PrevAliasTemplate); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 439 | |
| 440 | Inst->setAccess(D->getAccess()); |
| 441 | |
| 442 | if (!PrevAliasTemplate) |
| 443 | Inst->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 444 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 445 | Owner->addDecl(Inst); |
| 446 | |
| 447 | return Inst; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 450 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 451 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 454 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
| 455 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 457 | // If this is the variable for an anonymous struct or union, |
| 458 | // instantiate the anonymous struct/union type first. |
| 459 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 460 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 461 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 462 | return nullptr; |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 463 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 464 | // Do substitution on the type of the declaration |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 465 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), |
John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 466 | TemplateArgs, |
| 467 | D->getTypeSpecStartLoc(), |
| 468 | D->getDeclName()); |
| 469 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 470 | return nullptr; |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 472 | if (DI->getType()->isFunctionType()) { |
| 473 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 474 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 475 | return nullptr; |
Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 476 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 477 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 478 | DeclContext *DC = Owner; |
| 479 | if (D->isLocalExternDecl()) |
| 480 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 481 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 482 | // Build the instantiated declaration. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 483 | VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 484 | D->getLocation(), D->getIdentifier(), |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 485 | DI->getType(), DI, D->getStorageClass()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 487 | // In ARC, infer 'retaining' for variables of retainable type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 488 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 489 | SemaRef.inferObjCARCLifetime(Var)) |
| 490 | Var->setInvalidDecl(); |
| 491 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 492 | // Substitute the nested name specifier, if any. |
| 493 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 494 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 496 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 497 | StartingScope, InstantiatingVarTemplate); |
Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 498 | |
| 499 | if (D->isNRVOVariable()) { |
| 500 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
| 501 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, false)) |
| 502 | Var->setNRVOVariable(true); |
| 503 | } |
| 504 | |
Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 505 | Var->setImplicit(D->isImplicit()); |
| 506 | |
Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 507 | return Var; |
| 508 | } |
| 509 | |
Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 510 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 511 | AccessSpecDecl* AD |
| 512 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 513 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 514 | Owner->addHiddenDecl(AD); |
| 515 | return AD; |
| 516 | } |
| 517 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 518 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 519 | bool Invalid = false; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 520 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 521 | if (DI->getType()->isInstantiationDependentType() || |
Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 522 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 523 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 524 | D->getLocation(), D->getDeclName()); |
| 525 | if (!DI) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 526 | DI = D->getTypeSourceInfo(); |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 527 | Invalid = true; |
| 528 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 529 | // C++ [temp.arg.type]p3: |
| 530 | // If a declaration acquires a function type through a type |
| 531 | // dependent on a template-parameter and this causes a |
| 532 | // declaration that does not use the syntactic form of a |
| 533 | // function declarator to have function type, the program is |
| 534 | // ill-formed. |
| 535 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 536 | << DI->getType(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 537 | Invalid = true; |
| 538 | } |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 539 | } else { |
| 540 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | Expr *BitWidth = D->getBitWidth(); |
| 544 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 545 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 546 | else if (BitWidth) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 547 | // The bit-width expression is a constant expression. |
| 548 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 549 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 551 | ExprResult InstantiatedBitWidth |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 552 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 553 | if (InstantiatedBitWidth.isInvalid()) { |
| 554 | Invalid = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 555 | BitWidth = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 556 | } else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 557 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 558 | } |
| 559 | |
John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 560 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 561 | DI->getType(), DI, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | cast<RecordDecl>(Owner), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 563 | D->getLocation(), |
| 564 | D->isMutable(), |
| 565 | BitWidth, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 566 | D->getInClassInitStyle(), |
Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 567 | D->getInnerLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 568 | D->getAccess(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 569 | nullptr); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 570 | if (!Field) { |
| 571 | cast<Decl>(Owner)->setInvalidDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 572 | return nullptr; |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 575 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 576 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 577 | if (Field->hasAttrs()) |
| 578 | SemaRef.CheckAlignasUnderalignment(Field); |
| 579 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 580 | if (Invalid) |
| 581 | Field->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 583 | if (!Field->getDeclName()) { |
| 584 | // Keep track of where this decl came from. |
| 585 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 586 | } |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 587 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 588 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 589 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 590 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 591 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 593 | Field->setImplicit(D->isImplicit()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 594 | Field->setAccess(D->getAccess()); |
Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 595 | Owner->addDecl(Field); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 596 | |
| 597 | return Field; |
| 598 | } |
| 599 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 600 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 601 | bool Invalid = false; |
| 602 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 603 | |
| 604 | if (DI->getType()->isVariablyModifiedType()) { |
| 605 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 606 | << D; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 607 | Invalid = true; |
| 608 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 609 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 610 | D->getLocation(), D->getDeclName()); |
| 611 | if (!DI) { |
| 612 | DI = D->getTypeSourceInfo(); |
| 613 | Invalid = true; |
| 614 | } else if (DI->getType()->isFunctionType()) { |
| 615 | // C++ [temp.arg.type]p3: |
| 616 | // If a declaration acquires a function type through a type |
| 617 | // dependent on a template-parameter and this causes a |
| 618 | // declaration that does not use the syntactic form of a |
| 619 | // function declarator to have function type, the program is |
| 620 | // ill-formed. |
| 621 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 622 | << DI->getType(); |
| 623 | Invalid = true; |
| 624 | } |
| 625 | } else { |
| 626 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 627 | } |
| 628 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 629 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 630 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
| 631 | DI, D->getLocStart(), D->getGetterId(), D->getSetterId()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 632 | |
| 633 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 634 | StartingScope); |
| 635 | |
| 636 | if (Invalid) |
| 637 | Property->setInvalidDecl(); |
| 638 | |
| 639 | Property->setAccess(D->getAccess()); |
| 640 | Owner->addDecl(Property); |
| 641 | |
| 642 | return Property; |
| 643 | } |
| 644 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 645 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 646 | NamedDecl **NamedChain = |
| 647 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 648 | |
| 649 | int i = 0; |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 650 | for (auto *PI : D->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 651 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 652 | TemplateArgs); |
| 653 | if (!Next) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 654 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 656 | NamedChain[i++] = Next; |
| 657 | } |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 658 | |
Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 659 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 660 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 661 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
| 662 | NamedChain, D->getChainingSize()); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 663 | |
Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 664 | for (const auto *Attr : D->attrs())
|
| 665 | IndirectField->addAttr(Attr->clone(SemaRef.Context));
|
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 666 | |
| 667 | IndirectField->setImplicit(D->isImplicit()); |
| 668 | IndirectField->setAccess(D->getAccess()); |
| 669 | Owner->addDecl(IndirectField); |
| 670 | return IndirectField; |
| 671 | } |
| 672 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 673 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 674 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 675 | // parameters into the pattern type and checking the result. |
John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 676 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 677 | TypeSourceInfo *InstTy; |
| 678 | // If this is an unsupported friend, don't bother substituting template |
| 679 | // arguments into it. The actual type referred to won't be used by any |
| 680 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 681 | // same info for the instantiated friend. |
| 682 | if (D->isUnsupportedFriend()) { |
| 683 | InstTy = Ty; |
| 684 | } else { |
| 685 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 686 | D->getLocation(), DeclarationName()); |
| 687 | } |
| 688 | if (!InstTy) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 689 | return nullptr; |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 690 | |
Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 691 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(), |
Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 692 | D->getFriendLoc(), InstTy); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 693 | if (!FD) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 694 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 696 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 697 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 698 | Owner->addDecl(FD); |
| 699 | return FD; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 702 | NamedDecl *ND = D->getFriendDecl(); |
| 703 | assert(ND && "friend decl must be a decl or a type!"); |
| 704 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 705 | // All of the Visit implementations for the various potential friend |
| 706 | // declarations have to be carefully written to work for friend |
| 707 | // objects, with the most important detail being that the target |
| 708 | // decl should almost certainly not be placed in Owner. |
| 709 | Decl *NewND = Visit(ND); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 710 | if (!NewND) return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 712 | FriendDecl *FD = |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 713 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 714 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 715 | FD->setAccess(AS_public); |
John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 716 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 717 | Owner->addDecl(FD); |
| 718 | return FD; |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 721 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 722 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 724 | // The expression in a static assertion is a constant expression. |
| 725 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 726 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 728 | ExprResult InstantiatedAssertExpr |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 729 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 730 | if (InstantiatedAssertExpr.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 731 | return nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 732 | |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 733 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 734 | InstantiatedAssertExpr.get(), |
Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 735 | D->getMessage(), |
| 736 | D->getRParenLoc(), |
| 737 | D->isFailed()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 741 | EnumDecl *PrevDecl = nullptr; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 742 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 743 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 744 | PatternPrev, |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 745 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 746 | if (!Prev) return nullptr; |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 747 | PrevDecl = cast<EnumDecl>(Prev); |
| 748 | } |
| 749 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 750 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 751 | D->getLocation(), D->getIdentifier(), |
Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 752 | PrevDecl, D->isScoped(), |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 753 | D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 754 | if (D->isFixed()) { |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 755 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 756 | // If we have type source information for the underlying type, it means it |
| 757 | // has been explicitly set by the user. Perform substitution on it before |
| 758 | // moving on. |
| 759 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 760 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 761 | DeclarationName()); |
| 762 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 763 | Enum->setIntegerType(SemaRef.Context.IntTy); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 764 | else |
| 765 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 766 | } else { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 767 | assert(!D->getIntegerType()->isDependentType() |
| 768 | && "Dependent type without type source info"); |
| 769 | Enum->setIntegerType(D->getIntegerType()); |
| 770 | } |
| 771 | } |
| 772 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 773 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 774 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 775 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 776 | Enum->setAccess(D->getAccess()); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 777 | // Forward the mangling number from the template to the instantiated decl. |
| 778 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 779 | if (SubstQualifier(D, Enum)) return nullptr; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 780 | Owner->addDecl(Enum); |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 781 | |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 782 | EnumDecl *Def = D->getDefinition(); |
| 783 | if (Def && Def != D) { |
| 784 | // If this is an out-of-line definition of an enum member template, check |
| 785 | // that the underlying types match in the instantiation of both |
| 786 | // declarations. |
| 787 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 788 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 789 | QualType DefnUnderlying = |
| 790 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 791 | UnderlyingLoc, DeclarationName()); |
| 792 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
| 793 | DefnUnderlying, Enum); |
| 794 | } |
| 795 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 796 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 797 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 798 | // specialization causes the implicit instantiation of the declarations, but |
| 799 | // not the definitions of scoped member enumerations. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 800 | // |
| 801 | // DR1484 clarifies that enumeration definitions inside of a template |
| 802 | // declaration aren't considered entities that can be separately instantiated |
| 803 | // from the rest of the entity they are declared inside of. |
| 804 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 805 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 806 | InstantiateEnumDefinition(Enum, Def); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 807 | } |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 808 | |
| 809 | return Enum; |
| 810 | } |
| 811 | |
| 812 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 813 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 814 | Enum->startDefinition(); |
| 815 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 816 | // Update the location to refer to the definition. |
| 817 | Enum->setLocation(Pattern->getLocation()); |
| 818 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 819 | SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 820 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 821 | EnumConstantDecl *LastEnumConst = nullptr; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 822 | for (auto *EC : Pattern->enumerators()) { |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 823 | // The specified value for the enumerator. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 824 | ExprResult Value((Expr *)nullptr); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 825 | if (Expr *UninstValue = EC->getInitExpr()) { |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 826 | // The enumerator's value expression is a constant expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 828 | Sema::ConstantEvaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 830 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 831 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 832 | |
| 833 | // Drop the initial value and continue. |
| 834 | bool isInvalid = false; |
| 835 | if (Value.isInvalid()) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 836 | Value = nullptr; |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 837 | isInvalid = true; |
| 838 | } |
| 839 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 840 | EnumConstantDecl *EnumConst |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 841 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 842 | EC->getLocation(), EC->getIdentifier(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 843 | Value.get()); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 844 | |
| 845 | if (isInvalid) { |
| 846 | if (EnumConst) |
| 847 | EnumConst->setInvalidDecl(); |
| 848 | Enum->setInvalidDecl(); |
| 849 | } |
| 850 | |
| 851 | if (EnumConst) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 852 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 853 | |
John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 854 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 855 | Enum->addDecl(EnumConst); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 856 | Enumerators.push_back(EnumConst); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 857 | LastEnumConst = EnumConst; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 858 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 859 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 860 | !Enum->isScoped()) { |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 861 | // If the enumeration is within a function or method, record the enum |
| 862 | // constant as a local. |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 863 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 864 | } |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 868 | // FIXME: Fixup LBraceLoc |
| 869 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), |
| 870 | Enum->getRBraceLoc(), Enum, |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 871 | Enumerators, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 872 | nullptr, nullptr); |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 875 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 876 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 877 | } |
| 878 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 879 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 880 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 881 | |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 882 | // Create a local instantiation scope for this class template, which |
| 883 | // will contain the instantiations of the template parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 884 | LocalInstantiationScope Scope(SemaRef); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 885 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 886 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 888 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 889 | |
| 890 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 891 | |
| 892 | // Instantiate the qualifier. We have to do this first in case |
| 893 | // we're a friend declaration, because if we are then we need to put |
| 894 | // the new declaration in the appropriate context. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 895 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 896 | if (QualifierLoc) { |
| 897 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 898 | TemplateArgs); |
| 899 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 900 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 903 | CXXRecordDecl *PrevDecl = nullptr; |
| 904 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 905 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 906 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 907 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 908 | if (!Found.empty()) { |
| 909 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 910 | if (PrevClassTemplate) |
| 911 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 912 | } |
| 913 | } |
| 914 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 915 | // If this isn't a friend, then it's a member template, in which |
| 916 | // case we just want to build the instantiation in the |
| 917 | // specialization. If it is a friend, we want to build it in |
| 918 | // the appropriate context. |
| 919 | DeclContext *DC = Owner; |
| 920 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 921 | if (QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 922 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 923 | SS.Adopt(QualifierLoc); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 924 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 925 | if (!DC) return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 926 | } else { |
| 927 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 928 | Pattern->getDeclContext(), |
| 929 | TemplateArgs); |
| 930 | } |
| 931 | |
| 932 | // Look for a previous declaration of the template in the owning |
| 933 | // context. |
| 934 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| 935 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 936 | SemaRef.LookupQualifiedName(R, DC); |
| 937 | |
| 938 | if (R.isSingleResult()) { |
| 939 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 940 | if (PrevClassTemplate) |
| 941 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 942 | } |
| 943 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 944 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 945 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 946 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 947 | << QualifierLoc.getSourceRange(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 948 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 951 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 952 | if (PrevClassTemplate) { |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 953 | bool Complain = true; |
| 954 | |
| 955 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 956 | // template for struct std::tr1::__detail::_Map_base, where the |
| 957 | // template parameters of the friend declaration don't match the |
| 958 | // template parameters of the original declaration. In this one |
| 959 | // case, we don't complain about the ill-formed friend |
| 960 | // declaration. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 961 | if (isFriend && Pattern->getIdentifier() && |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 962 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 963 | DC->isNamespace() && |
| 964 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 965 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 966 | DeclContext *DCParent = DC->getParent(); |
| 967 | if (DCParent->isNamespace() && |
| 968 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 969 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 970 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 971 | Complain = false; |
| 972 | } |
| 973 | } |
| 974 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 975 | TemplateParameterList *PrevParams |
| 976 | = PrevClassTemplate->getTemplateParameters(); |
| 977 | |
| 978 | // Make sure the parameter lists match. |
| 979 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 980 | Complain, |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 981 | Sema::TPL_TemplateMatch)) { |
| 982 | if (Complain) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 983 | return nullptr; |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 984 | |
| 985 | AdoptedPreviousTemplateParams = true; |
| 986 | InstParams = PrevParams; |
| 987 | } |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 988 | |
| 989 | // Do some additional validation, then merge default arguments |
| 990 | // from the existing declarations. |
Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 991 | if (!AdoptedPreviousTemplateParams && |
| 992 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 993 | Sema::TPC_ClassTemplate)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 994 | return nullptr; |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 998 | CXXRecordDecl *RecordInst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 999 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1000 | Pattern->getLocStart(), Pattern->getLocation(), |
| 1001 | Pattern->getIdentifier(), PrevDecl, |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1002 | /*DelayTypeCreation=*/true); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1004 | if (QualifierLoc) |
| 1005 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1006 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1007 | ClassTemplateDecl *Inst |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1008 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| 1009 | D->getIdentifier(), InstParams, RecordInst, |
| 1010 | PrevClassTemplate); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1011 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1012 | |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1013 | if (isFriend) { |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1014 | if (PrevClassTemplate) |
| 1015 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1016 | else |
| 1017 | Inst->setAccess(D->getAccess()); |
| 1018 | |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1019 | Inst->setObjectOfFriendDecl(); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1020 | // TODO: do we want to track the instantiation progeny of this |
| 1021 | // friend target decl? |
| 1022 | } else { |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1023 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1024 | if (!PrevClassTemplate) |
| 1025 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1026 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1028 | // Trigger creation of the type for the instantiation. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1029 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1030 | Inst->getInjectedClassNameSpecialization()); |
John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1032 | // Finish handling of friends. |
John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1033 | if (isFriend) { |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1034 | DC->makeDeclVisibleInContext(Inst); |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1035 | Inst->setLexicalDeclContext(Owner); |
| 1036 | RecordInst->setLexicalDeclContext(Owner); |
Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1037 | return Inst; |
Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1038 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1039 | |
Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1040 | if (D->isOutOfLine()) { |
| 1041 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1042 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1043 | } |
| 1044 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1045 | Owner->addDecl(Inst); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1046 | |
| 1047 | if (!PrevClassTemplate) { |
| 1048 | // Queue up any out-of-line partial specializations of this member |
| 1049 | // class template; the client will force their instantiation once |
| 1050 | // the enclosing class has been instantiated. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1051 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1052 | D->getPartialSpecializations(PartialSpecs); |
| 1053 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1054 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1055 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1056 | } |
| 1057 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1058 | return Inst; |
| 1059 | } |
| 1060 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1061 | Decl * |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1062 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1063 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1064 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1065 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1066 | // Lookup the already-instantiated declaration in the instantiation |
| 1067 | // of the class template and return that. |
| 1068 | DeclContext::lookup_result Found |
| 1069 | = Owner->lookup(ClassTemplate->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1070 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1071 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1073 | ClassTemplateDecl *InstClassTemplate |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1074 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1075 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1076 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1078 | if (ClassTemplatePartialSpecializationDecl *Result |
| 1079 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1080 | return Result; |
| 1081 | |
| 1082 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1085 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1086 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1087 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1088 | |
| 1089 | // Create a local instantiation scope for this variable template, which |
| 1090 | // will contain the instantiations of the template parameters. |
| 1091 | LocalInstantiationScope Scope(SemaRef); |
| 1092 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1093 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1094 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1095 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1096 | |
| 1097 | VarDecl *Pattern = D->getTemplatedDecl(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1098 | VarTemplateDecl *PrevVarTemplate = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1099 | |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1100 | if (getPreviousDeclForInstantiation(Pattern)) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1101 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1102 | if (!Found.empty()) |
| 1103 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1104 | } |
| 1105 | |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1106 | VarDecl *VarInst = |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1107 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1108 | /*InstantiatingVarTemplate=*/true)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1109 | |
| 1110 | DeclContext *DC = Owner; |
| 1111 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1112 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1113 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1114 | VarInst); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1115 | VarInst->setDescribedVarTemplate(Inst); |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1116 | Inst->setPreviousDecl(PrevVarTemplate); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1117 | |
| 1118 | Inst->setAccess(D->getAccess()); |
| 1119 | if (!PrevVarTemplate) |
| 1120 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1121 | |
| 1122 | if (D->isOutOfLine()) { |
| 1123 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1124 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1125 | } |
| 1126 | |
| 1127 | Owner->addDecl(Inst); |
| 1128 | |
| 1129 | if (!PrevVarTemplate) { |
| 1130 | // Queue up any out-of-line partial specializations of this member |
| 1131 | // variable template; the client will force their instantiation once |
| 1132 | // the enclosing class has been instantiated. |
| 1133 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1134 | D->getPartialSpecializations(PartialSpecs); |
| 1135 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1136 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1137 | OutOfLineVarPartialSpecs.push_back( |
| 1138 | std::make_pair(Inst, PartialSpecs[I])); |
| 1139 | } |
| 1140 | |
| 1141 | return Inst; |
| 1142 | } |
| 1143 | |
| 1144 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1145 | VarTemplatePartialSpecializationDecl *D) { |
| 1146 | assert(D->isStaticDataMember() && |
| 1147 | "Only static data member templates are allowed."); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1148 | |
| 1149 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1150 | |
| 1151 | // Lookup the already-instantiated declaration and return that. |
| 1152 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1153 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1154 | |
| 1155 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1156 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1157 | |
| 1158 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1159 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1160 | return Result; |
| 1161 | |
| 1162 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1163 | } |
| 1164 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1165 | Decl * |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1166 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1167 | // Create a local instantiation scope for this function template, which |
| 1168 | // will contain the instantiations of the template parameters and then get |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1169 | // merged with the local instantiation scope for the function template |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1170 | // itself. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1171 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1172 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1173 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1174 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1176 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1177 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1178 | FunctionDecl *Instantiated = nullptr; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1179 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1180 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1181 | InstParams)); |
| 1182 | else |
| 1183 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1184 | D->getTemplatedDecl(), |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1185 | InstParams)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1186 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1187 | if (!Instantiated) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1188 | return nullptr; |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1189 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | // Link the instantiated function template declaration to the function |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1191 | // template from which it was instantiated. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1192 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1193 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1194 | InstTemplate->setAccess(D->getAccess()); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1195 | assert(InstTemplate && |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1196 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1197 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1198 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1199 | |
John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1200 | // Link the instantiation back to the pattern *unless* this is a |
| 1201 | // non-definition friend declaration. |
| 1202 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1203 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1204 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1205 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1206 | // Make declarations visible in the appropriate context. |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1207 | if (!isFriend) { |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1208 | Owner->addDecl(InstTemplate); |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1209 | } else if (InstTemplate->getDeclContext()->isRecord() && |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1210 | !getPreviousDeclForInstantiation(D)) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1211 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1212 | } |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1214 | return InstTemplate; |
| 1215 | } |
| 1216 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1217 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1218 | CXXRecordDecl *PrevDecl = nullptr; |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1219 | if (D->isInjectedClassName()) |
| 1220 | PrevDecl = cast<CXXRecordDecl>(Owner); |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1221 | else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1222 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1223 | PatternPrev, |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1224 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1225 | if (!Prev) return nullptr; |
John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1226 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1227 | } |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1228 | |
| 1229 | CXXRecordDecl *Record |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1231 | D->getLocStart(), D->getLocation(), |
| 1232 | D->getIdentifier(), PrevDecl); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Substitute the nested name specifier, if any. |
| 1235 | if (SubstQualifier(D, Record)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1236 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1238 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1239 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1240 | // the tag decls introduced by friend class declarations don't have an access |
| 1241 | // specifier. Remove once this area of the code gets sorted out. |
| 1242 | if (D->getAccess() != AS_none) |
| 1243 | Record->setAccess(D->getAccess()); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1244 | if (!D->isInjectedClassName()) |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1245 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1246 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1247 | // If the original function was part of a friend declaration, |
| 1248 | // inherit its namespace state. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1249 | if (D->getFriendObjectKind()) |
| 1250 | Record->setObjectOfFriendDecl(); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1252 | // Make sure that anonymous structs and unions are recorded. |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1253 | if (D->isAnonymousStructOrUnion()) |
Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1254 | Record->setAnonymousStructOrUnion(true); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (D->isLocalClass()) |
| 1257 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1258 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1259 | // Forward the mangling number from the template to the instantiated decl. |
| 1260 | SemaRef.Context.setManglingNumber(Record, |
| 1261 | SemaRef.Context.getManglingNumber(D)); |
| 1262 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1263 | Owner->addDecl(Record); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1264 | |
| 1265 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1266 | // of the instantiation of their enclosing entity. |
| 1267 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1268 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1269 | TSK_ImplicitInstantiation, |
| 1270 | /*Complain=*/true); |
| 1271 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1272 | TSK_ImplicitInstantiation); |
David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1273 | } |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1274 | |
| 1275 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1276 | |
Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1277 | return Record; |
| 1278 | } |
| 1279 | |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1280 | /// \brief Adjust the given function type for an instantiation of the |
| 1281 | /// given declaration, to cope with modifications to the function's type that |
| 1282 | /// aren't reflected in the type-source information. |
| 1283 | /// |
| 1284 | /// \param D The declaration we're instantiating. |
| 1285 | /// \param TInfo The already-instantiated type. |
| 1286 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1287 | FunctionDecl *D, |
| 1288 | TypeSourceInfo *TInfo) { |
Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1289 | const FunctionProtoType *OrigFunc |
| 1290 | = D->getType()->castAs<FunctionProtoType>(); |
| 1291 | const FunctionProtoType *NewFunc |
| 1292 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1293 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1294 | return TInfo->getType(); |
| 1295 | |
| 1296 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1297 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1298 | return Context.getFunctionType(NewFunc->getReturnType(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1299 | NewFunc->getParamTypes(), NewEPI); |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1302 | /// Normal class members are of more specific types and therefore |
| 1303 | /// don't make it here. This function serves two purposes: |
| 1304 | /// 1) instantiating function templates |
| 1305 | /// 2) substituting friend declarations |
Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1306 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1307 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1308 | // Check whether there is already a function template specialization for |
| 1309 | // this declaration. |
| 1310 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1311 | if (FunctionTemplate && !TemplateParams) { |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1312 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1314 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1315 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1316 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1318 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1319 | if (SpecFunc) |
| 1320 | return SpecFunc; |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1321 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1323 | bool isFriend; |
| 1324 | if (FunctionTemplate) |
| 1325 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1326 | else |
| 1327 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1328 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1329 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1330 | Owner->isFunctionOrMethod() || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1331 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1332 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1333 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1335 | SmallVector<ParmVarDecl *, 4> Params; |
David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1336 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1337 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1338 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1339 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1341 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1342 | if (QualifierLoc) { |
| 1343 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1344 | TemplateArgs); |
| 1345 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1346 | return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1349 | // If we're instantiating a local function declaration, put the result |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1350 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1351 | // context. |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1352 | DeclContext *DC; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1353 | if (D->isLocalExternDecl()) { |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1354 | DC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1355 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1356 | } else if (isFriend && QualifierLoc) { |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1357 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1358 | SS.Adopt(QualifierLoc); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1359 | DC = SemaRef.computeDeclContext(SS); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1360 | if (!DC) return nullptr; |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1361 | } else { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1362 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1363 | TemplateArgs); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1364 | } |
John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1365 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1366 | FunctionDecl *Function = |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1367 | FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
Abramo Bagnara | 0d4fce1 | 2012-10-04 21:40:42 +0000 | [diff] [blame] | 1368 | D->getNameInfo(), T, TInfo, |
Rafael Espindola | 9dd86de | 2013-04-16 02:29:15 +0000 | [diff] [blame] | 1369 | D->getCanonicalDecl()->getStorageClass(), |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1370 | D->isInlineSpecified(), D->hasWrittenPrototype(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1371 | D->isConstexpr()); |
Enea Zaffanella | 25723ce | 2013-07-19 18:02:36 +0000 | [diff] [blame] | 1372 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1373 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1374 | if (D->isInlined()) |
| 1375 | Function->setImplicitlyInline(); |
| 1376 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1377 | if (QualifierLoc) |
| 1378 | Function->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1379 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1380 | if (D->isLocalExternDecl()) |
| 1381 | Function->setLocalExternDecl(); |
| 1382 | |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1383 | DeclContext *LexicalDC = Owner; |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1384 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1385 | assert(D->getDeclContext()->isFileContext()); |
| 1386 | LexicalDC = D->getDeclContext(); |
| 1387 | } |
| 1388 | |
| 1389 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1391 | // Attach the parameters |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1392 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1393 | if (Params[P]) |
| 1394 | Params[P]->setOwningFunction(Function); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1395 | Function->setParams(Params); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1396 | |
Douglas Gregor | 1cd6ea0 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1397 | SourceLocation InstantiateAtPOI; |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1398 | if (TemplateParams) { |
| 1399 | // Our resulting instantiation is actually a function template, since we |
| 1400 | // are substituting only the outer template parameters. For example, given |
| 1401 | // |
| 1402 | // template<typename T> |
| 1403 | // struct X { |
| 1404 | // template<typename U> friend void f(T, U); |
| 1405 | // }; |
| 1406 | // |
| 1407 | // X<int> x; |
| 1408 | // |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1409 | // We are instantiating the friend function template "f" within X<int>, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1410 | // which means substituting int for T, but leaving "f" as a friend function |
| 1411 | // template. |
| 1412 | // Build the function template itself. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1413 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1414 | Function->getLocation(), |
| 1415 | Function->getDeclName(), |
| 1416 | TemplateParams, Function); |
| 1417 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1418 | |
| 1419 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1420 | |
| 1421 | if (isFriend && D->isThisDeclarationADefinition()) { |
| 1422 | // TODO: should we remember this connection regardless of whether |
| 1423 | // the friend declaration provided a body? |
| 1424 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1425 | D->getDescribedFunctionTemplate()); |
| 1426 | } |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1427 | } else if (FunctionTemplate) { |
| 1428 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1429 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1430 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1431 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1432 | Innermost.begin(), |
| 1433 | Innermost.size()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1434 | /*InsertPos=*/nullptr); |
Chandler Carruth | 48b2831 | 2011-08-18 09:09:59 +0000 | [diff] [blame] | 1435 | } else if (isFriend) { |
| 1436 | // Note, we need this connection even if the friend doesn't have a body. |
| 1437 | // Its body may exist but not have been attached yet due to deferred |
| 1438 | // parsing. |
| 1439 | // FIXME: It might be cleaner to set this when attaching the body to the |
| 1440 | // friend function declaration, however that would require finding all the |
| 1441 | // instantiations and modifying them. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1442 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1443 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1444 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1445 | if (InitFunctionInstantiation(Function, D)) |
| 1446 | Function->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1448 | bool isExplicitSpecialization = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1449 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1450 | LookupResult Previous( |
| 1451 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1452 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1453 | : Sema::LookupOrdinaryName, |
| 1454 | Sema::ForRedeclaration); |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1455 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1456 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1457 | = D->getDependentSpecializationInfo()) { |
| 1458 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1459 | |
| 1460 | // This needs to be set now for future sanity. |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1461 | Function->setObjectOfFriendDecl(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Instantiate the explicit template arguments. |
| 1464 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1465 | Info->getRAngleLoc()); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1466 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1467 | ExplicitArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1468 | return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Map the candidate templates to their instantiations. |
| 1471 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1472 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1473 | Info->getTemplate(I), |
| 1474 | TemplateArgs); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1475 | if (!Temp) return nullptr; |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1476 | |
| 1477 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1478 | } |
| 1479 | |
| 1480 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1481 | &ExplicitArgs, |
| 1482 | Previous)) |
| 1483 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1484 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1485 | isExplicitSpecialization = true; |
| 1486 | |
| 1487 | } else if (TemplateParams || !FunctionTemplate) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1488 | // Look only into the namespace where the friend would be declared to |
| 1489 | // find a previous declaration. This is the innermost enclosing namespace, |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1490 | // as described in ActOnFriendFunctionDecl. |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1491 | SemaRef.LookupQualifiedName(Previous, DC); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1493 | // In C++, the previous declaration we find might be a tag type |
| 1494 | // (class or enum). In this case, the new declaration will hide the |
| 1495 | // tag type. Note that this does does not apply if we're declaring a |
| 1496 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1497 | if (Previous.isSingleTagDecl()) |
| 1498 | Previous.clear(); |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1499 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1500 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1501 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1502 | isExplicitSpecialization); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1503 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1504 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1505 | ? cast<NamedDecl>(FunctionTemplate) |
| 1506 | : Function); |
| 1507 | |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1508 | // If the original function was part of a friend declaration, |
| 1509 | // inherit its namespace state and add it to the owner. |
John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1510 | if (isFriend) { |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1511 | PrincipalDecl->setObjectOfFriendDecl(); |
Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1512 | DC->makeDeclVisibleInContext(PrincipalDecl); |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1513 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1514 | bool QueuedInstantiation = false; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1515 | |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1516 | // C++11 [temp.friend]p4 (DR329): |
| 1517 | // When a function is defined in a friend function declaration in a class |
| 1518 | // template, the function is instantiated when the function is odr-used. |
| 1519 | // The same restrictions on multiple declarations and definitions that |
| 1520 | // apply to non-template function declarations and definitions also apply |
| 1521 | // to these implicit definitions. |
| 1522 | if (D->isThisDeclarationADefinition()) { |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1523 | // Check for a function body. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1524 | const FunctionDecl *Definition = nullptr; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1525 | if (Function->isDefined(Definition) && |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1526 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1527 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1528 | << Function->getDeclName(); |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1529 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1530 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1531 | // Check for redefinitions due to other instantiations of this or |
| 1532 | // a similar friend function. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1533 | else for (auto R : Function->redecls()) { |
| 1534 | if (R == Function) |
Gabor Greif | 122f1eb | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1535 | continue; |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1536 | |
| 1537 | // If some prior declaration of this function has been used, we need |
| 1538 | // to instantiate its definition. |
| 1539 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1540 | if (MemberSpecializationInfo *MSInfo = |
| 1541 | Function->getMemberSpecializationInfo()) { |
| 1542 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1543 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1544 | MSInfo->setPointOfInstantiation(Loc); |
| 1545 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1546 | std::make_pair(Function, Loc)); |
| 1547 | QueuedInstantiation = true; |
Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | // If some prior declaration of this function was a friend with an |
| 1553 | // uninstantiated definition, reject it. |
| 1554 | if (R->getFriendObjectKind()) { |
| 1555 | if (const FunctionDecl *RPattern = |
| 1556 | R->getTemplateInstantiationPattern()) { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1557 | if (RPattern->isDefined(RPattern)) { |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1558 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1559 | << Function->getDeclName(); |
Gabor Greif | ae849e4 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1560 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1561 | break; |
| 1562 | } |
Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1563 | } |
Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1569 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1570 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1571 | |
John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1572 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1573 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1574 | PrincipalDecl->setNonMemberOperator(); |
| 1575 | |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1576 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1577 | return Function; |
| 1578 | } |
| 1579 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1580 | Decl * |
| 1581 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1582 | TemplateParameterList *TemplateParams, |
| 1583 | bool IsClassScopeSpecialization) { |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1584 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1585 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | // We are creating a function template specialization from a function |
| 1587 | // template. Check whether there is already a function template |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1588 | // specialization for this particular set of template arguments. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1589 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1591 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1592 | FunctionDecl *SpecFunc |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1593 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1595 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1596 | if (SpecFunc) |
| 1597 | return SpecFunc; |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1600 | bool isFriend; |
| 1601 | if (FunctionTemplate) |
| 1602 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1603 | else |
| 1604 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1605 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1606 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1607 | !(isa<Decl>(Owner) && |
Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1608 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1609 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1610 | |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1611 | // Instantiate enclosing template arguments for friends. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1612 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1613 | unsigned NumTempParamLists = 0; |
| 1614 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 1615 | TempParamLists.set_size(NumTempParamLists); |
| 1616 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1617 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1618 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1619 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1620 | return nullptr; |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1621 | TempParamLists[I] = InstParams; |
| 1622 | } |
| 1623 | } |
| 1624 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1625 | SmallVector<ParmVarDecl *, 4> Params; |
Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1626 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1627 | if (!TInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1628 | return nullptr; |
Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1629 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1630 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1631 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1632 | if (QualifierLoc) { |
| 1633 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1634 | TemplateArgs); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1635 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1636 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | DeclContext *DC = Owner; |
| 1640 | if (isFriend) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1641 | if (QualifierLoc) { |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1642 | CXXScopeSpec SS; |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1643 | SS.Adopt(QualifierLoc); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1644 | DC = SemaRef.computeDeclContext(SS); |
John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1647 | return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1648 | } else { |
| 1649 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1650 | D->getDeclContext(), |
| 1651 | TemplateArgs); |
| 1652 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1653 | if (!DC) return nullptr; |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1656 | // Build the instantiated method declaration. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1657 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1658 | CXXMethodDecl *Method = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1660 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1661 | DeclarationNameInfo NameInfo |
| 1662 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1663 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1665 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | Constructor->isExplicit(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1667 | Constructor->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1668 | false, Constructor->isConstexpr()); |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1669 | |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1670 | // Claim that the instantiation of a constructor or constructor template |
| 1671 | // inherits the same constructor that the template does. |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1672 | if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>( |
| 1673 | Constructor->getInheritedConstructor())) { |
| 1674 | // If we're instantiating a specialization of a function template, our |
| 1675 | // "inherited constructor" will actually itself be a function template. |
| 1676 | // Instantiate a declaration of it, too. |
| 1677 | if (FunctionTemplate) { |
| 1678 | assert(!TemplateParams && Inh->getDescribedFunctionTemplate() && |
| 1679 | !Inh->getParent()->isDependentContext() && |
| 1680 | "inheriting constructor template in dependent context?"); |
| 1681 | Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(), |
| 1682 | Inh); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 1683 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1684 | return nullptr; |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1685 | Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext()); |
| 1686 | LocalInstantiationScope LocalScope(SemaRef); |
| 1687 | |
| 1688 | // Use the same template arguments that we deduced for the inheriting |
| 1689 | // constructor. There's no way they could be deduced differently. |
| 1690 | MultiLevelTemplateArgumentList InheritedArgs; |
| 1691 | InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost()); |
| 1692 | Inh = cast_or_null<CXXConstructorDecl>( |
| 1693 | SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs)); |
| 1694 | if (!Inh) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1695 | return nullptr; |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1696 | } |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1697 | cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh); |
Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1698 | } |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1699 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1700 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1701 | StartLoc, NameInfo, T, TInfo, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1702 | Destructor->isInlineSpecified(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1703 | false); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1704 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1705 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1706 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1707 | Conversion->isInlineSpecified(), |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1708 | Conversion->isExplicit(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1709 | Conversion->isConstexpr(), |
Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1710 | Conversion->getLocEnd()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1711 | } else { |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1712 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1713 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1714 | StartLoc, NameInfo, T, TInfo, |
Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1715 | SC, D->isInlineSpecified(), |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1716 | D->isConstexpr(), D->getLocEnd()); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1717 | } |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1718 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1719 | if (D->isInlined()) |
| 1720 | Method->setImplicitlyInline(); |
| 1721 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1722 | if (QualifierLoc) |
| 1723 | Method->setQualifierInfo(QualifierLoc); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1725 | if (TemplateParams) { |
| 1726 | // Our resulting instantiation is actually a function template, since we |
| 1727 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | // |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1729 | // template<typename T> |
| 1730 | // struct X { |
| 1731 | // template<typename U> void f(T, U); |
| 1732 | // }; |
| 1733 | // |
| 1734 | // X<int> x; |
| 1735 | // |
| 1736 | // We are instantiating the member template "f" within X<int>, which means |
| 1737 | // substituting int for T, but leaving "f" as a member function template. |
| 1738 | // Build the function template itself. |
| 1739 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1740 | Method->getLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | Method->getDeclName(), |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1742 | TemplateParams, Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1743 | if (isFriend) { |
| 1744 | FunctionTemplate->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1745 | FunctionTemplate->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1746 | } else if (D->isOutOfLine()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1748 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1749 | } else if (FunctionTemplate) { |
| 1750 | // Record this function template specialization. |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1751 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1752 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1753 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1754 | Innermost.begin(), |
| 1755 | Innermost.size()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1756 | /*InsertPos=*/nullptr); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1757 | } else if (!isFriend) { |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1758 | // Record that this is an instantiation of a member function. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1759 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1760 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1761 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | // If we are instantiating a member function defined |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1763 | // out-of-line, the instantiation will have the same lexical |
| 1764 | // context (which will be a namespace scope) as the template. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1765 | if (isFriend) { |
John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1766 | if (NumTempParamLists) |
| 1767 | Method->setTemplateParameterListsInfo(SemaRef.Context, |
| 1768 | NumTempParamLists, |
| 1769 | TempParamLists.data()); |
| 1770 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1771 | Method->setLexicalDeclContext(Owner); |
Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1772 | Method->setObjectOfFriendDecl(); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1773 | } else if (D->isOutOfLine()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1774 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1776 | // Attach the parameters |
| 1777 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1778 | Params[P]->setOwningFunction(Method); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1779 | Method->setParams(Params); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1780 | |
| 1781 | if (InitMethodInstantiation(Method, D)) |
| 1782 | Method->setInvalidDecl(); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1783 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1784 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 1785 | Sema::ForRedeclaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1787 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1788 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1790 | // In C++, the previous declaration we find might be a tag type |
| 1791 | // (class or enum). In this case, the new declaration will hide the |
| 1792 | // tag type. Note that this does does not apply if we're declaring a |
| 1793 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1794 | if (Previous.isSingleTagDecl()) |
| 1795 | Previous.clear(); |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1796 | } |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1797 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1798 | if (!IsClassScopeSpecialization) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1799 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, false); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1801 | if (D->isPure()) |
| 1802 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1803 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1804 | // Propagate access. For a non-friend declaration, the access is |
| 1805 | // whatever we're propagating from. For a friend, it should be the |
| 1806 | // previous declaration we just found. |
| 1807 | if (isFriend && Method->getPreviousDecl()) |
| 1808 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
| 1809 | else |
| 1810 | Method->setAccess(D->getAccess()); |
| 1811 | if (FunctionTemplate) |
| 1812 | FunctionTemplate->setAccess(Method->getAccess()); |
John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 1814 | SemaRef.CheckOverrideControl(Method); |
| 1815 | |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1816 | // 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] | 1817 | if (D->isExplicitlyDefaulted()) |
| 1818 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1819 | if (D->isDeletedAsWritten()) |
Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1820 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1821 | |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1822 | // If there's a function template, let our caller handle it. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1823 | if (FunctionTemplate) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1824 | // do nothing |
| 1825 | |
| 1826 | // Don't hide a (potentially) valid declaration with an invalid one. |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1827 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1828 | // do nothing |
| 1829 | |
| 1830 | // Otherwise, check access to friends and make them visible. |
| 1831 | } else if (isFriend) { |
| 1832 | // We only need to re-check access for methods which we didn't |
| 1833 | // manage to match during parsing. |
| 1834 | if (!D->getPreviousDecl()) |
| 1835 | SemaRef.CheckFriendAccess(Method); |
| 1836 | |
| 1837 | Record->makeDeclVisibleInContext(Method); |
| 1838 | |
| 1839 | // Otherwise, add the declaration. We don't need to do this for |
| 1840 | // class-scope specializations because we'll have matched them with |
| 1841 | // the appropriate template. |
| 1842 | } else if (!IsClassScopeSpecialization) { |
| 1843 | Owner->addDecl(Method); |
John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1844 | } |
Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1846 | return Method; |
| 1847 | } |
| 1848 | |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1849 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1850 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1853 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1854 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1857 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1858 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 1861 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1862 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 1863 | /*ExpectParameterPack=*/ false); |
Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1866 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1867 | TemplateTypeParmDecl *D) { |
| 1868 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1869 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1871 | TemplateTypeParmDecl *Inst = |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 1872 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, |
| 1873 | D->getLocStart(), D->getLocation(), |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1874 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1875 | D->getIndex(), D->getIdentifier(), |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1876 | D->wasDeclaredWithTypename(), |
| 1877 | D->isParameterPack()); |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1878 | Inst->setAccess(AS_public); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1879 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 1880 | if (D->hasDefaultArgument()) { |
| 1881 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1882 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 1883 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 1884 | if (InstantiatedDefaultArg) |
| 1885 | Inst->setDefaultArgument(InstantiatedDefaultArg, false); |
| 1886 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1887 | |
| 1888 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1889 | // scope. |
| 1890 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1891 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1892 | return Inst; |
| 1893 | } |
| 1894 | |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1895 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1896 | NonTypeTemplateParmDecl *D) { |
| 1897 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1898 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1899 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 1900 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1901 | bool IsExpandedParameterPack = false; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1902 | TypeSourceInfo *DI; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1903 | QualType T; |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1904 | bool Invalid = false; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1905 | |
| 1906 | if (D->isExpandedParameterPack()) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1907 | // The non-type template parameter pack is an already-expanded pack |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1908 | // expansion of types. Substitute into each of the expanded types. |
| 1909 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 1910 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 1911 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| 1912 | TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), |
| 1913 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1914 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1915 | D->getDeclName()); |
| 1916 | if (!NewDI) |
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 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1920 | QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(), |
| 1921 | D->getLocation()); |
| 1922 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1923 | return nullptr; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1924 | ExpandedParameterPackTypes.push_back(NewT); |
| 1925 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1927 | IsExpandedParameterPack = true; |
| 1928 | DI = D->getTypeSourceInfo(); |
| 1929 | T = DI->getType(); |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1930 | } else if (D->isPackExpansion()) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1931 | // The non-type template parameter pack's type is a pack expansion of types. |
| 1932 | // Determine whether we need to expand this parameter pack into separate |
| 1933 | // types. |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1934 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1935 | TypeLoc Pattern = Expansion.getPatternLoc(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1936 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1937 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1939 | // Determine whether the set of unexpanded parameter packs can and should |
| 1940 | // be expanded. |
| 1941 | bool Expand = true; |
| 1942 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1943 | Optional<unsigned> OrigNumExpansions |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1944 | = Expansion.getTypePtr()->getNumExpansions(); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1945 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1946 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 1947 | Pattern.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 1948 | Unexpanded, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1949 | TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1950 | Expand, RetainExpansion, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1951 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1952 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1954 | if (Expand) { |
| 1955 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1956 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 1957 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1958 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1959 | D->getDeclName()); |
| 1960 | if (!NewDI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1961 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1962 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1963 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1964 | QualType NewT = SemaRef.CheckNonTypeTemplateParameterType( |
| 1965 | NewDI->getType(), |
| 1966 | D->getLocation()); |
| 1967 | if (NewT.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1968 | return nullptr; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1969 | ExpandedParameterPackTypes.push_back(NewT); |
| 1970 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1972 | // Note that we have an expanded parameter pack. The "type" of this |
| 1973 | // expanded parameter pack is the original expansion type, but callers |
| 1974 | // will end up using the expanded parameter pack types for type-checking. |
| 1975 | IsExpandedParameterPack = true; |
| 1976 | DI = D->getTypeSourceInfo(); |
| 1977 | T = DI->getType(); |
| 1978 | } else { |
| 1979 | // We cannot fully expand the pack expansion now, so substitute into the |
| 1980 | // pattern and create a new pack expansion type. |
| 1981 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1982 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1983 | D->getLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1984 | D->getDeclName()); |
| 1985 | if (!NewPattern) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1986 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1987 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1988 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 1989 | NumExpansions); |
| 1990 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1991 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1993 | T = DI->getType(); |
| 1994 | } |
| 1995 | } else { |
| 1996 | // Simple case: substitution into a parameter that is not a parameter pack. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1997 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1998 | D->getLocation(), D->getDeclName()); |
| 1999 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2000 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2002 | // Check that this type is acceptable for a non-type template parameter. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2003 | T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2004 | D->getLocation()); |
| 2005 | if (T.isNull()) { |
| 2006 | T = SemaRef.Context.IntTy; |
| 2007 | Invalid = true; |
| 2008 | } |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2009 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2011 | NonTypeTemplateParmDecl *Param; |
| 2012 | if (IsExpandedParameterPack) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2013 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2014 | D->getInnerLocStart(), |
| 2015 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2016 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 2017 | D->getPosition(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2018 | D->getIdentifier(), T, |
| 2019 | DI, |
| 2020 | ExpandedParameterPackTypes.data(), |
| 2021 | ExpandedParameterPackTypes.size(), |
| 2022 | ExpandedParameterPackTypesAsWritten.data()); |
| 2023 | else |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2024 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2025 | D->getInnerLocStart(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2026 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2027 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 2028 | D->getPosition(), |
| 2029 | D->getIdentifier(), T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2030 | D->isParameterPack(), DI); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2032 | Param->setAccess(AS_public); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2033 | if (Invalid) |
| 2034 | Param->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2035 | |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2036 | if (D->hasDefaultArgument()) { |
| 2037 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 2038 | if (!Value.isInvalid()) |
| 2039 | Param->setDefaultArgument(Value.get(), false); |
| 2040 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2041 | |
| 2042 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2043 | // scope. |
| 2044 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2045 | return Param; |
| 2046 | } |
| 2047 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2048 | static void collectUnexpandedParameterPacks( |
| 2049 | Sema &S, |
| 2050 | TemplateParameterList *Params, |
| 2051 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 2052 | for (TemplateParameterList::const_iterator I = Params->begin(), |
| 2053 | E = Params->end(); I != E; ++I) { |
| 2054 | if ((*I)->isTemplateParameterPack()) |
| 2055 | continue; |
| 2056 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I)) |
| 2057 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 2058 | Unexpanded); |
| 2059 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I)) |
| 2060 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 2061 | Unexpanded); |
| 2062 | } |
| 2063 | } |
| 2064 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2065 | Decl * |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2066 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 2067 | TemplateTemplateParmDecl *D) { |
| 2068 | // Instantiate the template parameter list of the template template parameter. |
| 2069 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 2070 | TemplateParameterList *InstParams; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2071 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 2072 | |
| 2073 | bool IsExpandedParameterPack = false; |
| 2074 | |
| 2075 | if (D->isExpandedParameterPack()) { |
| 2076 | // The template template parameter pack is an already-expanded pack |
| 2077 | // expansion of template parameters. Substitute into each of the expanded |
| 2078 | // parameters. |
| 2079 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 2080 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 2081 | I != N; ++I) { |
| 2082 | LocalInstantiationScope Scope(SemaRef); |
| 2083 | TemplateParameterList *Expansion = |
| 2084 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2085 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2086 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2087 | ExpandedParams.push_back(Expansion); |
| 2088 | } |
| 2089 | |
| 2090 | IsExpandedParameterPack = true; |
| 2091 | InstParams = TempParams; |
| 2092 | } else if (D->isPackExpansion()) { |
| 2093 | // The template template parameter pack expands to a pack of template |
| 2094 | // template parameters. Determine whether we need to expand this parameter |
| 2095 | // pack into separate parameters. |
| 2096 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2097 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2098 | Unexpanded); |
| 2099 | |
| 2100 | // Determine whether the set of unexpanded parameter packs can and should |
| 2101 | // be expanded. |
| 2102 | bool Expand = true; |
| 2103 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2104 | Optional<unsigned> NumExpansions; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2105 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2106 | TempParams->getSourceRange(), |
| 2107 | Unexpanded, |
| 2108 | TemplateArgs, |
| 2109 | Expand, RetainExpansion, |
| 2110 | NumExpansions)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2111 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2112 | |
| 2113 | if (Expand) { |
| 2114 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2115 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2116 | LocalInstantiationScope Scope(SemaRef); |
| 2117 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2118 | if (!Expansion) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2119 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2120 | ExpandedParams.push_back(Expansion); |
| 2121 | } |
| 2122 | |
| 2123 | // Note that we have an expanded parameter pack. The "type" of this |
| 2124 | // expanded parameter pack is the original expansion type, but callers |
| 2125 | // will end up using the expanded parameter pack types for type-checking. |
| 2126 | IsExpandedParameterPack = true; |
| 2127 | InstParams = TempParams; |
| 2128 | } else { |
| 2129 | // We cannot fully expand the pack expansion now, so just substitute |
| 2130 | // into the pattern. |
| 2131 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2132 | |
| 2133 | LocalInstantiationScope Scope(SemaRef); |
| 2134 | InstParams = SubstTemplateParams(TempParams); |
| 2135 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2136 | return nullptr; |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } else { |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2139 | // Perform the actual substitution of template parameters within a new, |
| 2140 | // local instantiation scope. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2141 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2142 | InstParams = SubstTemplateParams(TempParams); |
| 2143 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2144 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2147 | // Build the template template parameter. |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2148 | TemplateTemplateParmDecl *Param; |
| 2149 | if (IsExpandedParameterPack) |
| 2150 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 2151 | D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2152 | D->getDepth() - TemplateArgs.getNumLevels(), |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2153 | D->getPosition(), |
| 2154 | D->getIdentifier(), InstParams, |
| 2155 | ExpandedParams); |
| 2156 | else |
| 2157 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, |
| 2158 | D->getLocation(), |
| 2159 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 2160 | D->getPosition(), |
| 2161 | D->isParameterPack(), |
| 2162 | D->getIdentifier(), InstParams); |
David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2163 | if (D->hasDefaultArgument()) { |
| 2164 | NestedNameSpecifierLoc QualifierLoc = |
| 2165 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2166 | QualifierLoc = |
| 2167 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2168 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2169 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2170 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2171 | if (!TName.isNull()) |
| 2172 | Param->setDefaultArgument( |
| 2173 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2174 | D->getDefaultArgument().getTemplateQualifierLoc(), |
| 2175 | D->getDefaultArgument().getTemplateNameLoc()), |
| 2176 | false); |
| 2177 | } |
Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2178 | Param->setAccess(AS_public); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2179 | |
| 2180 | // Introduce this template parameter's instantiation into the instantiation |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2181 | // scope. |
| 2182 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2183 | |
Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2184 | return Param; |
| 2185 | } |
| 2186 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2187 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2188 | // Using directives are never dependent (and never contain any types or |
| 2189 | // expressions), so they require no explicit instantiation work. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2191 | UsingDirectiveDecl *Inst |
| 2192 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2193 | D->getNamespaceKeyLocation(), |
Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2194 | D->getQualifierLoc(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2195 | D->getIdentLocation(), |
| 2196 | D->getNominatedNamespace(), |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2197 | D->getCommonAncestor()); |
Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Add the using directive to its declaration context |
| 2200 | // only if this is not a function or method. |
| 2201 | if (!Owner->isFunctionOrMethod()) |
| 2202 | Owner->addDecl(Inst); |
| 2203 | |
Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2204 | return Inst; |
| 2205 | } |
| 2206 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2207 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2208 | |
| 2209 | // The nested name specifier may be dependent, for example |
| 2210 | // template <typename T> struct t { |
| 2211 | // struct s1 { T f1(); }; |
| 2212 | // struct s2 : s1 { using s1::f1; }; |
| 2213 | // }; |
| 2214 | // template struct t<int>; |
| 2215 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2216 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2217 | NestedNameSpecifierLoc QualifierLoc |
| 2218 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2219 | TemplateArgs); |
| 2220 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2221 | return nullptr; |
Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2222 | |
| 2223 | // The name info is non-dependent, so no transformation |
| 2224 | // is required. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2225 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2226 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2227 | // We only need to do redeclaration lookups if we're in a class |
| 2228 | // scope (in fact, it's not really even possible in non-class |
| 2229 | // scopes). |
| 2230 | bool CheckRedeclaration = Owner->isRecord(); |
| 2231 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2232 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 2233 | Sema::ForRedeclaration); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2234 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2235 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2236 | D->getUsingLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2237 | QualifierLoc, |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2238 | NameInfo, |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2239 | D->hasTypename()); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2241 | CXXScopeSpec SS; |
| 2242 | SS.Adopt(QualifierLoc); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2243 | if (CheckRedeclaration) { |
| 2244 | Prev.setHideTags(false); |
| 2245 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2246 | |
| 2247 | // Check for invalid redeclarations. |
Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2248 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2249 | D->hasTypename(), SS, |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2250 | D->getLocation(), Prev)) |
| 2251 | NewUD->setInvalidDecl(); |
| 2252 | |
| 2253 | } |
| 2254 | |
| 2255 | if (!NewUD->isInvalidDecl() && |
Richard Smith | 7ad0b88 | 2014-04-02 21:44:35 +0000 | [diff] [blame] | 2256 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS, NameInfo, |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2257 | D->getLocation())) |
| 2258 | NewUD->setInvalidDecl(); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2259 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2260 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2261 | NewUD->setAccess(D->getAccess()); |
| 2262 | Owner->addDecl(NewUD); |
| 2263 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2264 | // Don't process the shadow decls for an invalid decl. |
| 2265 | if (NewUD->isInvalidDecl()) |
| 2266 | return NewUD; |
| 2267 | |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2268 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { |
Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2269 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2270 | return NewUD; |
| 2271 | } |
| 2272 | |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2273 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2274 | |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2275 | // Process the shadow decls. |
Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2276 | for (auto *Shadow : D->shadows()) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2277 | NamedDecl *InstTarget = |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2278 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| 2279 | Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2280 | if (!InstTarget) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2281 | return nullptr; |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2282 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2283 | UsingShadowDecl *PrevDecl = nullptr; |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2284 | if (CheckRedeclaration) { |
| 2285 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2286 | continue; |
Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 2287 | } else if (UsingShadowDecl *OldPrev = |
| 2288 | getPreviousDeclForInstantiation(Shadow)) { |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2289 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2290 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2291 | } |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2292 | |
Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2293 | UsingShadowDecl *InstShadow = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2294 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2295 | PrevDecl); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2296 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2297 | |
| 2298 | if (isFunctionScope) |
| 2299 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2300 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2301 | |
| 2302 | return NewUD; |
| 2303 | } |
| 2304 | |
| 2305 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2306 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2307 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2310 | Decl * TemplateDeclInstantiator |
| 2311 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2312 | NestedNameSpecifierLoc QualifierLoc |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2313 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2314 | TemplateArgs); |
| 2315 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2316 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2318 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2319 | SS.Adopt(QualifierLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2321 | // 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] | 2322 | // Hence, no transformation is required for it. |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2323 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | NamedDecl *UD = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2325 | SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(), |
| 2326 | D->getUsingLoc(), SS, NameInfo, nullptr, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2327 | /*instantiation*/ true, |
| 2328 | /*typename*/ true, D->getTypenameLoc()); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2329 | if (UD) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2330 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2331 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2332 | return UD; |
| 2333 | } |
| 2334 | |
| 2335 | Decl * TemplateDeclInstantiator |
| 2336 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2337 | NestedNameSpecifierLoc QualifierLoc |
| 2338 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs); |
| 2339 | if (!QualifierLoc) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2340 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2341 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2342 | CXXScopeSpec SS; |
Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2343 | SS.Adopt(QualifierLoc); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2344 | |
Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2345 | DeclarationNameInfo NameInfo |
| 2346 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2347 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2348 | NamedDecl *UD = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2349 | SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(), |
| 2350 | D->getUsingLoc(), SS, NameInfo, nullptr, |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2351 | /*instantiation*/ true, |
| 2352 | /*typename*/ false, SourceLocation()); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2353 | if (UD) |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2354 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 2355 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2356 | return UD; |
Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2359 | |
| 2360 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
| 2361 | ClassScopeFunctionSpecializationDecl *Decl) { |
| 2362 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2363 | CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2364 | nullptr, true)); |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2365 | |
| 2366 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, |
| 2367 | Sema::ForRedeclaration); |
| 2368 | |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2369 | TemplateArgumentListInfo TemplateArgs; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2370 | TemplateArgumentListInfo *TemplateArgsPtr = nullptr; |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2371 | if (Decl->hasExplicitTemplateArgs()) { |
| 2372 | TemplateArgs = Decl->templateArgs(); |
| 2373 | TemplateArgsPtr = &TemplateArgs; |
| 2374 | } |
| 2375 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2376 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); |
Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2377 | if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr, |
| 2378 | Previous)) { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2379 | NewFD->setInvalidDecl(); |
| 2380 | return NewFD; |
| 2381 | } |
| 2382 | |
| 2383 | // Associate the specialization with the pattern. |
| 2384 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); |
| 2385 | assert(Specialization && "Class scope Specialization is null"); |
| 2386 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); |
| 2387 | |
| 2388 | return NewFD; |
| 2389 | } |
| 2390 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2391 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2392 | OMPThreadPrivateDecl *D) { |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2393 | SmallVector<Expr *, 5> Vars; |
Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2394 | for (auto *I : D->varlists()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2395 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2396 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2397 | Vars.push_back(Var); |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | OMPThreadPrivateDecl *TD = |
| 2401 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2402 | |
Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2403 | TD->setAccess(AS_public); |
| 2404 | Owner->addDecl(TD); |
| 2405 | |
Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2406 | return TD; |
| 2407 | } |
| 2408 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2409 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2410 | return VisitFunctionDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2414 | return VisitCXXMethodDecl(D, nullptr); |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 2418 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 2419 | } |
| 2420 | |
| 2421 | Decl * |
| 2422 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 2423 | ClassTemplateSpecializationDecl *D) { |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2424 | // As a MS extension, we permit class-scope explicit specialization |
| 2425 | // of member class templates. |
| 2426 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 2427 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 2428 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 2429 | "can only instantiate an explicit specialization " |
| 2430 | "for a member class template"); |
| 2431 | |
| 2432 | // Lookup the already-instantiated declaration in the instantiation |
| 2433 | // of the class template. FIXME: Diagnose or assert if this fails? |
| 2434 | DeclContext::lookup_result Found |
| 2435 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 2436 | if (Found.empty()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2437 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2438 | ClassTemplateDecl *InstClassTemplate |
| 2439 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| 2440 | if (!InstClassTemplate) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2441 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2442 | |
| 2443 | // Substitute into the template arguments of the class template explicit |
| 2444 | // specialization. |
| 2445 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 2446 | castAs<TemplateSpecializationTypeLoc>(); |
| 2447 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 2448 | Loc.getRAngleLoc()); |
| 2449 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 2450 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 2451 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 2452 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 2453 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2454 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Check that the template argument list is well-formed for this |
| 2457 | // class template. |
| 2458 | SmallVector<TemplateArgument, 4> Converted; |
| 2459 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 2460 | D->getLocation(), |
| 2461 | InstTemplateArgs, |
| 2462 | false, |
| 2463 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2464 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2465 | |
| 2466 | // Figure out where to insert this class template explicit specialization |
| 2467 | // in the member template's set of class template explicit specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2468 | void *InsertPos = nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2469 | ClassTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2470 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2471 | |
| 2472 | // Check whether we've already seen a conflicting instantiation of this |
| 2473 | // declaration (for instance, if there was a prior implicit instantiation). |
| 2474 | bool Ignored; |
| 2475 | if (PrevDecl && |
| 2476 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 2477 | D->getSpecializationKind(), |
| 2478 | PrevDecl, |
| 2479 | PrevDecl->getSpecializationKind(), |
| 2480 | PrevDecl->getPointOfInstantiation(), |
| 2481 | Ignored)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2482 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2483 | |
| 2484 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 2485 | // This happens in cases like: |
| 2486 | // |
| 2487 | // template<typename T, typename U> |
| 2488 | // struct Outer { |
| 2489 | // template<typename X> struct Inner; |
| 2490 | // template<> struct Inner<T> {}; |
| 2491 | // template<> struct Inner<U> {}; |
| 2492 | // }; |
| 2493 | // |
| 2494 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 2495 | // // have the same signature. |
| 2496 | if (PrevDecl && PrevDecl->getDefinition() && |
| 2497 | D->isThisDeclarationADefinition()) { |
| 2498 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 2499 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 2500 | diag::note_previous_definition); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2501 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | // Create the class template partial specialization declaration. |
| 2505 | ClassTemplateSpecializationDecl *InstD |
| 2506 | = ClassTemplateSpecializationDecl::Create(SemaRef.Context, |
| 2507 | D->getTagKind(), |
| 2508 | Owner, |
| 2509 | D->getLocStart(), |
| 2510 | D->getLocation(), |
| 2511 | InstClassTemplate, |
| 2512 | Converted.data(), |
| 2513 | Converted.size(), |
| 2514 | PrevDecl); |
| 2515 | |
| 2516 | // Add this partial specialization to the set of class template partial |
| 2517 | // specializations. |
| 2518 | if (!PrevDecl) |
| 2519 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 2520 | |
| 2521 | // Substitute the nested name specifier, if any. |
| 2522 | if (SubstQualifier(D, InstD)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2523 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Build the canonical type that describes the converted template |
| 2526 | // arguments of the class template explicit specialization. |
| 2527 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 2528 | TemplateName(InstClassTemplate), Converted.data(), Converted.size(), |
| 2529 | SemaRef.Context.getRecordType(InstD)); |
| 2530 | |
| 2531 | // Build the fully-sugared type for this class template |
| 2532 | // specialization as the user wrote in the specialization |
| 2533 | // itself. This means that we'll pretty-print the type retrieved |
| 2534 | // from the specialization's declaration the way that the user |
| 2535 | // actually wrote the specialization, rather than formatting the |
| 2536 | // name based on the "canonical" representation used to store the |
| 2537 | // template arguments in the specialization. |
| 2538 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2539 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 2540 | CanonType); |
| 2541 | |
| 2542 | InstD->setAccess(D->getAccess()); |
| 2543 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 2544 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 2545 | InstD->setTypeAsWritten(WrittenTy); |
| 2546 | InstD->setExternLoc(D->getExternLoc()); |
| 2547 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 2548 | |
| 2549 | Owner->addDecl(InstD); |
| 2550 | |
| 2551 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 2552 | // We don't have support for lazy instantiation of an explicit specialization |
| 2553 | // yet, and MSVC eagerly instantiates in this case. |
| 2554 | if (D->isThisDeclarationADefinition() && |
| 2555 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 2556 | TSK_ImplicitInstantiation, |
| 2557 | /*Complain=*/true)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2558 | return nullptr; |
Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2559 | |
| 2560 | return InstD; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2563 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 2564 | VarTemplateSpecializationDecl *D) { |
| 2565 | |
| 2566 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 2567 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 2568 | assert(VarTemplate && |
| 2569 | "A template specialization without specialized template?"); |
| 2570 | |
| 2571 | // Substitute the current template arguments. |
| 2572 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 2573 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 2574 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 2575 | |
| 2576 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 2577 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2578 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2579 | |
| 2580 | // Check that the template argument list is well-formed for this template. |
| 2581 | SmallVector<TemplateArgument, 4> Converted; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2582 | if (SemaRef.CheckTemplateArgumentList( |
| 2583 | VarTemplate, VarTemplate->getLocStart(), |
| 2584 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, |
Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2585 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2586 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Find the variable template specialization declaration that |
| 2589 | // corresponds to these arguments. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2590 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2591 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2592 | Converted, InsertPos)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2593 | // If we already have a variable template specialization, return it. |
| 2594 | return VarSpec; |
| 2595 | |
| 2596 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, |
| 2597 | VarTemplateArgsInfo, Converted); |
| 2598 | } |
| 2599 | |
| 2600 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 2601 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 2602 | const TemplateArgumentListInfo &TemplateArgsInfo, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 2603 | ArrayRef<TemplateArgument> Converted) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2604 | |
| 2605 | // If this is the variable for an anonymous struct or union, |
| 2606 | // instantiate the anonymous struct/union type first. |
| 2607 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 2608 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 2609 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2610 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2611 | |
| 2612 | // Do substitution on the type of the declaration |
| 2613 | TypeSourceInfo *DI = |
| 2614 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 2615 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 2616 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2617 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2618 | |
| 2619 | if (DI->getType()->isFunctionType()) { |
| 2620 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 2621 | << D->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2622 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | // Build the instantiated declaration |
| 2626 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 2627 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2628 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(), |
| 2629 | Converted.size()); |
| 2630 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2631 | if (InsertPos) |
| 2632 | VarTemplate->AddSpecialization(Var, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2633 | |
| 2634 | // Substitute the nested name specifier, if any. |
| 2635 | if (SubstQualifier(D, Var)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2636 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2637 | |
| 2638 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2639 | Owner, StartingScope); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2640 | |
| 2641 | return Var; |
| 2642 | } |
| 2643 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2644 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 2645 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 2646 | } |
| 2647 | |
| 2648 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 2649 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 2650 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 2651 | DiagnosticsEngine::Error, |
| 2652 | "cannot instantiate %0 yet"); |
| 2653 | SemaRef.Diag(D->getLocation(), DiagID) |
| 2654 | << D->getDeclKindName(); |
| 2655 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2656 | return nullptr; |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 2660 | llvm_unreachable("Unexpected decl"); |
| 2661 | } |
| 2662 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2663 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2664 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 2665 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2666 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2667 | return nullptr; |
Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2668 | |
Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 2669 | return Instantiator.Visit(D); |
| 2670 | } |
| 2671 | |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2672 | /// \brief Instantiates a nested template parameter list in the current |
| 2673 | /// instantiation context. |
| 2674 | /// |
| 2675 | /// \param L The parameter list to instantiate |
| 2676 | /// |
| 2677 | /// \returns NULL if there was an error |
| 2678 | TemplateParameterList * |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2679 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2680 | // Get errors for all the parameters before bailing out. |
| 2681 | bool Invalid = false; |
| 2682 | |
| 2683 | unsigned N = L->size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2684 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2685 | ParamVector Params; |
| 2686 | Params.reserve(N); |
| 2687 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 2688 | PI != PE; ++PI) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2689 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2690 | Params.push_back(D); |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 2691 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | // Clean up if we had an error. |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 2695 | if (Invalid) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2696 | return nullptr; |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2697 | |
| 2698 | TemplateParameterList *InstL |
| 2699 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 2700 | L->getLAngleLoc(), &Params.front(), N, |
| 2701 | L->getRAngleLoc()); |
| 2702 | return InstL; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | } |
John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2704 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2705 | /// \brief Instantiate the declaration of a class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2706 | /// specialization. |
| 2707 | /// |
| 2708 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 2709 | // specialized by the instantiation of \p PartialSpec. |
| 2710 | /// |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2711 | /// \param PartialSpec the (uninstantiated) class template partial |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2712 | /// specialization that we are instantiating. |
| 2713 | /// |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2714 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 2715 | /// NULL to indicate an error. |
| 2716 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2717 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 2718 | ClassTemplateDecl *ClassTemplate, |
| 2719 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2720 | // Create a local instantiation scope for this class template partial |
| 2721 | // specialization, which will contain the instantiations of the template |
| 2722 | // parameters. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2723 | LocalInstantiationScope Scope(SemaRef); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2725 | // Substitute into the template parameters of the class template partial |
| 2726 | // specialization. |
| 2727 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 2728 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2729 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2730 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2732 | // Substitute into the template arguments of the class template partial |
| 2733 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2734 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 2735 | = PartialSpec->getTemplateArgsAsWritten(); |
| 2736 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 2737 | TemplArgInfo->RAngleLoc); |
| 2738 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 2739 | TemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2740 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2741 | return nullptr; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2743 | // Check that the template argument list is well-formed for this |
| 2744 | // class template. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2745 | SmallVector<TemplateArgument, 4> Converted; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2746 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2747 | PartialSpec->getLocation(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2748 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2749 | false, |
| 2750 | Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2751 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2752 | |
| 2753 | // Figure out where to insert this class template partial specialization |
| 2754 | // in the member template's set of class template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2755 | void *InsertPos = nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2756 | ClassTemplateSpecializationDecl *PrevDecl |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2757 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2759 | // Build the canonical type that describes the converted template |
| 2760 | // arguments of the class template partial specialization. |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2761 | QualType CanonType |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2762 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2763 | Converted.data(), |
| 2764 | Converted.size()); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2765 | |
| 2766 | // Build the fully-sugared type for this class template |
| 2767 | // specialization as the user wrote in the specialization |
| 2768 | // itself. This means that we'll pretty-print the type retrieved |
| 2769 | // from the specialization's declaration the way that the user |
| 2770 | // actually wrote the specialization, rather than formatting the |
| 2771 | // name based on the "canonical" representation used to store the |
| 2772 | // template arguments in the specialization. |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2773 | TypeSourceInfo *WrittenTy |
| 2774 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2775 | TemplateName(ClassTemplate), |
| 2776 | PartialSpec->getLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2777 | InstTemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2778 | CanonType); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2779 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2780 | if (PrevDecl) { |
| 2781 | // We've already seen a partial specialization with the same template |
| 2782 | // parameters and template arguments. This can happen, for example, when |
| 2783 | // substituting the outer template arguments ends up causing two |
| 2784 | // class template partial specializations of a member class template |
| 2785 | // to have identical forms, e.g., |
| 2786 | // |
| 2787 | // template<typename T, typename U> |
| 2788 | // struct Outer { |
| 2789 | // template<typename X, typename Y> struct Inner; |
| 2790 | // template<typename Y> struct Inner<T, Y>; |
| 2791 | // template<typename Y> struct Inner<U, Y>; |
| 2792 | // }; |
| 2793 | // |
| 2794 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2795 | // // have the same signature. |
| 2796 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2797 | << WrittenTy->getType(); |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2798 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 2799 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2800 | return nullptr; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2801 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2802 | |
| 2803 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2804 | // Create the class template partial specialization declaration. |
| 2805 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2806 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2807 | PartialSpec->getTagKind(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2808 | Owner, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2809 | PartialSpec->getLocStart(), |
| 2810 | PartialSpec->getLocation(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2811 | InstParams, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2812 | ClassTemplate, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2813 | Converted.data(), |
| 2814 | Converted.size(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2815 | InstTemplateArgs, |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2816 | CanonType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2817 | nullptr); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2818 | // Substitute the nested name specifier, if any. |
| 2819 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2820 | return nullptr; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2821 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2822 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2823 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2824 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2825 | // Add this partial specialization to the set of class template partial |
| 2826 | // specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2827 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 2828 | /*InsertPos=*/nullptr); |
Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2829 | return InstPartialSpec; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2832 | /// \brief Instantiate the declaration of a variable template partial |
| 2833 | /// specialization. |
| 2834 | /// |
| 2835 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 2836 | /// specialized by the instantiation of \p PartialSpec. |
| 2837 | /// |
| 2838 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 2839 | /// specialization that we are instantiating. |
| 2840 | /// |
| 2841 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 2842 | /// NULL to indicate an error. |
| 2843 | VarTemplatePartialSpecializationDecl * |
| 2844 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 2845 | VarTemplateDecl *VarTemplate, |
| 2846 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 2847 | // Create a local instantiation scope for this variable template partial |
| 2848 | // specialization, which will contain the instantiations of the template |
| 2849 | // parameters. |
| 2850 | LocalInstantiationScope Scope(SemaRef); |
| 2851 | |
| 2852 | // Substitute into the template parameters of the variable template partial |
| 2853 | // specialization. |
| 2854 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 2855 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2856 | if (!InstParams) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2857 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2858 | |
| 2859 | // Substitute into the template arguments of the variable template partial |
| 2860 | // specialization. |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2861 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 2862 | = PartialSpec->getTemplateArgsAsWritten(); |
| 2863 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 2864 | TemplArgInfo->RAngleLoc); |
| 2865 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 2866 | TemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2867 | InstTemplateArgs, TemplateArgs)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2868 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Check that the template argument list is well-formed for this |
| 2871 | // class template. |
| 2872 | SmallVector<TemplateArgument, 4> Converted; |
| 2873 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 2874 | InstTemplateArgs, false, Converted)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2875 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2876 | |
| 2877 | // Figure out where to insert this variable template partial specialization |
| 2878 | // in the member template's set of variable template partial specializations. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2879 | void *InsertPos = nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2880 | VarTemplateSpecializationDecl *PrevDecl = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2881 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Build the canonical type that describes the converted template |
| 2884 | // arguments of the variable template partial specialization. |
| 2885 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| 2886 | TemplateName(VarTemplate), Converted.data(), Converted.size()); |
| 2887 | |
| 2888 | // Build the fully-sugared type for this variable template |
| 2889 | // specialization as the user wrote in the specialization |
| 2890 | // itself. This means that we'll pretty-print the type retrieved |
| 2891 | // from the specialization's declaration the way that the user |
| 2892 | // actually wrote the specialization, rather than formatting the |
| 2893 | // name based on the "canonical" representation used to store the |
| 2894 | // template arguments in the specialization. |
| 2895 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2896 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 2897 | CanonType); |
| 2898 | |
| 2899 | if (PrevDecl) { |
| 2900 | // We've already seen a partial specialization with the same template |
| 2901 | // parameters and template arguments. This can happen, for example, when |
| 2902 | // substituting the outer template arguments ends up causing two |
| 2903 | // variable template partial specializations of a member variable template |
| 2904 | // to have identical forms, e.g., |
| 2905 | // |
| 2906 | // template<typename T, typename U> |
| 2907 | // struct Outer { |
| 2908 | // template<typename X, typename Y> pair<X,Y> p; |
| 2909 | // template<typename Y> pair<T, Y> p; |
| 2910 | // template<typename Y> pair<U, Y> p; |
| 2911 | // }; |
| 2912 | // |
| 2913 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2914 | // // have the same signature. |
| 2915 | SemaRef.Diag(PartialSpec->getLocation(), |
| 2916 | diag::err_var_partial_spec_redeclared) |
| 2917 | << WrittenTy->getType(); |
| 2918 | SemaRef.Diag(PrevDecl->getLocation(), |
| 2919 | diag::note_var_prev_partial_spec_here); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2920 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | // Do substitution on the type of the declaration |
| 2924 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 2925 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 2926 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 2927 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2928 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2929 | |
| 2930 | if (DI->getType()->isFunctionType()) { |
| 2931 | SemaRef.Diag(PartialSpec->getLocation(), |
| 2932 | diag::err_variable_instantiates_to_function) |
| 2933 | << PartialSpec->isStaticDataMember() << DI->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2934 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | // Create the variable template partial specialization declaration. |
| 2938 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 2939 | VarTemplatePartialSpecializationDecl::Create( |
| 2940 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 2941 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
| 2942 | DI, PartialSpec->getStorageClass(), Converted.data(), |
Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2943 | Converted.size(), InstTemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2944 | |
| 2945 | // Substitute the nested name specifier, if any. |
| 2946 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2947 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2948 | |
| 2949 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 2950 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 2951 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2952 | // Add this partial specialization to the set of variable template partial |
| 2953 | // specializations. The instantiation of the initializer is not necessary. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2954 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2955 | |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2956 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2957 | LateAttrs, Owner, StartingScope); |
Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2958 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2959 | return InstPartialSpec; |
| 2960 | } |
| 2961 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2962 | TypeSourceInfo* |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2963 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2964 | SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2965 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 2966 | assert(OldTInfo && "substituting function without type source info"); |
| 2967 | assert(Params.empty() && "parameter vector is non-empty at start"); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2968 | |
| 2969 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2970 | unsigned ThisTypeQuals = 0; |
| 2971 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 2972 | ThisContext = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2973 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2974 | } |
| 2975 | |
John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 2976 | TypeSourceInfo *NewTInfo |
| 2977 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 2978 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2979 | D->getDeclName(), |
| 2980 | ThisContext, ThisTypeQuals); |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2981 | if (!NewTInfo) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2982 | return nullptr; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2983 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2984 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 2985 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 2986 | if (NewTInfo != OldTInfo) { |
| 2987 | // Get parameters from the new type info. |
Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2988 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2989 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2990 | unsigned NewIdx = 0; |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2991 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2992 | OldIdx != NumOldParams; ++OldIdx) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2993 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2994 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 2995 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2996 | Optional<unsigned> NumArgumentsInExpansion; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2997 | if (OldParam->isParameterPack()) |
| 2998 | NumArgumentsInExpansion = |
| 2999 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 3000 | TemplateArgs); |
| 3001 | if (!NumArgumentsInExpansion) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3002 | // Simple case: normal parameter, or a parameter pack that's |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3003 | // instantiated to a (still-dependent) parameter pack. |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3004 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3005 | Params.push_back(NewParam); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3006 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 3007 | } else { |
| 3008 | // Parameter pack expansion: make the instantiation an argument pack. |
| 3009 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 3010 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3011 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3012 | Params.push_back(NewParam); |
| 3013 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 3014 | } |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3015 | } |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3016 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3017 | } else { |
| 3018 | // The function type itself was not dependent and therefore no |
| 3019 | // substitution occurred. However, we still need to instantiate |
| 3020 | // the function parameters themselves. |
| 3021 | const FunctionProtoType *OldProto = |
| 3022 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3023 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 3024 | ++i) { |
| 3025 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3026 | if (!OldParam) { |
| 3027 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3028 | D, D->getLocation(), OldProto->getParamType(i))); |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3029 | continue; |
| 3030 | } |
| 3031 | |
Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3032 | ParmVarDecl *Parm = |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3033 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3034 | if (!Parm) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3035 | return nullptr; |
Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3036 | Params.push_back(Parm); |
| 3037 | } |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3038 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3039 | } else { |
| 3040 | // If the type of this function, after ignoring parentheses, is not |
| 3041 | // *directly* a function type, then we're instantiating a function that |
| 3042 | // was declared via a typedef or with attributes, e.g., |
| 3043 | // |
| 3044 | // typedef int functype(int, int); |
| 3045 | // functype func; |
| 3046 | // int __cdecl meth(int, int); |
| 3047 | // |
| 3048 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 3049 | // synthesized in the method declaration. |
| 3050 | SmallVector<QualType, 4> ParamTypes; |
| 3051 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), |
| 3052 | D->getNumParams(), TemplateArgs, ParamTypes, |
| 3053 | &Params)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3054 | return nullptr; |
Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3055 | } |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3056 | |
John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3057 | return NewTInfo; |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3060 | /// Introduce the instantiated function parameters into the local |
| 3061 | /// instantiation scope, and set the parameter names to those used |
| 3062 | /// in the template. |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3063 | static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3064 | const FunctionDecl *PatternDecl, |
| 3065 | LocalInstantiationScope &Scope, |
| 3066 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3067 | unsigned FParamIdx = 0; |
| 3068 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 3069 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 3070 | if (!PatternParam->isParameterPack()) { |
| 3071 | // Simple case: not a parameter pack. |
| 3072 | assert(FParamIdx < Function->getNumParams()); |
| 3073 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3074 | // If the parameter's type is not dependent, update it to match the type |
| 3075 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 3076 | // the pattern's type here. If the type is dependent, they can't differ, |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3077 | // per core issue 1668. |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3078 | // FIXME: Updating the type to work around this is at best fragile. |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3079 | if (!PatternDecl->getType()->isDependentType()) |
| 3080 | FunctionParam->setType(PatternParam->getType()); |
Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3081 | |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3082 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3083 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3084 | ++FParamIdx; |
| 3085 | continue; |
| 3086 | } |
| 3087 | |
| 3088 | // Expand the parameter pack. |
| 3089 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3090 | Optional<unsigned> NumArgumentsInExpansion |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3091 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3092 | assert(NumArgumentsInExpansion && |
| 3093 | "should only be called when all template arguments are known"); |
| 3094 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3095 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3096 | if (!PatternDecl->getType()->isDependentType()) |
| 3097 | FunctionParam->setType(PatternParam->getType()); |
Richard Smith | 9c04bce | 2014-10-16 23:00:46 +0000 | [diff] [blame] | 3098 | |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3099 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3100 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3101 | ++FParamIdx; |
| 3102 | } |
| 3103 | } |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3104 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3105 | |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3106 | static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New, |
| 3107 | const FunctionProtoType *Proto, |
| 3108 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3109 | assert(Proto->getExceptionSpecType() != EST_Uninstantiated); |
| 3110 | |
| 3111 | // C++11 [expr.prim.general]p3: |
| 3112 | // If a declaration declares a member function or member function |
| 3113 | // template of a class X, the expression this is a prvalue of type |
| 3114 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 3115 | // and the end of the function-definition, member-declarator, or |
| 3116 | // declarator. |
| 3117 | CXXRecordDecl *ThisContext = nullptr; |
| 3118 | unsigned ThisTypeQuals = 0; |
| 3119 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) { |
| 3120 | ThisContext = Method->getParent(); |
| 3121 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 3122 | } |
| 3123 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals, |
| 3124 | SemaRef.getLangOpts().CPlusPlus11); |
| 3125 | |
| 3126 | // The function has an exception specification or a "noreturn" |
| 3127 | // attribute. Substitute into each of the exception types. |
| 3128 | SmallVector<QualType, 4> Exceptions; |
| 3129 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 3130 | // FIXME: Poor location information! |
| 3131 | if (const PackExpansionType *PackExpansion |
| 3132 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { |
| 3133 | // We have a pack expansion. Instantiate it. |
| 3134 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 3135 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), |
| 3136 | Unexpanded); |
| 3137 | assert(!Unexpanded.empty() && |
| 3138 | "Pack expansion without parameter packs?"); |
| 3139 | |
| 3140 | bool Expand = false; |
| 3141 | bool RetainExpansion = false; |
| 3142 | Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions(); |
| 3143 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), |
| 3144 | SourceRange(), |
| 3145 | Unexpanded, |
| 3146 | TemplateArgs, |
| 3147 | Expand, |
| 3148 | RetainExpansion, |
| 3149 | NumExpansions)) |
| 3150 | break; |
| 3151 | |
| 3152 | if (!Expand) { |
| 3153 | // We can't expand this pack expansion into separate arguments yet; |
| 3154 | // just substitute into the pattern and create a new pack expansion |
| 3155 | // type. |
| 3156 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 3157 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 3158 | TemplateArgs, |
| 3159 | New->getLocation(), New->getDeclName()); |
| 3160 | if (T.isNull()) |
| 3161 | break; |
| 3162 | |
| 3163 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); |
| 3164 | Exceptions.push_back(T); |
| 3165 | continue; |
| 3166 | } |
| 3167 | |
| 3168 | // Substitute into the pack expansion pattern for each template |
| 3169 | bool Invalid = false; |
| 3170 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { |
| 3171 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); |
| 3172 | |
| 3173 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 3174 | TemplateArgs, |
| 3175 | New->getLocation(), New->getDeclName()); |
| 3176 | if (T.isNull()) { |
| 3177 | Invalid = true; |
| 3178 | break; |
| 3179 | } |
| 3180 | |
| 3181 | Exceptions.push_back(T); |
| 3182 | } |
| 3183 | |
| 3184 | if (Invalid) |
| 3185 | break; |
| 3186 | |
| 3187 | continue; |
| 3188 | } |
| 3189 | |
| 3190 | QualType T |
| 3191 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 3192 | New->getLocation(), New->getDeclName()); |
| 3193 | if (T.isNull() || |
| 3194 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 3195 | continue; |
| 3196 | |
| 3197 | Exceptions.push_back(T); |
| 3198 | } |
| 3199 | Expr *NoexceptExpr = nullptr; |
| 3200 | if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) { |
| 3201 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 3202 | Sema::ConstantEvaluated); |
| 3203 | ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs); |
| 3204 | if (E.isUsable()) |
| 3205 | E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart()); |
| 3206 | |
| 3207 | if (E.isUsable()) { |
| 3208 | NoexceptExpr = E.get(); |
| 3209 | if (!NoexceptExpr->isTypeDependent() && |
| 3210 | !NoexceptExpr->isValueDependent()) |
| 3211 | NoexceptExpr |
| 3212 | = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr, |
| 3213 | nullptr, diag::err_noexcept_needs_constant_expression, |
| 3214 | /*AllowFold*/ false).get(); |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | FunctionProtoType::ExceptionSpecInfo ESI; |
| 3219 | ESI.Type = Proto->getExceptionSpecType(); |
| 3220 | ESI.Exceptions = Exceptions; |
| 3221 | ESI.NoexceptExpr = NoexceptExpr; |
| 3222 | |
| 3223 | SemaRef.UpdateExceptionSpec(New, ESI); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3227 | FunctionDecl *Decl) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3228 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3229 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3230 | return; |
| 3231 | |
| 3232 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3233 | InstantiatingTemplate::ExceptionSpecification()); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3234 | if (Inst.isInvalid()) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3235 | // We hit the instantiation depth limit. Clear the exception specification |
| 3236 | // so that our callers don't have to cope with EST_Uninstantiated. |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3237 | UpdateExceptionSpec(Decl, EST_None); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3238 | return; |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3239 | } |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3240 | |
| 3241 | // Enter the scope of this instantiation. We don't use |
| 3242 | // PushDeclContext because we don't have a scope. |
| 3243 | Sema::ContextRAII savedContext(*this, Decl); |
| 3244 | LocalInstantiationScope Scope(*this); |
| 3245 | |
| 3246 | MultiLevelTemplateArgumentList TemplateArgs = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3247 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3248 | |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3249 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3250 | addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3251 | |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3252 | ::InstantiateExceptionSpec(*this, Decl, |
| 3253 | Template->getType()->castAs<FunctionProtoType>(), |
| 3254 | TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3255 | } |
| 3256 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3258 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3259 | /// |
| 3260 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | bool |
| 3262 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3263 | FunctionDecl *Tmpl) { |
David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3264 | if (Tmpl->isDeleted()) |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3265 | New->setDeletedAsWritten(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3267 | // Forward the mangling number from the template to the instantiated decl. |
| 3268 | SemaRef.Context.setManglingNumber(New, |
| 3269 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3270 | |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3271 | // If we are performing substituting explicitly-specified template arguments |
| 3272 | // or deduced template arguments into a function template and we reach this |
| 3273 | // 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] | 3274 | // to keeping the new function template specialization. We therefore |
| 3275 | // convert the active template instantiation for the function template |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3276 | // into a template instantiation for this specific function template |
| 3277 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3278 | // further errors in the declaration itself. |
| 3279 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 3280 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 3281 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3282 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | if (FunctionTemplateDecl *FunTmpl |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3284 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3286 | "Deduction from the wrong function template?"); |
Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3287 | (void) FunTmpl; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3288 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3289 | ActiveInst.Entity = New; |
Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3293 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3294 | assert(Proto && "Function template without prototype?"); |
| 3295 | |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3296 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3297 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3298 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3299 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3300 | // exception specification. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3301 | if (SemaRef.getLangOpts().CPlusPlus11 && |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3302 | EPI.ExceptionSpec.Type != EST_None && |
| 3303 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
| 3304 | EPI.ExceptionSpec.Type != EST_BasicNoexcept) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3305 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3306 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3307 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3308 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3309 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3310 | NewEST = EST_Unevaluated; |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3311 | |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3312 | // Mark the function has having an uninstantiated exception specification. |
| 3313 | const FunctionProtoType *NewProto |
| 3314 | = New->getType()->getAs<FunctionProtoType>(); |
| 3315 | assert(NewProto && "Template instantiation without function prototype?"); |
| 3316 | EPI = NewProto->getExtProtoInfo(); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3317 | EPI.ExceptionSpec.Type = NewEST; |
| 3318 | EPI.ExceptionSpec.SourceDecl = New; |
| 3319 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3320 | New->setType(SemaRef.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3321 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3322 | } else { |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3323 | ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3324 | } |
Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3327 | // Get the definition. Leaves the variable unchanged if undefined. |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3328 | const FunctionDecl *Definition = Tmpl; |
Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3329 | Tmpl->isDefined(Definition); |
| 3330 | |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3331 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 3332 | LateAttrs, StartingScope); |
Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3333 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3334 | return false; |
| 3335 | } |
| 3336 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3337 | /// \brief Initializes common fields of an instantiated method |
| 3338 | /// declaration (New) from the corresponding fields of its template |
| 3339 | /// (Tmpl). |
| 3340 | /// |
| 3341 | /// \returns true if there was an error |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | bool |
| 3343 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3344 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3345 | if (InitFunctionInstantiation(New, Tmpl)) |
| 3346 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3348 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3349 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3350 | New->setVirtualAsWritten(true); |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3352 | // FIXME: New needs a pointer to Tmpl |
| 3353 | return false; |
| 3354 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3355 | |
| 3356 | /// \brief Instantiate the definition of the given function from its |
| 3357 | /// template. |
| 3358 | /// |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3359 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3360 | /// required. Note that this is not precisely a "point of instantiation" |
| 3361 | /// for the function, but it's close. |
| 3362 | /// |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3363 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3364 | /// function template specialization or member function of a class template |
| 3365 | /// specialization. |
| 3366 | /// |
| 3367 | /// \param Recursive if true, recursively instantiates any functions that |
| 3368 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3369 | /// |
| 3370 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3371 | /// instantiation where the body of the function is required. Complain if |
| 3372 | /// there is no such body. |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3373 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3374 | FunctionDecl *Function, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3375 | bool Recursive, |
| 3376 | bool DefinitionRequired) { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3377 | if (Function->isInvalidDecl() || Function->isDefined()) |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3378 | return; |
| 3379 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3380 | // Never instantiate an explicit specialization except if it is a class scope |
| 3381 | // explicit specialization. |
| 3382 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 3383 | !Function->getClassScopeSpecializationPattern()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3384 | return; |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3386 | // Find the function body that we'll be substituting. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3387 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3388 | assert(PatternDecl && "instantiating a non-template"); |
| 3389 | |
| 3390 | Stmt *Pattern = PatternDecl->getBody(PatternDecl); |
| 3391 | assert(PatternDecl && "template definition is not a template"); |
| 3392 | if (!Pattern) { |
| 3393 | // Try to find a defaulted definition |
| 3394 | PatternDecl->isDefined(PatternDecl); |
Alexis Hunt | 92a0adf | 2011-05-25 22:02:25 +0000 | [diff] [blame] | 3395 | } |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3396 | assert(PatternDecl && "template definition is not a template"); |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3397 | |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3398 | // Postpone late parsed template instantiations. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3399 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 3400 | !LateTemplateParser) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3401 | PendingInstantiations.push_back( |
| 3402 | std::make_pair(Function, PointOfInstantiation)); |
| 3403 | return; |
| 3404 | } |
| 3405 | |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3406 | // If we're performing recursive template instantiation, create our own |
| 3407 | // queue of pending implicit instantiations that we will instantiate later, |
| 3408 | // while we're still within our own instantiation context. |
| 3409 | // This has to happen before LateTemplateParser below is called, so that |
| 3410 | // it marks vtables used in late parsed templates as used. |
| 3411 | SavePendingLocalImplicitInstantiationsRAII |
| 3412 | SavedPendingLocalImplicitInstantiations(*this); |
| 3413 | std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII> |
| 3414 | SavePendingInstantiationsAndVTableUses; |
| 3415 | if (Recursive) { |
| 3416 | SavePendingInstantiationsAndVTableUses.reset( |
| 3417 | new SavePendingInstantiationsAndVTableUsesRAII(*this)); |
| 3418 | } |
| 3419 | |
David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 3420 | // Call the LateTemplateParser callback if there is a need to late parse |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3421 | // a templated function definition. |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3422 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3423 | LateTemplateParser) { |
Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3424 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 3425 | if (PatternDecl->isFromASTFile()) |
| 3426 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 3427 | |
| 3428 | LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl); |
| 3429 | assert(LPT && "missing LateParsedTemplate"); |
| 3430 | LateTemplateParser(OpaqueParser, *LPT); |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3431 | Pattern = PatternDecl->getBody(PatternDecl); |
| 3432 | } |
| 3433 | |
Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3434 | if (!Pattern && !PatternDecl->isDefaulted()) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3435 | if (DefinitionRequired) { |
| 3436 | if (Function->getPrimaryTemplate()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3437 | Diag(PointOfInstantiation, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3438 | diag::err_explicit_instantiation_undefined_func_template) |
| 3439 | << Function->getPrimaryTemplate(); |
| 3440 | else |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3441 | Diag(PointOfInstantiation, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3442 | diag::err_explicit_instantiation_undefined_member) |
| 3443 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3445 | if (PatternDecl) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3446 | Diag(PatternDecl->getLocation(), |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3447 | diag::note_explicit_instantiation_here); |
Douglas Gregor | fd7224f | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 3448 | Function->setInvalidDecl(); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3449 | } else if (Function->getTemplateSpecializationKind() |
| 3450 | == TSK_ExplicitInstantiationDefinition) { |
Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 3451 | assert(!Recursive); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3452 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3453 | std::make_pair(Function, PointOfInstantiation)); |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3454 | } |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3456 | return; |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3457 | } |
Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3458 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3459 | // C++1y [temp.explicit]p10: |
| 3460 | // Except for inline functions, declarations with types deduced from their |
| 3461 | // initializer or return value, and class template specializations, other |
| 3462 | // explicit instantiation declarations have the effect of suppressing the |
| 3463 | // implicit instantiation of the entity to which they refer. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3464 | if (Function->getTemplateSpecializationKind() == |
| 3465 | TSK_ExplicitInstantiationDeclaration && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3466 | !PatternDecl->isInlined() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3467 | !PatternDecl->getReturnType()->getContainedAutoType()) |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3468 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 3470 | if (PatternDecl->isInlined()) { |
| 3471 | // Function, and all later redeclarations of it (from imported modules, |
| 3472 | // for instance), are now implicitly inline. |
| 3473 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 3474 | D = D->getPreviousDecl()) { |
| 3475 | D->setImplicitlyInline(); |
| 3476 | if (D == Function) |
| 3477 | break; |
| 3478 | } |
| 3479 | } |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3480 | |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3481 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3482 | if (Inst.isInvalid()) |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3483 | return; |
| 3484 | |
Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 3485 | // Copy the inner loc start from the pattern. |
| 3486 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 3487 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3488 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3489 | Sema::PotentiallyEvaluated); |
Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3491 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3492 | // recorded, unless we're actually a member function within a local |
| 3493 | // class, in which case we need to merge our results with the parent |
| 3494 | // scope (of the enclosing function). |
| 3495 | bool MergeWithParentScope = false; |
| 3496 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 3497 | MergeWithParentScope = Rec->isLocalClass(); |
| 3498 | |
| 3499 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3501 | if (PatternDecl->isDefaulted()) |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3502 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3503 | else { |
Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 3504 | MultiLevelTemplateArgumentList TemplateArgs = |
| 3505 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
| 3506 | |
| 3507 | // Substitute into the qualifier; we can get a substitution failure here |
| 3508 | // through evil use of alias templates. |
| 3509 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 3510 | // of the) lexical context of the pattern? |
| 3511 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 3512 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3513 | ActOnStartOfFunctionDef(nullptr, Function); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3514 | |
| 3515 | // Enter the scope of this instantiation. We don't use |
| 3516 | // PushDeclContext because we don't have a scope. |
| 3517 | Sema::ContextRAII savedContext(*this, Function); |
| 3518 | |
NAKAMURA Takumi | 2322415 | 2014-10-17 12:48:37 +0000 | [diff] [blame] | 3519 | addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 3520 | TemplateArgs); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3521 | |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3522 | // If this is a constructor, instantiate the member initializers. |
| 3523 | if (const CXXConstructorDecl *Ctor = |
| 3524 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 3525 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 3526 | TemplateArgs); |
| 3527 | } |
| 3528 | |
| 3529 | // Instantiate the function body. |
| 3530 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); |
| 3531 | |
| 3532 | if (Body.isInvalid()) |
| 3533 | Function->setInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3534 | |
Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3535 | ActOnFinishFunctionBody(Function, Body.get(), |
| 3536 | /*IsInstantiation=*/true); |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3537 | |
| 3538 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 3539 | |
Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 3540 | if (auto *Listener = getASTMutationListener()) |
| 3541 | Listener->FunctionDefinitionInstantiated(Function); |
Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 3542 | |
Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3543 | savedContext.pop(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 3546 | DeclGroupRef DG(Function); |
| 3547 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3549 | // This class may have local implicit instantiations that need to be |
| 3550 | // instantiation within this scope. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3551 | PerformPendingInstantiations(/*LocalOnly=*/true); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3552 | Scope.Exit(); |
| 3553 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3554 | if (Recursive) { |
Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3555 | // Define any pending vtables. |
| 3556 | DefineUsedVTables(); |
| 3557 | |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3558 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3559 | // instantiation of this template. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3560 | PerformPendingInstantiations(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3561 | |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 3562 | // Restore PendingInstantiations and VTableUses. |
| 3563 | SavePendingInstantiationsAndVTableUses.reset(); |
Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3564 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3567 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 3568 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 3569 | const TemplateArgumentList &TemplateArgList, |
| 3570 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 3571 | SmallVectorImpl<TemplateArgument> &Converted, |
| 3572 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 3573 | LateInstantiatedAttrVec *LateAttrs, |
| 3574 | LocalInstantiationScope *StartingScope) { |
| 3575 | if (FromVar->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3576 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3577 | |
| 3578 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3579 | if (Inst.isInvalid()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3580 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3581 | |
| 3582 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3583 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 3584 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3585 | // Instantiate the first declaration of the variable template: for a partial |
| 3586 | // specialization of a static data member template, the first declaration may |
| 3587 | // or may not be the declaration in the class; if it's in the class, we want |
| 3588 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 3589 | // we want to instantiate a definition. |
Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3590 | // |
| 3591 | // If we're instantiating an explicitly-specialized member template or member |
| 3592 | // partial specialization, don't do this. The member specialization completely |
| 3593 | // replaces the original declaration in this case. |
| 3594 | bool IsMemberSpec = false; |
| 3595 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 3596 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 3597 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 3598 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 3599 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 3600 | if (!IsMemberSpec) |
| 3601 | FromVar = FromVar->getFirstDecl(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3602 | |
Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 3603 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 3604 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 3605 | MultiLevelList); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3606 | |
| 3607 | // TODO: Set LateAttrs and StartingScope ... |
| 3608 | |
| 3609 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 3610 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 3611 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 3612 | } |
| 3613 | |
| 3614 | /// \brief Instantiates a variable template specialization by completing it |
| 3615 | /// with appropriate type information and initializer. |
| 3616 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 3617 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 3618 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3619 | |
| 3620 | // Do substitution on the type of the declaration |
| 3621 | TypeSourceInfo *DI = |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3622 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3623 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 3624 | if (!DI) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3625 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Update the type of this variable template specialization. |
| 3628 | VarSpec->setType(DI->getType()); |
| 3629 | |
| 3630 | // Instantiate the initializer. |
| 3631 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 3632 | |
| 3633 | return VarSpec; |
| 3634 | } |
| 3635 | |
| 3636 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 3637 | /// Sets basic variable data and decides whether to postpone the |
| 3638 | /// variable instantiation. |
| 3639 | void Sema::BuildVariableInstantiation( |
| 3640 | VarDecl *NewVar, VarDecl *OldVar, |
| 3641 | const MultiLevelTemplateArgumentList &TemplateArgs, |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3642 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 3643 | LocalInstantiationScope *StartingScope, |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3644 | bool InstantiatingVarTemplate) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3645 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3646 | // If we are instantiating a local extern declaration, the |
| 3647 | // instantiation belongs lexically to the containing function. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3648 | // If we are instantiating a static data member defined |
| 3649 | // out-of-line, the instantiation will have the same lexical |
| 3650 | // context (which will be a namespace scope) as the template. |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3651 | if (OldVar->isLocalExternDecl()) { |
| 3652 | NewVar->setLocalExternDecl(); |
| 3653 | NewVar->setLexicalDeclContext(Owner); |
| 3654 | } else if (OldVar->isOutOfLine()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3655 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 3656 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 3657 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 3658 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
| 3659 | NewVar->setConstexpr(OldVar->isConstexpr()); |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 3660 | NewVar->setInitCapture(OldVar->isInitCapture()); |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3661 | NewVar->setPreviousDeclInSameBlockScope( |
| 3662 | OldVar->isPreviousDeclInSameBlockScope()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3663 | NewVar->setAccess(OldVar->getAccess()); |
| 3664 | |
Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 3665 | if (!OldVar->isStaticDataMember()) { |
Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3666 | if (OldVar->isUsed(false)) |
| 3667 | NewVar->setIsUsed(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3668 | NewVar->setReferenced(OldVar->isReferenced()); |
| 3669 | } |
| 3670 | |
David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3671 | // See if the old variable had a type-specifier that defined an anonymous tag. |
| 3672 | // If it did, mark the new variable as being the declarator for the new |
| 3673 | // anonymous tag. |
| 3674 | if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) { |
| 3675 | TagDecl *OldTag = OldTagType->getDecl(); |
| 3676 | if (OldTag->getDeclaratorForAnonDecl() == OldVar) { |
| 3677 | TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl(); |
| 3678 | assert(!NewTag->hasNameForLinkage() && |
| 3679 | !NewTag->hasDeclaratorForAnonDecl()); |
| 3680 | NewTag->setDeclaratorForAnonDecl(NewVar); |
| 3681 | } |
| 3682 | } |
| 3683 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3684 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 3685 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3686 | LookupResult Previous( |
| 3687 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 3688 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 3689 | : Sema::LookupOrdinaryName, |
| 3690 | Sema::ForRedeclaration); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3691 | |
Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 3692 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 3693 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 3694 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3695 | // We have a previous declaration. Use that one, so we merge with the |
| 3696 | // right type. |
| 3697 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 3698 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 3699 | Previous.addDecl(NewPrev); |
| 3700 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 3701 | OldVar->hasLinkage()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3702 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3703 | CheckVariableDeclaration(NewVar, Previous); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3704 | |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3705 | if (!InstantiatingVarTemplate) { |
| 3706 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 3707 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3708 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | if (!OldVar->isOutOfLine()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3712 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 3713 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 3714 | } |
| 3715 | |
| 3716 | // Link instantiations of static data members back to the template from |
| 3717 | // which they were instantiated. |
Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3718 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3719 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 3720 | TSK_ImplicitInstantiation); |
| 3721 | |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3722 | // Forward the mangling number from the template to the instantiated decl. |
| 3723 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3724 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3725 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3726 | // Delay instantiation of the initializer for variable templates until a |
Richard Smith | d292b24 | 2014-03-16 01:00:40 +0000 | [diff] [blame] | 3727 | // definition of the variable is needed. We need it right away if the type |
| 3728 | // contains 'auto'. |
| 3729 | if ((!isa<VarTemplateSpecializationDecl>(NewVar) && |
| 3730 | !InstantiatingVarTemplate) || |
| 3731 | NewVar->getType()->isUndeducedType()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3732 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 3733 | |
| 3734 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 3735 | // will have been deferred. |
| 3736 | if (!NewVar->isInvalidDecl() && |
Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 3737 | NewVar->getDeclContext()->isFunctionOrMethod() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3738 | OldVar->getType()->isDependentType()) |
| 3739 | DiagnoseUnusedDecl(NewVar); |
| 3740 | } |
| 3741 | |
| 3742 | /// \brief Instantiate the initializer of a variable. |
| 3743 | void Sema::InstantiateVariableInitializer( |
| 3744 | VarDecl *Var, VarDecl *OldVar, |
| 3745 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3746 | |
| 3747 | if (Var->getAnyInitializer()) |
| 3748 | // We already have an initializer in the class. |
| 3749 | return; |
| 3750 | |
| 3751 | if (OldVar->getInit()) { |
| 3752 | if (Var->isStaticDataMember() && !OldVar->isOutOfLine()) |
| 3753 | PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar); |
| 3754 | else |
| 3755 | PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar); |
| 3756 | |
| 3757 | // Instantiate the initializer. |
| 3758 | ExprResult Init = |
| 3759 | SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 3760 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 3761 | if (!Init.isInvalid()) { |
| 3762 | bool TypeMayContainAuto = true; |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3763 | Expr *InitExpr = Init.get(); |
| 3764 | |
Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 3765 | if (Var->hasAttr<DLLImportAttr>() && |
| 3766 | (!InitExpr || |
| 3767 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3768 | // Do not dynamically initialize dllimport variables. |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3769 | } else if (InitExpr) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3770 | bool DirectInit = OldVar->isDirectInit(); |
Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 3771 | AddInitializerToDecl(Var, InitExpr, DirectInit, TypeMayContainAuto); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3772 | } else |
| 3773 | ActOnUninitializedDecl(Var, TypeMayContainAuto); |
| 3774 | } else { |
| 3775 | // FIXME: Not too happy about invalidating the declaration |
| 3776 | // because of a bogus initializer. |
| 3777 | Var->setInvalidDecl(); |
| 3778 | } |
| 3779 | |
| 3780 | PopExpressionEvaluationContext(); |
| 3781 | } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) && |
| 3782 | !Var->isCXXForRangeDecl()) |
| 3783 | ActOnUninitializedDecl(Var, false); |
| 3784 | } |
| 3785 | |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3786 | /// \brief Instantiate the definition of the given variable from its |
| 3787 | /// template. |
| 3788 | /// |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3789 | /// \param PointOfInstantiation the point at which the instantiation was |
| 3790 | /// required. Note that this is not precisely a "point of instantiation" |
| 3791 | /// for the function, but it's close. |
| 3792 | /// |
| 3793 | /// \param Var the already-instantiated declaration of a static member |
| 3794 | /// variable of a class template specialization. |
| 3795 | /// |
| 3796 | /// \param Recursive if true, recursively instantiates any functions that |
| 3797 | /// are required by this instantiation. |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3798 | /// |
| 3799 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 3800 | /// instantiation where an out-of-line definition of the member variable |
| 3801 | /// is required. Complain if there is no such definition. |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3802 | void Sema::InstantiateStaticDataMemberDefinition( |
| 3803 | SourceLocation PointOfInstantiation, |
| 3804 | VarDecl *Var, |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3805 | bool Recursive, |
| 3806 | bool DefinitionRequired) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3807 | InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive, |
| 3808 | DefinitionRequired); |
| 3809 | } |
| 3810 | |
| 3811 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 3812 | VarDecl *Var, bool Recursive, |
| 3813 | bool DefinitionRequired) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3814 | if (Var->isInvalidDecl()) |
| 3815 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3817 | VarTemplateSpecializationDecl *VarSpec = |
| 3818 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3819 | VarDecl *PatternDecl = nullptr, *Def = nullptr; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3820 | MultiLevelTemplateArgumentList TemplateArgs = |
| 3821 | getTemplateInstantiationArgs(Var); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3823 | if (VarSpec) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3824 | // If this is a variable template specialization, make sure that it is |
| 3825 | // non-dependent, then find its instantiation pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3826 | bool InstantiationDependent = false; |
| 3827 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3828 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 3829 | "Only instantiate variable template specializations that are " |
| 3830 | "not type-dependent"); |
Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 3831 | (void)InstantiationDependent; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3832 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3833 | // Find the variable initialization that we'll be substituting. If the |
| 3834 | // pattern was instantiated from a member template, look back further to |
| 3835 | // find the real pattern. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3836 | assert(VarSpec->getSpecializedTemplate() && |
| 3837 | "Specialization without specialized template?"); |
| 3838 | llvm::PointerUnion<VarTemplateDecl *, |
| 3839 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 3840 | VarSpec->getSpecializedTemplateOrPartial(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3841 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3842 | VarTemplatePartialSpecializationDecl *Tmpl = |
| 3843 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); |
| 3844 | while (VarTemplatePartialSpecializationDecl *From = |
| 3845 | Tmpl->getInstantiatedFromMember()) { |
| 3846 | if (Tmpl->isMemberSpecialization()) |
| 3847 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3848 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3849 | Tmpl = From; |
| 3850 | } |
| 3851 | PatternDecl = Tmpl; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3852 | } else { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3853 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); |
| 3854 | while (VarTemplateDecl *From = |
| 3855 | Tmpl->getInstantiatedFromMemberTemplate()) { |
| 3856 | if (Tmpl->isMemberSpecialization()) |
| 3857 | break; |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3858 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3859 | Tmpl = From; |
| 3860 | } |
| 3861 | PatternDecl = Tmpl->getTemplatedDecl(); |
Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3862 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3863 | |
| 3864 | // If this is a static data member template, there might be an |
| 3865 | // uninstantiated initializer on the declaration. If so, instantiate |
| 3866 | // it now. |
| 3867 | if (PatternDecl->isStaticDataMember() && |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3868 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3869 | !Var->hasInit()) { |
| 3870 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 3871 | // code here. |
| 3872 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3873 | if (Inst.isInvalid()) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3874 | return; |
| 3875 | |
| 3876 | // If we're performing recursive template instantiation, create our own |
| 3877 | // queue of pending implicit instantiations that we will instantiate |
| 3878 | // later, while we're still within our own instantiation context. |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 3879 | std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII> |
| 3880 | SavePendingInstantiationsAndVTableUses; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3881 | if (Recursive) { |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 3882 | SavePendingInstantiationsAndVTableUses.reset( |
| 3883 | new SavePendingInstantiationsAndVTableUsesRAII(*this)); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | LocalInstantiationScope Local(*this); |
| 3887 | |
| 3888 | // Enter the scope of this instantiation. We don't use |
| 3889 | // PushDeclContext because we don't have a scope. |
| 3890 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 3891 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 3892 | PreviousContext.pop(); |
| 3893 | |
| 3894 | // FIXME: Need to inform the ASTConsumer that we instantiated the |
| 3895 | // initializer? |
| 3896 | |
| 3897 | // This variable may have local implicit instantiations that need to be |
| 3898 | // instantiated within this scope. |
| 3899 | PerformPendingInstantiations(/*LocalOnly=*/true); |
| 3900 | |
| 3901 | Local.Exit(); |
| 3902 | |
| 3903 | if (Recursive) { |
| 3904 | // Define any newly required vtables. |
| 3905 | DefineUsedVTables(); |
| 3906 | |
| 3907 | // Instantiate any pending implicit instantiations found during the |
| 3908 | // instantiation of this template. |
| 3909 | PerformPendingInstantiations(); |
| 3910 | |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 3911 | // Restore PendingInstantiations and VTableUses. |
| 3912 | SavePendingInstantiationsAndVTableUses.reset(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | // Find actual definition |
| 3917 | Def = PatternDecl->getDefinition(getASTContext()); |
| 3918 | } else { |
| 3919 | // If this is a static data member, find its out-of-line definition. |
| 3920 | assert(Var->isStaticDataMember() && "not a static data member?"); |
| 3921 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); |
| 3922 | |
| 3923 | assert(PatternDecl && "data member was not instantiated from a template?"); |
| 3924 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); |
| 3925 | Def = PatternDecl->getOutOfLineDefinition(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3928 | // If we don't have a definition of the variable template, we won't perform |
| 3929 | // any instantiation. Rather, we rely on the user to instantiate this |
| 3930 | // definition (or provide a specialization for it) in another translation |
| 3931 | // unit. |
| 3932 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3933 | if (DefinitionRequired) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3934 | if (VarSpec) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3935 | Diag(PointOfInstantiation, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3936 | diag::err_explicit_instantiation_undefined_var_template) << Var; |
| 3937 | else |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3938 | Diag(PointOfInstantiation, |
| 3939 | diag::err_explicit_instantiation_undefined_member) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3940 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 3941 | Diag(PatternDecl->getLocation(), |
| 3942 | diag::note_explicit_instantiation_here); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3943 | if (VarSpec) |
| 3944 | Var->setInvalidDecl(); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3945 | } else if (Var->getTemplateSpecializationKind() |
| 3946 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3947 | PendingInstantiations.push_back( |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3948 | std::make_pair(Var, PointOfInstantiation)); |
| 3949 | } |
| 3950 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3951 | return; |
| 3952 | } |
| 3953 | |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3954 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); |
| 3955 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3956 | // Never instantiate an explicit specialization. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3957 | if (TSK == TSK_ExplicitSpecialization) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3958 | return; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3959 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3960 | // C++11 [temp.explicit]p10: |
| 3961 | // Except for inline functions, [...] explicit instantiation declarations |
| 3962 | // have the effect of suppressing the implicit instantiation of the entity |
| 3963 | // to which they refer. |
Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3964 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3965 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3966 | |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3967 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 3968 | struct PassToConsumerRAII { |
| 3969 | ASTConsumer &Consumer; |
| 3970 | VarDecl *Var; |
| 3971 | |
| 3972 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 3973 | : Consumer(Consumer), Var(Var) { } |
| 3974 | |
| 3975 | ~PassToConsumerRAII() { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3976 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3977 | } |
| 3978 | } PassToConsumerRAII(Consumer, Var); |
Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3979 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3980 | // If we already have a definition, we're done. |
| 3981 | if (VarDecl *Def = Var->getDefinition()) { |
| 3982 | // We may be explicitly instantiating something we've already implicitly |
| 3983 | // instantiated. |
| 3984 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 3985 | PointOfInstantiation); |
| 3986 | return; |
Nick Lewycky | a995a47 | 2012-04-04 02:38:36 +0000 | [diff] [blame] | 3987 | } |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 3988 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3989 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3990 | if (Inst.isInvalid()) |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3991 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3992 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3993 | // If we're performing recursive template instantiation, create our own |
| 3994 | // queue of pending implicit instantiations that we will instantiate later, |
| 3995 | // while we're still within our own instantiation context. |
David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 3996 | SavePendingLocalImplicitInstantiationsRAII |
| 3997 | SavedPendingLocalImplicitInstantiations(*this); |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 3998 | std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII> |
| 3999 | SavePendingInstantiationsAndVTableUses; |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4000 | if (Recursive) { |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 4001 | SavePendingInstantiationsAndVTableUses.reset( |
| 4002 | new SavePendingInstantiationsAndVTableUsesRAII(*this)); |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4003 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4005 | // Enter the scope of this instantiation. We don't use |
| 4006 | // PushDeclContext because we don't have a scope. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4007 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4008 | LocalInstantiationScope Local(*this); |
John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 4009 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4010 | VarDecl *OldVar = Var; |
| 4011 | if (!VarSpec) |
| 4012 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4013 | TemplateArgs)); |
| 4014 | else if (Var->isStaticDataMember() && |
| 4015 | Var->getLexicalDeclContext()->isRecord()) { |
| 4016 | // We need to instantiate the definition of a static data member template, |
| 4017 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 4018 | // declaration of the definition. |
| 4019 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 4020 | TemplateArgs); |
| 4021 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4022 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4023 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 4024 | if (Var) { |
| 4025 | llvm::PointerUnion<VarTemplateDecl *, |
| 4026 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4027 | VarSpec->getSpecializedTemplateOrPartial(); |
| 4028 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 4029 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 4030 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 4031 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 4032 | |
| 4033 | // Merge the definition with the declaration. |
| 4034 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
| 4035 | LookupOrdinaryName, ForRedeclaration); |
| 4036 | R.addDecl(OldVar); |
| 4037 | MergeVarDecl(Var, R); |
| 4038 | |
| 4039 | // Attach the initializer. |
| 4040 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4041 | } |
| 4042 | } else |
| 4043 | // Complete the existing variable's definition with an appropriately |
| 4044 | // substituted type and initializer. |
| 4045 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4046 | |
| 4047 | PreviousContext.pop(); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4048 | |
| 4049 | if (Var) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4050 | PassToConsumerRAII.Var = Var; |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4051 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 4052 | OldVar->getPointOfInstantiation()); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4053 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4054 | |
| 4055 | // This variable may have local implicit instantiations that need to be |
| 4056 | // instantiated within this scope. |
| 4057 | PerformPendingInstantiations(/*LocalOnly=*/true); |
| 4058 | |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4059 | Local.Exit(); |
| 4060 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4061 | if (Recursive) { |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4062 | // Define any newly required vtables. |
| 4063 | DefineUsedVTables(); |
| 4064 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4065 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | // instantiation of this template. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4067 | PerformPendingInstantiations(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | |
Nico Weber | ccec9d8 | 2014-08-15 22:29:14 +0000 | [diff] [blame] | 4069 | // Restore PendingInstantiations and VTableUses. |
| 4070 | SavePendingInstantiationsAndVTableUses.reset(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | } |
Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4072 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4073 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4074 | void |
| 4075 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4076 | const CXXConstructorDecl *Tmpl, |
| 4077 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4078 | |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4079 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4080 | bool AnyErrors = Tmpl->isInvalidDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4081 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4082 | // Instantiate all the initializers. |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4083 | for (const auto *Init : Tmpl->inits()) { |
Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4084 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4085 | // ones. |
| 4086 | if (!Init->isWritten()) |
| 4087 | continue; |
| 4088 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4089 | SourceLocation EllipsisLoc; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4090 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4091 | if (Init->isPackExpansion()) { |
| 4092 | // This is a pack expansion. We should expand it now. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4093 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4094 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4095 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4096 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4097 | bool ShouldExpand = false; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4098 | bool RetainExpansion = false; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4099 | Optional<unsigned> NumExpansions; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4100 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4101 | BaseTL.getSourceRange(), |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4102 | Unexpanded, |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4103 | TemplateArgs, ShouldExpand, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4104 | RetainExpansion, |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4105 | NumExpansions)) { |
| 4106 | AnyErrors = true; |
| 4107 | New->setInvalidDecl(); |
| 4108 | continue; |
| 4109 | } |
| 4110 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4111 | |
| 4112 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4113 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4114 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4115 | |
| 4116 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4117 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4118 | /*CXXDirectInit=*/true); |
| 4119 | if (TempInit.isInvalid()) { |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4120 | AnyErrors = true; |
| 4121 | break; |
| 4122 | } |
| 4123 | |
| 4124 | // Instantiate the base type. |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4125 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4126 | TemplateArgs, |
| 4127 | Init->getSourceLocation(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4128 | New->getDeclName()); |
| 4129 | if (!BaseTInfo) { |
| 4130 | AnyErrors = true; |
| 4131 | break; |
| 4132 | } |
| 4133 | |
| 4134 | // Build the initializer. |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4135 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4136 | BaseTInfo, TempInit.get(), |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4137 | New->getParent(), |
| 4138 | SourceLocation()); |
| 4139 | if (NewInit.isInvalid()) { |
| 4140 | AnyErrors = true; |
| 4141 | break; |
| 4142 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4143 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4144 | NewInits.push_back(NewInit.get()); |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4145 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4146 | |
Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4147 | continue; |
| 4148 | } |
| 4149 | |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4150 | // Instantiate the initializer. |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4151 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4152 | /*CXXDirectInit=*/true); |
| 4153 | if (TempInit.isInvalid()) { |
Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4154 | AnyErrors = true; |
| 4155 | continue; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4156 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4157 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4158 | MemInitResult NewInit; |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4159 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4160 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4161 | TemplateArgs, |
| 4162 | Init->getSourceLocation(), |
| 4163 | New->getDeclName()); |
| 4164 | if (!TInfo) { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4165 | AnyErrors = true; |
Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4166 | New->setInvalidDecl(); |
| 4167 | continue; |
| 4168 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4169 | |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4170 | if (Init->isBaseInitializer()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4171 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4172 | New->getParent(), EllipsisLoc); |
| 4173 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4174 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4175 | cast<CXXRecordDecl>(CurContext->getParent())); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4176 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4177 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4178 | Init->getMemberLocation(), |
| 4179 | Init->getMember(), |
| 4180 | TemplateArgs)); |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4181 | if (!Member) { |
| 4182 | AnyErrors = true; |
| 4183 | New->setInvalidDecl(); |
| 4184 | continue; |
| 4185 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4186 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4187 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4188 | Init->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4189 | } else if (Init->isIndirectMemberInitializer()) { |
| 4190 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4191 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4192 | Init->getMemberLocation(), |
| 4193 | Init->getIndirectMember(), TemplateArgs)); |
| 4194 | |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4195 | if (!IndirectMember) { |
| 4196 | AnyErrors = true; |
| 4197 | New->setInvalidDecl(); |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4198 | continue; |
Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4199 | } |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4200 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4201 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4202 | Init->getSourceLocation()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4205 | if (NewInit.isInvalid()) { |
| 4206 | AnyErrors = true; |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4207 | New->setInvalidDecl(); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4208 | } else { |
Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4209 | NewInits.push_back(NewInit.get()); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4213 | // Assign all the initializers to the new constructor. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4214 | ActOnMemInitializers(New, |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4215 | /*FIXME: ColonLoc */ |
| 4216 | SourceLocation(), |
David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4217 | NewInits, |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4218 | AnyErrors); |
Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4221 | // TODO: this could be templated if the various decl types used the |
| 4222 | // same method name. |
| 4223 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4224 | ClassTemplateDecl *Instance) { |
| 4225 | Pattern = Pattern->getCanonicalDecl(); |
| 4226 | |
| 4227 | do { |
| 4228 | Instance = Instance->getCanonicalDecl(); |
| 4229 | if (Pattern == Instance) return true; |
| 4230 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4231 | } while (Instance); |
| 4232 | |
| 4233 | return false; |
| 4234 | } |
| 4235 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4236 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 4237 | FunctionTemplateDecl *Instance) { |
| 4238 | Pattern = Pattern->getCanonicalDecl(); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4239 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4240 | do { |
| 4241 | Instance = Instance->getCanonicalDecl(); |
| 4242 | if (Pattern == Instance) return true; |
| 4243 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4244 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4246 | return false; |
| 4247 | } |
| 4248 | |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4249 | static bool |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4250 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 4251 | ClassTemplatePartialSpecializationDecl *Instance) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4252 | Pattern |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4253 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 4254 | do { |
| 4255 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 4256 | Instance->getCanonicalDecl()); |
| 4257 | if (Pattern == Instance) |
| 4258 | return true; |
| 4259 | Instance = Instance->getInstantiatedFromMember(); |
| 4260 | } while (Instance); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4262 | return false; |
| 4263 | } |
| 4264 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4265 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 4266 | CXXRecordDecl *Instance) { |
| 4267 | Pattern = Pattern->getCanonicalDecl(); |
| 4268 | |
| 4269 | do { |
| 4270 | Instance = Instance->getCanonicalDecl(); |
| 4271 | if (Pattern == Instance) return true; |
| 4272 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 4273 | } while (Instance); |
| 4274 | |
| 4275 | return false; |
| 4276 | } |
| 4277 | |
| 4278 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 4279 | FunctionDecl *Instance) { |
| 4280 | Pattern = Pattern->getCanonicalDecl(); |
| 4281 | |
| 4282 | do { |
| 4283 | Instance = Instance->getCanonicalDecl(); |
| 4284 | if (Pattern == Instance) return true; |
| 4285 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 4286 | } while (Instance); |
| 4287 | |
| 4288 | return false; |
| 4289 | } |
| 4290 | |
| 4291 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 4292 | EnumDecl *Instance) { |
| 4293 | Pattern = Pattern->getCanonicalDecl(); |
| 4294 | |
| 4295 | do { |
| 4296 | Instance = Instance->getCanonicalDecl(); |
| 4297 | if (Pattern == Instance) return true; |
| 4298 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 4299 | } while (Instance); |
| 4300 | |
| 4301 | return false; |
| 4302 | } |
| 4303 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4304 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 4305 | UsingShadowDecl *Instance, |
| 4306 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4307 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 4308 | Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 4312 | UsingDecl *Instance, |
| 4313 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4314 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4317 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 4318 | UsingDecl *Instance, |
| 4319 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4320 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4324 | UsingDecl *Instance, |
| 4325 | ASTContext &C) { |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4326 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4329 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 4330 | VarDecl *Instance) { |
| 4331 | assert(Instance->isStaticDataMember()); |
| 4332 | |
| 4333 | Pattern = Pattern->getCanonicalDecl(); |
| 4334 | |
| 4335 | do { |
| 4336 | Instance = Instance->getCanonicalDecl(); |
| 4337 | if (Pattern == Instance) return true; |
| 4338 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 4339 | } while (Instance); |
| 4340 | |
| 4341 | return false; |
| 4342 | } |
| 4343 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4344 | // Other is the prospective instantiation |
| 4345 | // D is the prospective pattern |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4346 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4347 | if (D->getKind() != Other->getKind()) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4348 | if (UnresolvedUsingTypenameDecl *UUD |
| 4349 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 4350 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 4351 | return isInstantiationOf(UUD, UD, Ctx); |
| 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | if (UnresolvedUsingValueDecl *UUD |
| 4356 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4357 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 4358 | return isInstantiationOf(UUD, UD, Ctx); |
| 4359 | } |
| 4360 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4361 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4362 | return false; |
| 4363 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4364 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4365 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 4366 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4368 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 4369 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4370 | |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4371 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 4372 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4374 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4375 | if (Var->isStaticDataMember()) |
| 4376 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 4377 | |
| 4378 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 4379 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4381 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 4382 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 4383 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4384 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4385 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 4386 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 4387 | PartialSpec); |
| 4388 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4389 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 4390 | if (!Field->getDeclName()) { |
| 4391 | // This is an unnamed field. |
Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 4392 | return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 4393 | cast<FieldDecl>(D)); |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4394 | } |
| 4395 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4397 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 4398 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 4399 | |
| 4400 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 4401 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 4402 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4403 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 4404 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 4405 | } |
| 4406 | |
| 4407 | template<typename ForwardIterator> |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4408 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4409 | NamedDecl *D, |
| 4410 | ForwardIterator first, |
| 4411 | ForwardIterator last) { |
| 4412 | for (; first != last; ++first) |
| 4413 | if (isInstantiationOf(Ctx, D, *first)) |
| 4414 | return cast<NamedDecl>(*first); |
| 4415 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4416 | return nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4417 | } |
| 4418 | |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4419 | /// \brief Finds the instantiation of the given declaration context |
| 4420 | /// within the current instantiation. |
| 4421 | /// |
| 4422 | /// \returns NULL if there was an error |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4423 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4424 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4425 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4426 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4427 | return cast_or_null<DeclContext>(ID); |
| 4428 | } else return DC; |
| 4429 | } |
| 4430 | |
Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 4431 | /// \brief Find the instantiation of the given declaration within the |
| 4432 | /// current instantiation. |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4433 | /// |
| 4434 | /// This routine is intended to be used when \p D is a declaration |
| 4435 | /// referenced from within a template, that needs to mapped into the |
| 4436 | /// corresponding declaration within an instantiation. For example, |
| 4437 | /// given: |
| 4438 | /// |
| 4439 | /// \code |
| 4440 | /// template<typename T> |
| 4441 | /// struct X { |
| 4442 | /// enum Kind { |
| 4443 | /// KnownValue = sizeof(T) |
| 4444 | /// }; |
| 4445 | /// |
| 4446 | /// bool getKind() const { return KnownValue; } |
| 4447 | /// }; |
| 4448 | /// |
| 4449 | /// template struct X<int>; |
| 4450 | /// \endcode |
| 4451 | /// |
Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 4452 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the |
| 4453 | /// \p EnumConstantDecl for \p KnownValue (which refers to |
| 4454 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation |
| 4455 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs |
| 4456 | /// this mapping from within the instantiation of <tt>X<int></tt>. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4457 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4458 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4459 | DeclContext *ParentDC = D->getDeclContext(); |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4460 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
| 4461 | // parameters (p below) can have their ParentDC set to the translation-unit |
| 4462 | // - thus we can not consistently check if the ParentDC of such a parameter |
| 4463 | // is Dependent or/and a FunctionOrMethod. |
| 4464 | // For e.g. this code, during Template argument deduction tries to |
| 4465 | // find an instantiated decl for (T y) when the ParentDC for y is |
| 4466 | // the translation unit. |
| 4467 | // 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] | 4468 | // float baz(float(*)()) { return 0.0; } |
Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4469 | // Foo(baz); |
| 4470 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 4471 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 4472 | // For now: |
| 4473 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 4474 | // whose type is not instantiation dependent, do nothing to the decl |
| 4475 | // - otherwise find its instantiated decl. |
| 4476 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 4477 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 4478 | return D; |
Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 4479 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 4480 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4481 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) || |
| 4482 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4483 | // D is a local of some kind. Look into the map of local |
| 4484 | // declarations to their instantiations. |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 4485 | if (CurrentInstantiationScope) { |
| 4486 | if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 4487 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 4488 | return cast<NamedDecl>(FD); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4489 | |
Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 4490 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 4491 | assert(PackIdx != -1 && |
| 4492 | "found declaration pack but not pack expanding"); |
| 4493 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 4494 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 4495 | } |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 4498 | // If we're performing a partial substitution during template argument |
| 4499 | // deduction, we may not have values for template parameters yet. They |
| 4500 | // just map to themselves. |
| 4501 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 4502 | isa<TemplateTemplateParmDecl>(D)) |
| 4503 | return D; |
| 4504 | |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4505 | if (D->isInvalidDecl()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4506 | return nullptr; |
Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4507 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4508 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 4509 | // been found yet. Lazily instantiate it and return it now. |
| 4510 | assert(isa<LabelDecl>(D)); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4511 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4512 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 4513 | assert(Inst && "Failed to instantiate label??"); |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4514 | |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4515 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 4516 | return cast<LabelDecl>(Inst); |
Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4517 | } |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4518 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4519 | // For variable template specializations, update those that are still |
| 4520 | // type-dependent. |
| 4521 | if (VarTemplateSpecializationDecl *VarSpec = |
| 4522 | dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 4523 | bool InstantiationDependent = false; |
| 4524 | const TemplateArgumentListInfo &VarTemplateArgs = |
| 4525 | VarSpec->getTemplateArgsInfo(); |
| 4526 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
| 4527 | VarTemplateArgs, InstantiationDependent)) |
| 4528 | D = cast<NamedDecl>( |
| 4529 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); |
| 4530 | return D; |
| 4531 | } |
| 4532 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4533 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 4534 | if (!Record->isDependentContext()) |
| 4535 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4537 | // Determine whether this record is the "templated" declaration describing |
| 4538 | // a class template or class template partial specialization. |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4539 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4540 | if (ClassTemplate) |
| 4541 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 4542 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 4543 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 4544 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4546 | // Walk the current context to find either the record or an instantiation of |
| 4547 | // it. |
| 4548 | DeclContext *DC = CurContext; |
| 4549 | while (!DC->isFileContext()) { |
| 4550 | // If we're performing substitution while we're inside the template |
| 4551 | // definition, we'll find our own context. We're done. |
| 4552 | if (DC->Equals(Record)) |
| 4553 | return Record; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4555 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 4556 | // Check whether we're in the process of instantiating a class template |
| 4557 | // specialization of the template we're mapping. |
| 4558 | if (ClassTemplateSpecializationDecl *InstSpec |
| 4559 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 4560 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 4561 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 4562 | return InstRecord; |
| 4563 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4565 | // Check whether we're in the process of instantiating a member class. |
| 4566 | if (isInstantiationOf(Record, InstRecord)) |
| 4567 | return InstRecord; |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4568 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4570 | // Move to the outer template scope. |
| 4571 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 4572 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 4573 | DC = FD->getLexicalDeclContext(); |
| 4574 | continue; |
| 4575 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4576 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4577 | |
Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4578 | DC = DC->getParent(); |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4579 | } |
Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 4580 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4581 | // Fall through to deal with other dependent record types (e.g., |
| 4582 | // anonymous unions in class templates). |
| 4583 | } |
John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4584 | |
Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4585 | if (!ParentDC->isDependentContext()) |
| 4586 | return D; |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4587 | |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4588 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | if (!ParentDC) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4590 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4592 | if (ParentDC != D->getDeclContext()) { |
| 4593 | // We performed some kind of instantiation in the parent context, |
| 4594 | // so now we need to look into the instantiated parent context to |
| 4595 | // find the instantiation of the declaration D. |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4596 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4597 | // If our context used to be dependent, we may need to instantiate |
| 4598 | // it before performing lookup into that context. |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4599 | bool IsBeingInstantiated = false; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4600 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4601 | if (!Spec->isDependentContext()) { |
| 4602 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4603 | const RecordType *Tag = T->getAs<RecordType>(); |
| 4604 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4605 | if (Tag->isBeingDefined()) |
| 4606 | IsBeingInstantiated = true; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4607 | if (!Tag->isBeingDefined() && |
| 4608 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4609 | return nullptr; |
Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 4610 | |
| 4611 | ParentDC = Tag->getDecl(); |
Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4612 | } |
| 4613 | } |
| 4614 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4615 | NamedDecl *Result = nullptr; |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4616 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4617 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4618 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4619 | } else { |
| 4620 | // Since we don't have a name for the entity we're looking for, |
| 4621 | // our only option is to walk through all of the declarations to |
| 4622 | // find that name. This will occur in a few cases: |
| 4623 | // |
| 4624 | // - anonymous struct/union within a template |
| 4625 | // - unnamed class/struct/union/enum within a template |
| 4626 | // |
| 4627 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4629 | ParentDC->decls_begin(), |
| 4630 | ParentDC->decls_end()); |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4631 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4633 | if (!Result) { |
| 4634 | if (isa<UsingShadowDecl>(D)) { |
| 4635 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 4636 | } else if (Diags.hasErrorOccurred()) { |
| 4637 | // We've already complained about something, so most likely this |
| 4638 | // declaration failed to instantiate. There's no point in complaining |
| 4639 | // further, since this is normal in invalid code. |
| 4640 | } else if (IsBeingInstantiated) { |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4641 | // The class in which this member exists is currently being |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4642 | // instantiated, and we haven't gotten around to instantiating this |
| 4643 | // member yet. This can happen when the code uses forward declarations |
| 4644 | // of member classes, and introduces ordering dependencies via |
| 4645 | // template instantiation. |
| 4646 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 4647 | << D->getDeclName() |
| 4648 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 4649 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 4650 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 4651 | // This enumeration constant was found when the template was defined, |
| 4652 | // but can't be found in the instantiation. This can happen if an |
| 4653 | // unscoped enumeration member is explicitly specialized. |
| 4654 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 4655 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 4656 | TemplateArgs)); |
| 4657 | assert(Spec->getTemplateSpecializationKind() == |
| 4658 | TSK_ExplicitSpecialization); |
| 4659 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 4660 | << D->getDeclName() |
| 4661 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 4662 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 4663 | << Context.getTypeDeclType(Spec); |
Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4664 | } else { |
| 4665 | // We should have found something, but didn't. |
| 4666 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 4667 | } |
| 4668 | } |
NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4670 | D = Result; |
| 4671 | } |
| 4672 | |
Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4673 | return D; |
| 4674 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4675 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4677 | /// instantiations we have seen until this point. |
Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4678 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4679 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4680 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4681 | PendingImplicitInstantiation Inst; |
| 4682 | |
| 4683 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4684 | Inst = PendingInstantiations.front(); |
| 4685 | PendingInstantiations.pop_front(); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4686 | } else { |
| 4687 | Inst = PendingLocalImplicitInstantiations.front(); |
| 4688 | PendingLocalImplicitInstantiations.pop_front(); |
| 4689 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4691 | // Instantiate function definitions |
| 4692 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4693 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), |
| 4694 | "instantiating function definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4695 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 4696 | TSK_ExplicitInstantiationDefinition; |
| 4697 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, |
| 4698 | DefinitionRequired); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4699 | continue; |
| 4700 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4702 | // Instantiate variable definitions |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4703 | VarDecl *Var = cast<VarDecl>(Inst.first); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4704 | |
| 4705 | assert((Var->isStaticDataMember() || |
| 4706 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 4707 | "Not a static data member, nor a variable template" |
| 4708 | " specialization?"); |
Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 4709 | |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4710 | // Don't try to instantiate declarations if the most recent redeclaration |
| 4711 | // is invalid. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4712 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4713 | continue; |
| 4714 | |
| 4715 | // Check if the most recent declaration has changed the specialization kind |
| 4716 | // and removed the need for implicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4717 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4718 | case TSK_Undeclared: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4719 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4720 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4721 | case TSK_ExplicitSpecialization: |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4722 | continue; // No longer need to instantiate this type. |
| 4723 | case TSK_ExplicitInstantiationDefinition: |
| 4724 | // We only need an instantiation if the pending instantiation *is* the |
| 4725 | // explicit instantiation. |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4726 | if (Var != Var->getMostRecentDecl()) continue; |
Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4727 | case TSK_ImplicitInstantiation: |
| 4728 | break; |
| 4729 | } |
| 4730 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4731 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(), |
| 4732 | "instantiating variable definition"); |
Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4733 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 4734 | TSK_ExplicitInstantiationDefinition; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Instantiate static data member definitions or variable template |
| 4737 | // specializations. |
| 4738 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
| 4739 | DefinitionRequired); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4740 | } |
| 4741 | } |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 4742 | |
| 4743 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 4744 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 4745 | for (auto DD : Pattern->ddiags()) { |
John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 4746 | switch (DD->getKind()) { |
| 4747 | case DependentDiagnostic::Access: |
| 4748 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 4749 | break; |
| 4750 | } |
| 4751 | } |
| 4752 | } |