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