| 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" | 
|  | 15 | #include "clang/AST/DeclTemplate.h" | 
|  | 16 | #include "clang/AST/DeclVisitor.h" | 
| John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 17 | #include "clang/AST/DependentDiagnostic.h" | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" | 
| Douglas Gregor | 6131b44 | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" | 
| Douglas Gregor | 402250f | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Lookup.h" | 
|  | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" | 
|  | 24 | #include "clang/Sema/Template.h" | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 25 |  | 
|  | 26 | using namespace clang; | 
|  | 27 |  | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 28 | static bool isDeclWithinFunction(const Decl *D) { | 
|  | 29 | const DeclContext *DC = D->getDeclContext(); | 
|  | 30 | if (DC->isFunctionOrMethod()) | 
|  | 31 | return true; | 
|  | 32 |  | 
|  | 33 | if (DC->isRecord()) | 
|  | 34 | return cast<CXXRecordDecl>(DC)->isLocalClass(); | 
|  | 35 |  | 
|  | 36 | return false; | 
|  | 37 | } | 
|  | 38 |  | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 39 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, | 
|  | 40 | DeclaratorDecl *NewDecl) { | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 41 | if (!OldDecl->getQualifierLoc()) | 
|  | 42 | return false; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 43 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 44 | NestedNameSpecifierLoc NewQualifierLoc | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 45 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 46 | TemplateArgs); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 47 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 48 | if (!NewQualifierLoc) | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 49 | return true; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 50 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 51 | NewDecl->setQualifierInfo(NewQualifierLoc); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 52 | return false; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, | 
|  | 56 | TagDecl *NewDecl) { | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 57 | if (!OldDecl->getQualifierLoc()) | 
|  | 58 | return false; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 59 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 60 | NestedNameSpecifierLoc NewQualifierLoc | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 61 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 62 | TemplateArgs); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 63 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 64 | if (!NewQualifierLoc) | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 65 | return true; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 66 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 67 | NewDecl->setQualifierInfo(NewQualifierLoc); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 68 | return false; | 
|  | 69 | } | 
|  | 70 |  | 
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 71 | // Include attribute instantiation code. | 
|  | 72 | #include "clang/Sema/AttrTemplateInstantiate.inc" | 
|  | 73 |  | 
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 74 | static void instantiateDependentAlignedAttr( | 
|  | 75 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | 
|  | 76 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { | 
|  | 77 | if (Aligned->isAlignmentExpr()) { | 
|  | 78 | // The alignment expression is a constant expression. | 
|  | 79 | EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated); | 
|  | 80 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); | 
|  | 81 | if (!Result.isInvalid()) | 
|  | 82 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>(), | 
|  | 83 | Aligned->getSpellingListIndex(), IsPackExpansion); | 
|  | 84 | } else { | 
|  | 85 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), | 
|  | 86 | TemplateArgs, Aligned->getLocation(), | 
|  | 87 | DeclarationName()); | 
|  | 88 | if (Result) | 
|  | 89 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, | 
|  | 90 | Aligned->getSpellingListIndex(), IsPackExpansion); | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | static void instantiateDependentAlignedAttr( | 
|  | 95 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | 
|  | 96 | const AlignedAttr *Aligned, Decl *New) { | 
|  | 97 | if (!Aligned->isPackExpansion()) { | 
|  | 98 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); | 
|  | 99 | return; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | 
|  | 103 | if (Aligned->isAlignmentExpr()) | 
|  | 104 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), | 
|  | 105 | Unexpanded); | 
|  | 106 | else | 
|  | 107 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), | 
|  | 108 | Unexpanded); | 
|  | 109 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); | 
|  | 110 |  | 
|  | 111 | // Determine whether we can expand this attribute pack yet. | 
|  | 112 | bool Expand = true, RetainExpansion = false; | 
|  | 113 | Optional<unsigned> NumExpansions; | 
|  | 114 | // FIXME: Use the actual location of the ellipsis. | 
|  | 115 | SourceLocation EllipsisLoc = Aligned->getLocation(); | 
|  | 116 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), | 
|  | 117 | Unexpanded, TemplateArgs, Expand, | 
|  | 118 | RetainExpansion, NumExpansions)) | 
|  | 119 | return; | 
|  | 120 |  | 
|  | 121 | if (!Expand) { | 
|  | 122 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); | 
|  | 123 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); | 
|  | 124 | } else { | 
|  | 125 | for (unsigned I = 0; I != *NumExpansions; ++I) { | 
|  | 126 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); | 
|  | 127 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 132 | static void instantiateDependentEnableIfAttr( | 
|  | 133 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, | 
|  | 134 | const EnableIfAttr *A, const Decl *Tmpl, Decl *New) { | 
|  | 135 | Expr *Cond = 0; | 
|  | 136 | { | 
|  | 137 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); | 
|  | 138 | ExprResult Result = S.SubstExpr(A->getCond(), TemplateArgs); | 
|  | 139 | if (Result.isInvalid()) | 
|  | 140 | return; | 
|  | 141 | Cond = Result.takeAs<Expr>(); | 
|  | 142 | } | 
|  | 143 | if (A->getCond()->isTypeDependent() && !Cond->isTypeDependent()) { | 
|  | 144 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); | 
|  | 145 | if (Converted.isInvalid()) | 
|  | 146 | return; | 
|  | 147 | Cond = Converted.take(); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | SmallVector<PartialDiagnosticAt, 8> Diags; | 
|  | 151 | if (A->getCond()->isValueDependent() && !Cond->isValueDependent() && | 
|  | 152 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(Tmpl), | 
|  | 153 | Diags)) { | 
|  | 154 | S.Diag(A->getLocation(), diag::err_enable_if_never_constant_expr); | 
|  | 155 | for (int I = 0, N = Diags.size(); I != N; ++I) | 
|  | 156 | S.Diag(Diags[I].first, Diags[I].second); | 
|  | 157 | return; | 
|  | 158 | } | 
|  | 159 |  | 
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 160 | EnableIfAttr *EIA = new (S.getASTContext()) | 
|  | 161 | EnableIfAttr(A->getLocation(), S.getASTContext(), Cond, | 
|  | 162 | A->getMessage(), | 
|  | 163 | A->getSpellingListIndex()); | 
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 164 | New->addAttr(EIA); | 
|  | 165 | } | 
|  | 166 |  | 
| John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 167 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 168 | const Decl *Tmpl, Decl *New, | 
|  | 169 | LateInstantiatedAttrVec *LateAttrs, | 
|  | 170 | LocalInstantiationScope *OuterMostScope) { | 
| Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 171 | for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end(); | 
|  | 172 | i != e; ++i) { | 
|  | 173 | const Attr *TmplAttr = *i; | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 174 |  | 
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 175 | // FIXME: This should be generalized to more than just the AlignedAttr. | 
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 176 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); | 
|  | 177 | if (Aligned && Aligned->isAlignmentDependent()) { | 
|  | 178 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); | 
|  | 179 | continue; | 
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 182 | const EnableIfAttr *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr); | 
|  | 183 | if (EnableIf && EnableIf->getCond()->isValueDependent()) { | 
|  | 184 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, | 
|  | 185 | New); | 
|  | 186 | continue; | 
|  | 187 | } | 
|  | 188 |  | 
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 189 | assert(!TmplAttr->isPackExpansion()); | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 190 | if (TmplAttr->isLateParsed() && LateAttrs) { | 
|  | 191 | // Late parsed attributes must be instantiated and attached after the | 
|  | 192 | // enclosing class has been instantiated.  See Sema::InstantiateClass. | 
|  | 193 | LocalInstantiationScope *Saved = 0; | 
|  | 194 | if (CurrentInstantiationScope) | 
|  | 195 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); | 
|  | 196 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); | 
|  | 197 | } else { | 
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 198 | // Allow 'this' within late-parsed attributes. | 
|  | 199 | NamedDecl *ND = dyn_cast<NamedDecl>(New); | 
|  | 200 | CXXRecordDecl *ThisContext = | 
|  | 201 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); | 
|  | 202 | CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0, | 
|  | 203 | ND && ND->isCXXInstanceMember()); | 
|  | 204 |  | 
| Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 205 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, | 
|  | 206 | *this, TemplateArgs); | 
| Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 207 | if (NewAttr) | 
|  | 208 | New->addAttr(NewAttr); | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 209 | } | 
| Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 213 | Decl * | 
|  | 214 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 215 | llvm_unreachable("Translation units cannot be instantiated"); | 
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
|  | 218 | Decl * | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 219 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { | 
|  | 220 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
|  | 221 | D->getIdentifier()); | 
|  | 222 | Owner->addDecl(Inst); | 
|  | 223 | return Inst; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | Decl * | 
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 227 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 228 | llvm_unreachable("Namespaces cannot be instantiated"); | 
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 231 | Decl * | 
|  | 232 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { | 
|  | 233 | NamespaceAliasDecl *Inst | 
|  | 234 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, | 
|  | 235 | D->getNamespaceLoc(), | 
|  | 236 | D->getAliasLoc(), | 
| Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 237 | D->getIdentifier(), | 
|  | 238 | D->getQualifierLoc(), | 
| John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 239 | D->getTargetNameLoc(), | 
|  | 240 | D->getNamespace()); | 
|  | 241 | Owner->addDecl(Inst); | 
|  | 242 | return Inst; | 
|  | 243 | } | 
|  | 244 |  | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 245 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, | 
|  | 246 | bool IsTypeAlias) { | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 247 | bool Invalid = false; | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 248 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 249 | if (DI->getType()->isInstantiationDependentType() || | 
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 250 | DI->getType()->isVariablyModifiedType()) { | 
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 251 | DI = SemaRef.SubstType(DI, TemplateArgs, | 
|  | 252 | D->getLocation(), D->getDeclName()); | 
|  | 253 | if (!DI) { | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 254 | Invalid = true; | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 255 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 256 | } | 
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 257 | } else { | 
|  | 258 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 259 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 |  | 
| Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 261 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. | 
|  | 262 | // libstdc++ relies upon this bug in its implementation of common_type. | 
|  | 263 | // If we happen to be processing that implementation, fake up the g++ ?: | 
|  | 264 | // semantics. See LWG issue 2141 for more information on the bug. | 
|  | 265 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); | 
|  | 266 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); | 
|  | 267 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && | 
|  | 268 | DT->isReferenceType() && | 
|  | 269 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && | 
|  | 270 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && | 
|  | 271 | D->getIdentifier() && D->getIdentifier()->isStr("type") && | 
|  | 272 | SemaRef.getSourceManager().isInSystemHeader(D->getLocStart())) | 
|  | 273 | // Fold it to the (non-reference) type which g++ would have produced. | 
|  | 274 | DI = SemaRef.Context.getTrivialTypeSourceInfo( | 
|  | 275 | DI->getType().getNonReferenceType()); | 
|  | 276 |  | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 277 | // Create the new typedef | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 278 | TypedefNameDecl *Typedef; | 
|  | 279 | if (IsTypeAlias) | 
|  | 280 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(), | 
|  | 281 | D->getLocation(), D->getIdentifier(), DI); | 
|  | 282 | else | 
|  | 283 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(), | 
|  | 284 | D->getLocation(), D->getIdentifier(), DI); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 285 | if (Invalid) | 
|  | 286 | Typedef->setInvalidDecl(); | 
|  | 287 |  | 
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 288 | // If the old typedef was the name for linkage purposes of an anonymous | 
|  | 289 | // tag decl, re-establish that relationship for the new typedef. | 
|  | 290 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { | 
|  | 291 | TagDecl *oldTag = oldTagType->getDecl(); | 
| Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 292 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { | 
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 293 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); | 
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 294 | assert(!newTag->hasNameForLinkage()); | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 295 | newTag->setTypedefNameForAnonDecl(Typedef); | 
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 296 | } | 
| Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 297 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 298 |  | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 299 | if (TypedefNameDecl *Prev = D->getPreviousDecl()) { | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 300 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, | 
|  | 301 | TemplateArgs); | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 302 | if (!InstPrev) | 
|  | 303 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 304 |  | 
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 305 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); | 
|  | 306 |  | 
|  | 307 | // If the typedef types are not identical, reject them. | 
|  | 308 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); | 
|  | 309 |  | 
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 310 | Typedef->setPreviousDecl(InstPrevTypedef); | 
| John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
| John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 313 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); | 
| Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 314 |  | 
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 315 | Typedef->setAccess(D->getAccess()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 |  | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 317 | return Typedef; | 
|  | 318 | } | 
|  | 319 |  | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 320 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 321 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); | 
|  | 322 | Owner->addDecl(Typedef); | 
|  | 323 | return Typedef; | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 327 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); | 
|  | 328 | Owner->addDecl(Typedef); | 
|  | 329 | return Typedef; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | Decl * | 
|  | 333 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { | 
|  | 334 | // Create a local instantiation scope for this type alias template, which | 
|  | 335 | // will contain the instantiations of the template parameters. | 
|  | 336 | LocalInstantiationScope Scope(SemaRef); | 
|  | 337 |  | 
|  | 338 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
|  | 339 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 340 | if (!InstParams) | 
|  | 341 | return 0; | 
|  | 342 |  | 
|  | 343 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); | 
|  | 344 |  | 
|  | 345 | TypeAliasTemplateDecl *PrevAliasTemplate = 0; | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 346 | if (Pattern->getPreviousDecl()) { | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 347 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | 
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 348 | if (!Found.empty()) { | 
|  | 349 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 350 | } | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( | 
|  | 354 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); | 
|  | 355 | if (!AliasInst) | 
|  | 356 | return 0; | 
|  | 357 |  | 
|  | 358 | TypeAliasTemplateDecl *Inst | 
|  | 359 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
|  | 360 | D->getDeclName(), InstParams, AliasInst); | 
|  | 361 | if (PrevAliasTemplate) | 
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 362 | Inst->setPreviousDecl(PrevAliasTemplate); | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 363 |  | 
|  | 364 | Inst->setAccess(D->getAccess()); | 
|  | 365 |  | 
|  | 366 | if (!PrevAliasTemplate) | 
|  | 367 | Inst->setInstantiatedFromMemberTemplate(D); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 368 |  | 
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 369 | Owner->addDecl(Inst); | 
|  | 370 |  | 
|  | 371 | return Inst; | 
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 372 | } | 
|  | 373 |  | 
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 374 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 375 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 378 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, | 
|  | 379 | bool InstantiatingVarTemplate) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 380 |  | 
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 381 | // If this is the variable for an anonymous struct or union, | 
|  | 382 | // instantiate the anonymous struct/union type first. | 
|  | 383 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) | 
|  | 384 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) | 
|  | 385 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) | 
|  | 386 | return 0; | 
|  | 387 |  | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 388 | // Do substitution on the type of the declaration | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 389 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), | 
| John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 390 | TemplateArgs, | 
|  | 391 | D->getTypeSpecStartLoc(), | 
|  | 392 | D->getDeclName()); | 
|  | 393 | if (!DI) | 
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 394 | return 0; | 
|  | 395 |  | 
| Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 396 | if (DI->getType()->isFunctionType()) { | 
|  | 397 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) | 
|  | 398 | << D->isStaticDataMember() << DI->getType(); | 
|  | 399 | return 0; | 
|  | 400 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 401 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 402 | DeclContext *DC = Owner; | 
|  | 403 | if (D->isLocalExternDecl()) | 
|  | 404 | SemaRef.adjustContextForLocalExternDecl(DC); | 
|  | 405 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 406 | // Build the instantiated declaration. | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 407 | VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), | 
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 408 | D->getLocation(), D->getIdentifier(), | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 409 | DI->getType(), DI, D->getStorageClass()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 |  | 
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 411 | // In ARC, infer 'retaining' for variables of retainable type. | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 412 | if (SemaRef.getLangOpts().ObjCAutoRefCount && | 
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 413 | SemaRef.inferObjCARCLifetime(Var)) | 
|  | 414 | Var->setInvalidDecl(); | 
|  | 415 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 416 | // Substitute the nested name specifier, if any. | 
|  | 417 | if (SubstQualifier(D, Var)) | 
|  | 418 | return 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 420 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 421 | StartingScope, InstantiatingVarTemplate); | 
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 422 | return Var; | 
|  | 423 | } | 
|  | 424 |  | 
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 425 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { | 
|  | 426 | AccessSpecDecl* AD | 
|  | 427 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, | 
|  | 428 | D->getAccessSpecifierLoc(), D->getColonLoc()); | 
|  | 429 | Owner->addHiddenDecl(AD); | 
|  | 430 | return AD; | 
|  | 431 | } | 
|  | 432 |  | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 433 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { | 
|  | 434 | bool Invalid = false; | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 435 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | 
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 436 | if (DI->getType()->isInstantiationDependentType() || | 
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 437 | DI->getType()->isVariablyModifiedType())  { | 
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 438 | DI = SemaRef.SubstType(DI, TemplateArgs, | 
|  | 439 | D->getLocation(), D->getDeclName()); | 
|  | 440 | if (!DI) { | 
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 441 | DI = D->getTypeSourceInfo(); | 
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 442 | Invalid = true; | 
|  | 443 | } else if (DI->getType()->isFunctionType()) { | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 444 | // C++ [temp.arg.type]p3: | 
|  | 445 | //   If a declaration acquires a function type through a type | 
|  | 446 | //   dependent on a template-parameter and this causes a | 
|  | 447 | //   declaration that does not use the syntactic form of a | 
|  | 448 | //   function declarator to have function type, the program is | 
|  | 449 | //   ill-formed. | 
|  | 450 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) | 
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 451 | << DI->getType(); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 452 | Invalid = true; | 
|  | 453 | } | 
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 454 | } else { | 
|  | 455 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
|  | 458 | Expr *BitWidth = D->getBitWidth(); | 
|  | 459 | if (Invalid) | 
|  | 460 | BitWidth = 0; | 
|  | 461 | else if (BitWidth) { | 
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 462 | // The bit-width expression is a constant expression. | 
|  | 463 | EnterExpressionEvaluationContext Unevaluated(SemaRef, | 
|  | 464 | Sema::ConstantEvaluated); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 466 | ExprResult InstantiatedBitWidth | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 467 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 468 | if (InstantiatedBitWidth.isInvalid()) { | 
|  | 469 | Invalid = true; | 
|  | 470 | BitWidth = 0; | 
|  | 471 | } else | 
| Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 472 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 473 | } | 
|  | 474 |  | 
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 475 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), | 
|  | 476 | DI->getType(), DI, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | cast<RecordDecl>(Owner), | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 478 | D->getLocation(), | 
|  | 479 | D->isMutable(), | 
|  | 480 | BitWidth, | 
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 481 | D->getInClassInitStyle(), | 
| Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 482 | D->getInnerLocStart(), | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 483 | D->getAccess(), | 
|  | 484 | 0); | 
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 485 | if (!Field) { | 
|  | 486 | cast<Decl>(Owner)->setInvalidDecl(); | 
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 487 | return 0; | 
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 488 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 |  | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 490 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 491 |  | 
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 492 | if (Field->hasAttrs()) | 
|  | 493 | SemaRef.CheckAlignasUnderalignment(Field); | 
|  | 494 |  | 
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 495 | if (Invalid) | 
|  | 496 | Field->setInvalidDecl(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 |  | 
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 498 | if (!Field->getDeclName()) { | 
|  | 499 | // Keep track of where this decl came from. | 
|  | 500 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 501 | } | 
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 502 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { | 
|  | 503 | if (Parent->isAnonymousStructOrUnion() && | 
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 504 | Parent->getRedeclContext()->isFunctionOrMethod()) | 
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 505 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 506 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 |  | 
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 508 | Field->setImplicit(D->isImplicit()); | 
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 509 | Field->setAccess(D->getAccess()); | 
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 510 | Owner->addDecl(Field); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 511 |  | 
|  | 512 | return Field; | 
|  | 513 | } | 
|  | 514 |  | 
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 515 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { | 
|  | 516 | bool Invalid = false; | 
|  | 517 | TypeSourceInfo *DI = D->getTypeSourceInfo(); | 
|  | 518 |  | 
|  | 519 | if (DI->getType()->isVariablyModifiedType()) { | 
|  | 520 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) | 
| Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 521 | << D; | 
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 522 | Invalid = true; | 
|  | 523 | } else if (DI->getType()->isInstantiationDependentType())  { | 
|  | 524 | DI = SemaRef.SubstType(DI, TemplateArgs, | 
|  | 525 | D->getLocation(), D->getDeclName()); | 
|  | 526 | if (!DI) { | 
|  | 527 | DI = D->getTypeSourceInfo(); | 
|  | 528 | Invalid = true; | 
|  | 529 | } else if (DI->getType()->isFunctionType()) { | 
|  | 530 | // C++ [temp.arg.type]p3: | 
|  | 531 | //   If a declaration acquires a function type through a type | 
|  | 532 | //   dependent on a template-parameter and this causes a | 
|  | 533 | //   declaration that does not use the syntactic form of a | 
|  | 534 | //   function declarator to have function type, the program is | 
|  | 535 | //   ill-formed. | 
|  | 536 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) | 
|  | 537 | << DI->getType(); | 
|  | 538 | Invalid = true; | 
|  | 539 | } | 
|  | 540 | } else { | 
|  | 541 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); | 
|  | 542 | } | 
|  | 543 |  | 
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 544 | MSPropertyDecl *Property = MSPropertyDecl::Create( | 
|  | 545 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), | 
|  | 546 | DI, D->getLocStart(), D->getGetterId(), D->getSetterId()); | 
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 547 |  | 
|  | 548 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, | 
|  | 549 | StartingScope); | 
|  | 550 |  | 
|  | 551 | if (Invalid) | 
|  | 552 | Property->setInvalidDecl(); | 
|  | 553 |  | 
|  | 554 | Property->setAccess(D->getAccess()); | 
|  | 555 | Owner->addDecl(Property); | 
|  | 556 |  | 
|  | 557 | return Property; | 
|  | 558 | } | 
|  | 559 |  | 
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 560 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { | 
|  | 561 | NamedDecl **NamedChain = | 
|  | 562 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; | 
|  | 563 |  | 
|  | 564 | int i = 0; | 
|  | 565 | for (IndirectFieldDecl::chain_iterator PI = | 
|  | 566 | D->chain_begin(), PE = D->chain_end(); | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 567 | PI != PE; ++PI) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 568 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI, | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 569 | TemplateArgs); | 
|  | 570 | if (!Next) | 
|  | 571 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 572 |  | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 573 | NamedChain[i++] = Next; | 
|  | 574 | } | 
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 575 |  | 
| Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 576 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); | 
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 577 | IndirectFieldDecl* IndirectField | 
|  | 578 | = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
| Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 579 | D->getIdentifier(), T, | 
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 580 | NamedChain, D->getChainingSize()); | 
|  | 581 |  | 
|  | 582 |  | 
|  | 583 | IndirectField->setImplicit(D->isImplicit()); | 
|  | 584 | IndirectField->setAccess(D->getAccess()); | 
|  | 585 | Owner->addDecl(IndirectField); | 
|  | 586 | return IndirectField; | 
|  | 587 | } | 
|  | 588 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 589 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 590 | // Handle friend type expressions by simply substituting template | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 591 | // parameters into the pattern type and checking the result. | 
| John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 592 | if (TypeSourceInfo *Ty = D->getFriendType()) { | 
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 593 | TypeSourceInfo *InstTy; | 
|  | 594 | // If this is an unsupported friend, don't bother substituting template | 
|  | 595 | // arguments into it. The actual type referred to won't be used by any | 
|  | 596 | // parts of Clang, and may not be valid for instantiating. Just use the | 
|  | 597 | // same info for the instantiated friend. | 
|  | 598 | if (D->isUnsupportedFriend()) { | 
|  | 599 | InstTy = Ty; | 
|  | 600 | } else { | 
|  | 601 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, | 
|  | 602 | D->getLocation(), DeclarationName()); | 
|  | 603 | } | 
|  | 604 | if (!InstTy) | 
| Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 605 | return 0; | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 606 |  | 
| Richard Smith | a31a89a | 2012-09-20 01:31:00 +0000 | [diff] [blame] | 607 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(), | 
| Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 608 | D->getFriendLoc(), InstTy); | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 609 | if (!FD) | 
|  | 610 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 611 |  | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 612 | FD->setAccess(AS_public); | 
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 613 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 614 | Owner->addDecl(FD); | 
|  | 615 | return FD; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 616 | } | 
|  | 617 |  | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 618 | NamedDecl *ND = D->getFriendDecl(); | 
|  | 619 | assert(ND && "friend decl must be a decl or a type!"); | 
|  | 620 |  | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 621 | // All of the Visit implementations for the various potential friend | 
|  | 622 | // declarations have to be carefully written to work for friend | 
|  | 623 | // objects, with the most important detail being that the target | 
|  | 624 | // decl should almost certainly not be placed in Owner. | 
|  | 625 | Decl *NewND = Visit(ND); | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 626 | if (!NewND) return 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 628 | FriendDecl *FD = | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 629 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 630 | cast<NamedDecl>(NewND), D->getFriendLoc()); | 
| John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 631 | FD->setAccess(AS_public); | 
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 632 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 633 | Owner->addDecl(FD); | 
|  | 634 | return FD; | 
| John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 635 | } | 
|  | 636 |  | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 637 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { | 
|  | 638 | Expr *AssertExpr = D->getAssertExpr(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 |  | 
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 640 | // The expression in a static assertion is a constant expression. | 
|  | 641 | EnterExpressionEvaluationContext Unevaluated(SemaRef, | 
|  | 642 | Sema::ConstantEvaluated); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 |  | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 644 | ExprResult InstantiatedAssertExpr | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 645 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 646 | if (InstantiatedAssertExpr.isInvalid()) | 
|  | 647 | return 0; | 
|  | 648 |  | 
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 649 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), | 
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 650 | InstantiatedAssertExpr.get(), | 
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 651 | D->getMessage(), | 
|  | 652 | D->getRParenLoc(), | 
|  | 653 | D->isFailed()); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 654 | } | 
|  | 655 |  | 
|  | 656 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { | 
| Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 657 | EnumDecl *PrevDecl = 0; | 
|  | 658 | if (D->getPreviousDecl()) { | 
|  | 659 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), | 
|  | 660 | D->getPreviousDecl(), | 
|  | 661 | TemplateArgs); | 
|  | 662 | if (!Prev) return 0; | 
|  | 663 | PrevDecl = cast<EnumDecl>(Prev); | 
|  | 664 | } | 
|  | 665 |  | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 666 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(), | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 667 | D->getLocation(), D->getIdentifier(), | 
| Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 668 | PrevDecl, D->isScoped(), | 
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 669 | D->isScopedUsingClassTag(), D->isFixed()); | 
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 670 | if (D->isFixed()) { | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 671 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { | 
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 672 | // If we have type source information for the underlying type, it means it | 
|  | 673 | // has been explicitly set by the user. Perform substitution on it before | 
|  | 674 | // moving on. | 
|  | 675 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 676 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, | 
|  | 677 | DeclarationName()); | 
|  | 678 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) | 
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 679 | Enum->setIntegerType(SemaRef.Context.IntTy); | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 680 | else | 
|  | 681 | Enum->setIntegerTypeSourceInfo(NewTI); | 
|  | 682 | } else { | 
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 683 | assert(!D->getIntegerType()->isDependentType() | 
|  | 684 | && "Dependent type without type source info"); | 
|  | 685 | Enum->setIntegerType(D->getIntegerType()); | 
|  | 686 | } | 
|  | 687 | } | 
|  | 688 |  | 
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 689 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); | 
|  | 690 |  | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 691 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); | 
| Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 692 | Enum->setAccess(D->getAccess()); | 
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 693 | // Forward the mangling number from the template to the instantiated decl. | 
|  | 694 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 695 | if (SubstQualifier(D, Enum)) return 0; | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 696 | Owner->addDecl(Enum); | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 697 |  | 
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 698 | EnumDecl *Def = D->getDefinition(); | 
|  | 699 | if (Def && Def != D) { | 
|  | 700 | // If this is an out-of-line definition of an enum member template, check | 
|  | 701 | // that the underlying types match in the instantiation of both | 
|  | 702 | // declarations. | 
|  | 703 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { | 
|  | 704 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); | 
|  | 705 | QualType DefnUnderlying = | 
|  | 706 | SemaRef.SubstType(TI->getType(), TemplateArgs, | 
|  | 707 | UnderlyingLoc, DeclarationName()); | 
|  | 708 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), | 
|  | 709 | DefnUnderlying, Enum); | 
|  | 710 | } | 
|  | 711 | } | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 712 |  | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 713 | // C++11 [temp.inst]p1: The implicit instantiation of a class template | 
|  | 714 | // specialization causes the implicit instantiation of the declarations, but | 
|  | 715 | // not the definitions of scoped member enumerations. | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 716 | // | 
|  | 717 | // DR1484 clarifies that enumeration definitions inside of a template | 
|  | 718 | // declaration aren't considered entities that can be separately instantiated | 
|  | 719 | // from the rest of the entity they are declared inside of. | 
|  | 720 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { | 
|  | 721 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); | 
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 722 | InstantiateEnumDefinition(Enum, Def); | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 723 | } | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 724 |  | 
|  | 725 | return Enum; | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | void TemplateDeclInstantiator::InstantiateEnumDefinition( | 
|  | 729 | EnumDecl *Enum, EnumDecl *Pattern) { | 
|  | 730 | Enum->startDefinition(); | 
|  | 731 |  | 
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 732 | // Update the location to refer to the definition. | 
|  | 733 | Enum->setLocation(Pattern->getLocation()); | 
|  | 734 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 735 | SmallVector<Decl*, 4> Enumerators; | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 736 |  | 
|  | 737 | EnumConstantDecl *LastEnumConst = 0; | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 738 | for (EnumDecl::enumerator_iterator EC = Pattern->enumerator_begin(), | 
|  | 739 | ECEnd = Pattern->enumerator_end(); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 740 | EC != ECEnd; ++EC) { | 
|  | 741 | // The specified value for the enumerator. | 
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 742 | ExprResult Value = SemaRef.Owned((Expr *)0); | 
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 743 | if (Expr *UninstValue = EC->getInitExpr()) { | 
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 744 | // The enumerator's value expression is a constant expression. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | EnterExpressionEvaluationContext Unevaluated(SemaRef, | 
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 746 | Sema::ConstantEvaluated); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 |  | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 748 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); | 
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 749 | } | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 750 |  | 
|  | 751 | // Drop the initial value and continue. | 
|  | 752 | bool isInvalid = false; | 
|  | 753 | if (Value.isInvalid()) { | 
|  | 754 | Value = SemaRef.Owned((Expr *)0); | 
|  | 755 | isInvalid = true; | 
|  | 756 | } | 
|  | 757 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | EnumConstantDecl *EnumConst | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 759 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, | 
|  | 760 | EC->getLocation(), EC->getIdentifier(), | 
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 761 | Value.get()); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 762 |  | 
|  | 763 | if (isInvalid) { | 
|  | 764 | if (EnumConst) | 
|  | 765 | EnumConst->setInvalidDecl(); | 
|  | 766 | Enum->setInvalidDecl(); | 
|  | 767 | } | 
|  | 768 |  | 
|  | 769 | if (EnumConst) { | 
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 770 | SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst); | 
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 771 |  | 
| John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 772 | EnumConst->setAccess(Enum->getAccess()); | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 773 | Enum->addDecl(EnumConst); | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | Enumerators.push_back(EnumConst); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 775 | LastEnumConst = EnumConst; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 776 |  | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 777 | if (Pattern->getDeclContext()->isFunctionOrMethod() && | 
|  | 778 | !Enum->isScoped()) { | 
| Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 779 | // If the enumeration is within a function or method, record the enum | 
|  | 780 | // constant as a local. | 
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 781 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst); | 
| Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 782 | } | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 783 | } | 
|  | 784 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 |  | 
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 786 | // FIXME: Fixup LBraceLoc | 
|  | 787 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), | 
|  | 788 | Enum->getRBraceLoc(), Enum, | 
| Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 789 | Enumerators, | 
| Edward O'Callaghan | c69169d | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 790 | 0, 0); | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 791 | } | 
|  | 792 |  | 
| Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 793 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 794 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); | 
| Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 795 | } | 
|  | 796 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 797 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 798 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | 
|  | 799 |  | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 800 | // Create a local instantiation scope for this class template, which | 
|  | 801 | // will contain the instantiations of the template parameters. | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 802 | LocalInstantiationScope Scope(SemaRef); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 803 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 804 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | if (!InstParams) | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 806 | return NULL; | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 807 |  | 
|  | 808 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 809 |  | 
|  | 810 | // Instantiate the qualifier.  We have to do this first in case | 
|  | 811 | // we're a friend declaration, because if we are then we need to put | 
|  | 812 | // the new declaration in the appropriate context. | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 813 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); | 
|  | 814 | if (QualifierLoc) { | 
|  | 815 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | 
|  | 816 | TemplateArgs); | 
|  | 817 | if (!QualifierLoc) | 
|  | 818 | return 0; | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 819 | } | 
|  | 820 |  | 
|  | 821 | CXXRecordDecl *PrevDecl = 0; | 
|  | 822 | ClassTemplateDecl *PrevClassTemplate = 0; | 
|  | 823 |  | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 824 | if (!isFriend && Pattern->getPreviousDecl()) { | 
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 825 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | 
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 826 | if (!Found.empty()) { | 
|  | 827 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); | 
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 828 | if (PrevClassTemplate) | 
|  | 829 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); | 
|  | 830 | } | 
|  | 831 | } | 
|  | 832 |  | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 833 | // If this isn't a friend, then it's a member template, in which | 
|  | 834 | // case we just want to build the instantiation in the | 
|  | 835 | // specialization.  If it is a friend, we want to build it in | 
|  | 836 | // the appropriate context. | 
|  | 837 | DeclContext *DC = Owner; | 
|  | 838 | if (isFriend) { | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 839 | if (QualifierLoc) { | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 840 | CXXScopeSpec SS; | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 841 | SS.Adopt(QualifierLoc); | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 842 | DC = SemaRef.computeDeclContext(SS); | 
|  | 843 | if (!DC) return 0; | 
|  | 844 | } else { | 
|  | 845 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), | 
|  | 846 | Pattern->getDeclContext(), | 
|  | 847 | TemplateArgs); | 
|  | 848 | } | 
|  | 849 |  | 
|  | 850 | // Look for a previous declaration of the template in the owning | 
|  | 851 | // context. | 
|  | 852 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), | 
|  | 853 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); | 
|  | 854 | SemaRef.LookupQualifiedName(R, DC); | 
|  | 855 |  | 
|  | 856 | if (R.isSingleResult()) { | 
|  | 857 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); | 
|  | 858 | if (PrevClassTemplate) | 
|  | 859 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); | 
|  | 860 | } | 
|  | 861 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 862 | if (!PrevClassTemplate && QualifierLoc) { | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 863 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) | 
| Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 864 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 865 | << QualifierLoc.getSourceRange(); | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 866 | return 0; | 
|  | 867 | } | 
|  | 868 |  | 
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 869 | bool AdoptedPreviousTemplateParams = false; | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 870 | if (PrevClassTemplate) { | 
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 871 | bool Complain = true; | 
|  | 872 |  | 
|  | 873 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class | 
|  | 874 | // template for struct std::tr1::__detail::_Map_base, where the | 
|  | 875 | // template parameters of the friend declaration don't match the | 
|  | 876 | // template parameters of the original declaration. In this one | 
|  | 877 | // case, we don't complain about the ill-formed friend | 
|  | 878 | // declaration. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 879 | if (isFriend && Pattern->getIdentifier() && | 
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 880 | Pattern->getIdentifier()->isStr("_Map_base") && | 
|  | 881 | DC->isNamespace() && | 
|  | 882 | cast<NamespaceDecl>(DC)->getIdentifier() && | 
|  | 883 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { | 
|  | 884 | DeclContext *DCParent = DC->getParent(); | 
|  | 885 | if (DCParent->isNamespace() && | 
|  | 886 | cast<NamespaceDecl>(DCParent)->getIdentifier() && | 
|  | 887 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { | 
|  | 888 | DeclContext *DCParent2 = DCParent->getParent(); | 
|  | 889 | if (DCParent2->isNamespace() && | 
|  | 890 | cast<NamespaceDecl>(DCParent2)->getIdentifier() && | 
|  | 891 | cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") && | 
|  | 892 | DCParent2->getParent()->isTranslationUnit()) | 
|  | 893 | Complain = false; | 
|  | 894 | } | 
|  | 895 | } | 
|  | 896 |  | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 897 | TemplateParameterList *PrevParams | 
|  | 898 | = PrevClassTemplate->getTemplateParameters(); | 
|  | 899 |  | 
|  | 900 | // Make sure the parameter lists match. | 
|  | 901 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 902 | Complain, | 
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 903 | Sema::TPL_TemplateMatch)) { | 
|  | 904 | if (Complain) | 
|  | 905 | return 0; | 
|  | 906 |  | 
|  | 907 | AdoptedPreviousTemplateParams = true; | 
|  | 908 | InstParams = PrevParams; | 
|  | 909 | } | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 910 |  | 
|  | 911 | // Do some additional validation, then merge default arguments | 
|  | 912 | // from the existing declarations. | 
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 913 | if (!AdoptedPreviousTemplateParams && | 
|  | 914 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 915 | Sema::TPC_ClassTemplate)) | 
|  | 916 | return 0; | 
|  | 917 | } | 
|  | 918 | } | 
|  | 919 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 920 | CXXRecordDecl *RecordInst | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 921 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC, | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 922 | Pattern->getLocStart(), Pattern->getLocation(), | 
|  | 923 | Pattern->getIdentifier(), PrevDecl, | 
| Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 924 | /*DelayTypeCreation=*/true); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 925 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 926 | if (QualifierLoc) | 
|  | 927 | RecordInst->setQualifierInfo(QualifierLoc); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 928 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 929 | ClassTemplateDecl *Inst | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 930 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), | 
|  | 931 | D->getIdentifier(), InstParams, RecordInst, | 
|  | 932 | PrevClassTemplate); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 933 | RecordInst->setDescribedClassTemplate(Inst); | 
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 934 |  | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 935 | if (isFriend) { | 
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 936 | if (PrevClassTemplate) | 
|  | 937 | Inst->setAccess(PrevClassTemplate->getAccess()); | 
|  | 938 | else | 
|  | 939 | Inst->setAccess(D->getAccess()); | 
|  | 940 |  | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 941 | Inst->setObjectOfFriendDecl(); | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 942 | // TODO: do we want to track the instantiation progeny of this | 
|  | 943 | // friend target decl? | 
|  | 944 | } else { | 
| Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 945 | Inst->setAccess(D->getAccess()); | 
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 946 | if (!PrevClassTemplate) | 
|  | 947 | Inst->setInstantiatedFromMemberTemplate(D); | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 948 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 949 |  | 
| Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 950 | // Trigger creation of the type for the instantiation. | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 951 | SemaRef.Context.getInjectedClassNameType(RecordInst, | 
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 952 | Inst->getInjectedClassNameSpecialization()); | 
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 953 |  | 
| Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 954 | // Finish handling of friends. | 
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 955 | if (isFriend) { | 
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 956 | DC->makeDeclVisibleInContext(Inst); | 
| Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 957 | Inst->setLexicalDeclContext(Owner); | 
|  | 958 | RecordInst->setLexicalDeclContext(Owner); | 
| Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 959 | return Inst; | 
| Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 960 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 961 |  | 
| Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 962 | if (D->isOutOfLine()) { | 
|  | 963 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 964 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 965 | } | 
|  | 966 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 967 | Owner->addDecl(Inst); | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 968 |  | 
|  | 969 | if (!PrevClassTemplate) { | 
|  | 970 | // Queue up any out-of-line partial specializations of this member | 
|  | 971 | // class template; the client will force their instantiation once | 
|  | 972 | // the enclosing class has been instantiated. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 973 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 974 | D->getPartialSpecializations(PartialSpecs); | 
|  | 975 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) | 
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 976 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 977 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); | 
|  | 978 | } | 
|  | 979 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 980 | return Inst; | 
|  | 981 | } | 
|  | 982 |  | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 983 | Decl * | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 984 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( | 
|  | 985 | ClassTemplatePartialSpecializationDecl *D) { | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 986 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 987 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 988 | // Lookup the already-instantiated declaration in the instantiation | 
|  | 989 | // of the class template and return that. | 
|  | 990 | DeclContext::lookup_result Found | 
|  | 991 | = Owner->lookup(ClassTemplate->getDeclName()); | 
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 992 | if (Found.empty()) | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 993 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 994 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 995 | ClassTemplateDecl *InstClassTemplate | 
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 996 | = dyn_cast<ClassTemplateDecl>(Found.front()); | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 997 | if (!InstClassTemplate) | 
|  | 998 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 999 |  | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1000 | if (ClassTemplatePartialSpecializationDecl *Result | 
|  | 1001 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) | 
|  | 1002 | return Result; | 
|  | 1003 |  | 
|  | 1004 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1005 | } | 
|  | 1006 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1007 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { | 
|  | 1008 | assert(D->getTemplatedDecl()->isStaticDataMember() && | 
|  | 1009 | "Only static data member templates are allowed."); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1010 |  | 
|  | 1011 | // Create a local instantiation scope for this variable template, which | 
|  | 1012 | // will contain the instantiations of the template parameters. | 
|  | 1013 | LocalInstantiationScope Scope(SemaRef); | 
|  | 1014 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
|  | 1015 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 1016 | if (!InstParams) | 
|  | 1017 | return NULL; | 
|  | 1018 |  | 
|  | 1019 | VarDecl *Pattern = D->getTemplatedDecl(); | 
|  | 1020 | VarTemplateDecl *PrevVarTemplate = 0; | 
|  | 1021 |  | 
|  | 1022 | if (Pattern->getPreviousDecl()) { | 
|  | 1023 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); | 
|  | 1024 | if (!Found.empty()) | 
|  | 1025 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); | 
|  | 1026 | } | 
|  | 1027 |  | 
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1028 | VarDecl *VarInst = | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1029 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, | 
|  | 1030 | /*InstantiatingVarTemplate=*/true)); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1031 |  | 
|  | 1032 | DeclContext *DC = Owner; | 
|  | 1033 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1034 | VarTemplateDecl *Inst = VarTemplateDecl::Create( | 
|  | 1035 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, | 
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1036 | VarInst); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1037 | VarInst->setDescribedVarTemplate(Inst); | 
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1038 | Inst->setPreviousDecl(PrevVarTemplate); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1039 |  | 
|  | 1040 | Inst->setAccess(D->getAccess()); | 
|  | 1041 | if (!PrevVarTemplate) | 
|  | 1042 | Inst->setInstantiatedFromMemberTemplate(D); | 
|  | 1043 |  | 
|  | 1044 | if (D->isOutOfLine()) { | 
|  | 1045 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 1046 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | Owner->addDecl(Inst); | 
|  | 1050 |  | 
|  | 1051 | if (!PrevVarTemplate) { | 
|  | 1052 | // Queue up any out-of-line partial specializations of this member | 
|  | 1053 | // variable template; the client will force their instantiation once | 
|  | 1054 | // the enclosing class has been instantiated. | 
|  | 1055 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; | 
|  | 1056 | D->getPartialSpecializations(PartialSpecs); | 
|  | 1057 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) | 
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1058 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1059 | OutOfLineVarPartialSpecs.push_back( | 
|  | 1060 | std::make_pair(Inst, PartialSpecs[I])); | 
|  | 1061 | } | 
|  | 1062 |  | 
|  | 1063 | return Inst; | 
|  | 1064 | } | 
|  | 1065 |  | 
|  | 1066 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( | 
|  | 1067 | VarTemplatePartialSpecializationDecl *D) { | 
|  | 1068 | assert(D->isStaticDataMember() && | 
|  | 1069 | "Only static data member templates are allowed."); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1070 |  | 
|  | 1071 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); | 
|  | 1072 |  | 
|  | 1073 | // Lookup the already-instantiated declaration and return that. | 
|  | 1074 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); | 
|  | 1075 | assert(!Found.empty() && "Instantiation found nothing?"); | 
|  | 1076 |  | 
|  | 1077 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); | 
|  | 1078 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); | 
|  | 1079 |  | 
|  | 1080 | if (VarTemplatePartialSpecializationDecl *Result = | 
|  | 1081 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) | 
|  | 1082 | return Result; | 
|  | 1083 |  | 
|  | 1084 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); | 
|  | 1085 | } | 
|  | 1086 |  | 
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1087 | Decl * | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1088 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1089 | // Create a local instantiation scope for this function template, which | 
|  | 1090 | // will contain the instantiations of the template parameters and then get | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1091 | // merged with the local instantiation scope for the function template | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1092 | // itself. | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1093 | LocalInstantiationScope Scope(SemaRef); | 
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1094 |  | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1095 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
|  | 1096 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | if (!InstParams) | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1098 | return NULL; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1099 |  | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1100 | FunctionDecl *Instantiated = 0; | 
|  | 1101 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1102 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1103 | InstParams)); | 
|  | 1104 | else | 
|  | 1105 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1106 | D->getTemplatedDecl(), | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1107 | InstParams)); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1108 |  | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1109 | if (!Instantiated) | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1110 | return 0; | 
|  | 1111 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | // Link the instantiated function template declaration to the function | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1113 | // template from which it was instantiated. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1114 | FunctionTemplateDecl *InstTemplate | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1115 | = Instantiated->getDescribedFunctionTemplate(); | 
| Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1116 | InstTemplate->setAccess(D->getAccess()); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1117 | assert(InstTemplate && | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1118 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); | 
| John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1119 |  | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1120 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); | 
|  | 1121 |  | 
| John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1122 | // Link the instantiation back to the pattern *unless* this is a | 
|  | 1123 | // non-definition friend declaration. | 
|  | 1124 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1125 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1126 | InstTemplate->setInstantiatedFromMemberTemplate(D); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1127 |  | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1128 | // Make declarations visible in the appropriate context. | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1129 | if (!isFriend) { | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1130 | Owner->addDecl(InstTemplate); | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1131 | } else if (InstTemplate->getDeclContext()->isRecord() && | 
|  | 1132 | !D->getPreviousDecl()) { | 
|  | 1133 | SemaRef.CheckFriendAccess(InstTemplate); | 
|  | 1134 | } | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1135 |  | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1136 | return InstTemplate; | 
|  | 1137 | } | 
|  | 1138 |  | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1139 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { | 
|  | 1140 | CXXRecordDecl *PrevDecl = 0; | 
|  | 1141 | if (D->isInjectedClassName()) | 
|  | 1142 | PrevDecl = cast<CXXRecordDecl>(Owner); | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1143 | else if (D->getPreviousDecl()) { | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1144 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1145 | D->getPreviousDecl(), | 
| John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1146 | TemplateArgs); | 
|  | 1147 | if (!Prev) return 0; | 
|  | 1148 | PrevDecl = cast<CXXRecordDecl>(Prev); | 
|  | 1149 | } | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1150 |  | 
|  | 1151 | CXXRecordDecl *Record | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1153 | D->getLocStart(), D->getLocation(), | 
|  | 1154 | D->getIdentifier(), PrevDecl); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1155 |  | 
|  | 1156 | // Substitute the nested name specifier, if any. | 
|  | 1157 | if (SubstQualifier(D, Record)) | 
|  | 1158 | return 0; | 
|  | 1159 |  | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1160 | Record->setImplicit(D->isImplicit()); | 
| Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1161 | // FIXME: Check against AS_none is an ugly hack to work around the issue that | 
|  | 1162 | // the tag decls introduced by friend class declarations don't have an access | 
|  | 1163 | // specifier. Remove once this area of the code gets sorted out. | 
|  | 1164 | if (D->getAccess() != AS_none) | 
|  | 1165 | Record->setAccess(D->getAccess()); | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1166 | if (!D->isInjectedClassName()) | 
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1167 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1168 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1169 | // If the original function was part of a friend declaration, | 
|  | 1170 | // inherit its namespace state. | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1171 | if (D->getFriendObjectKind()) | 
|  | 1172 | Record->setObjectOfFriendDecl(); | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1173 |  | 
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1174 | // Make sure that anonymous structs and unions are recorded. | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1175 | if (D->isAnonymousStructOrUnion()) | 
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1176 | Record->setAnonymousStructOrUnion(true); | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1177 |  | 
|  | 1178 | if (D->isLocalClass()) | 
|  | 1179 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); | 
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1180 |  | 
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1181 | // Forward the mangling number from the template to the instantiated decl. | 
|  | 1182 | SemaRef.Context.setManglingNumber(Record, | 
|  | 1183 | SemaRef.Context.getManglingNumber(D)); | 
|  | 1184 |  | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1185 | Owner->addDecl(Record); | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1186 |  | 
|  | 1187 | // DR1484 clarifies that the members of a local class are instantiated as part | 
|  | 1188 | // of the instantiation of their enclosing entity. | 
|  | 1189 | if (D->isCompleteDefinition() && D->isLocalClass()) { | 
| David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1190 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, | 
|  | 1191 | TSK_ImplicitInstantiation, | 
|  | 1192 | /*Complain=*/true); | 
|  | 1193 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, | 
|  | 1194 | TSK_ImplicitInstantiation); | 
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1195 | } | 
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1196 | return Record; | 
|  | 1197 | } | 
|  | 1198 |  | 
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1199 | /// \brief Adjust the given function type for an instantiation of the | 
|  | 1200 | /// given declaration, to cope with modifications to the function's type that | 
|  | 1201 | /// aren't reflected in the type-source information. | 
|  | 1202 | /// | 
|  | 1203 | /// \param D The declaration we're instantiating. | 
|  | 1204 | /// \param TInfo The already-instantiated type. | 
|  | 1205 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, | 
|  | 1206 | FunctionDecl *D, | 
|  | 1207 | TypeSourceInfo *TInfo) { | 
| Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1208 | const FunctionProtoType *OrigFunc | 
|  | 1209 | = D->getType()->castAs<FunctionProtoType>(); | 
|  | 1210 | const FunctionProtoType *NewFunc | 
|  | 1211 | = TInfo->getType()->castAs<FunctionProtoType>(); | 
|  | 1212 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) | 
|  | 1213 | return TInfo->getType(); | 
|  | 1214 |  | 
|  | 1215 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); | 
|  | 1216 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1217 | return Context.getFunctionType(NewFunc->getReturnType(), | 
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1218 | NewFunc->getParamTypes(), NewEPI); | 
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1219 | } | 
|  | 1220 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1221 | /// Normal class members are of more specific types and therefore | 
|  | 1222 | /// don't make it here.  This function serves two purposes: | 
|  | 1223 | ///   1) instantiating function templates | 
|  | 1224 | ///   2) substituting friend declarations | 
| Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1225 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1226 | TemplateParameterList *TemplateParams) { | 
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1227 | // Check whether there is already a function template specialization for | 
|  | 1228 | // this declaration. | 
|  | 1229 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1230 | if (FunctionTemplate && !TemplateParams) { | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1231 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 |  | 
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1233 | void *InsertPos = 0; | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1234 | FunctionDecl *SpecFunc | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1235 | = FunctionTemplate->findSpecialization(Innermost.begin(), Innermost.size(), | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1236 | InsertPos); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1237 |  | 
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1238 | // If we already have a function template specialization, return it. | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1239 | if (SpecFunc) | 
|  | 1240 | return SpecFunc; | 
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1241 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 |  | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1243 | bool isFriend; | 
|  | 1244 | if (FunctionTemplate) | 
|  | 1245 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); | 
|  | 1246 | else | 
|  | 1247 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | 
|  | 1248 |  | 
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1249 | bool MergeWithParentScope = (TemplateParams != 0) || | 
| Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1250 | Owner->isFunctionOrMethod() || | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1251 | !(isa<Decl>(Owner) && | 
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1252 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1253 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1255 | SmallVector<ParmVarDecl *, 4> Params; | 
| David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1256 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1257 | if (!TInfo) | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1258 | return 0; | 
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1259 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); | 
| John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1260 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1261 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); | 
|  | 1262 | if (QualifierLoc) { | 
|  | 1263 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | 
|  | 1264 | TemplateArgs); | 
|  | 1265 | if (!QualifierLoc) | 
|  | 1266 | return 0; | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1267 | } | 
|  | 1268 |  | 
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1269 | // If we're instantiating a local function declaration, put the result | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1270 | // in the enclosing namespace; otherwise we need to find the instantiated | 
|  | 1271 | // context. | 
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1272 | DeclContext *DC; | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1273 | if (D->isLocalExternDecl()) { | 
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1274 | DC = Owner; | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1275 | SemaRef.adjustContextForLocalExternDecl(DC); | 
|  | 1276 | } else if (isFriend && QualifierLoc) { | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1277 | CXXScopeSpec SS; | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1278 | SS.Adopt(QualifierLoc); | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1279 | DC = SemaRef.computeDeclContext(SS); | 
|  | 1280 | if (!DC) return 0; | 
|  | 1281 | } else { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1282 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1283 | TemplateArgs); | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1284 | } | 
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1285 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1286 | FunctionDecl *Function = | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1287 | FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), | 
| Abramo Bagnara | 0d4fce1 | 2012-10-04 21:40:42 +0000 | [diff] [blame] | 1288 | D->getNameInfo(), T, TInfo, | 
| Rafael Espindola | 9dd86de | 2013-04-16 02:29:15 +0000 | [diff] [blame] | 1289 | D->getCanonicalDecl()->getStorageClass(), | 
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1290 | D->isInlineSpecified(), D->hasWrittenPrototype(), | 
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1291 | D->isConstexpr()); | 
| Enea Zaffanella | 25723ce | 2013-07-19 18:02:36 +0000 | [diff] [blame] | 1292 | Function->setRangeEnd(D->getSourceRange().getEnd()); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1293 |  | 
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1294 | if (D->isInlined()) | 
|  | 1295 | Function->setImplicitlyInline(); | 
|  | 1296 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1297 | if (QualifierLoc) | 
|  | 1298 | Function->setQualifierInfo(QualifierLoc); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1299 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1300 | if (D->isLocalExternDecl()) | 
|  | 1301 | Function->setLocalExternDecl(); | 
|  | 1302 |  | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1303 | DeclContext *LexicalDC = Owner; | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1304 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1305 | assert(D->getDeclContext()->isFileContext()); | 
|  | 1306 | LexicalDC = D->getDeclContext(); | 
|  | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | Function->setLexicalDeclContext(LexicalDC); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 |  | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1311 | // Attach the parameters | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1312 | for (unsigned P = 0; P < Params.size(); ++P) | 
|  | 1313 | if (Params[P]) | 
|  | 1314 | Params[P]->setOwningFunction(Function); | 
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1315 | Function->setParams(Params); | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1316 |  | 
| Douglas Gregor | 1cd6ea0 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1317 | SourceLocation InstantiateAtPOI; | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1318 | if (TemplateParams) { | 
|  | 1319 | // Our resulting instantiation is actually a function template, since we | 
|  | 1320 | // are substituting only the outer template parameters. For example, given | 
|  | 1321 | // | 
|  | 1322 | //   template<typename T> | 
|  | 1323 | //   struct X { | 
|  | 1324 | //     template<typename U> friend void f(T, U); | 
|  | 1325 | //   }; | 
|  | 1326 | // | 
|  | 1327 | //   X<int> x; | 
|  | 1328 | // | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1329 | // We are instantiating the friend function template "f" within X<int>, | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1330 | // which means substituting int for T, but leaving "f" as a friend function | 
|  | 1331 | // template. | 
|  | 1332 | // Build the function template itself. | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1333 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1334 | Function->getLocation(), | 
|  | 1335 | Function->getDeclName(), | 
|  | 1336 | TemplateParams, Function); | 
|  | 1337 | Function->setDescribedFunctionTemplate(FunctionTemplate); | 
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1338 |  | 
|  | 1339 | FunctionTemplate->setLexicalDeclContext(LexicalDC); | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1340 |  | 
|  | 1341 | if (isFriend && D->isThisDeclarationADefinition()) { | 
|  | 1342 | // TODO: should we remember this connection regardless of whether | 
|  | 1343 | // the friend declaration provided a body? | 
|  | 1344 | FunctionTemplate->setInstantiatedFromMemberTemplate( | 
|  | 1345 | D->getDescribedFunctionTemplate()); | 
|  | 1346 | } | 
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1347 | } else if (FunctionTemplate) { | 
|  | 1348 | // Record this function template specialization. | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1349 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | 
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1350 | Function->setFunctionTemplateSpecialization(FunctionTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1351 | TemplateArgumentList::CreateCopy(SemaRef.Context, | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1352 | Innermost.begin(), | 
|  | 1353 | Innermost.size()), | 
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1354 | /*InsertPos=*/0); | 
| Chandler Carruth | 48b2831 | 2011-08-18 09:09:59 +0000 | [diff] [blame] | 1355 | } else if (isFriend) { | 
|  | 1356 | // Note, we need this connection even if the friend doesn't have a body. | 
|  | 1357 | // Its body may exist but not have been attached yet due to deferred | 
|  | 1358 | // parsing. | 
|  | 1359 | // FIXME: It might be cleaner to set this when attaching the body to the | 
|  | 1360 | // friend function declaration, however that would require finding all the | 
|  | 1361 | // instantiations and modifying them. | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1362 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1363 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1364 |  | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1365 | if (InitFunctionInstantiation(Function, D)) | 
|  | 1366 | Function->setInvalidDecl(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 |  | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1368 | bool isExplicitSpecialization = false; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1369 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1370 | LookupResult Previous( | 
|  | 1371 | SemaRef, Function->getDeclName(), SourceLocation(), | 
|  | 1372 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage | 
|  | 1373 | : Sema::LookupOrdinaryName, | 
|  | 1374 | Sema::ForRedeclaration); | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1375 |  | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1376 | if (DependentFunctionTemplateSpecializationInfo *Info | 
|  | 1377 | = D->getDependentSpecializationInfo()) { | 
|  | 1378 | assert(isFriend && "non-friend has dependent specialization info?"); | 
|  | 1379 |  | 
|  | 1380 | // This needs to be set now for future sanity. | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1381 | Function->setObjectOfFriendDecl(); | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1382 |  | 
|  | 1383 | // Instantiate the explicit template arguments. | 
|  | 1384 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), | 
|  | 1385 | Info->getRAngleLoc()); | 
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1386 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), | 
|  | 1387 | ExplicitArgs, TemplateArgs)) | 
|  | 1388 | return 0; | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1389 |  | 
|  | 1390 | // Map the candidate templates to their instantiations. | 
|  | 1391 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { | 
|  | 1392 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), | 
|  | 1393 | Info->getTemplate(I), | 
|  | 1394 | TemplateArgs); | 
|  | 1395 | if (!Temp) return 0; | 
|  | 1396 |  | 
|  | 1397 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, | 
|  | 1401 | &ExplicitArgs, | 
|  | 1402 | Previous)) | 
|  | 1403 | Function->setInvalidDecl(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1404 |  | 
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1405 | isExplicitSpecialization = true; | 
|  | 1406 |  | 
|  | 1407 | } else if (TemplateParams || !FunctionTemplate) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1408 | // Look only into the namespace where the friend would be declared to | 
|  | 1409 | // find a previous declaration. This is the innermost enclosing namespace, | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1410 | // as described in ActOnFriendFunctionDecl. | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1411 | SemaRef.LookupQualifiedName(Previous, DC); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1412 |  | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1413 | // In C++, the previous declaration we find might be a tag type | 
|  | 1414 | // (class or enum). In this case, the new declaration will hide the | 
|  | 1415 | // tag type. Note that this does does not apply if we're declaring a | 
|  | 1416 | // typedef (C++ [dcl.typedef]p4). | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1417 | if (Previous.isSingleTagDecl()) | 
|  | 1418 | Previous.clear(); | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1419 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1420 |  | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1421 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, | 
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1422 | isExplicitSpecialization); | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1423 |  | 
| John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1424 | NamedDecl *PrincipalDecl = (TemplateParams | 
|  | 1425 | ? cast<NamedDecl>(FunctionTemplate) | 
|  | 1426 | : Function); | 
|  | 1427 |  | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1428 | // If the original function was part of a friend declaration, | 
|  | 1429 | // inherit its namespace state and add it to the owner. | 
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1430 | if (isFriend) { | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1431 | PrincipalDecl->setObjectOfFriendDecl(); | 
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1432 | DC->makeDeclVisibleInContext(PrincipalDecl); | 
| Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1433 |  | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1434 | bool QueuedInstantiation = false; | 
| Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1435 |  | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1436 | // C++11 [temp.friend]p4 (DR329): | 
|  | 1437 | //   When a function is defined in a friend function declaration in a class | 
|  | 1438 | //   template, the function is instantiated when the function is odr-used. | 
|  | 1439 | //   The same restrictions on multiple declarations and definitions that | 
|  | 1440 | //   apply to non-template function declarations and definitions also apply | 
|  | 1441 | //   to these implicit definitions. | 
|  | 1442 | if (D->isThisDeclarationADefinition()) { | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1443 | // Check for a function body. | 
|  | 1444 | const FunctionDecl *Definition = 0; | 
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1445 | if (Function->isDefined(Definition) && | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1446 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1447 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) | 
|  | 1448 | << Function->getDeclName(); | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1449 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1450 | } | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1451 | // Check for redefinitions due to other instantiations of this or | 
|  | 1452 | // a similar friend function. | 
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1453 | else for (auto R : Function->redecls()) { | 
|  | 1454 | if (R == Function) | 
| Gabor Greif | 122f1eb | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1455 | continue; | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1456 |  | 
|  | 1457 | // If some prior declaration of this function has been used, we need | 
|  | 1458 | // to instantiate its definition. | 
|  | 1459 | if (!QueuedInstantiation && R->isUsed(false)) { | 
|  | 1460 | if (MemberSpecializationInfo *MSInfo = | 
|  | 1461 | Function->getMemberSpecializationInfo()) { | 
|  | 1462 | if (MSInfo->getPointOfInstantiation().isInvalid()) { | 
|  | 1463 | SourceLocation Loc = R->getLocation(); // FIXME | 
|  | 1464 | MSInfo->setPointOfInstantiation(Loc); | 
|  | 1465 | SemaRef.PendingLocalImplicitInstantiations.push_back( | 
|  | 1466 | std::make_pair(Function, Loc)); | 
|  | 1467 | QueuedInstantiation = true; | 
| Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1468 | } | 
|  | 1469 | } | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1470 | } | 
|  | 1471 |  | 
|  | 1472 | // If some prior declaration of this function was a friend with an | 
|  | 1473 | // uninstantiated definition, reject it. | 
|  | 1474 | if (R->getFriendObjectKind()) { | 
|  | 1475 | if (const FunctionDecl *RPattern = | 
|  | 1476 | R->getTemplateInstantiationPattern()) { | 
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1477 | if (RPattern->isDefined(RPattern)) { | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1478 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1479 | << Function->getDeclName(); | 
| Gabor Greif | ae849e4 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1480 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1481 | break; | 
|  | 1482 | } | 
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1483 | } | 
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1484 | } | 
|  | 1485 | } | 
|  | 1486 | } | 
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1487 | } | 
|  | 1488 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1489 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) | 
|  | 1490 | DC->makeDeclVisibleInContext(PrincipalDecl); | 
|  | 1491 |  | 
| John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1492 | if (Function->isOverloadedOperator() && !DC->isRecord() && | 
|  | 1493 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) | 
|  | 1494 | PrincipalDecl->setNonMemberOperator(); | 
|  | 1495 |  | 
| Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1496 | assert(!D->isDefaulted() && "only methods should be defaulted"); | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1497 | return Function; | 
|  | 1498 | } | 
|  | 1499 |  | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1500 | Decl * | 
|  | 1501 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1502 | TemplateParameterList *TemplateParams, | 
|  | 1503 | bool IsClassScopeSpecialization) { | 
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1504 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1505 | if (FunctionTemplate && !TemplateParams) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | // We are creating a function template specialization from a function | 
|  | 1507 | // template. Check whether there is already a function template | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1508 | // specialization for this particular set of template arguments. | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1509 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 |  | 
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1511 | void *InsertPos = 0; | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1512 | FunctionDecl *SpecFunc | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1513 | = FunctionTemplate->findSpecialization(Innermost.begin(), | 
|  | 1514 | Innermost.size(), | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1515 | InsertPos); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 |  | 
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1517 | // If we already have a function template specialization, return it. | 
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1518 | if (SpecFunc) | 
|  | 1519 | return SpecFunc; | 
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1520 | } | 
|  | 1521 |  | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1522 | bool isFriend; | 
|  | 1523 | if (FunctionTemplate) | 
|  | 1524 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); | 
|  | 1525 | else | 
|  | 1526 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); | 
|  | 1527 |  | 
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1528 | bool MergeWithParentScope = (TemplateParams != 0) || | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1529 | !(isa<Decl>(Owner) && | 
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1530 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1531 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); | 
| Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1532 |  | 
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1533 | // Instantiate enclosing template arguments for friends. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1534 | SmallVector<TemplateParameterList *, 4> TempParamLists; | 
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1535 | unsigned NumTempParamLists = 0; | 
|  | 1536 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { | 
|  | 1537 | TempParamLists.set_size(NumTempParamLists); | 
|  | 1538 | for (unsigned I = 0; I != NumTempParamLists; ++I) { | 
|  | 1539 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); | 
|  | 1540 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 1541 | if (!InstParams) | 
|  | 1542 | return NULL; | 
|  | 1543 | TempParamLists[I] = InstParams; | 
|  | 1544 | } | 
|  | 1545 | } | 
|  | 1546 |  | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1547 | SmallVector<ParmVarDecl *, 4> Params; | 
| Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1548 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1549 | if (!TInfo) | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1550 | return 0; | 
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1551 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1552 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1553 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); | 
|  | 1554 | if (QualifierLoc) { | 
|  | 1555 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1556 | TemplateArgs); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1557 | if (!QualifierLoc) | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1558 | return 0; | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1559 | } | 
|  | 1560 |  | 
|  | 1561 | DeclContext *DC = Owner; | 
|  | 1562 | if (isFriend) { | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1563 | if (QualifierLoc) { | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1564 | CXXScopeSpec SS; | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1565 | SS.Adopt(QualifierLoc); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1566 | DC = SemaRef.computeDeclContext(SS); | 
| John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1567 |  | 
|  | 1568 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) | 
|  | 1569 | return 0; | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1570 | } else { | 
|  | 1571 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), | 
|  | 1572 | D->getDeclContext(), | 
|  | 1573 | TemplateArgs); | 
|  | 1574 | } | 
|  | 1575 | if (!DC) return 0; | 
|  | 1576 | } | 
|  | 1577 |  | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1578 | // Build the instantiated method declaration. | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1579 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1580 | CXXMethodDecl *Method = 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 |  | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1582 | SourceLocation StartLoc = D->getInnerLocStart(); | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1583 | DeclarationNameInfo NameInfo | 
|  | 1584 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); | 
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1585 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1587 | StartLoc, NameInfo, T, TInfo, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | Constructor->isExplicit(), | 
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1589 | Constructor->isInlineSpecified(), | 
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1590 | false, Constructor->isConstexpr()); | 
| Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1591 |  | 
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1592 | // Claim that the instantiation of a constructor or constructor template | 
|  | 1593 | // inherits the same constructor that the template does. | 
| Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1594 | if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>( | 
|  | 1595 | Constructor->getInheritedConstructor())) { | 
|  | 1596 | // If we're instantiating a specialization of a function template, our | 
|  | 1597 | // "inherited constructor" will actually itself be a function template. | 
|  | 1598 | // Instantiate a declaration of it, too. | 
|  | 1599 | if (FunctionTemplate) { | 
|  | 1600 | assert(!TemplateParams && Inh->getDescribedFunctionTemplate() && | 
|  | 1601 | !Inh->getParent()->isDependentContext() && | 
|  | 1602 | "inheriting constructor template in dependent context?"); | 
|  | 1603 | Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(), | 
|  | 1604 | Inh); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 1605 | if (Inst.isInvalid()) | 
| Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1606 | return 0; | 
|  | 1607 | Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext()); | 
|  | 1608 | LocalInstantiationScope LocalScope(SemaRef); | 
|  | 1609 |  | 
|  | 1610 | // Use the same template arguments that we deduced for the inheriting | 
|  | 1611 | // constructor. There's no way they could be deduced differently. | 
|  | 1612 | MultiLevelTemplateArgumentList InheritedArgs; | 
|  | 1613 | InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost()); | 
|  | 1614 | Inh = cast_or_null<CXXConstructorDecl>( | 
|  | 1615 | SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs)); | 
|  | 1616 | if (!Inh) | 
|  | 1617 | return 0; | 
|  | 1618 | } | 
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 1619 | cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh); | 
| Richard Smith | 4c163a0 | 2013-05-17 02:19:35 +0000 | [diff] [blame] | 1620 | } | 
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1621 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { | 
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1622 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1623 | StartLoc, NameInfo, T, TInfo, | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1624 | Destructor->isInlineSpecified(), | 
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1625 | false); | 
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1626 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { | 
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1627 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1628 | StartLoc, NameInfo, T, TInfo, | 
| Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1629 | Conversion->isInlineSpecified(), | 
| Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1630 | Conversion->isExplicit(), | 
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1631 | Conversion->isConstexpr(), | 
| Richard Smith | eb3c10c | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 1632 | Conversion->getLocEnd()); | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1633 | } else { | 
| Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1634 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1635 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1636 | StartLoc, NameInfo, T, TInfo, | 
| Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1637 | SC, D->isInlineSpecified(), | 
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1638 | D->isConstexpr(), D->getLocEnd()); | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1639 | } | 
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1640 |  | 
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1641 | if (D->isInlined()) | 
|  | 1642 | Method->setImplicitlyInline(); | 
|  | 1643 |  | 
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1644 | if (QualifierLoc) | 
|  | 1645 | Method->setQualifierInfo(QualifierLoc); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1646 |  | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1647 | if (TemplateParams) { | 
|  | 1648 | // Our resulting instantiation is actually a function template, since we | 
|  | 1649 | // are substituting only the outer template parameters. For example, given | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | // | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1651 | //   template<typename T> | 
|  | 1652 | //   struct X { | 
|  | 1653 | //     template<typename U> void f(T, U); | 
|  | 1654 | //   }; | 
|  | 1655 | // | 
|  | 1656 | //   X<int> x; | 
|  | 1657 | // | 
|  | 1658 | // We are instantiating the member template "f" within X<int>, which means | 
|  | 1659 | // substituting int for T, but leaving "f" as a member function template. | 
|  | 1660 | // Build the function template itself. | 
|  | 1661 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, | 
|  | 1662 | Method->getLocation(), | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | Method->getDeclName(), | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1664 | TemplateParams, Method); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1665 | if (isFriend) { | 
|  | 1666 | FunctionTemplate->setLexicalDeclContext(Owner); | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1667 | FunctionTemplate->setObjectOfFriendDecl(); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1668 | } else if (D->isOutOfLine()) | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); | 
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1670 | Method->setDescribedFunctionTemplate(FunctionTemplate); | 
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1671 | } else if (FunctionTemplate) { | 
|  | 1672 | // Record this function template specialization. | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1673 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); | 
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1674 | Method->setFunctionTemplateSpecialization(FunctionTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1675 | TemplateArgumentList::CreateCopy(SemaRef.Context, | 
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1676 | Innermost.begin(), | 
|  | 1677 | Innermost.size()), | 
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 1678 | /*InsertPos=*/0); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1679 | } else if (!isFriend) { | 
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1680 | // Record that this is an instantiation of a member function. | 
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1681 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); | 
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1682 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1683 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | // If we are instantiating a member function defined | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1685 | // out-of-line, the instantiation will have the same lexical | 
|  | 1686 | // context (which will be a namespace scope) as the template. | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1687 | if (isFriend) { | 
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1688 | if (NumTempParamLists) | 
|  | 1689 | Method->setTemplateParameterListsInfo(SemaRef.Context, | 
|  | 1690 | NumTempParamLists, | 
|  | 1691 | TempParamLists.data()); | 
|  | 1692 |  | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1693 | Method->setLexicalDeclContext(Owner); | 
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1694 | Method->setObjectOfFriendDecl(); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1695 | } else if (D->isOutOfLine()) | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1696 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 |  | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1698 | // Attach the parameters | 
|  | 1699 | for (unsigned P = 0; P < Params.size(); ++P) | 
|  | 1700 | Params[P]->setOwningFunction(Method); | 
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1701 | Method->setParams(Params); | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1702 |  | 
|  | 1703 | if (InitMethodInstantiation(Method, D)) | 
|  | 1704 | Method->setInvalidDecl(); | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1705 |  | 
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1706 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, | 
|  | 1707 | Sema::ForRedeclaration); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1708 |  | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1709 | if (!FunctionTemplate || TemplateParams || isFriend) { | 
|  | 1710 | SemaRef.LookupQualifiedName(Previous, Record); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 |  | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1712 | // In C++, the previous declaration we find might be a tag type | 
|  | 1713 | // (class or enum). In this case, the new declaration will hide the | 
|  | 1714 | // tag type. Note that this does does not apply if we're declaring a | 
|  | 1715 | // typedef (C++ [dcl.typedef]p4). | 
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1716 | if (Previous.isSingleTagDecl()) | 
|  | 1717 | Previous.clear(); | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1718 | } | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1719 |  | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1720 | if (!IsClassScopeSpecialization) | 
| Kaelyn Uhrain | 4dc695d | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 1721 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false); | 
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1722 |  | 
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1723 | if (D->isPure()) | 
|  | 1724 | SemaRef.CheckPureMethod(Method, SourceRange()); | 
|  | 1725 |  | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1726 | // Propagate access.  For a non-friend declaration, the access is | 
|  | 1727 | // whatever we're propagating from.  For a friend, it should be the | 
|  | 1728 | // previous declaration we just found. | 
|  | 1729 | if (isFriend && Method->getPreviousDecl()) | 
|  | 1730 | Method->setAccess(Method->getPreviousDecl()->getAccess()); | 
|  | 1731 | else | 
|  | 1732 | Method->setAccess(D->getAccess()); | 
|  | 1733 | if (FunctionTemplate) | 
|  | 1734 | FunctionTemplate->setAccess(Method->getAccess()); | 
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1735 |  | 
| Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 1736 | SemaRef.CheckOverrideControl(Method); | 
|  | 1737 |  | 
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1738 | // 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] | 1739 | if (D->isExplicitlyDefaulted()) | 
|  | 1740 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); | 
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1741 | if (D->isDeletedAsWritten()) | 
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1742 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); | 
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 1743 |  | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1744 | // If there's a function template, let our caller handle it. | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1745 | if (FunctionTemplate) { | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1746 | // do nothing | 
|  | 1747 |  | 
|  | 1748 | // Don't hide a (potentially) valid declaration with an invalid one. | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1749 | } else if (Method->isInvalidDecl() && !Previous.empty()) { | 
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1750 | // do nothing | 
|  | 1751 |  | 
|  | 1752 | // Otherwise, check access to friends and make them visible. | 
|  | 1753 | } else if (isFriend) { | 
|  | 1754 | // We only need to re-check access for methods which we didn't | 
|  | 1755 | // manage to match during parsing. | 
|  | 1756 | if (!D->getPreviousDecl()) | 
|  | 1757 | SemaRef.CheckFriendAccess(Method); | 
|  | 1758 |  | 
|  | 1759 | Record->makeDeclVisibleInContext(Method); | 
|  | 1760 |  | 
|  | 1761 | // Otherwise, add the declaration.  We don't need to do this for | 
|  | 1762 | // class-scope specializations because we'll have matched them with | 
|  | 1763 | // the appropriate template. | 
|  | 1764 | } else if (!IsClassScopeSpecialization) { | 
|  | 1765 | Owner->addDecl(Method); | 
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1766 | } | 
| Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1767 |  | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1768 | return Method; | 
|  | 1769 | } | 
|  | 1770 |  | 
| Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1771 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { | 
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1772 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1773 | } | 
|  | 1774 |  | 
| Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1775 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { | 
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1776 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1777 | } | 
|  | 1778 |  | 
| Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1779 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { | 
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1780 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1781 | } | 
|  | 1782 |  | 
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 1783 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { | 
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1784 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, | 
|  | 1785 | /*ExpectParameterPack=*/ false); | 
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1786 | } | 
|  | 1787 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1788 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( | 
|  | 1789 | TemplateTypeParmDecl *D) { | 
|  | 1790 | // TODO: don't always clone when decls are refcounted. | 
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1791 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1793 | TemplateTypeParmDecl *Inst = | 
| Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 1794 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, | 
|  | 1795 | D->getLocStart(), D->getLocation(), | 
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1796 | D->getDepth() - TemplateArgs.getNumLevels(), | 
|  | 1797 | D->getIndex(), D->getIdentifier(), | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1798 | D->wasDeclaredWithTypename(), | 
|  | 1799 | D->isParameterPack()); | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1800 | Inst->setAccess(AS_public); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1801 |  | 
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 1802 | if (D->hasDefaultArgument()) { | 
|  | 1803 | TypeSourceInfo *InstantiatedDefaultArg = | 
|  | 1804 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, | 
|  | 1805 | D->getDefaultArgumentLoc(), D->getDeclName()); | 
|  | 1806 | if (InstantiatedDefaultArg) | 
|  | 1807 | Inst->setDefaultArgument(InstantiatedDefaultArg, false); | 
|  | 1808 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1809 |  | 
|  | 1810 | // Introduce this template parameter's instantiation into the instantiation | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1811 | // scope. | 
|  | 1812 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1813 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1814 | return Inst; | 
|  | 1815 | } | 
|  | 1816 |  | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1817 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( | 
|  | 1818 | NonTypeTemplateParmDecl *D) { | 
|  | 1819 | // Substitute into the type of the non-type template parameter. | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1820 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1821 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; | 
|  | 1822 | SmallVector<QualType, 4> ExpandedParameterPackTypes; | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1823 | bool IsExpandedParameterPack = false; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1824 | TypeSourceInfo *DI; | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1825 | QualType T; | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1826 | bool Invalid = false; | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1827 |  | 
|  | 1828 | if (D->isExpandedParameterPack()) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1829 | // The non-type template parameter pack is an already-expanded pack | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1830 | // expansion of types. Substitute into each of the expanded types. | 
|  | 1831 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); | 
|  | 1832 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); | 
|  | 1833 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { | 
|  | 1834 | TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), | 
|  | 1835 | TemplateArgs, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1836 | D->getLocation(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1837 | D->getDeclName()); | 
|  | 1838 | if (!NewDI) | 
|  | 1839 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1840 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1841 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); | 
|  | 1842 | QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(), | 
|  | 1843 | D->getLocation()); | 
|  | 1844 | if (NewT.isNull()) | 
|  | 1845 | return 0; | 
|  | 1846 | ExpandedParameterPackTypes.push_back(NewT); | 
|  | 1847 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1848 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1849 | IsExpandedParameterPack = true; | 
|  | 1850 | DI = D->getTypeSourceInfo(); | 
|  | 1851 | T = DI->getType(); | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1852 | } else if (D->isPackExpansion()) { | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1853 | // The non-type template parameter pack's type is a pack expansion of types. | 
|  | 1854 | // Determine whether we need to expand this parameter pack into separate | 
|  | 1855 | // types. | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 1856 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1857 | TypeLoc Pattern = Expansion.getPatternLoc(); | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1858 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1859 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1860 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1861 | // Determine whether the set of unexpanded parameter packs can and should | 
|  | 1862 | // be expanded. | 
|  | 1863 | bool Expand = true; | 
|  | 1864 | bool RetainExpansion = false; | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1865 | Optional<unsigned> OrigNumExpansions | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1866 | = Expansion.getTypePtr()->getNumExpansions(); | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1867 | Optional<unsigned> NumExpansions = OrigNumExpansions; | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1868 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), | 
|  | 1869 | Pattern.getSourceRange(), | 
| David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 1870 | Unexpanded, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1871 | TemplateArgs, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1872 | Expand, RetainExpansion, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1873 | NumExpansions)) | 
|  | 1874 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1875 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1876 | if (Expand) { | 
|  | 1877 | for (unsigned I = 0; I != *NumExpansions; ++I) { | 
|  | 1878 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | 
|  | 1879 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1880 | D->getLocation(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1881 | D->getDeclName()); | 
|  | 1882 | if (!NewDI) | 
|  | 1883 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1884 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1885 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); | 
|  | 1886 | QualType NewT = SemaRef.CheckNonTypeTemplateParameterType( | 
|  | 1887 | NewDI->getType(), | 
|  | 1888 | D->getLocation()); | 
|  | 1889 | if (NewT.isNull()) | 
|  | 1890 | return 0; | 
|  | 1891 | ExpandedParameterPackTypes.push_back(NewT); | 
|  | 1892 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1893 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1894 | // Note that we have an expanded parameter pack. The "type" of this | 
|  | 1895 | // expanded parameter pack is the original expansion type, but callers | 
|  | 1896 | // will end up using the expanded parameter pack types for type-checking. | 
|  | 1897 | IsExpandedParameterPack = true; | 
|  | 1898 | DI = D->getTypeSourceInfo(); | 
|  | 1899 | T = DI->getType(); | 
|  | 1900 | } else { | 
|  | 1901 | // We cannot fully expand the pack expansion now, so substitute into the | 
|  | 1902 | // pattern and create a new pack expansion type. | 
|  | 1903 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | 
|  | 1904 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1905 | D->getLocation(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1906 | D->getDeclName()); | 
|  | 1907 | if (!NewPattern) | 
|  | 1908 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1909 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1910 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), | 
|  | 1911 | NumExpansions); | 
|  | 1912 | if (!DI) | 
|  | 1913 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1914 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1915 | T = DI->getType(); | 
|  | 1916 | } | 
|  | 1917 | } else { | 
|  | 1918 | // Simple case: substitution into a parameter that is not a parameter pack. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1919 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1920 | D->getLocation(), D->getDeclName()); | 
|  | 1921 | if (!DI) | 
|  | 1922 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1923 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1924 | // Check that this type is acceptable for a non-type template parameter. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1925 | T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1926 | D->getLocation()); | 
|  | 1927 | if (T.isNull()) { | 
|  | 1928 | T = SemaRef.Context.IntTy; | 
|  | 1929 | Invalid = true; | 
|  | 1930 | } | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1931 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1932 |  | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1933 | NonTypeTemplateParmDecl *Param; | 
|  | 1934 | if (IsExpandedParameterPack) | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1935 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1936 | D->getInnerLocStart(), | 
|  | 1937 | D->getLocation(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1938 | D->getDepth() - TemplateArgs.getNumLevels(), | 
|  | 1939 | D->getPosition(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1940 | D->getIdentifier(), T, | 
|  | 1941 | DI, | 
|  | 1942 | ExpandedParameterPackTypes.data(), | 
|  | 1943 | ExpandedParameterPackTypes.size(), | 
|  | 1944 | ExpandedParameterPackTypesAsWritten.data()); | 
|  | 1945 | else | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1946 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, | 
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1947 | D->getInnerLocStart(), | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1948 | D->getLocation(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1949 | D->getDepth() - TemplateArgs.getNumLevels(), | 
|  | 1950 | D->getPosition(), | 
|  | 1951 | D->getIdentifier(), T, | 
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1952 | D->isParameterPack(), DI); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1953 |  | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1954 | Param->setAccess(AS_public); | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1955 | if (Invalid) | 
|  | 1956 | Param->setInvalidDecl(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1957 |  | 
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 1958 | if (D->hasDefaultArgument()) { | 
|  | 1959 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); | 
|  | 1960 | if (!Value.isInvalid()) | 
|  | 1961 | Param->setDefaultArgument(Value.get(), false); | 
|  | 1962 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1963 |  | 
|  | 1964 | // Introduce this template parameter's instantiation into the instantiation | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1965 | // scope. | 
|  | 1966 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); | 
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1967 | return Param; | 
|  | 1968 | } | 
|  | 1969 |  | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1970 | static void collectUnexpandedParameterPacks( | 
|  | 1971 | Sema &S, | 
|  | 1972 | TemplateParameterList *Params, | 
|  | 1973 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { | 
|  | 1974 | for (TemplateParameterList::const_iterator I = Params->begin(), | 
|  | 1975 | E = Params->end(); I != E; ++I) { | 
|  | 1976 | if ((*I)->isTemplateParameterPack()) | 
|  | 1977 | continue; | 
|  | 1978 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I)) | 
|  | 1979 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), | 
|  | 1980 | Unexpanded); | 
|  | 1981 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I)) | 
|  | 1982 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), | 
|  | 1983 | Unexpanded); | 
|  | 1984 | } | 
|  | 1985 | } | 
|  | 1986 |  | 
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1987 | Decl * | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1988 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( | 
|  | 1989 | TemplateTemplateParmDecl *D) { | 
|  | 1990 | // Instantiate the template parameter list of the template template parameter. | 
|  | 1991 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
|  | 1992 | TemplateParameterList *InstParams; | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 1993 | SmallVector<TemplateParameterList*, 8> ExpandedParams; | 
|  | 1994 |  | 
|  | 1995 | bool IsExpandedParameterPack = false; | 
|  | 1996 |  | 
|  | 1997 | if (D->isExpandedParameterPack()) { | 
|  | 1998 | // The template template parameter pack is an already-expanded pack | 
|  | 1999 | // expansion of template parameters. Substitute into each of the expanded | 
|  | 2000 | // parameters. | 
|  | 2001 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); | 
|  | 2002 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); | 
|  | 2003 | I != N; ++I) { | 
|  | 2004 | LocalInstantiationScope Scope(SemaRef); | 
|  | 2005 | TemplateParameterList *Expansion = | 
|  | 2006 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); | 
|  | 2007 | if (!Expansion) | 
|  | 2008 | return 0; | 
|  | 2009 | ExpandedParams.push_back(Expansion); | 
|  | 2010 | } | 
|  | 2011 |  | 
|  | 2012 | IsExpandedParameterPack = true; | 
|  | 2013 | InstParams = TempParams; | 
|  | 2014 | } else if (D->isPackExpansion()) { | 
|  | 2015 | // The template template parameter pack expands to a pack of template | 
|  | 2016 | // template parameters. Determine whether we need to expand this parameter | 
|  | 2017 | // pack into separate parameters. | 
|  | 2018 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | 
|  | 2019 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), | 
|  | 2020 | Unexpanded); | 
|  | 2021 |  | 
|  | 2022 | // Determine whether the set of unexpanded parameter packs can and should | 
|  | 2023 | // be expanded. | 
|  | 2024 | bool Expand = true; | 
|  | 2025 | bool RetainExpansion = false; | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2026 | Optional<unsigned> NumExpansions; | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2027 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), | 
|  | 2028 | TempParams->getSourceRange(), | 
|  | 2029 | Unexpanded, | 
|  | 2030 | TemplateArgs, | 
|  | 2031 | Expand, RetainExpansion, | 
|  | 2032 | NumExpansions)) | 
|  | 2033 | return 0; | 
|  | 2034 |  | 
|  | 2035 | if (Expand) { | 
|  | 2036 | for (unsigned I = 0; I != *NumExpansions; ++I) { | 
|  | 2037 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); | 
|  | 2038 | LocalInstantiationScope Scope(SemaRef); | 
|  | 2039 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); | 
|  | 2040 | if (!Expansion) | 
|  | 2041 | return 0; | 
|  | 2042 | ExpandedParams.push_back(Expansion); | 
|  | 2043 | } | 
|  | 2044 |  | 
|  | 2045 | // Note that we have an expanded parameter pack. The "type" of this | 
|  | 2046 | // expanded parameter pack is the original expansion type, but callers | 
|  | 2047 | // will end up using the expanded parameter pack types for type-checking. | 
|  | 2048 | IsExpandedParameterPack = true; | 
|  | 2049 | InstParams = TempParams; | 
|  | 2050 | } else { | 
|  | 2051 | // We cannot fully expand the pack expansion now, so just substitute | 
|  | 2052 | // into the pattern. | 
|  | 2053 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | 
|  | 2054 |  | 
|  | 2055 | LocalInstantiationScope Scope(SemaRef); | 
|  | 2056 | InstParams = SubstTemplateParams(TempParams); | 
|  | 2057 | if (!InstParams) | 
|  | 2058 | return 0; | 
|  | 2059 | } | 
|  | 2060 | } else { | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2061 | // Perform the actual substitution of template parameters within a new, | 
|  | 2062 | // local instantiation scope. | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2063 | LocalInstantiationScope Scope(SemaRef); | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2064 | InstParams = SubstTemplateParams(TempParams); | 
|  | 2065 | if (!InstParams) | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2066 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2067 | } | 
|  | 2068 |  | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2069 | // Build the template template parameter. | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2070 | TemplateTemplateParmDecl *Param; | 
|  | 2071 | if (IsExpandedParameterPack) | 
|  | 2072 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, | 
|  | 2073 | D->getLocation(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2074 | D->getDepth() - TemplateArgs.getNumLevels(), | 
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2075 | D->getPosition(), | 
|  | 2076 | D->getIdentifier(), InstParams, | 
|  | 2077 | ExpandedParams); | 
|  | 2078 | else | 
|  | 2079 | Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, | 
|  | 2080 | D->getLocation(), | 
|  | 2081 | D->getDepth() - TemplateArgs.getNumLevels(), | 
|  | 2082 | D->getPosition(), | 
|  | 2083 | D->isParameterPack(), | 
|  | 2084 | D->getIdentifier(), InstParams); | 
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2085 | if (D->hasDefaultArgument()) { | 
|  | 2086 | NestedNameSpecifierLoc QualifierLoc = | 
|  | 2087 | D->getDefaultArgument().getTemplateQualifierLoc(); | 
|  | 2088 | QualifierLoc = | 
|  | 2089 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); | 
|  | 2090 | TemplateName TName = SemaRef.SubstTemplateName( | 
|  | 2091 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), | 
|  | 2092 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); | 
|  | 2093 | if (!TName.isNull()) | 
|  | 2094 | Param->setDefaultArgument( | 
|  | 2095 | TemplateArgumentLoc(TemplateArgument(TName), | 
|  | 2096 | D->getDefaultArgument().getTemplateQualifierLoc(), | 
|  | 2097 | D->getDefaultArgument().getTemplateNameLoc()), | 
|  | 2098 | false); | 
|  | 2099 | } | 
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2100 | Param->setAccess(AS_public); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2101 |  | 
|  | 2102 | // Introduce this template parameter's instantiation into the instantiation | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2103 | // scope. | 
|  | 2104 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2105 |  | 
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2106 | return Param; | 
|  | 2107 | } | 
|  | 2108 |  | 
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2109 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { | 
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2110 | // Using directives are never dependent (and never contain any types or | 
|  | 2111 | // expressions), so they require no explicit instantiation work. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2112 |  | 
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2113 | UsingDirectiveDecl *Inst | 
|  | 2114 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2115 | D->getNamespaceKeyLocation(), | 
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2116 | D->getQualifierLoc(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2117 | D->getIdentLocation(), | 
|  | 2118 | D->getNominatedNamespace(), | 
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2119 | D->getCommonAncestor()); | 
| Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2120 |  | 
|  | 2121 | // Add the using directive to its declaration context | 
|  | 2122 | // only if this is not a function or method. | 
|  | 2123 | if (!Owner->isFunctionOrMethod()) | 
|  | 2124 | Owner->addDecl(Inst); | 
|  | 2125 |  | 
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2126 | return Inst; | 
|  | 2127 | } | 
|  | 2128 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2129 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { | 
| Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2130 |  | 
|  | 2131 | // The nested name specifier may be dependent, for example | 
|  | 2132 | //     template <typename T> struct t { | 
|  | 2133 | //       struct s1 { T f1(); }; | 
|  | 2134 | //       struct s2 : s1 { using s1::f1; }; | 
|  | 2135 | //     }; | 
|  | 2136 | //     template struct t<int>; | 
|  | 2137 | // Here, in using s1::f1, s1 refers to t<T>::s1; | 
|  | 2138 | // we need to substitute for t<int>::s1. | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2139 | NestedNameSpecifierLoc QualifierLoc | 
|  | 2140 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), | 
|  | 2141 | TemplateArgs); | 
|  | 2142 | if (!QualifierLoc) | 
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 2143 | return 0; | 
| Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2144 |  | 
|  | 2145 | // The name info is non-dependent, so no transformation | 
|  | 2146 | // is required. | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2147 | DeclarationNameInfo NameInfo = D->getNameInfo(); | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2148 |  | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2149 | // We only need to do redeclaration lookups if we're in a class | 
|  | 2150 | // scope (in fact, it's not really even possible in non-class | 
|  | 2151 | // scopes). | 
|  | 2152 | bool CheckRedeclaration = Owner->isRecord(); | 
|  | 2153 |  | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2154 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, | 
|  | 2155 | Sema::ForRedeclaration); | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2156 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2157 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, | 
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2158 | D->getUsingLoc(), | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2159 | QualifierLoc, | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2160 | NameInfo, | 
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2161 | D->hasTypename()); | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2162 |  | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2163 | CXXScopeSpec SS; | 
|  | 2164 | SS.Adopt(QualifierLoc); | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2165 | if (CheckRedeclaration) { | 
|  | 2166 | Prev.setHideTags(false); | 
|  | 2167 | SemaRef.LookupQualifiedName(Prev, Owner); | 
|  | 2168 |  | 
|  | 2169 | // Check for invalid redeclarations. | 
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2170 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), | 
|  | 2171 | D->hasTypename(), SS, | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2172 | D->getLocation(), Prev)) | 
|  | 2173 | NewUD->setInvalidDecl(); | 
|  | 2174 |  | 
|  | 2175 | } | 
|  | 2176 |  | 
|  | 2177 | if (!NewUD->isInvalidDecl() && | 
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2178 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS, | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2179 | D->getLocation())) | 
|  | 2180 | NewUD->setInvalidDecl(); | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2181 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2182 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); | 
|  | 2183 | NewUD->setAccess(D->getAccess()); | 
|  | 2184 | Owner->addDecl(NewUD); | 
|  | 2185 |  | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2186 | // Don't process the shadow decls for an invalid decl. | 
|  | 2187 | if (NewUD->isInvalidDecl()) | 
|  | 2188 | return NewUD; | 
|  | 2189 |  | 
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2190 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) { | 
|  | 2191 | if (SemaRef.CheckInheritingConstructorUsingDecl(NewUD)) | 
|  | 2192 | NewUD->setInvalidDecl(); | 
|  | 2193 | return NewUD; | 
|  | 2194 | } | 
|  | 2195 |  | 
| John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2196 | bool isFunctionScope = Owner->isFunctionOrMethod(); | 
|  | 2197 |  | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2198 | // Process the shadow decls. | 
|  | 2199 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); | 
|  | 2200 | I != E; ++I) { | 
|  | 2201 | UsingShadowDecl *Shadow = *I; | 
|  | 2202 | NamedDecl *InstTarget = | 
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2203 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( | 
|  | 2204 | Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs)); | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2205 | if (!InstTarget) | 
|  | 2206 | return 0; | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2207 |  | 
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2208 | UsingShadowDecl *PrevDecl = 0; | 
|  | 2209 | if (CheckRedeclaration) { | 
|  | 2210 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) | 
|  | 2211 | continue; | 
|  | 2212 | } else if (UsingShadowDecl *OldPrev = Shadow->getPreviousDecl()) { | 
|  | 2213 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( | 
|  | 2214 | Shadow->getLocation(), OldPrev, TemplateArgs)); | 
|  | 2215 | } | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2216 |  | 
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2217 | UsingShadowDecl *InstShadow = | 
|  | 2218 | SemaRef.BuildUsingShadowDecl(/*Scope*/0, NewUD, InstTarget, PrevDecl); | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2219 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); | 
| John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2220 |  | 
|  | 2221 | if (isFunctionScope) | 
|  | 2222 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2223 | } | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2224 |  | 
|  | 2225 | return NewUD; | 
|  | 2226 | } | 
|  | 2227 |  | 
|  | 2228 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { | 
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2229 | // Ignore these;  we handle them in bulk when processing the UsingDecl. | 
|  | 2230 | return 0; | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2231 | } | 
|  | 2232 |  | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2233 | Decl * TemplateDeclInstantiator | 
|  | 2234 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2235 | NestedNameSpecifierLoc QualifierLoc | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2236 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2237 | TemplateArgs); | 
|  | 2238 | if (!QualifierLoc) | 
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2239 | return 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 |  | 
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2241 | CXXScopeSpec SS; | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2242 | SS.Adopt(QualifierLoc); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 |  | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2244 | // 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] | 2245 | // Hence, no transformation is required for it. | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2246 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | NamedDecl *UD = | 
| John McCall | 3f74682 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 2248 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2249 | D->getUsingLoc(), SS, NameInfo, 0, | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2250 | /*instantiation*/ true, | 
|  | 2251 | /*typename*/ true, D->getTypenameLoc()); | 
| Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2252 | if (UD) | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2253 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); | 
|  | 2254 |  | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2255 | return UD; | 
|  | 2256 | } | 
|  | 2257 |  | 
|  | 2258 | Decl * TemplateDeclInstantiator | 
|  | 2259 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2260 | NestedNameSpecifierLoc QualifierLoc | 
|  | 2261 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs); | 
|  | 2262 | if (!QualifierLoc) | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2263 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2264 |  | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2265 | CXXScopeSpec SS; | 
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2266 | SS.Adopt(QualifierLoc); | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2267 |  | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2268 | DeclarationNameInfo NameInfo | 
|  | 2269 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); | 
|  | 2270 |  | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2271 | NamedDecl *UD = | 
|  | 2272 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), | 
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2273 | D->getUsingLoc(), SS, NameInfo, 0, | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2274 | /*instantiation*/ true, | 
|  | 2275 | /*typename*/ false, SourceLocation()); | 
| Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2276 | if (UD) | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2277 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); | 
|  | 2278 |  | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2279 | return UD; | 
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2280 | } | 
|  | 2281 |  | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2282 |  | 
|  | 2283 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( | 
|  | 2284 | ClassScopeFunctionSpecializationDecl *Decl) { | 
|  | 2285 | CXXMethodDecl *OldFD = Decl->getSpecialization(); | 
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2286 | CXXMethodDecl *NewFD = cast<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, | 
|  | 2287 | 0, true)); | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2288 |  | 
|  | 2289 | LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName, | 
|  | 2290 | Sema::ForRedeclaration); | 
|  | 2291 |  | 
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2292 | TemplateArgumentListInfo TemplateArgs; | 
|  | 2293 | TemplateArgumentListInfo* TemplateArgsPtr = 0; | 
|  | 2294 | if (Decl->hasExplicitTemplateArgs()) { | 
|  | 2295 | TemplateArgs = Decl->templateArgs(); | 
|  | 2296 | TemplateArgsPtr = &TemplateArgs; | 
|  | 2297 | } | 
|  | 2298 |  | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2299 | SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext); | 
| Nico Weber | 7b5a716 | 2012-06-25 17:21:05 +0000 | [diff] [blame] | 2300 | if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr, | 
|  | 2301 | Previous)) { | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2302 | NewFD->setInvalidDecl(); | 
|  | 2303 | return NewFD; | 
|  | 2304 | } | 
|  | 2305 |  | 
|  | 2306 | // Associate the specialization with the pattern. | 
|  | 2307 | FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl()); | 
|  | 2308 | assert(Specialization && "Class scope Specialization is null"); | 
|  | 2309 | SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD); | 
|  | 2310 |  | 
|  | 2311 | return NewFD; | 
|  | 2312 | } | 
|  | 2313 |  | 
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2314 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( | 
|  | 2315 | OMPThreadPrivateDecl *D) { | 
| Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2316 | SmallVector<Expr *, 5> Vars; | 
|  | 2317 | for (ArrayRef<Expr *>::iterator I = D->varlist_begin(), | 
|  | 2318 | E = D->varlist_end(); | 
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2319 | I != E; ++I) { | 
|  | 2320 | Expr *Var = SemaRef.SubstExpr(*I, TemplateArgs).take(); | 
|  | 2321 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); | 
| Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2322 | Vars.push_back(Var); | 
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2323 | } | 
|  | 2324 |  | 
|  | 2325 | OMPThreadPrivateDecl *TD = | 
|  | 2326 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); | 
|  | 2327 |  | 
|  | 2328 | return TD; | 
|  | 2329 | } | 
|  | 2330 |  | 
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2331 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { | 
|  | 2332 | return VisitFunctionDecl(D, 0); | 
|  | 2333 | } | 
|  | 2334 |  | 
|  | 2335 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { | 
|  | 2336 | return VisitCXXMethodDecl(D, 0); | 
|  | 2337 | } | 
|  | 2338 |  | 
|  | 2339 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { | 
|  | 2340 | llvm_unreachable("There are only CXXRecordDecls in C++"); | 
|  | 2341 | } | 
|  | 2342 |  | 
|  | 2343 | Decl * | 
|  | 2344 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( | 
|  | 2345 | ClassTemplateSpecializationDecl *D) { | 
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 2346 | // As a MS extension, we permit class-scope explicit specialization | 
|  | 2347 | // of member class templates. | 
|  | 2348 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); | 
|  | 2349 | assert(ClassTemplate->getDeclContext()->isRecord() && | 
|  | 2350 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && | 
|  | 2351 | "can only instantiate an explicit specialization " | 
|  | 2352 | "for a member class template"); | 
|  | 2353 |  | 
|  | 2354 | // Lookup the already-instantiated declaration in the instantiation | 
|  | 2355 | // of the class template. FIXME: Diagnose or assert if this fails? | 
|  | 2356 | DeclContext::lookup_result Found | 
|  | 2357 | = Owner->lookup(ClassTemplate->getDeclName()); | 
|  | 2358 | if (Found.empty()) | 
|  | 2359 | return 0; | 
|  | 2360 | ClassTemplateDecl *InstClassTemplate | 
|  | 2361 | = dyn_cast<ClassTemplateDecl>(Found.front()); | 
|  | 2362 | if (!InstClassTemplate) | 
|  | 2363 | return 0; | 
|  | 2364 |  | 
|  | 2365 | // Substitute into the template arguments of the class template explicit | 
|  | 2366 | // specialization. | 
|  | 2367 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). | 
|  | 2368 | castAs<TemplateSpecializationTypeLoc>(); | 
|  | 2369 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), | 
|  | 2370 | Loc.getRAngleLoc()); | 
|  | 2371 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; | 
|  | 2372 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) | 
|  | 2373 | ArgLocs.push_back(Loc.getArgLoc(I)); | 
|  | 2374 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), | 
|  | 2375 | InstTemplateArgs, TemplateArgs)) | 
|  | 2376 | return 0; | 
|  | 2377 |  | 
|  | 2378 | // Check that the template argument list is well-formed for this | 
|  | 2379 | // class template. | 
|  | 2380 | SmallVector<TemplateArgument, 4> Converted; | 
|  | 2381 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, | 
|  | 2382 | D->getLocation(), | 
|  | 2383 | InstTemplateArgs, | 
|  | 2384 | false, | 
|  | 2385 | Converted)) | 
|  | 2386 | return 0; | 
|  | 2387 |  | 
|  | 2388 | // Figure out where to insert this class template explicit specialization | 
|  | 2389 | // in the member template's set of class template explicit specializations. | 
|  | 2390 | void *InsertPos = 0; | 
|  | 2391 | ClassTemplateSpecializationDecl *PrevDecl = | 
|  | 2392 | InstClassTemplate->findSpecialization(Converted.data(), Converted.size(), | 
|  | 2393 | InsertPos); | 
|  | 2394 |  | 
|  | 2395 | // Check whether we've already seen a conflicting instantiation of this | 
|  | 2396 | // declaration (for instance, if there was a prior implicit instantiation). | 
|  | 2397 | bool Ignored; | 
|  | 2398 | if (PrevDecl && | 
|  | 2399 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), | 
|  | 2400 | D->getSpecializationKind(), | 
|  | 2401 | PrevDecl, | 
|  | 2402 | PrevDecl->getSpecializationKind(), | 
|  | 2403 | PrevDecl->getPointOfInstantiation(), | 
|  | 2404 | Ignored)) | 
|  | 2405 | return 0; | 
|  | 2406 |  | 
|  | 2407 | // If PrevDecl was a definition and D is also a definition, diagnose. | 
|  | 2408 | // This happens in cases like: | 
|  | 2409 | // | 
|  | 2410 | //   template<typename T, typename U> | 
|  | 2411 | //   struct Outer { | 
|  | 2412 | //     template<typename X> struct Inner; | 
|  | 2413 | //     template<> struct Inner<T> {}; | 
|  | 2414 | //     template<> struct Inner<U> {}; | 
|  | 2415 | //   }; | 
|  | 2416 | // | 
|  | 2417 | //   Outer<int, int> outer; // error: the explicit specializations of Inner | 
|  | 2418 | //                          // have the same signature. | 
|  | 2419 | if (PrevDecl && PrevDecl->getDefinition() && | 
|  | 2420 | D->isThisDeclarationADefinition()) { | 
|  | 2421 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; | 
|  | 2422 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), | 
|  | 2423 | diag::note_previous_definition); | 
|  | 2424 | return 0; | 
|  | 2425 | } | 
|  | 2426 |  | 
|  | 2427 | // Create the class template partial specialization declaration. | 
|  | 2428 | ClassTemplateSpecializationDecl *InstD | 
|  | 2429 | = ClassTemplateSpecializationDecl::Create(SemaRef.Context, | 
|  | 2430 | D->getTagKind(), | 
|  | 2431 | Owner, | 
|  | 2432 | D->getLocStart(), | 
|  | 2433 | D->getLocation(), | 
|  | 2434 | InstClassTemplate, | 
|  | 2435 | Converted.data(), | 
|  | 2436 | Converted.size(), | 
|  | 2437 | PrevDecl); | 
|  | 2438 |  | 
|  | 2439 | // Add this partial specialization to the set of class template partial | 
|  | 2440 | // specializations. | 
|  | 2441 | if (!PrevDecl) | 
|  | 2442 | InstClassTemplate->AddSpecialization(InstD, InsertPos); | 
|  | 2443 |  | 
|  | 2444 | // Substitute the nested name specifier, if any. | 
|  | 2445 | if (SubstQualifier(D, InstD)) | 
|  | 2446 | return 0; | 
|  | 2447 |  | 
|  | 2448 | // Build the canonical type that describes the converted template | 
|  | 2449 | // arguments of the class template explicit specialization. | 
|  | 2450 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( | 
|  | 2451 | TemplateName(InstClassTemplate), Converted.data(), Converted.size(), | 
|  | 2452 | SemaRef.Context.getRecordType(InstD)); | 
|  | 2453 |  | 
|  | 2454 | // Build the fully-sugared type for this class template | 
|  | 2455 | // specialization as the user wrote in the specialization | 
|  | 2456 | // itself. This means that we'll pretty-print the type retrieved | 
|  | 2457 | // from the specialization's declaration the way that the user | 
|  | 2458 | // actually wrote the specialization, rather than formatting the | 
|  | 2459 | // name based on the "canonical" representation used to store the | 
|  | 2460 | // template arguments in the specialization. | 
|  | 2461 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( | 
|  | 2462 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, | 
|  | 2463 | CanonType); | 
|  | 2464 |  | 
|  | 2465 | InstD->setAccess(D->getAccess()); | 
|  | 2466 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); | 
|  | 2467 | InstD->setSpecializationKind(D->getSpecializationKind()); | 
|  | 2468 | InstD->setTypeAsWritten(WrittenTy); | 
|  | 2469 | InstD->setExternLoc(D->getExternLoc()); | 
|  | 2470 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); | 
|  | 2471 |  | 
|  | 2472 | Owner->addDecl(InstD); | 
|  | 2473 |  | 
|  | 2474 | // Instantiate the members of the class-scope explicit specialization eagerly. | 
|  | 2475 | // We don't have support for lazy instantiation of an explicit specialization | 
|  | 2476 | // yet, and MSVC eagerly instantiates in this case. | 
|  | 2477 | if (D->isThisDeclarationADefinition() && | 
|  | 2478 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, | 
|  | 2479 | TSK_ImplicitInstantiation, | 
|  | 2480 | /*Complain=*/true)) | 
|  | 2481 | return 0; | 
|  | 2482 |  | 
|  | 2483 | return InstD; | 
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2484 | } | 
|  | 2485 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2486 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( | 
|  | 2487 | VarTemplateSpecializationDecl *D) { | 
|  | 2488 |  | 
|  | 2489 | TemplateArgumentListInfo VarTemplateArgsInfo; | 
|  | 2490 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); | 
|  | 2491 | assert(VarTemplate && | 
|  | 2492 | "A template specialization without specialized template?"); | 
|  | 2493 |  | 
|  | 2494 | // Substitute the current template arguments. | 
|  | 2495 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); | 
|  | 2496 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); | 
|  | 2497 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); | 
|  | 2498 |  | 
|  | 2499 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), | 
|  | 2500 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) | 
|  | 2501 | return 0; | 
|  | 2502 |  | 
|  | 2503 | // Check that the template argument list is well-formed for this template. | 
|  | 2504 | SmallVector<TemplateArgument, 4> Converted; | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2505 | if (SemaRef.CheckTemplateArgumentList( | 
|  | 2506 | VarTemplate, VarTemplate->getLocStart(), | 
|  | 2507 | const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false, | 
| Richard Smith | 83b11aa | 2014-01-09 02:22:22 +0000 | [diff] [blame] | 2508 | Converted)) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2509 | return 0; | 
|  | 2510 |  | 
|  | 2511 | // Find the variable template specialization declaration that | 
|  | 2512 | // corresponds to these arguments. | 
|  | 2513 | void *InsertPos = 0; | 
|  | 2514 | if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization( | 
|  | 2515 | Converted.data(), Converted.size(), InsertPos)) | 
|  | 2516 | // If we already have a variable template specialization, return it. | 
|  | 2517 | return VarSpec; | 
|  | 2518 |  | 
|  | 2519 | return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos, | 
|  | 2520 | VarTemplateArgsInfo, Converted); | 
|  | 2521 | } | 
|  | 2522 |  | 
|  | 2523 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( | 
|  | 2524 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, | 
|  | 2525 | const TemplateArgumentListInfo &TemplateArgsInfo, | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2526 | llvm::ArrayRef<TemplateArgument> Converted) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2527 |  | 
|  | 2528 | // If this is the variable for an anonymous struct or union, | 
|  | 2529 | // instantiate the anonymous struct/union type first. | 
|  | 2530 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) | 
|  | 2531 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) | 
|  | 2532 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) | 
|  | 2533 | return 0; | 
|  | 2534 |  | 
|  | 2535 | // Do substitution on the type of the declaration | 
|  | 2536 | TypeSourceInfo *DI = | 
|  | 2537 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, | 
|  | 2538 | D->getTypeSpecStartLoc(), D->getDeclName()); | 
|  | 2539 | if (!DI) | 
|  | 2540 | return 0; | 
|  | 2541 |  | 
|  | 2542 | if (DI->getType()->isFunctionType()) { | 
|  | 2543 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) | 
|  | 2544 | << D->isStaticDataMember() << DI->getType(); | 
|  | 2545 | return 0; | 
|  | 2546 | } | 
|  | 2547 |  | 
|  | 2548 | // Build the instantiated declaration | 
|  | 2549 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( | 
|  | 2550 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), | 
|  | 2551 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(), | 
|  | 2552 | Converted.size()); | 
|  | 2553 | Var->setTemplateArgsInfo(TemplateArgsInfo); | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2554 | if (InsertPos) | 
|  | 2555 | VarTemplate->AddSpecialization(Var, InsertPos); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2556 |  | 
|  | 2557 | // Substitute the nested name specifier, if any. | 
|  | 2558 | if (SubstQualifier(D, Var)) | 
|  | 2559 | return 0; | 
|  | 2560 |  | 
|  | 2561 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2562 | Owner, StartingScope); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2563 |  | 
|  | 2564 | return Var; | 
|  | 2565 | } | 
|  | 2566 |  | 
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2567 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { | 
|  | 2568 | llvm_unreachable("@defs is not supported in Objective-C++"); | 
|  | 2569 | } | 
|  | 2570 |  | 
|  | 2571 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { | 
|  | 2572 | // FIXME: We need to be able to instantiate FriendTemplateDecls. | 
|  | 2573 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( | 
|  | 2574 | DiagnosticsEngine::Error, | 
|  | 2575 | "cannot instantiate %0 yet"); | 
|  | 2576 | SemaRef.Diag(D->getLocation(), DiagID) | 
|  | 2577 | << D->getDeclKindName(); | 
|  | 2578 |  | 
|  | 2579 | return 0; | 
|  | 2580 | } | 
|  | 2581 |  | 
|  | 2582 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { | 
|  | 2583 | llvm_unreachable("Unexpected decl"); | 
|  | 2584 | } | 
|  | 2585 |  | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2586 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, | 
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2587 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 2588 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); | 
| Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 2589 | if (D->isInvalidDecl()) | 
|  | 2590 | return 0; | 
|  | 2591 |  | 
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 2592 | return Instantiator.Visit(D); | 
|  | 2593 | } | 
|  | 2594 |  | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2595 | /// \brief Instantiates a nested template parameter list in the current | 
|  | 2596 | /// instantiation context. | 
|  | 2597 | /// | 
|  | 2598 | /// \param L The parameter list to instantiate | 
|  | 2599 | /// | 
|  | 2600 | /// \returns NULL if there was an error | 
|  | 2601 | TemplateParameterList * | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2602 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2603 | // Get errors for all the parameters before bailing out. | 
|  | 2604 | bool Invalid = false; | 
|  | 2605 |  | 
|  | 2606 | unsigned N = L->size(); | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2607 | typedef SmallVector<NamedDecl *, 8> ParamVector; | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2608 | ParamVector Params; | 
|  | 2609 | Params.reserve(N); | 
|  | 2610 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); | 
|  | 2611 | PI != PE; ++PI) { | 
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2612 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2613 | Params.push_back(D); | 
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 2614 | Invalid = Invalid || !D || D->isInvalidDecl(); | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2615 | } | 
|  | 2616 |  | 
|  | 2617 | // Clean up if we had an error. | 
| Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 2618 | if (Invalid) | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2619 | return NULL; | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2620 |  | 
|  | 2621 | TemplateParameterList *InstL | 
|  | 2622 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), | 
|  | 2623 | L->getLAngleLoc(), &Params.front(), N, | 
|  | 2624 | L->getRAngleLoc()); | 
|  | 2625 | return InstL; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | } | 
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2627 |  | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2628 | /// \brief Instantiate the declaration of a class template partial | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2629 | /// specialization. | 
|  | 2630 | /// | 
|  | 2631 | /// \param ClassTemplate the (instantiated) class template that is partially | 
|  | 2632 | // specialized by the instantiation of \p PartialSpec. | 
|  | 2633 | /// | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2634 | /// \param PartialSpec the (uninstantiated) class template partial | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2635 | /// specialization that we are instantiating. | 
|  | 2636 | /// | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2637 | /// \returns The instantiated partial specialization, if successful; otherwise, | 
|  | 2638 | /// NULL to indicate an error. | 
|  | 2639 | ClassTemplatePartialSpecializationDecl * | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2640 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( | 
|  | 2641 | ClassTemplateDecl *ClassTemplate, | 
|  | 2642 | ClassTemplatePartialSpecializationDecl *PartialSpec) { | 
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2643 | // Create a local instantiation scope for this class template partial | 
|  | 2644 | // specialization, which will contain the instantiations of the template | 
|  | 2645 | // parameters. | 
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2646 | LocalInstantiationScope Scope(SemaRef); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2647 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2648 | // Substitute into the template parameters of the class template partial | 
|  | 2649 | // specialization. | 
|  | 2650 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); | 
|  | 2651 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 2652 | if (!InstParams) | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2653 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2654 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2655 | // Substitute into the template arguments of the class template partial | 
|  | 2656 | // specialization. | 
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2657 | const ASTTemplateArgumentListInfo *TemplArgInfo | 
|  | 2658 | = PartialSpec->getTemplateArgsAsWritten(); | 
|  | 2659 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, | 
|  | 2660 | TemplArgInfo->RAngleLoc); | 
|  | 2661 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), | 
|  | 2662 | TemplArgInfo->NumTemplateArgs, | 
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2663 | InstTemplateArgs, TemplateArgs)) | 
|  | 2664 | return 0; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2665 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2666 | // Check that the template argument list is well-formed for this | 
|  | 2667 | // class template. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2668 | SmallVector<TemplateArgument, 4> Converted; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2669 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2670 | PartialSpec->getLocation(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2671 | InstTemplateArgs, | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2672 | false, | 
|  | 2673 | Converted)) | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2674 | return 0; | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2675 |  | 
|  | 2676 | // Figure out where to insert this class template partial specialization | 
|  | 2677 | // in the member template's set of class template partial specializations. | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2678 | void *InsertPos = 0; | 
|  | 2679 | ClassTemplateSpecializationDecl *PrevDecl | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2680 | = ClassTemplate->findPartialSpecialization(Converted.data(), | 
|  | 2681 | Converted.size(), InsertPos); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2682 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2683 | // Build the canonical type that describes the converted template | 
|  | 2684 | // arguments of the class template partial specialization. | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2685 | QualType CanonType | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2686 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2687 | Converted.data(), | 
|  | 2688 | Converted.size()); | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2689 |  | 
|  | 2690 | // Build the fully-sugared type for this class template | 
|  | 2691 | // specialization as the user wrote in the specialization | 
|  | 2692 | // itself. This means that we'll pretty-print the type retrieved | 
|  | 2693 | // from the specialization's declaration the way that the user | 
|  | 2694 | // actually wrote the specialization, rather than formatting the | 
|  | 2695 | // name based on the "canonical" representation used to store the | 
|  | 2696 | // template arguments in the specialization. | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2697 | TypeSourceInfo *WrittenTy | 
|  | 2698 | = SemaRef.Context.getTemplateSpecializationTypeInfo( | 
|  | 2699 | TemplateName(ClassTemplate), | 
|  | 2700 | PartialSpec->getLocation(), | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2701 | InstTemplateArgs, | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2702 | CanonType); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2703 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2704 | if (PrevDecl) { | 
|  | 2705 | // We've already seen a partial specialization with the same template | 
|  | 2706 | // parameters and template arguments. This can happen, for example, when | 
|  | 2707 | // substituting the outer template arguments ends up causing two | 
|  | 2708 | // class template partial specializations of a member class template | 
|  | 2709 | // to have identical forms, e.g., | 
|  | 2710 | // | 
|  | 2711 | //   template<typename T, typename U> | 
|  | 2712 | //   struct Outer { | 
|  | 2713 | //     template<typename X, typename Y> struct Inner; | 
|  | 2714 | //     template<typename Y> struct Inner<T, Y>; | 
|  | 2715 | //     template<typename Y> struct Inner<U, Y>; | 
|  | 2716 | //   }; | 
|  | 2717 | // | 
|  | 2718 | //   Outer<int, int> outer; // error: the partial specializations of Inner | 
|  | 2719 | //                          // have the same signature. | 
|  | 2720 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2721 | << WrittenTy->getType(); | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2722 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) | 
|  | 2723 | << SemaRef.Context.getTypeDeclType(PrevDecl); | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2724 | return 0; | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2725 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2726 |  | 
|  | 2727 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2728 | // Create the class template partial specialization declaration. | 
|  | 2729 | ClassTemplatePartialSpecializationDecl *InstPartialSpec | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2730 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, | 
| Douglas Gregor | e902956 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2731 | PartialSpec->getTagKind(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2732 | Owner, | 
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2733 | PartialSpec->getLocStart(), | 
|  | 2734 | PartialSpec->getLocation(), | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2735 | InstParams, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2736 | ClassTemplate, | 
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2737 | Converted.data(), | 
|  | 2738 | Converted.size(), | 
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2739 | InstTemplateArgs, | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2740 | CanonType, | 
| Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2741 | 0); | 
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2742 | // Substitute the nested name specifier, if any. | 
|  | 2743 | if (SubstQualifier(PartialSpec, InstPartialSpec)) | 
|  | 2744 | return 0; | 
|  | 2745 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2746 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); | 
| Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2747 | InstPartialSpec->setTypeAsWritten(WrittenTy); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2748 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2749 | // Add this partial specialization to the set of class template partial | 
|  | 2750 | // specializations. | 
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2751 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0); | 
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2752 | return InstPartialSpec; | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2753 | } | 
|  | 2754 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2755 | /// \brief Instantiate the declaration of a variable template partial | 
|  | 2756 | /// specialization. | 
|  | 2757 | /// | 
|  | 2758 | /// \param VarTemplate the (instantiated) variable template that is partially | 
|  | 2759 | /// specialized by the instantiation of \p PartialSpec. | 
|  | 2760 | /// | 
|  | 2761 | /// \param PartialSpec the (uninstantiated) variable template partial | 
|  | 2762 | /// specialization that we are instantiating. | 
|  | 2763 | /// | 
|  | 2764 | /// \returns The instantiated partial specialization, if successful; otherwise, | 
|  | 2765 | /// NULL to indicate an error. | 
|  | 2766 | VarTemplatePartialSpecializationDecl * | 
|  | 2767 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( | 
|  | 2768 | VarTemplateDecl *VarTemplate, | 
|  | 2769 | VarTemplatePartialSpecializationDecl *PartialSpec) { | 
|  | 2770 | // Create a local instantiation scope for this variable template partial | 
|  | 2771 | // specialization, which will contain the instantiations of the template | 
|  | 2772 | // parameters. | 
|  | 2773 | LocalInstantiationScope Scope(SemaRef); | 
|  | 2774 |  | 
|  | 2775 | // Substitute into the template parameters of the variable template partial | 
|  | 2776 | // specialization. | 
|  | 2777 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); | 
|  | 2778 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 2779 | if (!InstParams) | 
|  | 2780 | return 0; | 
|  | 2781 |  | 
|  | 2782 | // Substitute into the template arguments of the variable template partial | 
|  | 2783 | // specialization. | 
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2784 | const ASTTemplateArgumentListInfo *TemplArgInfo | 
|  | 2785 | = PartialSpec->getTemplateArgsAsWritten(); | 
|  | 2786 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, | 
|  | 2787 | TemplArgInfo->RAngleLoc); | 
|  | 2788 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), | 
|  | 2789 | TemplArgInfo->NumTemplateArgs, | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2790 | InstTemplateArgs, TemplateArgs)) | 
|  | 2791 | return 0; | 
|  | 2792 |  | 
|  | 2793 | // Check that the template argument list is well-formed for this | 
|  | 2794 | // class template. | 
|  | 2795 | SmallVector<TemplateArgument, 4> Converted; | 
|  | 2796 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), | 
|  | 2797 | InstTemplateArgs, false, Converted)) | 
|  | 2798 | return 0; | 
|  | 2799 |  | 
|  | 2800 | // Figure out where to insert this variable template partial specialization | 
|  | 2801 | // in the member template's set of variable template partial specializations. | 
|  | 2802 | void *InsertPos = 0; | 
|  | 2803 | VarTemplateSpecializationDecl *PrevDecl = | 
|  | 2804 | VarTemplate->findPartialSpecialization(Converted.data(), Converted.size(), | 
|  | 2805 | InsertPos); | 
|  | 2806 |  | 
|  | 2807 | // Build the canonical type that describes the converted template | 
|  | 2808 | // arguments of the variable template partial specialization. | 
|  | 2809 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( | 
|  | 2810 | TemplateName(VarTemplate), Converted.data(), Converted.size()); | 
|  | 2811 |  | 
|  | 2812 | // Build the fully-sugared type for this variable template | 
|  | 2813 | // specialization as the user wrote in the specialization | 
|  | 2814 | // itself. This means that we'll pretty-print the type retrieved | 
|  | 2815 | // from the specialization's declaration the way that the user | 
|  | 2816 | // actually wrote the specialization, rather than formatting the | 
|  | 2817 | // name based on the "canonical" representation used to store the | 
|  | 2818 | // template arguments in the specialization. | 
|  | 2819 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( | 
|  | 2820 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, | 
|  | 2821 | CanonType); | 
|  | 2822 |  | 
|  | 2823 | if (PrevDecl) { | 
|  | 2824 | // We've already seen a partial specialization with the same template | 
|  | 2825 | // parameters and template arguments. This can happen, for example, when | 
|  | 2826 | // substituting the outer template arguments ends up causing two | 
|  | 2827 | // variable template partial specializations of a member variable template | 
|  | 2828 | // to have identical forms, e.g., | 
|  | 2829 | // | 
|  | 2830 | //   template<typename T, typename U> | 
|  | 2831 | //   struct Outer { | 
|  | 2832 | //     template<typename X, typename Y> pair<X,Y> p; | 
|  | 2833 | //     template<typename Y> pair<T, Y> p; | 
|  | 2834 | //     template<typename Y> pair<U, Y> p; | 
|  | 2835 | //   }; | 
|  | 2836 | // | 
|  | 2837 | //   Outer<int, int> outer; // error: the partial specializations of Inner | 
|  | 2838 | //                          // have the same signature. | 
|  | 2839 | SemaRef.Diag(PartialSpec->getLocation(), | 
|  | 2840 | diag::err_var_partial_spec_redeclared) | 
|  | 2841 | << WrittenTy->getType(); | 
|  | 2842 | SemaRef.Diag(PrevDecl->getLocation(), | 
|  | 2843 | diag::note_var_prev_partial_spec_here); | 
|  | 2844 | return 0; | 
|  | 2845 | } | 
|  | 2846 |  | 
|  | 2847 | // Do substitution on the type of the declaration | 
|  | 2848 | TypeSourceInfo *DI = SemaRef.SubstType( | 
|  | 2849 | PartialSpec->getTypeSourceInfo(), TemplateArgs, | 
|  | 2850 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); | 
|  | 2851 | if (!DI) | 
|  | 2852 | return 0; | 
|  | 2853 |  | 
|  | 2854 | if (DI->getType()->isFunctionType()) { | 
|  | 2855 | SemaRef.Diag(PartialSpec->getLocation(), | 
|  | 2856 | diag::err_variable_instantiates_to_function) | 
|  | 2857 | << PartialSpec->isStaticDataMember() << DI->getType(); | 
|  | 2858 | return 0; | 
|  | 2859 | } | 
|  | 2860 |  | 
|  | 2861 | // Create the variable template partial specialization declaration. | 
|  | 2862 | VarTemplatePartialSpecializationDecl *InstPartialSpec = | 
|  | 2863 | VarTemplatePartialSpecializationDecl::Create( | 
|  | 2864 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), | 
|  | 2865 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), | 
|  | 2866 | DI, PartialSpec->getStorageClass(), Converted.data(), | 
| Richard Smith | b2f61b4 | 2013-08-22 23:27:37 +0000 | [diff] [blame] | 2867 | Converted.size(), InstTemplateArgs); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2868 |  | 
|  | 2869 | // Substitute the nested name specifier, if any. | 
|  | 2870 | if (SubstQualifier(PartialSpec, InstPartialSpec)) | 
|  | 2871 | return 0; | 
|  | 2872 |  | 
|  | 2873 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); | 
|  | 2874 | InstPartialSpec->setTypeAsWritten(WrittenTy); | 
|  | 2875 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2876 | // Add this partial specialization to the set of variable template partial | 
|  | 2877 | // specializations. The instantiation of the initializer is not necessary. | 
|  | 2878 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/0); | 
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2879 |  | 
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2880 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 2881 | LateAttrs, Owner, StartingScope); | 
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 2882 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2883 | return InstPartialSpec; | 
|  | 2884 | } | 
|  | 2885 |  | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2886 | TypeSourceInfo* | 
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2887 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2888 | SmallVectorImpl<ParmVarDecl *> &Params) { | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2889 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); | 
|  | 2890 | assert(OldTInfo && "substituting function without type source info"); | 
|  | 2891 | assert(Params.empty() && "parameter vector is non-empty at start"); | 
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2892 |  | 
|  | 2893 | CXXRecordDecl *ThisContext = 0; | 
|  | 2894 | unsigned ThisTypeQuals = 0; | 
|  | 2895 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | 
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 2896 | ThisContext = cast<CXXRecordDecl>(Owner); | 
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2897 | ThisTypeQuals = Method->getTypeQualifiers(); | 
|  | 2898 | } | 
|  | 2899 |  | 
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 2900 | TypeSourceInfo *NewTInfo | 
|  | 2901 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, | 
|  | 2902 | D->getTypeSpecStartLoc(), | 
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2903 | D->getDeclName(), | 
|  | 2904 | ThisContext, ThisTypeQuals); | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2905 | if (!NewTInfo) | 
|  | 2906 | return 0; | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2907 |  | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2908 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); | 
|  | 2909 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { | 
|  | 2910 | if (NewTInfo != OldTInfo) { | 
|  | 2911 | // Get parameters from the new type info. | 
| Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2912 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); | 
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2913 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2914 | unsigned NewIdx = 0; | 
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2915 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); | 
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2916 | OldIdx != NumOldParams; ++OldIdx) { | 
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2917 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2918 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; | 
|  | 2919 |  | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2920 | Optional<unsigned> NumArgumentsInExpansion; | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2921 | if (OldParam->isParameterPack()) | 
|  | 2922 | NumArgumentsInExpansion = | 
|  | 2923 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), | 
|  | 2924 | TemplateArgs); | 
|  | 2925 | if (!NumArgumentsInExpansion) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2926 | // Simple case: normal parameter, or a parameter pack that's | 
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2927 | // instantiated to a (still-dependent) parameter pack. | 
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2928 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); | 
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2929 | Params.push_back(NewParam); | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2930 | Scope->InstantiatedLocal(OldParam, NewParam); | 
|  | 2931 | } else { | 
|  | 2932 | // Parameter pack expansion: make the instantiation an argument pack. | 
|  | 2933 | Scope->MakeInstantiatedLocalArgPack(OldParam); | 
|  | 2934 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { | 
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2935 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 2936 | Params.push_back(NewParam); | 
|  | 2937 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); | 
|  | 2938 | } | 
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2939 | } | 
| Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2940 | } | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2941 | } else { | 
|  | 2942 | // The function type itself was not dependent and therefore no | 
|  | 2943 | // substitution occurred. However, we still need to instantiate | 
|  | 2944 | // the function parameters themselves. | 
|  | 2945 | const FunctionProtoType *OldProto = | 
|  | 2946 | cast<FunctionProtoType>(OldProtoLoc.getType()); | 
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2947 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; | 
|  | 2948 | ++i) { | 
|  | 2949 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2950 | if (!OldParam) { | 
|  | 2951 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( | 
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2952 | D, D->getLocation(), OldProto->getParamType(i))); | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2953 | continue; | 
|  | 2954 | } | 
|  | 2955 |  | 
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2956 | ParmVarDecl *Parm = | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2957 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); | 
| Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2958 | if (!Parm) | 
|  | 2959 | return 0; | 
|  | 2960 | Params.push_back(Parm); | 
|  | 2961 | } | 
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2962 | } | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2963 | } else { | 
|  | 2964 | // If the type of this function, after ignoring parentheses, is not | 
|  | 2965 | // *directly* a function type, then we're instantiating a function that | 
|  | 2966 | // was declared via a typedef or with attributes, e.g., | 
|  | 2967 | // | 
|  | 2968 | //   typedef int functype(int, int); | 
|  | 2969 | //   functype func; | 
|  | 2970 | //   int __cdecl meth(int, int); | 
|  | 2971 | // | 
|  | 2972 | // In this case, we'll just go instantiate the ParmVarDecls that we | 
|  | 2973 | // synthesized in the method declaration. | 
|  | 2974 | SmallVector<QualType, 4> ParamTypes; | 
|  | 2975 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), | 
|  | 2976 | D->getNumParams(), TemplateArgs, ParamTypes, | 
|  | 2977 | &Params)) | 
|  | 2978 | return 0; | 
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2979 | } | 
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 2980 |  | 
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2981 | return NewTInfo; | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2982 | } | 
|  | 2983 |  | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2984 | /// Introduce the instantiated function parameters into the local | 
|  | 2985 | /// instantiation scope, and set the parameter names to those used | 
|  | 2986 | /// in the template. | 
|  | 2987 | static void addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, | 
|  | 2988 | const FunctionDecl *PatternDecl, | 
|  | 2989 | LocalInstantiationScope &Scope, | 
|  | 2990 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
|  | 2991 | unsigned FParamIdx = 0; | 
|  | 2992 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { | 
|  | 2993 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); | 
|  | 2994 | if (!PatternParam->isParameterPack()) { | 
|  | 2995 | // Simple case: not a parameter pack. | 
|  | 2996 | assert(FParamIdx < Function->getNumParams()); | 
|  | 2997 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); | 
|  | 2998 | FunctionParam->setDeclName(PatternParam->getDeclName()); | 
|  | 2999 | Scope.InstantiatedLocal(PatternParam, FunctionParam); | 
|  | 3000 | ++FParamIdx; | 
|  | 3001 | continue; | 
|  | 3002 | } | 
|  | 3003 |  | 
|  | 3004 | // Expand the parameter pack. | 
|  | 3005 | Scope.MakeInstantiatedLocalArgPack(PatternParam); | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3006 | Optional<unsigned> NumArgumentsInExpansion | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3007 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); | 
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3008 | assert(NumArgumentsInExpansion && | 
|  | 3009 | "should only be called when all template arguments are known"); | 
|  | 3010 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3011 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); | 
|  | 3012 | FunctionParam->setDeclName(PatternParam->getDeclName()); | 
|  | 3013 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); | 
|  | 3014 | ++FParamIdx; | 
|  | 3015 | } | 
|  | 3016 | } | 
|  | 3017 | } | 
|  | 3018 |  | 
|  | 3019 | static void InstantiateExceptionSpec(Sema &SemaRef, FunctionDecl *New, | 
|  | 3020 | const FunctionProtoType *Proto, | 
|  | 3021 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3022 | assert(Proto->getExceptionSpecType() != EST_Uninstantiated); | 
|  | 3023 |  | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3024 | // C++11 [expr.prim.general]p3: | 
|  | 3025 | //   If a declaration declares a member function or member function | 
|  | 3026 | //   template of a class X, the expression this is a prvalue of type | 
|  | 3027 | //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq | 
|  | 3028 | //   and the end of the function-definition, member-declarator, or | 
|  | 3029 | //   declarator. | 
|  | 3030 | CXXRecordDecl *ThisContext = 0; | 
|  | 3031 | unsigned ThisTypeQuals = 0; | 
|  | 3032 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(New)) { | 
|  | 3033 | ThisContext = Method->getParent(); | 
|  | 3034 | ThisTypeQuals = Method->getTypeQualifiers(); | 
|  | 3035 | } | 
|  | 3036 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, ThisTypeQuals, | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3037 | SemaRef.getLangOpts().CPlusPlus11); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3038 |  | 
|  | 3039 | // The function has an exception specification or a "noreturn" | 
|  | 3040 | // attribute. Substitute into each of the exception types. | 
|  | 3041 | SmallVector<QualType, 4> Exceptions; | 
|  | 3042 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { | 
|  | 3043 | // FIXME: Poor location information! | 
|  | 3044 | if (const PackExpansionType *PackExpansion | 
|  | 3045 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { | 
|  | 3046 | // We have a pack expansion. Instantiate it. | 
|  | 3047 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; | 
|  | 3048 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), | 
|  | 3049 | Unexpanded); | 
|  | 3050 | assert(!Unexpanded.empty() && | 
|  | 3051 | "Pack expansion without parameter packs?"); | 
|  | 3052 |  | 
|  | 3053 | bool Expand = false; | 
|  | 3054 | bool RetainExpansion = false; | 
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3055 | Optional<unsigned> NumExpansions = PackExpansion->getNumExpansions(); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3056 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), | 
|  | 3057 | SourceRange(), | 
|  | 3058 | Unexpanded, | 
|  | 3059 | TemplateArgs, | 
|  | 3060 | Expand, | 
|  | 3061 | RetainExpansion, | 
|  | 3062 | NumExpansions)) | 
|  | 3063 | break; | 
|  | 3064 |  | 
|  | 3065 | if (!Expand) { | 
|  | 3066 | // We can't expand this pack expansion into separate arguments yet; | 
|  | 3067 | // just substitute into the pattern and create a new pack expansion | 
|  | 3068 | // type. | 
|  | 3069 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); | 
|  | 3070 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), | 
|  | 3071 | TemplateArgs, | 
|  | 3072 | New->getLocation(), New->getDeclName()); | 
|  | 3073 | if (T.isNull()) | 
|  | 3074 | break; | 
|  | 3075 |  | 
|  | 3076 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); | 
|  | 3077 | Exceptions.push_back(T); | 
|  | 3078 | continue; | 
|  | 3079 | } | 
|  | 3080 |  | 
|  | 3081 | // Substitute into the pack expansion pattern for each template | 
|  | 3082 | bool Invalid = false; | 
|  | 3083 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { | 
|  | 3084 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); | 
|  | 3085 |  | 
|  | 3086 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), | 
|  | 3087 | TemplateArgs, | 
|  | 3088 | New->getLocation(), New->getDeclName()); | 
|  | 3089 | if (T.isNull()) { | 
|  | 3090 | Invalid = true; | 
|  | 3091 | break; | 
|  | 3092 | } | 
|  | 3093 |  | 
|  | 3094 | Exceptions.push_back(T); | 
|  | 3095 | } | 
|  | 3096 |  | 
|  | 3097 | if (Invalid) | 
|  | 3098 | break; | 
|  | 3099 |  | 
|  | 3100 | continue; | 
|  | 3101 | } | 
|  | 3102 |  | 
|  | 3103 | QualType T | 
|  | 3104 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, | 
|  | 3105 | New->getLocation(), New->getDeclName()); | 
|  | 3106 | if (T.isNull() || | 
|  | 3107 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) | 
|  | 3108 | continue; | 
|  | 3109 |  | 
|  | 3110 | Exceptions.push_back(T); | 
|  | 3111 | } | 
|  | 3112 | Expr *NoexceptExpr = 0; | 
|  | 3113 | if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) { | 
|  | 3114 | EnterExpressionEvaluationContext Unevaluated(SemaRef, | 
|  | 3115 | Sema::ConstantEvaluated); | 
|  | 3116 | ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs); | 
|  | 3117 | if (E.isUsable()) | 
|  | 3118 | E = SemaRef.CheckBooleanCondition(E.get(), E.get()->getLocStart()); | 
|  | 3119 |  | 
|  | 3120 | if (E.isUsable()) { | 
|  | 3121 | NoexceptExpr = E.take(); | 
|  | 3122 | if (!NoexceptExpr->isTypeDependent() && | 
|  | 3123 | !NoexceptExpr->isValueDependent()) | 
| Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3124 | NoexceptExpr | 
|  | 3125 | = SemaRef.VerifyIntegerConstantExpression(NoexceptExpr, | 
|  | 3126 | 0, diag::err_noexcept_needs_constant_expression, | 
|  | 3127 | /*AllowFold*/ false).take(); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3128 | } | 
|  | 3129 | } | 
|  | 3130 |  | 
|  | 3131 | // Rebuild the function type | 
|  | 3132 | const FunctionProtoType *NewProto | 
|  | 3133 | = New->getType()->getAs<FunctionProtoType>(); | 
|  | 3134 | assert(NewProto && "Template instantiation without function prototype?"); | 
|  | 3135 |  | 
|  | 3136 | FunctionProtoType::ExtProtoInfo EPI = NewProto->getExtProtoInfo(); | 
|  | 3137 | EPI.ExceptionSpecType = Proto->getExceptionSpecType(); | 
|  | 3138 | EPI.NumExceptions = Exceptions.size(); | 
|  | 3139 | EPI.Exceptions = Exceptions.data(); | 
|  | 3140 | EPI.NoexceptExpr = NoexceptExpr; | 
|  | 3141 |  | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3142 | New->setType(SemaRef.Context.getFunctionType(NewProto->getReturnType(), | 
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3143 | NewProto->getParamTypes(), EPI)); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3144 | } | 
|  | 3145 |  | 
|  | 3146 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, | 
|  | 3147 | FunctionDecl *Decl) { | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3148 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); | 
|  | 3149 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3150 | return; | 
|  | 3151 |  | 
|  | 3152 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, | 
|  | 3153 | InstantiatingTemplate::ExceptionSpecification()); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3154 | if (Inst.isInvalid()) { | 
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3155 | // We hit the instantiation depth limit. Clear the exception specification | 
|  | 3156 | // so that our callers don't have to cope with EST_Uninstantiated. | 
|  | 3157 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); | 
|  | 3158 | EPI.ExceptionSpecType = EST_None; | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3159 | Decl->setType(Context.getFunctionType(Proto->getReturnType(), | 
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3160 | Proto->getParamTypes(), EPI)); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3161 | return; | 
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3162 | } | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3163 |  | 
|  | 3164 | // Enter the scope of this instantiation. We don't use | 
|  | 3165 | // PushDeclContext because we don't have a scope. | 
|  | 3166 | Sema::ContextRAII savedContext(*this, Decl); | 
|  | 3167 | LocalInstantiationScope Scope(*this); | 
|  | 3168 |  | 
|  | 3169 | MultiLevelTemplateArgumentList TemplateArgs = | 
|  | 3170 | getTemplateInstantiationArgs(Decl, 0, /*RelativeToPrimary*/true); | 
|  | 3171 |  | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3172 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); | 
|  | 3173 | addInstantiatedParametersToScope(*this, Decl, Template, Scope, TemplateArgs); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3174 |  | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3175 | ::InstantiateExceptionSpec(*this, Decl, | 
|  | 3176 | Template->getType()->castAs<FunctionProtoType>(), | 
|  | 3177 | TemplateArgs); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3178 | } | 
|  | 3179 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3180 | /// \brief Initializes the common fields of an instantiation function | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3181 | /// declaration (New) from the corresponding fields of its template (Tmpl). | 
|  | 3182 | /// | 
|  | 3183 | /// \returns true if there was an error | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | bool | 
|  | 3185 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3186 | FunctionDecl *Tmpl) { | 
| David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3187 | if (Tmpl->isDeleted()) | 
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3188 | New->setDeletedAsWritten(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 |  | 
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3190 | // Forward the mangling number from the template to the instantiated decl. | 
|  | 3191 | SemaRef.Context.setManglingNumber(New, | 
|  | 3192 | SemaRef.Context.getManglingNumber(Tmpl)); | 
|  | 3193 |  | 
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3194 | // If we are performing substituting explicitly-specified template arguments | 
|  | 3195 | // or deduced template arguments into a function template and we reach this | 
|  | 3196 | // 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] | 3197 | // to keeping the new function template specialization. We therefore | 
|  | 3198 | // convert the active template instantiation for the function template | 
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3199 | // into a template instantiation for this specific function template | 
|  | 3200 | // specialization, which is not a SFINAE context, so that we diagnose any | 
|  | 3201 | // further errors in the declaration itself. | 
|  | 3202 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; | 
|  | 3203 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); | 
|  | 3204 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || | 
|  | 3205 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | if (FunctionTemplateDecl *FunTmpl | 
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3207 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | assert(FunTmpl->getTemplatedDecl() == Tmpl && | 
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3209 | "Deduction from the wrong function template?"); | 
| Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3210 | (void) FunTmpl; | 
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3211 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; | 
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3212 | ActiveInst.Entity = New; | 
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3213 | } | 
|  | 3214 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 |  | 
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3216 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); | 
|  | 3217 | assert(Proto && "Function template without prototype?"); | 
|  | 3218 |  | 
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3219 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { | 
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3220 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); | 
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3221 |  | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3222 | // DR1330: In C++11, defer instantiation of a non-trivial | 
|  | 3223 | // exception specification. | 
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3224 | if (SemaRef.getLangOpts().CPlusPlus11 && | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3225 | EPI.ExceptionSpecType != EST_None && | 
|  | 3226 | EPI.ExceptionSpecType != EST_DynamicNone && | 
|  | 3227 | EPI.ExceptionSpecType != EST_BasicNoexcept) { | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3228 | FunctionDecl *ExceptionSpecTemplate = Tmpl; | 
|  | 3229 | if (EPI.ExceptionSpecType == EST_Uninstantiated) | 
|  | 3230 | ExceptionSpecTemplate = EPI.ExceptionSpecTemplate; | 
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3231 | ExceptionSpecificationType NewEST = EST_Uninstantiated; | 
|  | 3232 | if (EPI.ExceptionSpecType == EST_Unevaluated) | 
|  | 3233 | NewEST = EST_Unevaluated; | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3234 |  | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3235 | // Mark the function has having an uninstantiated exception specification. | 
|  | 3236 | const FunctionProtoType *NewProto | 
|  | 3237 | = New->getType()->getAs<FunctionProtoType>(); | 
|  | 3238 | assert(NewProto && "Template instantiation without function prototype?"); | 
|  | 3239 | EPI = NewProto->getExtProtoInfo(); | 
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3240 | EPI.ExceptionSpecType = NewEST; | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3241 | EPI.ExceptionSpecDecl = New; | 
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3242 | EPI.ExceptionSpecTemplate = ExceptionSpecTemplate; | 
| Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 3243 | New->setType(SemaRef.Context.getFunctionType( | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3244 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3245 | } else { | 
|  | 3246 | ::InstantiateExceptionSpec(SemaRef, New, Proto, TemplateArgs); | 
|  | 3247 | } | 
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3248 | } | 
|  | 3249 |  | 
| Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3250 | // Get the definition. Leaves the variable unchanged if undefined. | 
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3251 | const FunctionDecl *Definition = Tmpl; | 
| Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 3252 | Tmpl->isDefined(Definition); | 
|  | 3253 |  | 
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 3254 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, | 
|  | 3255 | LateAttrs, StartingScope); | 
| Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 3256 |  | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3257 | return false; | 
|  | 3258 | } | 
|  | 3259 |  | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3260 | /// \brief Initializes common fields of an instantiated method | 
|  | 3261 | /// declaration (New) from the corresponding fields of its template | 
|  | 3262 | /// (Tmpl). | 
|  | 3263 | /// | 
|  | 3264 | /// \returns true if there was an error | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | bool | 
|  | 3266 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3267 | CXXMethodDecl *Tmpl) { | 
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3268 | if (InitFunctionInstantiation(New, Tmpl)) | 
|  | 3269 | return true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 |  | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3271 | New->setAccess(Tmpl->getAccess()); | 
| Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 3272 | if (Tmpl->isVirtualAsWritten()) | 
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 3273 | New->setVirtualAsWritten(true); | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3274 |  | 
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3275 | // FIXME: New needs a pointer to Tmpl | 
|  | 3276 | return false; | 
|  | 3277 | } | 
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3278 |  | 
|  | 3279 | /// \brief Instantiate the definition of the given function from its | 
|  | 3280 | /// template. | 
|  | 3281 | /// | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3282 | /// \param PointOfInstantiation the point at which the instantiation was | 
|  | 3283 | /// required. Note that this is not precisely a "point of instantiation" | 
|  | 3284 | /// for the function, but it's close. | 
|  | 3285 | /// | 
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3286 | /// \param Function the already-instantiated declaration of a | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3287 | /// function template specialization or member function of a class template | 
|  | 3288 | /// specialization. | 
|  | 3289 | /// | 
|  | 3290 | /// \param Recursive if true, recursively instantiates any functions that | 
|  | 3291 | /// are required by this instantiation. | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3292 | /// | 
|  | 3293 | /// \param DefinitionRequired if true, then we are performing an explicit | 
|  | 3294 | /// instantiation where the body of the function is required. Complain if | 
|  | 3295 | /// there is no such body. | 
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3296 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3297 | FunctionDecl *Function, | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3298 | bool Recursive, | 
|  | 3299 | bool DefinitionRequired) { | 
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3300 | if (Function->isInvalidDecl() || Function->isDefined()) | 
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3301 | return; | 
|  | 3302 |  | 
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3303 | // Never instantiate an explicit specialization except if it is a class scope | 
|  | 3304 | // explicit specialization. | 
|  | 3305 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && | 
|  | 3306 | !Function->getClassScopeSpecializationPattern()) | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3307 | return; | 
| Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3308 |  | 
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3309 | // Find the function body that we'll be substituting. | 
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3310 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); | 
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3311 | assert(PatternDecl && "instantiating a non-template"); | 
|  | 3312 |  | 
|  | 3313 | Stmt *Pattern = PatternDecl->getBody(PatternDecl); | 
|  | 3314 | assert(PatternDecl && "template definition is not a template"); | 
|  | 3315 | if (!Pattern) { | 
|  | 3316 | // Try to find a defaulted definition | 
|  | 3317 | PatternDecl->isDefined(PatternDecl); | 
| Alexis Hunt | 92a0adf | 2011-05-25 22:02:25 +0000 | [diff] [blame] | 3318 | } | 
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3319 | assert(PatternDecl && "template definition is not a template"); | 
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3320 |  | 
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3321 | // Postpone late parsed template instantiations. | 
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3322 | if (PatternDecl->isLateTemplateParsed() && | 
| Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 3323 | !LateTemplateParser) { | 
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3324 | PendingInstantiations.push_back( | 
|  | 3325 | std::make_pair(Function, PointOfInstantiation)); | 
|  | 3326 | return; | 
|  | 3327 | } | 
|  | 3328 |  | 
| David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 3329 | // Call the LateTemplateParser callback if there is a need to late parse | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3330 | // a templated function definition. | 
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3331 | if (!Pattern && PatternDecl->isLateTemplateParsed() && | 
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3332 | LateTemplateParser) { | 
| Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 3333 | // FIXME: Optimize to allow individual templates to be deserialized. | 
|  | 3334 | if (PatternDecl->isFromASTFile()) | 
|  | 3335 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); | 
|  | 3336 |  | 
|  | 3337 | LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl); | 
|  | 3338 | assert(LPT && "missing LateParsedTemplate"); | 
|  | 3339 | LateTemplateParser(OpaqueParser, *LPT); | 
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 3340 | Pattern = PatternDecl->getBody(PatternDecl); | 
|  | 3341 | } | 
|  | 3342 |  | 
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 3343 | if (!Pattern && !PatternDecl->isDefaulted()) { | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3344 | if (DefinitionRequired) { | 
|  | 3345 | if (Function->getPrimaryTemplate()) | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3346 | Diag(PointOfInstantiation, | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3347 | diag::err_explicit_instantiation_undefined_func_template) | 
|  | 3348 | << Function->getPrimaryTemplate(); | 
|  | 3349 | else | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3350 | Diag(PointOfInstantiation, | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3351 | diag::err_explicit_instantiation_undefined_member) | 
|  | 3352 | << 1 << Function->getDeclName() << Function->getDeclContext(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3353 |  | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3354 | if (PatternDecl) | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3355 | Diag(PatternDecl->getLocation(), | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3356 | diag::note_explicit_instantiation_here); | 
| Douglas Gregor | fd7224f | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 3357 | Function->setInvalidDecl(); | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3358 | } else if (Function->getTemplateSpecializationKind() | 
|  | 3359 | == TSK_ExplicitInstantiationDefinition) { | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3360 | PendingInstantiations.push_back( | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3361 | std::make_pair(Function, PointOfInstantiation)); | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3362 | } | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3363 |  | 
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3364 | return; | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3365 | } | 
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 3366 |  | 
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3367 | // C++1y [temp.explicit]p10: | 
|  | 3368 | //   Except for inline functions, declarations with types deduced from their | 
|  | 3369 | //   initializer or return value, and class template specializations, other | 
|  | 3370 | //   explicit instantiation declarations have the effect of suppressing the | 
|  | 3371 | //   implicit instantiation of the entity to which they refer. | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3372 | if (Function->getTemplateSpecializationKind() == | 
|  | 3373 | TSK_ExplicitInstantiationDeclaration && | 
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3374 | !PatternDecl->isInlined() && | 
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3375 | !PatternDecl->getReturnType()->getContainedAutoType()) | 
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3376 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 |  | 
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3378 | if (PatternDecl->isInlined()) | 
|  | 3379 | Function->setImplicitlyInline(); | 
|  | 3380 |  | 
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 3381 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3382 | if (Inst.isInvalid()) | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3383 | return; | 
|  | 3384 |  | 
| Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 3385 | // Copy the inner loc start from the pattern. | 
|  | 3386 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); | 
|  | 3387 |  | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3388 | // If we're performing recursive template instantiation, create our own | 
|  | 3389 | // queue of pending implicit instantiations that we will instantiate later, | 
|  | 3390 | // while we're still within our own instantiation context. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3391 | SmallVector<VTableUse, 16> SavedVTableUses; | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3392 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; | 
| David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 3393 | SavePendingLocalImplicitInstantiationsRAII | 
|  | 3394 | SavedPendingLocalImplicitInstantiations(*this); | 
| Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3395 | if (Recursive) { | 
|  | 3396 | VTableUses.swap(SavedVTableUses); | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3397 | PendingInstantiations.swap(SavedPendingInstantiations); | 
| Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3398 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 |  | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3400 | EnterExpressionEvaluationContext EvalContext(*this, | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3401 | Sema::PotentiallyEvaluated); | 
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 3402 |  | 
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 3403 | // Introduce a new scope where local variable instantiations will be | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3404 | // recorded, unless we're actually a member function within a local | 
|  | 3405 | // class, in which case we need to merge our results with the parent | 
|  | 3406 | // scope (of the enclosing function). | 
|  | 3407 | bool MergeWithParentScope = false; | 
|  | 3408 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) | 
|  | 3409 | MergeWithParentScope = Rec->isLocalClass(); | 
|  | 3410 |  | 
|  | 3411 | LocalInstantiationScope Scope(*this, MergeWithParentScope); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 |  | 
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3413 | if (PatternDecl->isDefaulted()) | 
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3414 | SetDeclDefaulted(Function, PatternDecl->getLocation()); | 
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3415 | else { | 
|  | 3416 | ActOnStartOfFunctionDef(0, Function); | 
|  | 3417 |  | 
|  | 3418 | // Enter the scope of this instantiation. We don't use | 
|  | 3419 | // PushDeclContext because we don't have a scope. | 
|  | 3420 | Sema::ContextRAII savedContext(*this, Function); | 
|  | 3421 |  | 
|  | 3422 | MultiLevelTemplateArgumentList TemplateArgs = | 
|  | 3423 | getTemplateInstantiationArgs(Function, 0, false, PatternDecl); | 
|  | 3424 |  | 
|  | 3425 | addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, | 
|  | 3426 | TemplateArgs); | 
|  | 3427 |  | 
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3428 | // If this is a constructor, instantiate the member initializers. | 
|  | 3429 | if (const CXXConstructorDecl *Ctor = | 
|  | 3430 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { | 
|  | 3431 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, | 
|  | 3432 | TemplateArgs); | 
|  | 3433 | } | 
|  | 3434 |  | 
|  | 3435 | // Instantiate the function body. | 
|  | 3436 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); | 
|  | 3437 |  | 
|  | 3438 | if (Body.isInvalid()) | 
|  | 3439 | Function->setInvalidDecl(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3440 |  | 
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 3441 | ActOnFinishFunctionBody(Function, Body.get(), | 
|  | 3442 | /*IsInstantiation=*/true); | 
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 3443 |  | 
|  | 3444 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); | 
|  | 3445 |  | 
|  | 3446 | savedContext.pop(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | } | 
|  | 3448 |  | 
| Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 3449 | DeclGroupRef DG(Function); | 
|  | 3450 | Consumer.HandleTopLevelDecl(DG); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 |  | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3452 | // This class may have local implicit instantiations that need to be | 
|  | 3453 | // instantiation within this scope. | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3454 | PerformPendingInstantiations(/*LocalOnly=*/true); | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3455 | Scope.Exit(); | 
|  | 3456 |  | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3457 | if (Recursive) { | 
| Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3458 | // Define any pending vtables. | 
|  | 3459 | DefineUsedVTables(); | 
|  | 3460 |  | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3461 | // Instantiate any pending implicit instantiations found during the | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | // instantiation of this template. | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3463 | PerformPendingInstantiations(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 |  | 
| Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3465 | // Restore the set of pending vtables. | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3466 | assert(VTableUses.empty() && | 
|  | 3467 | "VTableUses should be empty before it is discarded."); | 
| Nick Lewycky | ef4f456 | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 3468 | VTableUses.swap(SavedVTableUses); | 
|  | 3469 |  | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3470 | // Restore the set of pending implicit instantiations. | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3471 | assert(PendingInstantiations.empty() && | 
|  | 3472 | "PendingInstantiations should be empty before it is discarded."); | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3473 | PendingInstantiations.swap(SavedPendingInstantiations); | 
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 3474 | } | 
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3475 | } | 
|  | 3476 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3477 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( | 
|  | 3478 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, | 
|  | 3479 | const TemplateArgumentList &TemplateArgList, | 
|  | 3480 | const TemplateArgumentListInfo &TemplateArgsInfo, | 
|  | 3481 | SmallVectorImpl<TemplateArgument> &Converted, | 
|  | 3482 | SourceLocation PointOfInstantiation, void *InsertPos, | 
|  | 3483 | LateInstantiatedAttrVec *LateAttrs, | 
|  | 3484 | LocalInstantiationScope *StartingScope) { | 
|  | 3485 | if (FromVar->isInvalidDecl()) | 
|  | 3486 | return 0; | 
|  | 3487 |  | 
|  | 3488 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3489 | if (Inst.isInvalid()) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3490 | return 0; | 
|  | 3491 |  | 
|  | 3492 | MultiLevelTemplateArgumentList TemplateArgLists; | 
|  | 3493 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); | 
|  | 3494 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3495 | // Instantiate the first declaration of the variable template: for a partial | 
|  | 3496 | // specialization of a static data member template, the first declaration may | 
|  | 3497 | // or may not be the declaration in the class; if it's in the class, we want | 
|  | 3498 | // to instantiate a member in the class (a declaration), and if it's outside, | 
|  | 3499 | // we want to instantiate a definition. | 
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 3500 | // | 
|  | 3501 | // If we're instantiating an explicitly-specialized member template or member | 
|  | 3502 | // partial specialization, don't do this. The member specialization completely | 
|  | 3503 | // replaces the original declaration in this case. | 
|  | 3504 | bool IsMemberSpec = false; | 
|  | 3505 | if (VarTemplatePartialSpecializationDecl *PartialSpec = | 
|  | 3506 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) | 
|  | 3507 | IsMemberSpec = PartialSpec->isMemberSpecialization(); | 
|  | 3508 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) | 
|  | 3509 | IsMemberSpec = FromTemplate->isMemberSpecialization(); | 
|  | 3510 | if (!IsMemberSpec) | 
|  | 3511 | FromVar = FromVar->getFirstDecl(); | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3512 |  | 
| Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 3513 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); | 
|  | 3514 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), | 
|  | 3515 | MultiLevelList); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3516 |  | 
|  | 3517 | // TODO: Set LateAttrs and StartingScope ... | 
|  | 3518 |  | 
|  | 3519 | return cast_or_null<VarTemplateSpecializationDecl>( | 
|  | 3520 | Instantiator.VisitVarTemplateSpecializationDecl( | 
|  | 3521 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); | 
|  | 3522 | } | 
|  | 3523 |  | 
|  | 3524 | /// \brief Instantiates a variable template specialization by completing it | 
|  | 3525 | /// with appropriate type information and initializer. | 
|  | 3526 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( | 
|  | 3527 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, | 
|  | 3528 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
|  | 3529 |  | 
|  | 3530 | // Do substitution on the type of the declaration | 
|  | 3531 | TypeSourceInfo *DI = | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3532 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3533 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); | 
|  | 3534 | if (!DI) | 
|  | 3535 | return 0; | 
|  | 3536 |  | 
|  | 3537 | // Update the type of this variable template specialization. | 
|  | 3538 | VarSpec->setType(DI->getType()); | 
|  | 3539 |  | 
|  | 3540 | // Instantiate the initializer. | 
|  | 3541 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); | 
|  | 3542 |  | 
|  | 3543 | return VarSpec; | 
|  | 3544 | } | 
|  | 3545 |  | 
|  | 3546 | /// BuildVariableInstantiation - Used after a new variable has been created. | 
|  | 3547 | /// Sets basic variable data and decides whether to postpone the | 
|  | 3548 | /// variable instantiation. | 
|  | 3549 | void Sema::BuildVariableInstantiation( | 
|  | 3550 | VarDecl *NewVar, VarDecl *OldVar, | 
|  | 3551 | const MultiLevelTemplateArgumentList &TemplateArgs, | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3552 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, | 
|  | 3553 | LocalInstantiationScope *StartingScope, | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3554 | bool InstantiatingVarTemplate) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3555 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3556 | // If we are instantiating a local extern declaration, the | 
|  | 3557 | // instantiation belongs lexically to the containing function. | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3558 | // If we are instantiating a static data member defined | 
|  | 3559 | // out-of-line, the instantiation will have the same lexical | 
|  | 3560 | // context (which will be a namespace scope) as the template. | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3561 | if (OldVar->isLocalExternDecl()) { | 
|  | 3562 | NewVar->setLocalExternDecl(); | 
|  | 3563 | NewVar->setLexicalDeclContext(Owner); | 
|  | 3564 | } else if (OldVar->isOutOfLine()) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3565 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); | 
|  | 3566 | NewVar->setTSCSpec(OldVar->getTSCSpec()); | 
|  | 3567 | NewVar->setInitStyle(OldVar->getInitStyle()); | 
|  | 3568 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); | 
|  | 3569 | NewVar->setConstexpr(OldVar->isConstexpr()); | 
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 3570 | NewVar->setInitCapture(OldVar->isInitCapture()); | 
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3571 | NewVar->setPreviousDeclInSameBlockScope( | 
|  | 3572 | OldVar->isPreviousDeclInSameBlockScope()); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3573 | NewVar->setAccess(OldVar->getAccess()); | 
|  | 3574 |  | 
| Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 3575 | if (!OldVar->isStaticDataMember()) { | 
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 3576 | if (OldVar->isUsed(false)) | 
|  | 3577 | NewVar->setIsUsed(); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3578 | NewVar->setReferenced(OldVar->isReferenced()); | 
|  | 3579 | } | 
|  | 3580 |  | 
| David Majnemer | 50ce835 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3581 | // See if the old variable had a type-specifier that defined an anonymous tag. | 
|  | 3582 | // If it did, mark the new variable as being the declarator for the new | 
|  | 3583 | // anonymous tag. | 
|  | 3584 | if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) { | 
|  | 3585 | TagDecl *OldTag = OldTagType->getDecl(); | 
|  | 3586 | if (OldTag->getDeclaratorForAnonDecl() == OldVar) { | 
|  | 3587 | TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl(); | 
|  | 3588 | assert(!NewTag->hasNameForLinkage() && | 
|  | 3589 | !NewTag->hasDeclaratorForAnonDecl()); | 
|  | 3590 | NewTag->setDeclaratorForAnonDecl(NewVar); | 
|  | 3591 | } | 
|  | 3592 | } | 
|  | 3593 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3594 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); | 
|  | 3595 |  | 
|  | 3596 | if (NewVar->hasAttrs()) | 
|  | 3597 | CheckAlignasUnderalignment(NewVar); | 
|  | 3598 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3599 | LookupResult Previous( | 
|  | 3600 | *this, NewVar->getDeclName(), NewVar->getLocation(), | 
|  | 3601 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage | 
|  | 3602 | : Sema::LookupOrdinaryName, | 
|  | 3603 | Sema::ForRedeclaration); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3604 |  | 
| Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 3605 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && | 
|  | 3606 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || | 
|  | 3607 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { | 
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 3608 | // We have a previous declaration. Use that one, so we merge with the | 
|  | 3609 | // right type. | 
|  | 3610 | if (NamedDecl *NewPrev = FindInstantiatedDecl( | 
|  | 3611 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) | 
|  | 3612 | Previous.addDecl(NewPrev); | 
|  | 3613 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && | 
|  | 3614 | OldVar->hasLinkage()) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3615 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3616 | CheckVariableDeclaration(NewVar, Previous); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3617 |  | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3618 | if (!InstantiatingVarTemplate) { | 
|  | 3619 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); | 
|  | 3620 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3621 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); | 
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3622 | } | 
|  | 3623 |  | 
|  | 3624 | if (!OldVar->isOutOfLine()) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3625 | if (NewVar->getDeclContext()->isFunctionOrMethod()) | 
|  | 3626 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); | 
|  | 3627 | } | 
|  | 3628 |  | 
|  | 3629 | // Link instantiations of static data members back to the template from | 
|  | 3630 | // which they were instantiated. | 
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 3631 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3632 | NewVar->setInstantiationOfStaticDataMember(OldVar, | 
|  | 3633 | TSK_ImplicitInstantiation); | 
|  | 3634 |  | 
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3635 | // Forward the mangling number from the template to the instantiated decl. | 
|  | 3636 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); | 
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 3637 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); | 
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3638 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3639 | // Delay instantiation of the initializer for variable templates until a | 
|  | 3640 | // definition of the variable is needed. | 
|  | 3641 | if (!isa<VarTemplateSpecializationDecl>(NewVar) && !InstantiatingVarTemplate) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3642 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); | 
|  | 3643 |  | 
|  | 3644 | // Diagnose unused local variables with dependent types, where the diagnostic | 
|  | 3645 | // will have been deferred. | 
|  | 3646 | if (!NewVar->isInvalidDecl() && | 
|  | 3647 | NewVar->getDeclContext()->isFunctionOrMethod() && !NewVar->isUsed() && | 
|  | 3648 | OldVar->getType()->isDependentType()) | 
|  | 3649 | DiagnoseUnusedDecl(NewVar); | 
|  | 3650 | } | 
|  | 3651 |  | 
|  | 3652 | /// \brief Instantiate the initializer of a variable. | 
|  | 3653 | void Sema::InstantiateVariableInitializer( | 
|  | 3654 | VarDecl *Var, VarDecl *OldVar, | 
|  | 3655 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
|  | 3656 |  | 
|  | 3657 | if (Var->getAnyInitializer()) | 
|  | 3658 | // We already have an initializer in the class. | 
|  | 3659 | return; | 
|  | 3660 |  | 
|  | 3661 | if (OldVar->getInit()) { | 
|  | 3662 | if (Var->isStaticDataMember() && !OldVar->isOutOfLine()) | 
|  | 3663 | PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar); | 
|  | 3664 | else | 
|  | 3665 | PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar); | 
|  | 3666 |  | 
|  | 3667 | // Instantiate the initializer. | 
|  | 3668 | ExprResult Init = | 
|  | 3669 | SubstInitializer(OldVar->getInit(), TemplateArgs, | 
|  | 3670 | OldVar->getInitStyle() == VarDecl::CallInit); | 
|  | 3671 | if (!Init.isInvalid()) { | 
|  | 3672 | bool TypeMayContainAuto = true; | 
|  | 3673 | if (Init.get()) { | 
|  | 3674 | bool DirectInit = OldVar->isDirectInit(); | 
|  | 3675 | AddInitializerToDecl(Var, Init.take(), DirectInit, TypeMayContainAuto); | 
|  | 3676 | } else | 
|  | 3677 | ActOnUninitializedDecl(Var, TypeMayContainAuto); | 
|  | 3678 | } else { | 
|  | 3679 | // FIXME: Not too happy about invalidating the declaration | 
|  | 3680 | // because of a bogus initializer. | 
|  | 3681 | Var->setInvalidDecl(); | 
|  | 3682 | } | 
|  | 3683 |  | 
|  | 3684 | PopExpressionEvaluationContext(); | 
|  | 3685 | } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) && | 
|  | 3686 | !Var->isCXXForRangeDecl()) | 
|  | 3687 | ActOnUninitializedDecl(Var, false); | 
|  | 3688 | } | 
|  | 3689 |  | 
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3690 | /// \brief Instantiate the definition of the given variable from its | 
|  | 3691 | /// template. | 
|  | 3692 | /// | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3693 | /// \param PointOfInstantiation the point at which the instantiation was | 
|  | 3694 | /// required. Note that this is not precisely a "point of instantiation" | 
|  | 3695 | /// for the function, but it's close. | 
|  | 3696 | /// | 
|  | 3697 | /// \param Var the already-instantiated declaration of a static member | 
|  | 3698 | /// variable of a class template specialization. | 
|  | 3699 | /// | 
|  | 3700 | /// \param Recursive if true, recursively instantiates any functions that | 
|  | 3701 | /// are required by this instantiation. | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3702 | /// | 
|  | 3703 | /// \param DefinitionRequired if true, then we are performing an explicit | 
|  | 3704 | /// instantiation where an out-of-line definition of the member variable | 
|  | 3705 | /// is required. Complain if there is no such definition. | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3706 | void Sema::InstantiateStaticDataMemberDefinition( | 
|  | 3707 | SourceLocation PointOfInstantiation, | 
|  | 3708 | VarDecl *Var, | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3709 | bool Recursive, | 
|  | 3710 | bool DefinitionRequired) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3711 | InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive, | 
|  | 3712 | DefinitionRequired); | 
|  | 3713 | } | 
|  | 3714 |  | 
|  | 3715 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, | 
|  | 3716 | VarDecl *Var, bool Recursive, | 
|  | 3717 | bool DefinitionRequired) { | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3718 | if (Var->isInvalidDecl()) | 
|  | 3719 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3721 | VarTemplateSpecializationDecl *VarSpec = | 
|  | 3722 | dyn_cast<VarTemplateSpecializationDecl>(Var); | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3723 | VarDecl *PatternDecl = 0, *Def = 0; | 
|  | 3724 | MultiLevelTemplateArgumentList TemplateArgs = | 
|  | 3725 | getTemplateInstantiationArgs(Var); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3727 | if (VarSpec) { | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3728 | // If this is a variable template specialization, make sure that it is | 
|  | 3729 | // non-dependent, then find its instantiation pattern. | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3730 | bool InstantiationDependent = false; | 
|  | 3731 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( | 
|  | 3732 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && | 
|  | 3733 | "Only instantiate variable template specializations that are " | 
|  | 3734 | "not type-dependent"); | 
| Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 3735 | (void)InstantiationDependent; | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3736 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3737 | // Find the variable initialization that we'll be substituting. If the | 
|  | 3738 | // pattern was instantiated from a member template, look back further to | 
|  | 3739 | // find the real pattern. | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3740 | assert(VarSpec->getSpecializedTemplate() && | 
|  | 3741 | "Specialization without specialized template?"); | 
|  | 3742 | llvm::PointerUnion<VarTemplateDecl *, | 
|  | 3743 | VarTemplatePartialSpecializationDecl *> PatternPtr = | 
|  | 3744 | VarSpec->getSpecializedTemplateOrPartial(); | 
| Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3745 | if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) { | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3746 | VarTemplatePartialSpecializationDecl *Tmpl = | 
|  | 3747 | PatternPtr.get<VarTemplatePartialSpecializationDecl *>(); | 
|  | 3748 | while (VarTemplatePartialSpecializationDecl *From = | 
|  | 3749 | Tmpl->getInstantiatedFromMember()) { | 
|  | 3750 | if (Tmpl->isMemberSpecialization()) | 
|  | 3751 | break; | 
| Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3752 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3753 | Tmpl = From; | 
|  | 3754 | } | 
|  | 3755 | PatternDecl = Tmpl; | 
| Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3756 | } else { | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3757 | VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>(); | 
|  | 3758 | while (VarTemplateDecl *From = | 
|  | 3759 | Tmpl->getInstantiatedFromMemberTemplate()) { | 
|  | 3760 | if (Tmpl->isMemberSpecialization()) | 
|  | 3761 | break; | 
| Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3762 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3763 | Tmpl = From; | 
|  | 3764 | } | 
|  | 3765 | PatternDecl = Tmpl->getTemplatedDecl(); | 
| Larisse Voufo | a11bd8a | 2013-08-13 02:02:26 +0000 | [diff] [blame] | 3766 | } | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3767 |  | 
|  | 3768 | // If this is a static data member template, there might be an | 
|  | 3769 | // uninstantiated initializer on the declaration. If so, instantiate | 
|  | 3770 | // it now. | 
|  | 3771 | if (PatternDecl->isStaticDataMember() && | 
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3772 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3773 | !Var->hasInit()) { | 
|  | 3774 | // FIXME: Factor out the duplicated instantiation context setup/tear down | 
|  | 3775 | // code here. | 
|  | 3776 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3777 | if (Inst.isInvalid()) | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3778 | return; | 
|  | 3779 |  | 
|  | 3780 | // If we're performing recursive template instantiation, create our own | 
|  | 3781 | // queue of pending implicit instantiations that we will instantiate | 
|  | 3782 | // later, while we're still within our own instantiation context. | 
|  | 3783 | SmallVector<VTableUse, 16> SavedVTableUses; | 
|  | 3784 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; | 
|  | 3785 | if (Recursive) { | 
|  | 3786 | VTableUses.swap(SavedVTableUses); | 
|  | 3787 | PendingInstantiations.swap(SavedPendingInstantiations); | 
|  | 3788 | } | 
|  | 3789 |  | 
|  | 3790 | LocalInstantiationScope Local(*this); | 
|  | 3791 |  | 
|  | 3792 | // Enter the scope of this instantiation. We don't use | 
|  | 3793 | // PushDeclContext because we don't have a scope. | 
|  | 3794 | ContextRAII PreviousContext(*this, Var->getDeclContext()); | 
|  | 3795 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); | 
|  | 3796 | PreviousContext.pop(); | 
|  | 3797 |  | 
|  | 3798 | // FIXME: Need to inform the ASTConsumer that we instantiated the | 
|  | 3799 | // initializer? | 
|  | 3800 |  | 
|  | 3801 | // This variable may have local implicit instantiations that need to be | 
|  | 3802 | // instantiated within this scope. | 
|  | 3803 | PerformPendingInstantiations(/*LocalOnly=*/true); | 
|  | 3804 |  | 
|  | 3805 | Local.Exit(); | 
|  | 3806 |  | 
|  | 3807 | if (Recursive) { | 
|  | 3808 | // Define any newly required vtables. | 
|  | 3809 | DefineUsedVTables(); | 
|  | 3810 |  | 
|  | 3811 | // Instantiate any pending implicit instantiations found during the | 
|  | 3812 | // instantiation of this template. | 
|  | 3813 | PerformPendingInstantiations(); | 
|  | 3814 |  | 
|  | 3815 | // Restore the set of pending vtables. | 
|  | 3816 | assert(VTableUses.empty() && | 
|  | 3817 | "VTableUses should be empty before it is discarded."); | 
|  | 3818 | VTableUses.swap(SavedVTableUses); | 
|  | 3819 |  | 
|  | 3820 | // Restore the set of pending implicit instantiations. | 
|  | 3821 | assert(PendingInstantiations.empty() && | 
|  | 3822 | "PendingInstantiations should be empty before it is discarded."); | 
|  | 3823 | PendingInstantiations.swap(SavedPendingInstantiations); | 
|  | 3824 | } | 
|  | 3825 | } | 
|  | 3826 |  | 
|  | 3827 | // Find actual definition | 
|  | 3828 | Def = PatternDecl->getDefinition(getASTContext()); | 
|  | 3829 | } else { | 
|  | 3830 | // If this is a static data member, find its out-of-line definition. | 
|  | 3831 | assert(Var->isStaticDataMember() && "not a static data member?"); | 
|  | 3832 | PatternDecl = Var->getInstantiatedFromStaticDataMember(); | 
|  | 3833 |  | 
|  | 3834 | assert(PatternDecl && "data member was not instantiated from a template?"); | 
|  | 3835 | assert(PatternDecl->isStaticDataMember() && "not a static data member?"); | 
|  | 3836 | Def = PatternDecl->getOutOfLineDefinition(); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3837 | } | 
|  | 3838 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3839 | // If we don't have a definition of the variable template, we won't perform | 
|  | 3840 | // any instantiation. Rather, we rely on the user to instantiate this | 
|  | 3841 | // definition (or provide a specialization for it) in another translation | 
|  | 3842 | // unit. | 
|  | 3843 | if (!Def) { | 
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 3844 | if (DefinitionRequired) { | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3845 | if (VarSpec) | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3846 | Diag(PointOfInstantiation, | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3847 | diag::err_explicit_instantiation_undefined_var_template) << Var; | 
|  | 3848 | else | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3849 | Diag(PointOfInstantiation, | 
|  | 3850 | diag::err_explicit_instantiation_undefined_member) | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3851 | << 2 << Var->getDeclName() << Var->getDeclContext(); | 
|  | 3852 | Diag(PatternDecl->getLocation(), | 
|  | 3853 | diag::note_explicit_instantiation_here); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3854 | if (VarSpec) | 
|  | 3855 | Var->setInvalidDecl(); | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3856 | } else if (Var->getTemplateSpecializationKind() | 
|  | 3857 | == TSK_ExplicitInstantiationDefinition) { | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3858 | PendingInstantiations.push_back( | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3859 | std::make_pair(Var, PointOfInstantiation)); | 
|  | 3860 | } | 
|  | 3861 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3862 | return; | 
|  | 3863 | } | 
|  | 3864 |  | 
| Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3865 | TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind(); | 
|  | 3866 |  | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3867 | // Never instantiate an explicit specialization. | 
| Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3868 | if (TSK == TSK_ExplicitSpecialization) | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3869 | return; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3870 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3871 | // C++11 [temp.explicit]p10: | 
|  | 3872 | //   Except for inline functions, [...] explicit instantiation declarations | 
|  | 3873 | //   have the effect of suppressing the implicit instantiation of the entity | 
|  | 3874 | //   to which they refer. | 
| Rafael Espindola | 189fa74 | 2012-03-05 10:54:55 +0000 | [diff] [blame] | 3875 | if (TSK == TSK_ExplicitInstantiationDeclaration) | 
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3876 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 |  | 
| Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3878 | // Make sure to pass the instantiated variable to the consumer at the end. | 
|  | 3879 | struct PassToConsumerRAII { | 
|  | 3880 | ASTConsumer &Consumer; | 
|  | 3881 | VarDecl *Var; | 
|  | 3882 |  | 
|  | 3883 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) | 
|  | 3884 | : Consumer(Consumer), Var(Var) { } | 
|  | 3885 |  | 
|  | 3886 | ~PassToConsumerRAII() { | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3887 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); | 
| Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 3888 | } | 
|  | 3889 | } PassToConsumerRAII(Consumer, Var); | 
| Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 3890 |  | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3891 | // If we already have a definition, we're done. | 
|  | 3892 | if (VarDecl *Def = Var->getDefinition()) { | 
|  | 3893 | // We may be explicitly instantiating something we've already implicitly | 
|  | 3894 | // instantiated. | 
|  | 3895 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), | 
|  | 3896 | PointOfInstantiation); | 
|  | 3897 | return; | 
| Nick Lewycky | a995a47 | 2012-04-04 02:38:36 +0000 | [diff] [blame] | 3898 | } | 
| Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 3899 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3900 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); | 
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3901 | if (Inst.isInvalid()) | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3902 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3903 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3904 | // If we're performing recursive template instantiation, create our own | 
|  | 3905 | // queue of pending implicit instantiations that we will instantiate later, | 
|  | 3906 | // while we're still within our own instantiation context. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3907 | SmallVector<VTableUse, 16> SavedVTableUses; | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3908 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; | 
| David Majnemer | fd6c685f | 2013-11-27 22:57:44 +0000 | [diff] [blame] | 3909 | SavePendingLocalImplicitInstantiationsRAII | 
|  | 3910 | SavedPendingLocalImplicitInstantiations(*this); | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3911 | if (Recursive) { | 
|  | 3912 | VTableUses.swap(SavedVTableUses); | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3913 | PendingInstantiations.swap(SavedPendingInstantiations); | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3914 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3916 | // Enter the scope of this instantiation. We don't use | 
|  | 3917 | // PushDeclContext because we don't have a scope. | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3918 | ContextRAII PreviousContext(*this, Var->getDeclContext()); | 
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3919 | LocalInstantiationScope Local(*this); | 
| John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 3920 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3921 | VarDecl *OldVar = Var; | 
|  | 3922 | if (!VarSpec) | 
|  | 3923 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3924 | TemplateArgs)); | 
|  | 3925 | else if (Var->isStaticDataMember() && | 
|  | 3926 | Var->getLexicalDeclContext()->isRecord()) { | 
|  | 3927 | // We need to instantiate the definition of a static data member template, | 
|  | 3928 | // and all we have is the in-class declaration of it. Instantiate a separate | 
|  | 3929 | // declaration of the definition. | 
|  | 3930 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), | 
|  | 3931 | TemplateArgs); | 
|  | 3932 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( | 
|  | 3933 | VarSpec->getSpecializedTemplate(), Def, 0, | 
|  | 3934 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); | 
|  | 3935 | if (Var) { | 
|  | 3936 | llvm::PointerUnion<VarTemplateDecl *, | 
|  | 3937 | VarTemplatePartialSpecializationDecl *> PatternPtr = | 
|  | 3938 | VarSpec->getSpecializedTemplateOrPartial(); | 
|  | 3939 | if (VarTemplatePartialSpecializationDecl *Partial = | 
|  | 3940 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) | 
|  | 3941 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( | 
|  | 3942 | Partial, &VarSpec->getTemplateInstantiationArgs()); | 
|  | 3943 |  | 
|  | 3944 | // Merge the definition with the declaration. | 
|  | 3945 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), | 
|  | 3946 | LookupOrdinaryName, ForRedeclaration); | 
|  | 3947 | R.addDecl(OldVar); | 
|  | 3948 | MergeVarDecl(Var, R); | 
|  | 3949 |  | 
|  | 3950 | // Attach the initializer. | 
|  | 3951 | InstantiateVariableInitializer(Var, Def, TemplateArgs); | 
|  | 3952 | } | 
|  | 3953 | } else | 
|  | 3954 | // Complete the existing variable's definition with an appropriately | 
|  | 3955 | // substituted type and initializer. | 
|  | 3956 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3957 |  | 
|  | 3958 | PreviousContext.pop(); | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3959 |  | 
|  | 3960 | if (Var) { | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3961 | PassToConsumerRAII.Var = Var; | 
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3962 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), | 
|  | 3963 | OldVar->getPointOfInstantiation()); | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3964 | } | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3965 |  | 
|  | 3966 | // This variable may have local implicit instantiations that need to be | 
|  | 3967 | // instantiated within this scope. | 
|  | 3968 | PerformPendingInstantiations(/*LocalOnly=*/true); | 
|  | 3969 |  | 
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3970 | Local.Exit(); | 
|  | 3971 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3972 | if (Recursive) { | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3973 | // Define any newly required vtables. | 
|  | 3974 | DefineUsedVTables(); | 
|  | 3975 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3976 | // Instantiate any pending implicit instantiations found during the | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | // instantiation of this template. | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3978 | PerformPendingInstantiations(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 |  | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3980 | // Restore the set of pending vtables. | 
|  | 3981 | assert(VTableUses.empty() && | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3982 | "VTableUses should be empty before it is discarded."); | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3983 | VTableUses.swap(SavedVTableUses); | 
|  | 3984 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3985 | // Restore the set of pending implicit instantiations. | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3986 | assert(PendingInstantiations.empty() && | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3987 | "PendingInstantiations should be empty before it is discarded."); | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3988 | PendingInstantiations.swap(SavedPendingInstantiations); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | } | 
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 3990 | } | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3991 |  | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 3992 | void | 
|  | 3993 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, | 
|  | 3994 | const CXXConstructorDecl *Tmpl, | 
|  | 3995 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 |  | 
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 3997 | SmallVector<CXXCtorInitializer*, 4> NewInits; | 
| Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 3998 | bool AnyErrors = Tmpl->isInvalidDecl(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3999 |  | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4000 | // Instantiate all the initializers. | 
|  | 4001 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), | 
| Douglas Gregor | a3a3f6f | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 4002 | InitsEnd = Tmpl->init_end(); | 
|  | 4003 | Inits != InitsEnd; ++Inits) { | 
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 4004 | CXXCtorInitializer *Init = *Inits; | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4005 |  | 
| Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4006 | // Only instantiate written initializers, let Sema re-construct implicit | 
|  | 4007 | // ones. | 
|  | 4008 | if (!Init->isWritten()) | 
|  | 4009 | continue; | 
|  | 4010 |  | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4011 | SourceLocation EllipsisLoc; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4012 |  | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4013 | if (Init->isPackExpansion()) { | 
|  | 4014 | // This is a pack expansion. We should expand it now. | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4015 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); | 
| Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4016 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4017 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); | 
| Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4018 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4019 | bool ShouldExpand = false; | 
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4020 | bool RetainExpansion = false; | 
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4021 | Optional<unsigned> NumExpansions; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4022 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4023 | BaseTL.getSourceRange(), | 
| David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4024 | Unexpanded, | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4025 | TemplateArgs, ShouldExpand, | 
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4026 | RetainExpansion, | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4027 | NumExpansions)) { | 
|  | 4028 | AnyErrors = true; | 
|  | 4029 | New->setInvalidDecl(); | 
|  | 4030 | continue; | 
|  | 4031 | } | 
|  | 4032 | assert(ShouldExpand && "Partial instantiation of base initializer?"); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4033 |  | 
|  | 4034 | // Loop over all of the arguments in the argument pack(s), | 
| Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4035 | for (unsigned I = 0; I != *NumExpansions; ++I) { | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4036 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); | 
|  | 4037 |  | 
|  | 4038 | // Instantiate the initializer. | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4039 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, | 
|  | 4040 | /*CXXDirectInit=*/true); | 
|  | 4041 | if (TempInit.isInvalid()) { | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4042 | AnyErrors = true; | 
|  | 4043 | break; | 
|  | 4044 | } | 
|  | 4045 |  | 
|  | 4046 | // Instantiate the base type. | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4047 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4048 | TemplateArgs, | 
|  | 4049 | Init->getSourceLocation(), | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4050 | New->getDeclName()); | 
|  | 4051 | if (!BaseTInfo) { | 
|  | 4052 | AnyErrors = true; | 
|  | 4053 | break; | 
|  | 4054 | } | 
|  | 4055 |  | 
|  | 4056 | // Build the initializer. | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4057 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4058 | BaseTInfo, TempInit.take(), | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4059 | New->getParent(), | 
|  | 4060 | SourceLocation()); | 
|  | 4061 | if (NewInit.isInvalid()) { | 
|  | 4062 | AnyErrors = true; | 
|  | 4063 | break; | 
|  | 4064 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4065 |  | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4066 | NewInits.push_back(NewInit.get()); | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4067 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4068 |  | 
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4069 | continue; | 
|  | 4070 | } | 
|  | 4071 |  | 
| Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4072 | // Instantiate the initializer. | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4073 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, | 
|  | 4074 | /*CXXDirectInit=*/true); | 
|  | 4075 | if (TempInit.isInvalid()) { | 
| Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4076 | AnyErrors = true; | 
|  | 4077 | continue; | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4078 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4079 |  | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4080 | MemInitResult NewInit; | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4081 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { | 
|  | 4082 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), | 
|  | 4083 | TemplateArgs, | 
|  | 4084 | Init->getSourceLocation(), | 
|  | 4085 | New->getDeclName()); | 
|  | 4086 | if (!TInfo) { | 
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4087 | AnyErrors = true; | 
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4088 | New->setInvalidDecl(); | 
|  | 4089 | continue; | 
|  | 4090 | } | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4091 |  | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4092 | if (Init->isBaseInitializer()) | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4093 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.take(), | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4094 | New->getParent(), EllipsisLoc); | 
|  | 4095 | else | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4096 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.take(), | 
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4097 | cast<CXXRecordDecl>(CurContext->getParent())); | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4098 | } else if (Init->isMemberInitializer()) { | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4099 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4100 | Init->getMemberLocation(), | 
|  | 4101 | Init->getMember(), | 
|  | 4102 | TemplateArgs)); | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4103 | if (!Member) { | 
|  | 4104 | AnyErrors = true; | 
|  | 4105 | New->setInvalidDecl(); | 
|  | 4106 | continue; | 
|  | 4107 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 |  | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4109 | NewInit = BuildMemberInitializer(Member, TempInit.take(), | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4110 | Init->getSourceLocation()); | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4111 | } else if (Init->isIndirectMemberInitializer()) { | 
|  | 4112 | IndirectFieldDecl *IndirectMember = | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4113 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( | 
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4114 | Init->getMemberLocation(), | 
|  | 4115 | Init->getIndirectMember(), TemplateArgs)); | 
|  | 4116 |  | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4117 | if (!IndirectMember) { | 
|  | 4118 | AnyErrors = true; | 
|  | 4119 | New->setInvalidDecl(); | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4120 | continue; | 
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4121 | } | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4122 |  | 
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4123 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.take(), | 
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4124 | Init->getSourceLocation()); | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4125 | } | 
|  | 4126 |  | 
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4127 | if (NewInit.isInvalid()) { | 
|  | 4128 | AnyErrors = true; | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4129 | New->setInvalidDecl(); | 
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4130 | } else { | 
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4131 | NewInits.push_back(NewInit.get()); | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4132 | } | 
|  | 4133 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 |  | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4135 | // Assign all the initializers to the new constructor. | 
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4136 | ActOnMemInitializers(New, | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4137 | /*FIXME: ColonLoc */ | 
|  | 4138 | SourceLocation(), | 
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4139 | NewInits, | 
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4140 | AnyErrors); | 
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4141 | } | 
|  | 4142 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4143 | // TODO: this could be templated if the various decl types used the | 
|  | 4144 | // same method name. | 
|  | 4145 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, | 
|  | 4146 | ClassTemplateDecl *Instance) { | 
|  | 4147 | Pattern = Pattern->getCanonicalDecl(); | 
|  | 4148 |  | 
|  | 4149 | do { | 
|  | 4150 | Instance = Instance->getCanonicalDecl(); | 
|  | 4151 | if (Pattern == Instance) return true; | 
|  | 4152 | Instance = Instance->getInstantiatedFromMemberTemplate(); | 
|  | 4153 | } while (Instance); | 
|  | 4154 |  | 
|  | 4155 | return false; | 
|  | 4156 | } | 
|  | 4157 |  | 
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4158 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, | 
|  | 4159 | FunctionTemplateDecl *Instance) { | 
|  | 4160 | Pattern = Pattern->getCanonicalDecl(); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4161 |  | 
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4162 | do { | 
|  | 4163 | Instance = Instance->getCanonicalDecl(); | 
|  | 4164 | if (Pattern == Instance) return true; | 
|  | 4165 | Instance = Instance->getInstantiatedFromMemberTemplate(); | 
|  | 4166 | } while (Instance); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4167 |  | 
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4168 | return false; | 
|  | 4169 | } | 
|  | 4170 |  | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4171 | static bool | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4172 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, | 
|  | 4173 | ClassTemplatePartialSpecializationDecl *Instance) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4174 | Pattern | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4175 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); | 
|  | 4176 | do { | 
|  | 4177 | Instance = cast<ClassTemplatePartialSpecializationDecl>( | 
|  | 4178 | Instance->getCanonicalDecl()); | 
|  | 4179 | if (Pattern == Instance) | 
|  | 4180 | return true; | 
|  | 4181 | Instance = Instance->getInstantiatedFromMember(); | 
|  | 4182 | } while (Instance); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4183 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4184 | return false; | 
|  | 4185 | } | 
|  | 4186 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4187 | static bool isInstantiationOf(CXXRecordDecl *Pattern, | 
|  | 4188 | CXXRecordDecl *Instance) { | 
|  | 4189 | Pattern = Pattern->getCanonicalDecl(); | 
|  | 4190 |  | 
|  | 4191 | do { | 
|  | 4192 | Instance = Instance->getCanonicalDecl(); | 
|  | 4193 | if (Pattern == Instance) return true; | 
|  | 4194 | Instance = Instance->getInstantiatedFromMemberClass(); | 
|  | 4195 | } while (Instance); | 
|  | 4196 |  | 
|  | 4197 | return false; | 
|  | 4198 | } | 
|  | 4199 |  | 
|  | 4200 | static bool isInstantiationOf(FunctionDecl *Pattern, | 
|  | 4201 | FunctionDecl *Instance) { | 
|  | 4202 | Pattern = Pattern->getCanonicalDecl(); | 
|  | 4203 |  | 
|  | 4204 | do { | 
|  | 4205 | Instance = Instance->getCanonicalDecl(); | 
|  | 4206 | if (Pattern == Instance) return true; | 
|  | 4207 | Instance = Instance->getInstantiatedFromMemberFunction(); | 
|  | 4208 | } while (Instance); | 
|  | 4209 |  | 
|  | 4210 | return false; | 
|  | 4211 | } | 
|  | 4212 |  | 
|  | 4213 | static bool isInstantiationOf(EnumDecl *Pattern, | 
|  | 4214 | EnumDecl *Instance) { | 
|  | 4215 | Pattern = Pattern->getCanonicalDecl(); | 
|  | 4216 |  | 
|  | 4217 | do { | 
|  | 4218 | Instance = Instance->getCanonicalDecl(); | 
|  | 4219 | if (Pattern == Instance) return true; | 
|  | 4220 | Instance = Instance->getInstantiatedFromMemberEnum(); | 
|  | 4221 | } while (Instance); | 
|  | 4222 |  | 
|  | 4223 | return false; | 
|  | 4224 | } | 
|  | 4225 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4226 | static bool isInstantiationOf(UsingShadowDecl *Pattern, | 
|  | 4227 | UsingShadowDecl *Instance, | 
|  | 4228 | ASTContext &C) { | 
|  | 4229 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; | 
|  | 4230 | } | 
|  | 4231 |  | 
|  | 4232 | static bool isInstantiationOf(UsingDecl *Pattern, | 
|  | 4233 | UsingDecl *Instance, | 
|  | 4234 | ASTContext &C) { | 
|  | 4235 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; | 
|  | 4236 | } | 
|  | 4237 |  | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4238 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, | 
|  | 4239 | UsingDecl *Instance, | 
|  | 4240 | ASTContext &C) { | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4241 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4242 | } | 
|  | 4243 |  | 
|  | 4244 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4245 | UsingDecl *Instance, | 
|  | 4246 | ASTContext &C) { | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4247 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4248 | } | 
|  | 4249 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4250 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, | 
|  | 4251 | VarDecl *Instance) { | 
|  | 4252 | assert(Instance->isStaticDataMember()); | 
|  | 4253 |  | 
|  | 4254 | Pattern = Pattern->getCanonicalDecl(); | 
|  | 4255 |  | 
|  | 4256 | do { | 
|  | 4257 | Instance = Instance->getCanonicalDecl(); | 
|  | 4258 | if (Pattern == Instance) return true; | 
|  | 4259 | Instance = Instance->getInstantiatedFromStaticDataMember(); | 
|  | 4260 | } while (Instance); | 
|  | 4261 |  | 
|  | 4262 | return false; | 
|  | 4263 | } | 
|  | 4264 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4265 | // Other is the prospective instantiation | 
|  | 4266 | // D is the prospective pattern | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4267 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4268 | if (D->getKind() != Other->getKind()) { | 
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 4269 | if (UnresolvedUsingTypenameDecl *UUD | 
|  | 4270 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { | 
|  | 4271 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { | 
|  | 4272 | return isInstantiationOf(UUD, UD, Ctx); | 
|  | 4273 | } | 
|  | 4274 | } | 
|  | 4275 |  | 
|  | 4276 | if (UnresolvedUsingValueDecl *UUD | 
|  | 4277 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4278 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { | 
|  | 4279 | return isInstantiationOf(UUD, UD, Ctx); | 
|  | 4280 | } | 
|  | 4281 | } | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4282 |  | 
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 4283 | return false; | 
|  | 4284 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4285 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4286 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) | 
|  | 4287 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4289 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) | 
|  | 4290 | return isInstantiationOf(cast<FunctionDecl>(D), Function); | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4291 |  | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4292 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) | 
|  | 4293 | return isInstantiationOf(cast<EnumDecl>(D), Enum); | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4294 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4295 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4296 | if (Var->isStaticDataMember()) | 
|  | 4297 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); | 
|  | 4298 |  | 
|  | 4299 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) | 
|  | 4300 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); | 
| Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 4301 |  | 
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 4302 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) | 
|  | 4303 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); | 
|  | 4304 |  | 
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4305 | if (ClassTemplatePartialSpecializationDecl *PartialSpec | 
|  | 4306 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) | 
|  | 4307 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), | 
|  | 4308 | PartialSpec); | 
|  | 4309 |  | 
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4310 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { | 
|  | 4311 | if (!Field->getDeclName()) { | 
|  | 4312 | // This is an unnamed field. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4313 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == | 
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 4314 | cast<FieldDecl>(D); | 
|  | 4315 | } | 
|  | 4316 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4317 |  | 
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4318 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) | 
|  | 4319 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); | 
|  | 4320 |  | 
|  | 4321 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) | 
|  | 4322 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); | 
|  | 4323 |  | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4324 | return D->getDeclName() && isa<NamedDecl>(Other) && | 
|  | 4325 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); | 
|  | 4326 | } | 
|  | 4327 |  | 
|  | 4328 | template<typename ForwardIterator> | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4330 | NamedDecl *D, | 
|  | 4331 | ForwardIterator first, | 
|  | 4332 | ForwardIterator last) { | 
|  | 4333 | for (; first != last; ++first) | 
|  | 4334 | if (isInstantiationOf(Ctx, D, *first)) | 
|  | 4335 | return cast<NamedDecl>(*first); | 
|  | 4336 |  | 
|  | 4337 | return 0; | 
|  | 4338 | } | 
|  | 4339 |  | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4340 | /// \brief Finds the instantiation of the given declaration context | 
|  | 4341 | /// within the current instantiation. | 
|  | 4342 | /// | 
|  | 4343 | /// \returns NULL if there was an error | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4344 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4345 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4346 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4347 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); | 
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 4348 | return cast_or_null<DeclContext>(ID); | 
|  | 4349 | } else return DC; | 
|  | 4350 | } | 
|  | 4351 |  | 
| Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 4352 | /// \brief Find the instantiation of the given declaration within the | 
|  | 4353 | /// current instantiation. | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4354 | /// | 
|  | 4355 | /// This routine is intended to be used when \p D is a declaration | 
|  | 4356 | /// referenced from within a template, that needs to mapped into the | 
|  | 4357 | /// corresponding declaration within an instantiation. For example, | 
|  | 4358 | /// given: | 
|  | 4359 | /// | 
|  | 4360 | /// \code | 
|  | 4361 | /// template<typename T> | 
|  | 4362 | /// struct X { | 
|  | 4363 | ///   enum Kind { | 
|  | 4364 | ///     KnownValue = sizeof(T) | 
|  | 4365 | ///   }; | 
|  | 4366 | /// | 
|  | 4367 | ///   bool getKind() const { return KnownValue; } | 
|  | 4368 | /// }; | 
|  | 4369 | /// | 
|  | 4370 | /// template struct X<int>; | 
|  | 4371 | /// \endcode | 
|  | 4372 | /// | 
| Serge Pavlov | ed5fe90 | 2013-07-10 04:59:14 +0000 | [diff] [blame] | 4373 | /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the | 
|  | 4374 | /// \p EnumConstantDecl for \p KnownValue (which refers to | 
|  | 4375 | /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation | 
|  | 4376 | /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs | 
|  | 4377 | /// this mapping from within the instantiation of <tt>X<int></tt>. | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4378 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4379 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4380 | DeclContext *ParentDC = D->getDeclContext(); | 
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4381 | // FIXME: Parmeters of pointer to functions (y below) that are themselves | 
|  | 4382 | // parameters (p below) can have their ParentDC set to the translation-unit | 
|  | 4383 | // - thus we can not consistently check if the ParentDC of such a parameter | 
|  | 4384 | // is Dependent or/and a FunctionOrMethod. | 
|  | 4385 | // For e.g. this code, during Template argument deduction tries to | 
|  | 4386 | // find an instantiated decl for (T y) when the ParentDC for y is | 
|  | 4387 | // the translation unit. | 
|  | 4388 | //   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] | 4389 | //   float baz(float(*)()) { return 0.0; } | 
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 4390 | //   Foo(baz); | 
|  | 4391 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time | 
|  | 4392 | // it gets here, always has a FunctionOrMethod as its ParentDC?? | 
|  | 4393 | // For now: | 
|  | 4394 | //  - as long as we have a ParmVarDecl whose parent is non-dependent and | 
|  | 4395 | //    whose type is not instantiation dependent, do nothing to the decl | 
|  | 4396 | //  - otherwise find its instantiated decl. | 
|  | 4397 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && | 
|  | 4398 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) | 
|  | 4399 | return D; | 
| Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 4400 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || | 
| Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 4401 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || | 
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4402 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) || | 
|  | 4403 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { | 
| Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4404 | // D is a local of some kind. Look into the map of local | 
|  | 4405 | // declarations to their instantiations. | 
| Chris Lattner | 50c3c13 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 4406 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; | 
|  | 4407 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found | 
|  | 4408 | = CurrentInstantiationScope->findInstantiationOf(D); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4409 |  | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4410 | if (Found) { | 
|  | 4411 | if (Decl *FD = Found->dyn_cast<Decl *>()) | 
|  | 4412 | return cast<NamedDecl>(FD); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4413 |  | 
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 4414 | int PackIdx = ArgumentPackSubstitutionIndex; | 
|  | 4415 | assert(PackIdx != -1 && "found declaration pack but not pack expanding"); | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4416 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); | 
|  | 4417 | } | 
|  | 4418 |  | 
| Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 4419 | // If we're performing a partial substitution during template argument | 
|  | 4420 | // deduction, we may not have values for template parameters yet. They | 
|  | 4421 | // just map to themselves. | 
|  | 4422 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || | 
|  | 4423 | isa<TemplateTemplateParmDecl>(D)) | 
|  | 4424 | return D; | 
|  | 4425 |  | 
| Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 4426 | if (D->isInvalidDecl()) | 
|  | 4427 | return 0; | 
|  | 4428 |  | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4429 | // If we didn't find the decl, then we must have a label decl that hasn't | 
|  | 4430 | // been found yet.  Lazily instantiate it and return it now. | 
|  | 4431 | assert(isa<LabelDecl>(D)); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4432 |  | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4433 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); | 
|  | 4434 | assert(Inst && "Failed to instantiate label??"); | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4435 |  | 
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 4436 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); | 
|  | 4437 | return cast<LabelDecl>(Inst); | 
| Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 4438 | } | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4439 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4440 | // For variable template specializations, update those that are still | 
|  | 4441 | // type-dependent. | 
|  | 4442 | if (VarTemplateSpecializationDecl *VarSpec = | 
|  | 4443 | dyn_cast<VarTemplateSpecializationDecl>(D)) { | 
|  | 4444 | bool InstantiationDependent = false; | 
|  | 4445 | const TemplateArgumentListInfo &VarTemplateArgs = | 
|  | 4446 | VarSpec->getTemplateArgsInfo(); | 
|  | 4447 | if (TemplateSpecializationType::anyDependentTemplateArguments( | 
|  | 4448 | VarTemplateArgs, InstantiationDependent)) | 
|  | 4449 | D = cast<NamedDecl>( | 
|  | 4450 | SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs)); | 
|  | 4451 | return D; | 
|  | 4452 | } | 
|  | 4453 |  | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4454 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { | 
|  | 4455 | if (!Record->isDependentContext()) | 
|  | 4456 | return D; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4457 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4458 | // Determine whether this record is the "templated" declaration describing | 
|  | 4459 | // a class template or class template partial specialization. | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4460 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4461 | if (ClassTemplate) | 
|  | 4462 | ClassTemplate = ClassTemplate->getCanonicalDecl(); | 
|  | 4463 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec | 
|  | 4464 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) | 
|  | 4465 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4466 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4467 | // Walk the current context to find either the record or an instantiation of | 
|  | 4468 | // it. | 
|  | 4469 | DeclContext *DC = CurContext; | 
|  | 4470 | while (!DC->isFileContext()) { | 
|  | 4471 | // If we're performing substitution while we're inside the template | 
|  | 4472 | // definition, we'll find our own context. We're done. | 
|  | 4473 | if (DC->Equals(Record)) | 
|  | 4474 | return Record; | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4475 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4476 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { | 
|  | 4477 | // Check whether we're in the process of instantiating a class template | 
|  | 4478 | // specialization of the template we're mapping. | 
|  | 4479 | if (ClassTemplateSpecializationDecl *InstSpec | 
|  | 4480 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ | 
|  | 4481 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); | 
|  | 4482 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) | 
|  | 4483 | return InstRecord; | 
|  | 4484 | } | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4485 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4486 | // Check whether we're in the process of instantiating a member class. | 
|  | 4487 | if (isInstantiationOf(Record, InstRecord)) | 
|  | 4488 | return InstRecord; | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4489 | } | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4490 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4491 | // Move to the outer template scope. | 
|  | 4492 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { | 
|  | 4493 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ | 
|  | 4494 | DC = FD->getLexicalDeclContext(); | 
|  | 4495 | continue; | 
|  | 4496 | } | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4497 | } | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4498 |  | 
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 4499 | DC = DC->getParent(); | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4500 | } | 
| Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 4501 |  | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4502 | // Fall through to deal with other dependent record types (e.g., | 
|  | 4503 | // anonymous unions in class templates). | 
|  | 4504 | } | 
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4505 |  | 
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 4506 | if (!ParentDC->isDependentContext()) | 
|  | 4507 | return D; | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4508 |  | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4509 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4510 | if (!ParentDC) | 
| Douglas Gregor | 2ffd965 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 4511 | return 0; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4512 |  | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4513 | if (ParentDC != D->getDeclContext()) { | 
|  | 4514 | // We performed some kind of instantiation in the parent context, | 
|  | 4515 | // so now we need to look into the instantiated parent context to | 
|  | 4516 | // find the instantiation of the declaration D. | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4517 |  | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4518 | // If our context used to be dependent, we may need to instantiate | 
|  | 4519 | // it before performing lookup into that context. | 
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4520 | bool IsBeingInstantiated = false; | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4521 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4522 | if (!Spec->isDependentContext()) { | 
|  | 4523 | QualType T = Context.getTypeDeclType(Spec); | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4524 | const RecordType *Tag = T->getAs<RecordType>(); | 
|  | 4525 | assert(Tag && "type of non-dependent record is not a RecordType"); | 
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4526 | if (Tag->isBeingDefined()) | 
|  | 4527 | IsBeingInstantiated = true; | 
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4528 | if (!Tag->isBeingDefined() && | 
|  | 4529 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) | 
|  | 4530 | return 0; | 
| Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 4531 |  | 
|  | 4532 | ParentDC = Tag->getDecl(); | 
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 4533 | } | 
|  | 4534 | } | 
|  | 4535 |  | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4536 | NamedDecl *Result = 0; | 
|  | 4537 | if (D->getDeclName()) { | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4538 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); | 
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4539 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4540 | } else { | 
|  | 4541 | // Since we don't have a name for the entity we're looking for, | 
|  | 4542 | // our only option is to walk through all of the declarations to | 
|  | 4543 | // find that name. This will occur in a few cases: | 
|  | 4544 | // | 
|  | 4545 | //   - anonymous struct/union within a template | 
|  | 4546 | //   - unnamed class/struct/union/enum within a template | 
|  | 4547 | // | 
|  | 4548 | // FIXME: Find a better way to find these instantiations! | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4549 | Result = findInstantiationOf(Context, D, | 
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4550 | ParentDC->decls_begin(), | 
|  | 4551 | ParentDC->decls_end()); | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4552 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4553 |  | 
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4554 | if (!Result) { | 
|  | 4555 | if (isa<UsingShadowDecl>(D)) { | 
|  | 4556 | // UsingShadowDecls can instantiate to nothing because of using hiding. | 
|  | 4557 | } else if (Diags.hasErrorOccurred()) { | 
|  | 4558 | // We've already complained about something, so most likely this | 
|  | 4559 | // declaration failed to instantiate. There's no point in complaining | 
|  | 4560 | // further, since this is normal in invalid code. | 
|  | 4561 | } else if (IsBeingInstantiated) { | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4562 | // The class in which this member exists is currently being | 
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4563 | // instantiated, and we haven't gotten around to instantiating this | 
|  | 4564 | // member yet. This can happen when the code uses forward declarations | 
|  | 4565 | // of member classes, and introduces ordering dependencies via | 
|  | 4566 | // template instantiation. | 
|  | 4567 | Diag(Loc, diag::err_member_not_yet_instantiated) | 
|  | 4568 | << D->getDeclName() | 
|  | 4569 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); | 
|  | 4570 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); | 
| Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 4571 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { | 
|  | 4572 | // This enumeration constant was found when the template was defined, | 
|  | 4573 | // but can't be found in the instantiation. This can happen if an | 
|  | 4574 | // unscoped enumeration member is explicitly specialized. | 
|  | 4575 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); | 
|  | 4576 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, | 
|  | 4577 | TemplateArgs)); | 
|  | 4578 | assert(Spec->getTemplateSpecializationKind() == | 
|  | 4579 | TSK_ExplicitSpecialization); | 
|  | 4580 | Diag(Loc, diag::err_enumerator_does_not_exist) | 
|  | 4581 | << D->getDeclName() | 
|  | 4582 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); | 
|  | 4583 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) | 
|  | 4584 | << Context.getTypeDeclType(Spec); | 
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 4585 | } else { | 
|  | 4586 | // We should have found something, but didn't. | 
|  | 4587 | llvm_unreachable("Unable to find instantiation of declaration!"); | 
|  | 4588 | } | 
|  | 4589 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4590 |  | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4591 | D = Result; | 
|  | 4592 | } | 
|  | 4593 |  | 
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4594 | return D; | 
|  | 4595 | } | 
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4596 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4597 | /// \brief Performs template instantiation for all implicit template | 
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4598 | /// instantiations we have seen until this point. | 
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 4599 | void Sema::PerformPendingInstantiations(bool LocalOnly) { | 
| Douglas Gregor | e39f97c | 2011-07-28 19:49:54 +0000 | [diff] [blame] | 4600 | // Load pending instantiations from the external source. | 
|  | 4601 | if (!LocalOnly && ExternalSource) { | 
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 4602 | SmallVector<PendingImplicitInstantiation, 4> Pending; | 
| Douglas Gregor | e39f97c | 2011-07-28 19:49:54 +0000 | [diff] [blame] | 4603 | ExternalSource->ReadPendingInstantiations(Pending); | 
|  | 4604 | PendingInstantiations.insert(PendingInstantiations.begin(), | 
|  | 4605 | Pending.begin(), Pending.end()); | 
|  | 4606 | } | 
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4607 |  | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4608 | while (!PendingLocalImplicitInstantiations.empty() || | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4609 | (!LocalOnly && !PendingInstantiations.empty())) { | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4610 | PendingImplicitInstantiation Inst; | 
|  | 4611 |  | 
|  | 4612 | if (PendingLocalImplicitInstantiations.empty()) { | 
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4613 | Inst = PendingInstantiations.front(); | 
|  | 4614 | PendingInstantiations.pop_front(); | 
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4615 | } else { | 
|  | 4616 | Inst = PendingLocalImplicitInstantiations.front(); | 
|  | 4617 | PendingLocalImplicitInstantiations.pop_front(); | 
|  | 4618 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4619 |  | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4620 | // Instantiate function definitions | 
|  | 4621 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { | 
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4622 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), | 
|  | 4623 | "instantiating function definition"); | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4624 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == | 
|  | 4625 | TSK_ExplicitInstantiationDefinition; | 
|  | 4626 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, | 
|  | 4627 | DefinitionRequired); | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4628 | continue; | 
|  | 4629 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4630 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4631 | // Instantiate variable definitions | 
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4632 | VarDecl *Var = cast<VarDecl>(Inst.first); | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4633 |  | 
|  | 4634 | assert((Var->isStaticDataMember() || | 
|  | 4635 | isa<VarTemplateSpecializationDecl>(Var)) && | 
|  | 4636 | "Not a static data member, nor a variable template" | 
|  | 4637 | " specialization?"); | 
| Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 4638 |  | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4639 | // Don't try to instantiate declarations if the most recent redeclaration | 
|  | 4640 | // is invalid. | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4641 | if (Var->getMostRecentDecl()->isInvalidDecl()) | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4642 | continue; | 
|  | 4643 |  | 
|  | 4644 | // Check if the most recent declaration has changed the specialization kind | 
|  | 4645 | // and removed the need for implicit instantiation. | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4646 | switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) { | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4647 | case TSK_Undeclared: | 
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4648 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4649 | case TSK_ExplicitInstantiationDeclaration: | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4650 | case TSK_ExplicitSpecialization: | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4651 | continue;  // No longer need to instantiate this type. | 
|  | 4652 | case TSK_ExplicitInstantiationDefinition: | 
|  | 4653 | // We only need an instantiation if the pending instantiation *is* the | 
|  | 4654 | // explicit instantiation. | 
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4655 | if (Var != Var->getMostRecentDecl()) continue; | 
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 4656 | case TSK_ImplicitInstantiation: | 
|  | 4657 | break; | 
|  | 4658 | } | 
|  | 4659 |  | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4660 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(), | 
|  | 4661 | "instantiating variable definition"); | 
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4662 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == | 
|  | 4663 | TSK_ExplicitInstantiationDefinition; | 
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4664 |  | 
|  | 4665 | // Instantiate static data member definitions or variable template | 
|  | 4666 | // specializations. | 
|  | 4667 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, | 
|  | 4668 | DefinitionRequired); | 
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 4669 | } | 
|  | 4670 | } | 
| John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 4671 |  | 
|  | 4672 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, | 
|  | 4673 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
|  | 4674 | for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(), | 
|  | 4675 | E = Pattern->ddiag_end(); I != E; ++I) { | 
|  | 4676 | DependentDiagnostic *DD = *I; | 
|  | 4677 |  | 
|  | 4678 | switch (DD->getKind()) { | 
|  | 4679 | case DependentDiagnostic::Access: | 
|  | 4680 | HandleDependentAccessCheck(*DD, TemplateArgs); | 
|  | 4681 | break; | 
|  | 4682 | } | 
|  | 4683 | } | 
|  | 4684 | } |