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