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