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