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