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