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