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