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