blob: 2d67d080e862c505310d00edaa444ca9d863e373 [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- 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//===----------------------------------------------------------------------===/
12#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000013#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000018#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000019#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000020#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000021#include "clang/AST/TypeLoc.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000022#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000023#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000024
25using namespace clang;
26
27namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000028 class TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000029 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000030 Sema &SemaRef;
31 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000032 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Douglas Gregor7d2bcc72010-05-19 03:39:53 +000034 bool InstantiateAttrs(Decl *Tmpl, Decl *New);
Anders Carlssond8fe2d52009-11-07 06:07:58 +000035
Douglas Gregor8dbc2692009-03-17 21:15:40 +000036 public:
37 typedef Sema::OwningExprResult OwningExprResult;
38
39 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000040 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000041 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000042
Mike Stump390b4cc2009-05-16 07:39:55 +000043 // FIXME: Once we get closer to completion, replace these manually-written
44 // declarations with automatically-generated ones from
45 // clang/AST/DeclNodes.def.
Douglas Gregor4f722be2009-03-25 15:45:12 +000046 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 Decl *VisitNamespaceDecl(NamespaceDecl *D);
John McCall3dbd3d52010-02-16 06:53:13 +000048 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000049 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000050 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000051 Decl *VisitFieldDecl(FieldDecl *D);
52 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
53 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000054 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000055 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000056 Decl *VisitFunctionDecl(FunctionDecl *D,
57 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000058 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000059 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
60 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000061 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000062 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000063 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000064 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000065 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000066 Decl *VisitClassTemplatePartialSpecializationDecl(
67 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000068 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000069 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000070 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000071 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000072 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000073 Decl *VisitUsingDecl(UsingDecl *D);
74 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000075 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
76 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor8dbc2692009-03-17 21:15:40 +000078 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000079 Decl *VisitDecl(Decl *D) {
80 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
81 Diagnostic::Error,
82 "cannot instantiate %0 yet");
83 SemaRef.Diag(D->getLocation(), DiagID)
84 << D->getDeclKindName();
85
Douglas Gregor8dbc2692009-03-17 21:15:40 +000086 return 0;
87 }
Douglas Gregor5545e162009-03-24 00:38:23 +000088
John McCallfd810b12009-08-14 02:03:10 +000089 const LangOptions &getLangOptions() {
90 return SemaRef.getLangOptions();
91 }
92
Douglas Gregor5545e162009-03-24 00:38:23 +000093 // Helper functions for instantiating methods.
John McCall21ef0fa2010-03-11 09:03:00 +000094 TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000095 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000096 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000097 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000098
99 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000100 SubstTemplateParams(TemplateParameterList *List);
John McCallb6217662010-03-15 10:12:16 +0000101
102 bool SubstQualifier(const DeclaratorDecl *OldDecl,
103 DeclaratorDecl *NewDecl);
104 bool SubstQualifier(const TagDecl *OldDecl,
105 TagDecl *NewDecl);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000106
107 bool InstantiateClassTemplatePartialSpecialization(
108 ClassTemplateDecl *ClassTemplate,
109 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000110 };
111}
112
John McCallb6217662010-03-15 10:12:16 +0000113bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
114 DeclaratorDecl *NewDecl) {
115 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
116 if (!OldQual) return false;
117
118 SourceRange QualRange = OldDecl->getQualifierRange();
119
120 NestedNameSpecifier *NewQual
121 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
122 if (!NewQual)
123 return true;
124
125 NewDecl->setQualifierInfo(NewQual, QualRange);
126 return false;
127}
128
129bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
130 TagDecl *NewDecl) {
131 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
132 if (!OldQual) return false;
133
134 SourceRange QualRange = OldDecl->getQualifierRange();
135
136 NestedNameSpecifier *NewQual
137 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
138 if (!NewQual)
139 return true;
140
141 NewDecl->setQualifierInfo(NewQual, QualRange);
142 return false;
143}
144
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000145// FIXME: Is this too simple?
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000146bool TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000147 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
148 TmplAttr = TmplAttr->getNext()) {
149
150 // FIXME: Is cloning correct for all attributes?
151 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
152
153 New->addAttr(NewAttr);
154 }
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000155
156 return false;
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000157}
158
Douglas Gregor4f722be2009-03-25 15:45:12 +0000159Decl *
160TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
161 assert(false && "Translation units cannot be instantiated");
162 return D;
163}
164
165Decl *
166TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
167 assert(false && "Namespaces cannot be instantiated");
168 return D;
169}
170
John McCall3dbd3d52010-02-16 06:53:13 +0000171Decl *
172TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
173 NamespaceAliasDecl *Inst
174 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
175 D->getNamespaceLoc(),
176 D->getAliasLoc(),
177 D->getNamespace()->getIdentifier(),
178 D->getQualifierRange(),
179 D->getQualifier(),
180 D->getTargetNameLoc(),
181 D->getNamespace());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000182 InstantiateAttrs(D, Inst);
John McCall3dbd3d52010-02-16 06:53:13 +0000183 Owner->addDecl(Inst);
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000184
John McCall3dbd3d52010-02-16 06:53:13 +0000185 return Inst;
186}
187
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000188Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
189 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000190 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000191 if (DI->getType()->isDependentType()) {
192 DI = SemaRef.SubstType(DI, TemplateArgs,
193 D->getLocation(), D->getDeclName());
194 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000195 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000196 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000197 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000198 } else {
199 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000202 // Create the new typedef
203 TypedefDecl *Typedef
204 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000205 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000206 if (Invalid)
207 Typedef->setInvalidDecl();
208
Douglas Gregord57a38e2010-04-23 16:25:07 +0000209 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
210 TagDecl *TD = TT->getDecl();
211
212 // If the TagDecl that the TypedefDecl points to is an anonymous decl
213 // keep track of the TypedefDecl.
214 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
215 TD->setTypedefForAnonDecl(Typedef);
216 }
217
John McCall5126fd02009-12-30 00:31:22 +0000218 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000219 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
220 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000221 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
222 }
223
Douglas Gregora6b09072010-05-17 23:46:49 +0000224 InstantiateAttrs(D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000225
John McCall46460a62010-01-20 21:53:11 +0000226 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000227 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000229 return Typedef;
230}
231
Douglas Gregor6eef5192009-12-14 19:27:10 +0000232/// \brief Instantiate the arguments provided as part of initialization.
233///
234/// \returns true if an error occurred, false otherwise.
235static bool InstantiateInitializationArguments(Sema &SemaRef,
236 Expr **Args, unsigned NumArgs,
237 const MultiLevelTemplateArgumentList &TemplateArgs,
238 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
239 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
240 for (unsigned I = 0; I != NumArgs; ++I) {
241 // When we hit the first defaulted argument, break out of the loop:
242 // we don't pass those default arguments on.
243 if (Args[I]->isDefaultArgument())
244 break;
245
246 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
247 if (Arg.isInvalid())
248 return true;
249
250 Expr *ArgExpr = (Expr *)Arg.get();
251 InitArgs.push_back(Arg.release());
252
253 // FIXME: We're faking all of the comma locations. Do we need them?
254 FakeCommaLocs.push_back(
255 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
256 }
257
258 return false;
259}
260
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000261/// \brief Instantiate an initializer, breaking it into separate
262/// initialization arguments.
263///
264/// \param S The semantic analysis object.
265///
266/// \param Init The initializer to instantiate.
267///
268/// \param TemplateArgs Template arguments to be substituted into the
269/// initializer.
270///
271/// \param NewArgs Will be filled in with the instantiation arguments.
272///
273/// \returns true if an error occurred, false otherwise
274static bool InstantiateInitializer(Sema &S, Expr *Init,
275 const MultiLevelTemplateArgumentList &TemplateArgs,
276 SourceLocation &LParenLoc,
277 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
278 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
279 SourceLocation &RParenLoc) {
280 NewArgs.clear();
281 LParenLoc = SourceLocation();
282 RParenLoc = SourceLocation();
283
284 if (!Init)
285 return false;
286
287 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
288 Init = ExprTemp->getSubExpr();
289
290 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
291 Init = Binder->getSubExpr();
292
293 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
294 Init = ICE->getSubExprAsWritten();
295
296 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
297 LParenLoc = ParenList->getLParenLoc();
298 RParenLoc = ParenList->getRParenLoc();
299 return InstantiateInitializationArguments(S, ParenList->getExprs(),
300 ParenList->getNumExprs(),
301 TemplateArgs, CommaLocs,
302 NewArgs);
303 }
304
305 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000306 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
307 if (InstantiateInitializationArguments(S,
308 Construct->getArgs(),
309 Construct->getNumArgs(),
310 TemplateArgs,
311 CommaLocs, NewArgs))
312 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000313
Douglas Gregor28329e52010-03-24 21:22:47 +0000314 // FIXME: Fake locations!
315 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
316 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
317 return false;
318 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000319 }
320
321 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
322 if (Result.isInvalid())
323 return true;
324
325 NewArgs.push_back(Result.takeAs<Expr>());
326 return false;
327}
328
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000329Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000330 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000331 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000332 TemplateArgs,
333 D->getTypeSpecStartLoc(),
334 D->getDeclName());
335 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000336 return 0;
337
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000338 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000339 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
340 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000341 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000342 D->getStorageClass(),
343 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000344 Var->setThreadSpecified(D->isThreadSpecified());
345 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
346 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000347
John McCallb6217662010-03-15 10:12:16 +0000348 // Substitute the nested name specifier, if any.
349 if (SubstQualifier(D, Var))
350 return 0;
351
Mike Stump1eb44332009-09-09 15:08:12 +0000352 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000353 // out-of-line, the instantiation will have the same lexical
354 // context (which will be a namespace scope) as the template.
355 if (D->isOutOfLine())
356 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000357
John McCall46460a62010-01-20 21:53:11 +0000358 Var->setAccess(D->getAccess());
Douglas Gregor5764f612010-05-08 23:05:03 +0000359 Var->setUsed(D->isUsed());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000360 InstantiateAttrs(D, Var);
361
Mike Stump390b4cc2009-05-16 07:39:55 +0000362 // FIXME: In theory, we could have a previous declaration for variables that
363 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000364 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000365 // FIXME: having to fake up a LookupResult is dumb.
366 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000367 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000368 if (D->isStaticDataMember())
369 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000370 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregor7caa6822009-07-24 20:34:43 +0000372 if (D->isOutOfLine()) {
373 D->getLexicalDeclContext()->addDecl(Var);
374 Owner->makeDeclVisibleInContext(Var);
375 } else {
376 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000377
378 if (Owner->isFunctionOrMethod())
379 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000382 // Link instantiations of static data members back to the template from
383 // which they were instantiated.
384 if (Var->isStaticDataMember())
385 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000386 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000387
Douglas Gregor60c93c92010-02-09 07:26:29 +0000388 if (Var->getAnyInitializer()) {
389 // We already have an initializer in the class.
390 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000391 if (Var->isStaticDataMember() && !D->isOutOfLine())
392 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
393 else
394 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
395
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000396 // Instantiate the initializer.
397 SourceLocation LParenLoc, RParenLoc;
398 llvm::SmallVector<SourceLocation, 4> CommaLocs;
399 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
400 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
401 CommaLocs, InitArgs, RParenLoc)) {
402 // Attach the initializer to the declaration.
403 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000404 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000405 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000406 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000407 move_arg(InitArgs),
408 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000409 RParenLoc);
410 } else if (InitArgs.size() == 1) {
411 Expr *Init = (Expr*)(InitArgs.take()[0]);
412 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
413 SemaRef.Owned(Init),
414 false);
415 } else {
416 assert(InitArgs.size() == 0);
417 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000418 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000419 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000420 // FIXME: Not too happy about invalidating the declaration
421 // because of a bogus initializer.
422 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000423 }
424
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000425 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000426 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
427 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000428
Douglas Gregor5764f612010-05-08 23:05:03 +0000429 // Diagnose unused local variables.
430 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
431 SemaRef.DiagnoseUnusedDecl(Var);
432
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000433 return Var;
434}
435
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000436Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
437 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000438 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000439 if (DI->getType()->isDependentType()) {
440 DI = SemaRef.SubstType(DI, TemplateArgs,
441 D->getLocation(), D->getDeclName());
442 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000443 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000444 Invalid = true;
445 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000446 // C++ [temp.arg.type]p3:
447 // If a declaration acquires a function type through a type
448 // dependent on a template-parameter and this causes a
449 // declaration that does not use the syntactic form of a
450 // function declarator to have function type, the program is
451 // ill-formed.
452 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000453 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000454 Invalid = true;
455 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000456 } else {
457 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000458 }
459
460 Expr *BitWidth = D->getBitWidth();
461 if (Invalid)
462 BitWidth = 0;
463 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000464 // The bit-width expression is not potentially evaluated.
465 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000467 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000468 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 if (InstantiatedBitWidth.isInvalid()) {
470 Invalid = true;
471 BitWidth = 0;
472 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000473 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474 }
475
John McCall07fb6be2009-10-22 23:33:21 +0000476 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
477 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000478 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000479 D->getLocation(),
480 D->isMutable(),
481 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000482 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483 D->getAccess(),
484 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000485 if (!Field) {
486 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000487 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000490 InstantiateAttrs(D, Field);
491
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000492 if (Invalid)
493 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000495 if (!Field->getDeclName()) {
496 // Keep track of where this decl came from.
497 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000500 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000501 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000502 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000503
504 return Field;
505}
506
John McCall02cace72009-08-28 07:59:38 +0000507Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000508 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000509 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000510 if (TypeSourceInfo *Ty = D->getFriendType()) {
511 TypeSourceInfo *InstTy =
512 SemaRef.SubstType(Ty, TemplateArgs,
513 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000514 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000515 return 0;
John McCall02cace72009-08-28 07:59:38 +0000516
Douglas Gregor06245bf2010-04-07 17:57:12 +0000517 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
518 if (!FD)
519 return 0;
520
521 FD->setAccess(AS_public);
522 Owner->addDecl(FD);
523 return FD;
524 }
525
526 NamedDecl *ND = D->getFriendDecl();
527 assert(ND && "friend decl must be a decl or a type!");
528
John McCallaf2094e2010-04-08 09:05:18 +0000529 // All of the Visit implementations for the various potential friend
530 // declarations have to be carefully written to work for friend
531 // objects, with the most important detail being that the target
532 // decl should almost certainly not be placed in Owner.
533 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000534 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
John McCall02cace72009-08-28 07:59:38 +0000536 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000537 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
538 cast<NamedDecl>(NewND), D->getFriendLoc());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000539 InstantiateAttrs(D, FD);
540
John McCall5fee1102009-08-29 03:50:18 +0000541 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000542 Owner->addDecl(FD);
543 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000544}
545
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000546Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
547 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregorac7610d2009-06-22 20:57:11 +0000549 // The expression in a static assertion is not potentially evaluated.
550 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000552 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000553 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000554 if (InstantiatedAssertExpr.isInvalid())
555 return 0;
556
Douglas Gregor43d9d922009-08-08 01:41:12 +0000557 OwningExprResult Message(SemaRef, D->getMessage());
558 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000559 Decl *StaticAssert
560 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000561 move(InstantiatedAssertExpr),
562 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000563 return StaticAssert;
564}
565
566Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000567 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000569 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000570 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000571 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000572 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000573 if (SubstQualifier(D, Enum)) return 0;
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000574 InstantiateAttrs(D, Enum);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000575 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000576 Enum->startDefinition();
577
Douglas Gregor96084f12010-03-01 19:00:07 +0000578 if (D->getDeclContext()->isFunctionOrMethod())
579 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
580
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000581 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000582
583 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000584 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
585 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000586 EC != ECEnd; ++EC) {
587 // The specified value for the enumerator.
588 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000589 if (Expr *UninstValue = EC->getInitExpr()) {
590 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000591 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000592 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
John McCallce3ff2b2009-08-25 22:02:44 +0000594 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000595 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000596
597 // Drop the initial value and continue.
598 bool isInvalid = false;
599 if (Value.isInvalid()) {
600 Value = SemaRef.Owned((Expr *)0);
601 isInvalid = true;
602 }
603
Mike Stump1eb44332009-09-09 15:08:12 +0000604 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000605 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
606 EC->getLocation(), EC->getIdentifier(),
607 move(Value));
608
609 if (isInvalid) {
610 if (EnumConst)
611 EnumConst->setInvalidDecl();
612 Enum->setInvalidDecl();
613 }
614
615 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000616 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000617 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000618 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000619 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000620
621 if (D->getDeclContext()->isFunctionOrMethod()) {
622 // If the enumeration is within a function or method, record the enum
623 // constant as a local.
624 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
625 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000626 }
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000629 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000630 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000631 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
632 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000633 &Enumerators[0], Enumerators.size(),
634 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000635
636 return Enum;
637}
638
Douglas Gregor6477b692009-03-25 15:04:13 +0000639Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
640 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
641 return 0;
642}
643
John McCalle29ba202009-08-20 01:44:21 +0000644Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000645 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
646
Douglas Gregor550d9b22009-10-31 17:21:17 +0000647 // Create a local instantiation scope for this class template, which
648 // will contain the instantiations of the template parameters.
649 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000650 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000651 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000652 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000653 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000654
655 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000656
657 // Instantiate the qualifier. We have to do this first in case
658 // we're a friend declaration, because if we are then we need to put
659 // the new declaration in the appropriate context.
660 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
661 if (Qualifier) {
662 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
663 Pattern->getQualifierRange(),
664 TemplateArgs);
665 if (!Qualifier) return 0;
666 }
667
668 CXXRecordDecl *PrevDecl = 0;
669 ClassTemplateDecl *PrevClassTemplate = 0;
670
671 // If this isn't a friend, then it's a member template, in which
672 // case we just want to build the instantiation in the
673 // specialization. If it is a friend, we want to build it in
674 // the appropriate context.
675 DeclContext *DC = Owner;
676 if (isFriend) {
677 if (Qualifier) {
678 CXXScopeSpec SS;
679 SS.setScopeRep(Qualifier);
680 SS.setRange(Pattern->getQualifierRange());
681 DC = SemaRef.computeDeclContext(SS);
682 if (!DC) return 0;
683 } else {
684 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
685 Pattern->getDeclContext(),
686 TemplateArgs);
687 }
688
689 // Look for a previous declaration of the template in the owning
690 // context.
691 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
692 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
693 SemaRef.LookupQualifiedName(R, DC);
694
695 if (R.isSingleResult()) {
696 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
697 if (PrevClassTemplate)
698 PrevDecl = PrevClassTemplate->getTemplatedDecl();
699 }
700
701 if (!PrevClassTemplate && Qualifier) {
702 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000703 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
704 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000705 return 0;
706 }
707
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000708 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000709 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000710 bool Complain = true;
711
712 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
713 // template for struct std::tr1::__detail::_Map_base, where the
714 // template parameters of the friend declaration don't match the
715 // template parameters of the original declaration. In this one
716 // case, we don't complain about the ill-formed friend
717 // declaration.
718 if (isFriend && Pattern->getIdentifier() &&
719 Pattern->getIdentifier()->isStr("_Map_base") &&
720 DC->isNamespace() &&
721 cast<NamespaceDecl>(DC)->getIdentifier() &&
722 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
723 DeclContext *DCParent = DC->getParent();
724 if (DCParent->isNamespace() &&
725 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
726 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
727 DeclContext *DCParent2 = DCParent->getParent();
728 if (DCParent2->isNamespace() &&
729 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
730 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
731 DCParent2->getParent()->isTranslationUnit())
732 Complain = false;
733 }
734 }
735
John McCall93ba8572010-03-25 06:39:04 +0000736 TemplateParameterList *PrevParams
737 = PrevClassTemplate->getTemplateParameters();
738
739 // Make sure the parameter lists match.
740 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000741 Complain,
742 Sema::TPL_TemplateMatch)) {
743 if (Complain)
744 return 0;
745
746 AdoptedPreviousTemplateParams = true;
747 InstParams = PrevParams;
748 }
John McCall93ba8572010-03-25 06:39:04 +0000749
750 // Do some additional validation, then merge default arguments
751 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000752 if (!AdoptedPreviousTemplateParams &&
753 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000754 Sema::TPC_ClassTemplate))
755 return 0;
756 }
757 }
758
John McCalle29ba202009-08-20 01:44:21 +0000759 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000760 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000761 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000762 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000763 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000764
John McCall93ba8572010-03-25 06:39:04 +0000765 if (Qualifier)
766 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000767
John McCalle29ba202009-08-20 01:44:21 +0000768 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000769 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
770 D->getIdentifier(), InstParams, RecordInst,
771 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000772 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000773
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000774 InstantiateAttrs(D, Inst);
775
John McCall93ba8572010-03-25 06:39:04 +0000776 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000777 if (PrevClassTemplate)
778 Inst->setAccess(PrevClassTemplate->getAccess());
779 else
780 Inst->setAccess(D->getAccess());
781
John McCall93ba8572010-03-25 06:39:04 +0000782 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
783 // TODO: do we want to track the instantiation progeny of this
784 // friend target decl?
785 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000786 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000787 Inst->setInstantiatedFromMemberTemplate(D);
788 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000789
790 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000791 SemaRef.Context.getInjectedClassNameType(RecordInst,
792 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000793
Douglas Gregor259571e2009-10-30 22:42:42 +0000794 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000795 if (isFriend) {
796 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000797 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000798 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000799
John McCalle29ba202009-08-20 01:44:21 +0000800 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000801
Douglas Gregored9c0f92009-10-29 00:04:11 +0000802 // Instantiate all of the partial specializations of this member class
803 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000804 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
805 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000806 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
807 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
808
John McCalle29ba202009-08-20 01:44:21 +0000809 return Inst;
810}
811
Douglas Gregord60e1052009-08-27 16:57:43 +0000812Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000813TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
814 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000815 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
816
817 // Lookup the already-instantiated declaration in the instantiation
818 // of the class template and return that.
819 DeclContext::lookup_result Found
820 = Owner->lookup(ClassTemplate->getDeclName());
821 if (Found.first == Found.second)
822 return 0;
823
824 ClassTemplateDecl *InstClassTemplate
825 = dyn_cast<ClassTemplateDecl>(*Found.first);
826 if (!InstClassTemplate)
827 return 0;
828
829 Decl *DCanon = D->getCanonicalDecl();
830 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
831 P = InstClassTemplate->getPartialSpecializations().begin(),
832 PEnd = InstClassTemplate->getPartialSpecializations().end();
833 P != PEnd; ++P) {
834 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
835 return &*P;
836 }
837
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000838 return 0;
839}
840
841Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000842TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000843 // Create a local instantiation scope for this function template, which
844 // will contain the instantiations of the template parameters and then get
845 // merged with the local instantiation scope for the function template
846 // itself.
847 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000848
Douglas Gregord60e1052009-08-27 16:57:43 +0000849 TemplateParameterList *TempParams = D->getTemplateParameters();
850 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000851 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000852 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000853
Douglas Gregora735b202009-10-13 14:39:41 +0000854 FunctionDecl *Instantiated = 0;
855 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
856 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
857 InstParams));
858 else
859 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
860 D->getTemplatedDecl(),
861 InstParams));
862
863 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000864 return 0;
865
John McCall46460a62010-01-20 21:53:11 +0000866 Instantiated->setAccess(D->getAccess());
867
Mike Stump1eb44332009-09-09 15:08:12 +0000868 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000869 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000870 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000871 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000872 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000873 assert(InstTemplate &&
874 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000875
John McCallb1a56e72010-03-26 23:10:15 +0000876 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
877
John McCalle976ffe2009-12-14 23:19:40 +0000878 // Link the instantiation back to the pattern *unless* this is a
879 // non-definition friend declaration.
880 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000881 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000882 InstTemplate->setInstantiatedFromMemberTemplate(D);
883
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000884 InstantiateAttrs(D, InstTemplate);
885
John McCallb1a56e72010-03-26 23:10:15 +0000886 // Make declarations visible in the appropriate context.
887 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000888 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000889
Douglas Gregord60e1052009-08-27 16:57:43 +0000890 return InstTemplate;
891}
892
Douglas Gregord475b8d2009-03-25 21:17:03 +0000893Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
894 CXXRecordDecl *PrevDecl = 0;
895 if (D->isInjectedClassName())
896 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000897 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000898 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
899 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000900 TemplateArgs);
901 if (!Prev) return 0;
902 PrevDecl = cast<CXXRecordDecl>(Prev);
903 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000904
905 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000906 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000907 D->getLocation(), D->getIdentifier(),
908 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000909
Douglas Gregor7d2bcc72010-05-19 03:39:53 +0000910 InstantiateAttrs(D, Record);
911
John McCallb6217662010-03-15 10:12:16 +0000912 // Substitute the nested name specifier, if any.
913 if (SubstQualifier(D, Record))
914 return 0;
915
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000917 // FIXME: Check against AS_none is an ugly hack to work around the issue that
918 // the tag decls introduced by friend class declarations don't have an access
919 // specifier. Remove once this area of the code gets sorted out.
920 if (D->getAccess() != AS_none)
921 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000923 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000924
John McCall02cace72009-08-28 07:59:38 +0000925 // If the original function was part of a friend declaration,
926 // inherit its namespace state.
927 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
928 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
929
Anders Carlssond8b285f2009-09-01 04:26:58 +0000930 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
931
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000933 return Record;
934}
935
John McCall02cace72009-08-28 07:59:38 +0000936/// Normal class members are of more specific types and therefore
937/// don't make it here. This function serves two purposes:
938/// 1) instantiating function templates
939/// 2) substituting friend declarations
940/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000941Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000942 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000943 // Check whether there is already a function template specialization for
944 // this declaration.
945 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
946 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000947 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000948 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000949 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000950 TemplateArgs.getInnermost().getFlatArgumentList(),
951 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000952 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
954 FunctionTemplateSpecializationInfo *Info
955 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000956 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor127102b2009-06-29 20:59:39 +0000958 // If we already have a function template specialization, return it.
959 if (Info)
960 return Info->Function;
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
John McCallb0cb0222010-03-27 05:57:59 +0000963 bool isFriend;
964 if (FunctionTemplate)
965 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
966 else
967 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
968
Douglas Gregor79c22782010-01-16 20:21:20 +0000969 bool MergeWithParentScope = (TemplateParams != 0) ||
970 !(isa<Decl>(Owner) &&
971 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
972 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregore53060f2009-06-25 22:08:12 +0000974 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000975 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
976 TInfo = SubstFunctionType(D, Params);
977 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000978 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000979 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000980
John McCalld325daa2010-03-26 04:53:08 +0000981 NestedNameSpecifier *Qualifier = D->getQualifier();
982 if (Qualifier) {
983 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
984 D->getQualifierRange(),
985 TemplateArgs);
986 if (!Qualifier) return 0;
987 }
988
John McCall68b6b872010-02-06 01:50:47 +0000989 // If we're instantiating a local function declaration, put the result
990 // in the owner; otherwise we need to find the instantiated context.
991 DeclContext *DC;
992 if (D->getDeclContext()->isFunctionOrMethod())
993 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000994 else if (isFriend && Qualifier) {
995 CXXScopeSpec SS;
996 SS.setScopeRep(Qualifier);
997 SS.setRange(D->getQualifierRange());
998 DC = SemaRef.computeDeclContext(SS);
999 if (!DC) return 0;
1000 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001001 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1002 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001003 }
John McCall68b6b872010-02-06 01:50:47 +00001004
John McCall02cace72009-08-28 07:59:38 +00001005 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001006 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001007 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001008 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001009 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001010
John McCalld325daa2010-03-26 04:53:08 +00001011 if (Qualifier)
1012 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001013
John McCallb1a56e72010-03-26 23:10:15 +00001014 DeclContext *LexicalDC = Owner;
1015 if (!isFriend && D->isOutOfLine()) {
1016 assert(D->getDeclContext()->isFileContext());
1017 LexicalDC = D->getDeclContext();
1018 }
1019
1020 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregore53060f2009-06-25 22:08:12 +00001022 // Attach the parameters
1023 for (unsigned P = 0; P < Params.size(); ++P)
1024 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001025 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001026
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001027 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001028 if (TemplateParams) {
1029 // Our resulting instantiation is actually a function template, since we
1030 // are substituting only the outer template parameters. For example, given
1031 //
1032 // template<typename T>
1033 // struct X {
1034 // template<typename U> friend void f(T, U);
1035 // };
1036 //
1037 // X<int> x;
1038 //
1039 // We are instantiating the friend function template "f" within X<int>,
1040 // which means substituting int for T, but leaving "f" as a friend function
1041 // template.
1042 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001043 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001044 Function->getLocation(),
1045 Function->getDeclName(),
1046 TemplateParams, Function);
1047 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001048
1049 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001050
1051 if (isFriend && D->isThisDeclarationADefinition()) {
1052 // TODO: should we remember this connection regardless of whether
1053 // the friend declaration provided a body?
1054 FunctionTemplate->setInstantiatedFromMemberTemplate(
1055 D->getDescribedFunctionTemplate());
1056 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001057 } else if (FunctionTemplate) {
1058 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001059 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001060 &TemplateArgs.getInnermost(),
1061 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001062 } else if (isFriend && D->isThisDeclarationADefinition()) {
1063 // TODO: should we remember this connection regardless of whether
1064 // the friend declaration provided a body?
1065 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001066 }
Douglas Gregora735b202009-10-13 14:39:41 +00001067
Douglas Gregore53060f2009-06-25 22:08:12 +00001068 if (InitFunctionInstantiation(Function, D))
1069 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregore53060f2009-06-25 22:08:12 +00001071 bool Redeclaration = false;
1072 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001073 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001074
John McCall68263142009-11-18 22:49:29 +00001075 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1076 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1077
John McCallaf2094e2010-04-08 09:05:18 +00001078 if (DependentFunctionTemplateSpecializationInfo *Info
1079 = D->getDependentSpecializationInfo()) {
1080 assert(isFriend && "non-friend has dependent specialization info?");
1081
1082 // This needs to be set now for future sanity.
1083 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1084
1085 // Instantiate the explicit template arguments.
1086 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1087 Info->getRAngleLoc());
1088 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1089 TemplateArgumentLoc Loc;
1090 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1091 return 0;
1092
1093 ExplicitArgs.addArgument(Loc);
1094 }
1095
1096 // Map the candidate templates to their instantiations.
1097 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1098 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1099 Info->getTemplate(I),
1100 TemplateArgs);
1101 if (!Temp) return 0;
1102
1103 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1104 }
1105
1106 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1107 &ExplicitArgs,
1108 Previous))
1109 Function->setInvalidDecl();
1110
1111 isExplicitSpecialization = true;
1112
1113 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001114 // Look only into the namespace where the friend would be declared to
1115 // find a previous declaration. This is the innermost enclosing namespace,
1116 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001117 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001118
Douglas Gregora735b202009-10-13 14:39:41 +00001119 // In C++, the previous declaration we find might be a tag type
1120 // (class or enum). In this case, the new declaration will hide the
1121 // tag type. Note that this does does not apply if we're declaring a
1122 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001123 if (Previous.isSingleTagDecl())
1124 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001125 }
1126
John McCall9f54ad42009-12-10 09:41:52 +00001127 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001128 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001129 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001130
John McCall76d32642010-04-24 01:30:58 +00001131 NamedDecl *PrincipalDecl = (TemplateParams
1132 ? cast<NamedDecl>(FunctionTemplate)
1133 : Function);
1134
Douglas Gregora735b202009-10-13 14:39:41 +00001135 // If the original function was part of a friend declaration,
1136 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001137 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001138 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001139 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001140 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001141 else
Douglas Gregora735b202009-10-13 14:39:41 +00001142 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001143
1144 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1145 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001146
1147 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1148 D->isThisDeclarationADefinition()) {
1149 // Check for a function body.
1150 const FunctionDecl *Definition = 0;
1151 if (Function->getBody(Definition) &&
1152 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1153 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1154 << Function->getDeclName();
1155 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1156 Function->setInvalidDecl();
1157 }
1158 // Check for redefinitions due to other instantiations of this or
1159 // a similar friend function.
1160 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1161 REnd = Function->redecls_end();
1162 R != REnd; ++R) {
1163 if (*R != Function &&
1164 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1165 if (const FunctionDecl *RPattern
1166 = (*R)->getTemplateInstantiationPattern())
1167 if (RPattern->getBody(RPattern)) {
1168 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1169 << Function->getDeclName();
1170 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1171 Function->setInvalidDecl();
1172 break;
1173 }
1174 }
1175 }
1176 }
1177
Douglas Gregora735b202009-10-13 14:39:41 +00001178 }
1179
John McCall76d32642010-04-24 01:30:58 +00001180 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1181 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1182 PrincipalDecl->setNonMemberOperator();
1183
Douglas Gregore53060f2009-06-25 22:08:12 +00001184 return Function;
1185}
1186
Douglas Gregord60e1052009-08-27 16:57:43 +00001187Decl *
1188TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1189 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001190 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1191 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001192 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001193 // We are creating a function template specialization from a function
1194 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001195 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001196 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001197 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001198 TemplateArgs.getInnermost().getFlatArgumentList(),
1199 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001200 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
1202 FunctionTemplateSpecializationInfo *Info
1203 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001204 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregor6b906862009-08-21 00:16:32 +00001206 // If we already have a function template specialization, return it.
1207 if (Info)
1208 return Info->Function;
1209 }
1210
John McCallb0cb0222010-03-27 05:57:59 +00001211 bool isFriend;
1212 if (FunctionTemplate)
1213 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1214 else
1215 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1216
Douglas Gregor79c22782010-01-16 20:21:20 +00001217 bool MergeWithParentScope = (TemplateParams != 0) ||
1218 !(isa<Decl>(Owner) &&
1219 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1220 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001221
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001222 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001223 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1224 TInfo = SubstFunctionType(D, Params);
1225 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001226 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001227 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001228
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001229 // \brief If the type of this function is not *directly* a function
1230 // type, then we're instantiating the a function that was declared
1231 // via a typedef, e.g.,
1232 //
1233 // typedef int functype(int, int);
1234 // functype func;
1235 //
1236 // In this case, we'll just go instantiate the ParmVarDecls that we
1237 // synthesized in the method declaration.
1238 if (!isa<FunctionProtoType>(T)) {
1239 assert(!Params.size() && "Instantiating type could not yield parameters");
1240 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1241 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1242 TemplateArgs);
1243 if (!P)
1244 return 0;
1245
1246 Params.push_back(P);
1247 }
1248 }
1249
John McCallb0cb0222010-03-27 05:57:59 +00001250 NestedNameSpecifier *Qualifier = D->getQualifier();
1251 if (Qualifier) {
1252 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1253 D->getQualifierRange(),
1254 TemplateArgs);
1255 if (!Qualifier) return 0;
1256 }
1257
1258 DeclContext *DC = Owner;
1259 if (isFriend) {
1260 if (Qualifier) {
1261 CXXScopeSpec SS;
1262 SS.setScopeRep(Qualifier);
1263 SS.setRange(D->getQualifierRange());
1264 DC = SemaRef.computeDeclContext(SS);
1265 } else {
1266 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1267 D->getDeclContext(),
1268 TemplateArgs);
1269 }
1270 if (!DC) return 0;
1271 }
1272
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001273 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001274 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001275 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Douglas Gregordec06662009-08-21 18:42:58 +00001277 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001278 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001279 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1280 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1281 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001282 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1283 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001284 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001285 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001286 Constructor->isInlineSpecified(),
1287 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001288 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1289 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1290 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1291 SemaRef.Context.getCanonicalType(ClassTy));
1292 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1293 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001294 T, Destructor->isInlineSpecified(),
1295 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001296 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001297 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001298 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001299 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001300 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1301 ConvTy);
1302 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1303 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001304 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001305 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001306 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001307 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001308 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001309 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001310 D->isStatic(),
1311 D->getStorageClassAsWritten(),
1312 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001313 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001314
John McCallb0cb0222010-03-27 05:57:59 +00001315 if (Qualifier)
1316 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001317
Douglas Gregord60e1052009-08-27 16:57:43 +00001318 if (TemplateParams) {
1319 // Our resulting instantiation is actually a function template, since we
1320 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001321 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001322 // template<typename T>
1323 // struct X {
1324 // template<typename U> void f(T, U);
1325 // };
1326 //
1327 // X<int> x;
1328 //
1329 // We are instantiating the member template "f" within X<int>, which means
1330 // substituting int for T, but leaving "f" as a member function template.
1331 // Build the function template itself.
1332 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1333 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001334 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001335 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001336 if (isFriend) {
1337 FunctionTemplate->setLexicalDeclContext(Owner);
1338 FunctionTemplate->setObjectOfFriendDecl(true);
1339 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001340 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001341 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001342 } else if (FunctionTemplate) {
1343 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001344 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001345 &TemplateArgs.getInnermost(),
1346 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001347 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001348 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001349 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001350 }
1351
Mike Stump1eb44332009-09-09 15:08:12 +00001352 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001353 // out-of-line, the instantiation will have the same lexical
1354 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001355 if (isFriend) {
1356 Method->setLexicalDeclContext(Owner);
1357 Method->setObjectOfFriendDecl(true);
1358 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001359 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Douglas Gregor5545e162009-03-24 00:38:23 +00001361 // Attach the parameters
1362 for (unsigned P = 0; P < Params.size(); ++P)
1363 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001364 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001365
1366 if (InitMethodInstantiation(Method, D))
1367 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001368
John McCall68263142009-11-18 22:49:29 +00001369 LookupResult Previous(SemaRef, Name, SourceLocation(),
1370 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
John McCallb0cb0222010-03-27 05:57:59 +00001372 if (!FunctionTemplate || TemplateParams || isFriend) {
1373 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Douglas Gregordec06662009-08-21 18:42:58 +00001375 // In C++, the previous declaration we find might be a tag type
1376 // (class or enum). In this case, the new declaration will hide the
1377 // tag type. Note that this does does not apply if we're declaring a
1378 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001379 if (Previous.isSingleTagDecl())
1380 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001381 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001382
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001383 bool Redeclaration = false;
1384 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001385 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001386 /*FIXME:*/OverloadableAttrRequired);
1387
Douglas Gregor4ba31362009-12-01 17:24:26 +00001388 if (D->isPure())
1389 SemaRef.CheckPureMethod(Method, SourceRange());
1390
John McCall46460a62010-01-20 21:53:11 +00001391 Method->setAccess(D->getAccess());
1392
John McCallb0cb0222010-03-27 05:57:59 +00001393 if (FunctionTemplate) {
1394 // If there's a function template, let our caller handle it.
1395 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1396 // Don't hide a (potentially) valid declaration with an invalid one.
1397 } else {
1398 NamedDecl *DeclToAdd = (TemplateParams
1399 ? cast<NamedDecl>(FunctionTemplate)
1400 : Method);
1401 if (isFriend)
1402 Record->makeDeclVisibleInContext(DeclToAdd);
1403 else
1404 Owner->addDecl(DeclToAdd);
1405 }
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001407 return Method;
1408}
1409
Douglas Gregor615c5d42009-03-24 16:43:20 +00001410Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001411 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001412}
1413
Douglas Gregor03b2b072009-03-24 00:15:49 +00001414Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001415 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001416}
1417
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001418Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001419 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001420}
1421
Douglas Gregor6477b692009-03-25 15:04:13 +00001422ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001423 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001424}
1425
John McCalle29ba202009-08-20 01:44:21 +00001426Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1427 TemplateTypeParmDecl *D) {
1428 // TODO: don't always clone when decls are refcounted.
1429 const Type* T = D->getTypeForDecl();
1430 assert(T->isTemplateTypeParmType());
1431 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001432
John McCalle29ba202009-08-20 01:44:21 +00001433 TemplateTypeParmDecl *Inst =
1434 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001435 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001436 TTPT->getName(),
1437 D->wasDeclaredWithTypename(),
1438 D->isParameterPack());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001439 InstantiateAttrs(D, Inst);
John McCalle29ba202009-08-20 01:44:21 +00001440
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001441 if (D->hasDefaultArgument())
1442 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001443
Douglas Gregor550d9b22009-10-31 17:21:17 +00001444 // Introduce this template parameter's instantiation into the instantiation
1445 // scope.
1446 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1447
John McCalle29ba202009-08-20 01:44:21 +00001448 return Inst;
1449}
1450
Douglas Gregor33642df2009-10-23 23:25:44 +00001451Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1452 NonTypeTemplateParmDecl *D) {
1453 // Substitute into the type of the non-type template parameter.
1454 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001455 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001456 if (DI) {
1457 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1458 D->getDeclName());
1459 if (DI) T = DI->getType();
1460 } else {
1461 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1462 D->getDeclName());
1463 DI = 0;
1464 }
1465 if (T.isNull())
1466 return 0;
1467
1468 // Check that this type is acceptable for a non-type template parameter.
1469 bool Invalid = false;
1470 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1471 if (T.isNull()) {
1472 T = SemaRef.Context.IntTy;
1473 Invalid = true;
1474 }
1475
1476 NonTypeTemplateParmDecl *Param
1477 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1478 D->getDepth() - 1, D->getPosition(),
1479 D->getIdentifier(), T, DI);
1480 if (Invalid)
1481 Param->setInvalidDecl();
1482
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001483 InstantiateAttrs(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001484 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001485
1486 // Introduce this template parameter's instantiation into the instantiation
1487 // scope.
1488 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001489 return Param;
1490}
1491
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001492Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001493TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1494 TemplateTemplateParmDecl *D) {
1495 // Instantiate the template parameter list of the template template parameter.
1496 TemplateParameterList *TempParams = D->getTemplateParameters();
1497 TemplateParameterList *InstParams;
1498 {
1499 // Perform the actual substitution of template parameters within a new,
1500 // local instantiation scope.
1501 Sema::LocalInstantiationScope Scope(SemaRef);
1502 InstParams = SubstTemplateParams(TempParams);
1503 if (!InstParams)
1504 return NULL;
1505 }
1506
1507 // Build the template template parameter.
1508 TemplateTemplateParmDecl *Param
1509 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1510 D->getDepth() - 1, D->getPosition(),
1511 D->getIdentifier(), InstParams);
1512 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001513 InstantiateAttrs(D, Param);
1514
Douglas Gregor9106ef72009-11-11 16:58:32 +00001515 // Introduce this template parameter's instantiation into the instantiation
1516 // scope.
1517 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1518
1519 return Param;
1520}
1521
Douglas Gregor48c32a72009-11-17 06:07:40 +00001522Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1523 // Using directives are never dependent, so they require no explicit
1524
1525 UsingDirectiveDecl *Inst
1526 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1527 D->getNamespaceKeyLocation(),
1528 D->getQualifierRange(), D->getQualifier(),
1529 D->getIdentLocation(),
1530 D->getNominatedNamespace(),
1531 D->getCommonAncestor());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001532 InstantiateAttrs(D, Inst);
1533
Douglas Gregor48c32a72009-11-17 06:07:40 +00001534 Owner->addDecl(Inst);
1535 return Inst;
1536}
1537
John McCalled976492009-12-04 22:46:56 +00001538Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1539 // The nested name specifier is non-dependent, so no transformation
1540 // is required.
1541
John McCall9f54ad42009-12-10 09:41:52 +00001542 // We only need to do redeclaration lookups if we're in a class
1543 // scope (in fact, it's not really even possible in non-class
1544 // scopes).
1545 bool CheckRedeclaration = Owner->isRecord();
1546
1547 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1548 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1549
John McCalled976492009-12-04 22:46:56 +00001550 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1551 D->getLocation(),
1552 D->getNestedNameRange(),
1553 D->getUsingLocation(),
1554 D->getTargetNestedNameDecl(),
1555 D->getDeclName(),
1556 D->isTypeName());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001557 InstantiateAttrs(D, NewUD);
John McCalled976492009-12-04 22:46:56 +00001558
1559 CXXScopeSpec SS;
1560 SS.setScopeRep(D->getTargetNestedNameDecl());
1561 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001562
1563 if (CheckRedeclaration) {
1564 Prev.setHideTags(false);
1565 SemaRef.LookupQualifiedName(Prev, Owner);
1566
1567 // Check for invalid redeclarations.
1568 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1569 D->isTypeName(), SS,
1570 D->getLocation(), Prev))
1571 NewUD->setInvalidDecl();
1572
1573 }
1574
1575 if (!NewUD->isInvalidDecl() &&
1576 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001577 D->getLocation()))
1578 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001579
John McCalled976492009-12-04 22:46:56 +00001580 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1581 NewUD->setAccess(D->getAccess());
1582 Owner->addDecl(NewUD);
1583
John McCall9f54ad42009-12-10 09:41:52 +00001584 // Don't process the shadow decls for an invalid decl.
1585 if (NewUD->isInvalidDecl())
1586 return NewUD;
1587
John McCall323c3102009-12-22 22:26:37 +00001588 bool isFunctionScope = Owner->isFunctionOrMethod();
1589
John McCall9f54ad42009-12-10 09:41:52 +00001590 // Process the shadow decls.
1591 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1592 I != E; ++I) {
1593 UsingShadowDecl *Shadow = *I;
1594 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001595 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1596 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001597 TemplateArgs));
1598
1599 if (CheckRedeclaration &&
1600 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1601 continue;
1602
1603 UsingShadowDecl *InstShadow
1604 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1605 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001606
1607 if (isFunctionScope)
1608 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001609 }
John McCalled976492009-12-04 22:46:56 +00001610
1611 return NewUD;
1612}
1613
1614Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001615 // Ignore these; we handle them in bulk when processing the UsingDecl.
1616 return 0;
John McCalled976492009-12-04 22:46:56 +00001617}
1618
John McCall7ba107a2009-11-18 02:36:19 +00001619Decl * TemplateDeclInstantiator
1620 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001621 NestedNameSpecifier *NNS =
1622 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1623 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001624 TemplateArgs);
1625 if (!NNS)
1626 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001628 CXXScopeSpec SS;
1629 SS.setRange(D->getTargetNestedNameRange());
1630 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
1632 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001633 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001634 D->getUsingLoc(), SS, D->getLocation(),
1635 D->getDeclName(), 0,
1636 /*instantiation*/ true,
1637 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001638 if (UD) {
1639 InstantiateAttrs(D, UD);
John McCalled976492009-12-04 22:46:56 +00001640 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001641 }
John McCalled976492009-12-04 22:46:56 +00001642
John McCall7ba107a2009-11-18 02:36:19 +00001643 return UD;
1644}
1645
1646Decl * TemplateDeclInstantiator
1647 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1648 NestedNameSpecifier *NNS =
1649 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1650 D->getTargetNestedNameRange(),
1651 TemplateArgs);
1652 if (!NNS)
1653 return 0;
1654
1655 CXXScopeSpec SS;
1656 SS.setRange(D->getTargetNestedNameRange());
1657 SS.setScopeRep(NNS);
1658
1659 NamedDecl *UD =
1660 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1661 D->getUsingLoc(), SS, D->getLocation(),
1662 D->getDeclName(), 0,
1663 /*instantiation*/ true,
1664 /*typename*/ false, SourceLocation());
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001665 if (UD) {
1666 InstantiateAttrs(D, UD);
John McCalled976492009-12-04 22:46:56 +00001667 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001668 }
John McCalled976492009-12-04 22:46:56 +00001669
Anders Carlsson0d8df782009-08-29 19:37:28 +00001670 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001671}
1672
John McCallce3ff2b2009-08-25 22:02:44 +00001673Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001674 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001675 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001676 if (D->isInvalidDecl())
1677 return 0;
1678
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001679 return Instantiator.Visit(D);
1680}
1681
John McCalle29ba202009-08-20 01:44:21 +00001682/// \brief Instantiates a nested template parameter list in the current
1683/// instantiation context.
1684///
1685/// \param L The parameter list to instantiate
1686///
1687/// \returns NULL if there was an error
1688TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001689TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001690 // Get errors for all the parameters before bailing out.
1691 bool Invalid = false;
1692
1693 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001694 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001695 ParamVector Params;
1696 Params.reserve(N);
1697 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1698 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001699 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001700 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001701 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001702 }
1703
1704 // Clean up if we had an error.
1705 if (Invalid) {
1706 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1707 PI != PE; ++PI)
1708 if (*PI)
1709 (*PI)->Destroy(SemaRef.Context);
1710 return NULL;
1711 }
1712
1713 TemplateParameterList *InstL
1714 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1715 L->getLAngleLoc(), &Params.front(), N,
1716 L->getRAngleLoc());
1717 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001718}
John McCalle29ba202009-08-20 01:44:21 +00001719
Douglas Gregored9c0f92009-10-29 00:04:11 +00001720/// \brief Instantiate the declaration of a class template partial
1721/// specialization.
1722///
1723/// \param ClassTemplate the (instantiated) class template that is partially
1724// specialized by the instantiation of \p PartialSpec.
1725///
1726/// \param PartialSpec the (uninstantiated) class template partial
1727/// specialization that we are instantiating.
1728///
1729/// \returns true if there was an error, false otherwise.
1730bool
1731TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1732 ClassTemplateDecl *ClassTemplate,
1733 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001734 // Create a local instantiation scope for this class template partial
1735 // specialization, which will contain the instantiations of the template
1736 // parameters.
1737 Sema::LocalInstantiationScope Scope(SemaRef);
1738
Douglas Gregored9c0f92009-10-29 00:04:11 +00001739 // Substitute into the template parameters of the class template partial
1740 // specialization.
1741 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1742 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1743 if (!InstParams)
1744 return true;
1745
1746 // Substitute into the template arguments of the class template partial
1747 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001748 const TemplateArgumentLoc *PartialSpecTemplateArgs
1749 = PartialSpec->getTemplateArgsAsWritten();
1750 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1751
John McCalld5532b62009-11-23 01:53:49 +00001752 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001753 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001754 TemplateArgumentLoc Loc;
1755 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001756 return true;
John McCalld5532b62009-11-23 01:53:49 +00001757 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001758 }
1759
1760
1761 // Check that the template argument list is well-formed for this
1762 // class template.
1763 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1764 InstTemplateArgs.size());
1765 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1766 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001767 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001768 false,
1769 Converted))
1770 return true;
1771
1772 // Figure out where to insert this class template partial specialization
1773 // in the member template's set of class template partial specializations.
1774 llvm::FoldingSetNodeID ID;
1775 ClassTemplatePartialSpecializationDecl::Profile(ID,
1776 Converted.getFlatArguments(),
1777 Converted.flatSize(),
1778 SemaRef.Context);
1779 void *InsertPos = 0;
1780 ClassTemplateSpecializationDecl *PrevDecl
1781 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1782 InsertPos);
1783
1784 // Build the canonical type that describes the converted template
1785 // arguments of the class template partial specialization.
1786 QualType CanonType
1787 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1788 Converted.getFlatArguments(),
1789 Converted.flatSize());
1790
1791 // Build the fully-sugared type for this class template
1792 // specialization as the user wrote in the specialization
1793 // itself. This means that we'll pretty-print the type retrieved
1794 // from the specialization's declaration the way that the user
1795 // actually wrote the specialization, rather than formatting the
1796 // name based on the "canonical" representation used to store the
1797 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001798 TypeSourceInfo *WrittenTy
1799 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1800 TemplateName(ClassTemplate),
1801 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001802 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001803 CanonType);
1804
1805 if (PrevDecl) {
1806 // We've already seen a partial specialization with the same template
1807 // parameters and template arguments. This can happen, for example, when
1808 // substituting the outer template arguments ends up causing two
1809 // class template partial specializations of a member class template
1810 // to have identical forms, e.g.,
1811 //
1812 // template<typename T, typename U>
1813 // struct Outer {
1814 // template<typename X, typename Y> struct Inner;
1815 // template<typename Y> struct Inner<T, Y>;
1816 // template<typename Y> struct Inner<U, Y>;
1817 // };
1818 //
1819 // Outer<int, int> outer; // error: the partial specializations of Inner
1820 // // have the same signature.
1821 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1822 << WrittenTy;
1823 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1824 << SemaRef.Context.getTypeDeclType(PrevDecl);
1825 return true;
1826 }
1827
1828
1829 // Create the class template partial specialization declaration.
1830 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001831 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1832 PartialSpec->getTagKind(),
1833 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001834 PartialSpec->getLocation(),
1835 InstParams,
1836 ClassTemplate,
1837 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001838 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001839 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001840 0,
1841 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001842 // Substitute the nested name specifier, if any.
1843 if (SubstQualifier(PartialSpec, InstPartialSpec))
1844 return 0;
1845
Douglas Gregored9c0f92009-10-29 00:04:11 +00001846 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001847 InstPartialSpec->setTypeAsWritten(WrittenTy);
1848 InstantiateAttrs(PartialSpec, InstPartialSpec);
1849
Douglas Gregored9c0f92009-10-29 00:04:11 +00001850 // Add this partial specialization to the set of class template partial
1851 // specializations.
1852 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1853 InsertPos);
1854 return false;
1855}
1856
John McCall21ef0fa2010-03-11 09:03:00 +00001857TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001858TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001859 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001860 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1861 assert(OldTInfo && "substituting function without type source info");
1862 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001863 TypeSourceInfo *NewTInfo
1864 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1865 D->getTypeSpecStartLoc(),
1866 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001867 if (!NewTInfo)
1868 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001869
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001870 if (NewTInfo != OldTInfo) {
1871 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001872 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001873 if (FunctionProtoTypeLoc *OldProtoLoc
1874 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1875 TypeLoc NewTL = NewTInfo->getTypeLoc();
1876 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1877 assert(NewProtoLoc && "Missing prototype?");
1878 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1879 // FIXME: Variadic templates will break this.
1880 Params.push_back(NewProtoLoc->getArg(i));
1881 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001882 OldProtoLoc->getArg(i),
1883 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001884 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001885 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001886 } else {
1887 // The function type itself was not dependent and therefore no
1888 // substitution occurred. However, we still need to instantiate
1889 // the function parameters themselves.
1890 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001891 if (FunctionProtoTypeLoc *OldProtoLoc
1892 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1893 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1894 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1895 if (!Parm)
1896 return 0;
1897 Params.push_back(Parm);
1898 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001899 }
1900 }
John McCall21ef0fa2010-03-11 09:03:00 +00001901 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001902}
1903
Mike Stump1eb44332009-09-09 15:08:12 +00001904/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001905/// declaration (New) from the corresponding fields of its template (Tmpl).
1906///
1907/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001908bool
1909TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001910 FunctionDecl *Tmpl) {
1911 if (Tmpl->isDeleted())
1912 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Douglas Gregor7d2bcc72010-05-19 03:39:53 +00001914 if (InstantiateAttrs(Tmpl, New))
1915 return true;
1916
Douglas Gregorcca9e962009-07-01 22:01:06 +00001917 // If we are performing substituting explicitly-specified template arguments
1918 // or deduced template arguments into a function template and we reach this
1919 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001920 // to keeping the new function template specialization. We therefore
1921 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001922 // into a template instantiation for this specific function template
1923 // specialization, which is not a SFINAE context, so that we diagnose any
1924 // further errors in the declaration itself.
1925 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1926 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1927 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1928 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001929 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001930 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001931 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001932 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001933 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001934 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1935 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001936 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001937 }
1938 }
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001940 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1941 assert(Proto && "Function template without prototype?");
1942
1943 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1944 Proto->getNoReturnAttr()) {
1945 // The function has an exception specification or a "noreturn"
1946 // attribute. Substitute into each of the exception types.
1947 llvm::SmallVector<QualType, 4> Exceptions;
1948 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1949 // FIXME: Poor location information!
1950 QualType T
1951 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1952 New->getLocation(), New->getDeclName());
1953 if (T.isNull() ||
1954 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1955 continue;
1956
1957 Exceptions.push_back(T);
1958 }
1959
1960 // Rebuild the function type
1961
1962 const FunctionProtoType *NewProto
1963 = New->getType()->getAs<FunctionProtoType>();
1964 assert(NewProto && "Template instantiation without function prototype?");
1965 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1966 NewProto->arg_type_begin(),
1967 NewProto->getNumArgs(),
1968 NewProto->isVariadic(),
1969 NewProto->getTypeQuals(),
1970 Proto->hasExceptionSpec(),
1971 Proto->hasAnyExceptionSpec(),
1972 Exceptions.size(),
1973 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001974 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001975 }
1976
Douglas Gregore53060f2009-06-25 22:08:12 +00001977 return false;
1978}
1979
Douglas Gregor5545e162009-03-24 00:38:23 +00001980/// \brief Initializes common fields of an instantiated method
1981/// declaration (New) from the corresponding fields of its template
1982/// (Tmpl).
1983///
1984/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001985bool
1986TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001987 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001988 if (InitFunctionInstantiation(New, Tmpl))
1989 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Douglas Gregor5545e162009-03-24 00:38:23 +00001991 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1992 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001993 if (Tmpl->isVirtualAsWritten())
1994 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001995
1996 // FIXME: attributes
1997 // FIXME: New needs a pointer to Tmpl
1998 return false;
1999}
Douglas Gregora58861f2009-05-13 20:28:22 +00002000
2001/// \brief Instantiate the definition of the given function from its
2002/// template.
2003///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002004/// \param PointOfInstantiation the point at which the instantiation was
2005/// required. Note that this is not precisely a "point of instantiation"
2006/// for the function, but it's close.
2007///
Douglas Gregora58861f2009-05-13 20:28:22 +00002008/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002009/// function template specialization or member function of a class template
2010/// specialization.
2011///
2012/// \param Recursive if true, recursively instantiates any functions that
2013/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002014///
2015/// \param DefinitionRequired if true, then we are performing an explicit
2016/// instantiation where the body of the function is required. Complain if
2017/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002018void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002019 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002020 bool Recursive,
2021 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00002022 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002023 return;
2024
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002025 // Never instantiate an explicit specialization.
2026 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2027 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002028
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002029 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002030 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002031 Stmt *Pattern = 0;
2032 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002033 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002034
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002035 if (!Pattern) {
2036 if (DefinitionRequired) {
2037 if (Function->getPrimaryTemplate())
2038 Diag(PointOfInstantiation,
2039 diag::err_explicit_instantiation_undefined_func_template)
2040 << Function->getPrimaryTemplate();
2041 else
2042 Diag(PointOfInstantiation,
2043 diag::err_explicit_instantiation_undefined_member)
2044 << 1 << Function->getDeclName() << Function->getDeclContext();
2045
2046 if (PatternDecl)
2047 Diag(PatternDecl->getLocation(),
2048 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002049 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002050 }
2051
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002052 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002053 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002054
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002055 // C++0x [temp.explicit]p9:
2056 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002057 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002058 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002059 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002060 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002061 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002062 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002064 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2065 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002066 return;
2067
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002068 // If we're performing recursive template instantiation, create our own
2069 // queue of pending implicit instantiations that we will instantiate later,
2070 // while we're still within our own instantiation context.
2071 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2072 if (Recursive)
2073 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Douglas Gregor9679caf2010-05-12 17:27:19 +00002075 EnterExpressionEvaluationContext EvalContext(*this,
2076 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002077 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2078
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002079 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002080 // recorded, unless we're actually a member function within a local
2081 // class, in which case we need to merge our results with the parent
2082 // scope (of the enclosing function).
2083 bool MergeWithParentScope = false;
2084 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2085 MergeWithParentScope = Rec->isLocalClass();
2086
2087 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002088
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002089 // Introduce the instantiated function parameters into the local
2090 // instantiation scope.
2091 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2092 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2093 Function->getParamDecl(I));
2094
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002095 // Enter the scope of this instantiation. We don't use
2096 // PushDeclContext because we don't have a scope.
2097 DeclContext *PreviousContext = CurContext;
2098 CurContext = Function;
2099
Mike Stump1eb44332009-09-09 15:08:12 +00002100 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002101 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002102
2103 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002104 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002105 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2106 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2107 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002108 }
2109
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002110 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002111 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002112
Douglas Gregor52604ab2009-09-11 21:19:12 +00002113 if (Body.isInvalid())
2114 Function->setInvalidDecl();
2115
Mike Stump1eb44332009-09-09 15:08:12 +00002116 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002117 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002118
John McCall0c01d182010-03-24 05:22:00 +00002119 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2120
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002121 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002122
2123 DeclGroupRef DG(Function);
2124 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Douglas Gregor60406be2010-01-16 22:29:39 +00002126 // This class may have local implicit instantiations that need to be
2127 // instantiation within this scope.
2128 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2129 Scope.Exit();
2130
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002131 if (Recursive) {
2132 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002133 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002134 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002135
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002136 // Restore the set of pending implicit instantiations.
2137 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2138 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002139}
2140
2141/// \brief Instantiate the definition of the given variable from its
2142/// template.
2143///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002144/// \param PointOfInstantiation the point at which the instantiation was
2145/// required. Note that this is not precisely a "point of instantiation"
2146/// for the function, but it's close.
2147///
2148/// \param Var the already-instantiated declaration of a static member
2149/// variable of a class template specialization.
2150///
2151/// \param Recursive if true, recursively instantiates any functions that
2152/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002153///
2154/// \param DefinitionRequired if true, then we are performing an explicit
2155/// instantiation where an out-of-line definition of the member variable
2156/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002157void Sema::InstantiateStaticDataMemberDefinition(
2158 SourceLocation PointOfInstantiation,
2159 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002160 bool Recursive,
2161 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002162 if (Var->isInvalidDecl())
2163 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Douglas Gregor7caa6822009-07-24 20:34:43 +00002165 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002166 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002167 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002168 assert(Def->isStaticDataMember() && "Not a static data member?");
2169 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Douglas Gregor0d035142009-10-27 18:42:08 +00002171 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002172 // We did not find an out-of-line definition of this static data member,
2173 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002174 // instantiate this definition (or provide a specialization for it) in
2175 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002176 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002177 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002178 Diag(PointOfInstantiation,
2179 diag::err_explicit_instantiation_undefined_member)
2180 << 2 << Var->getDeclName() << Var->getDeclContext();
2181 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2182 }
2183
Douglas Gregor7caa6822009-07-24 20:34:43 +00002184 return;
2185 }
2186
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002187 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002188 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002189 return;
2190
2191 // C++0x [temp.explicit]p9:
2192 // Except for inline functions, other explicit instantiation declarations
2193 // have the effect of suppressing the implicit instantiation of the entity
2194 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002195 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002196 == TSK_ExplicitInstantiationDeclaration)
2197 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002198
Douglas Gregor7caa6822009-07-24 20:34:43 +00002199 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2200 if (Inst)
2201 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Douglas Gregor7caa6822009-07-24 20:34:43 +00002203 // If we're performing recursive template instantiation, create our own
2204 // queue of pending implicit instantiations that we will instantiate later,
2205 // while we're still within our own instantiation context.
2206 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2207 if (Recursive)
2208 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Douglas Gregor7caa6822009-07-24 20:34:43 +00002210 // Enter the scope of this instantiation. We don't use
2211 // PushDeclContext because we don't have a scope.
2212 DeclContext *PreviousContext = CurContext;
2213 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002215 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002216 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002217 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002218 CurContext = PreviousContext;
2219
2220 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002221 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2222 assert(MSInfo && "Missing member specialization information?");
2223 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2224 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002225 DeclGroupRef DG(Var);
2226 Consumer.HandleTopLevelDecl(DG);
2227 }
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Douglas Gregor7caa6822009-07-24 20:34:43 +00002229 if (Recursive) {
2230 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002231 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002232 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Douglas Gregor7caa6822009-07-24 20:34:43 +00002234 // Restore the set of pending implicit instantiations.
2235 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002236 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002237}
Douglas Gregor815215d2009-05-27 05:35:12 +00002238
Anders Carlsson09025312009-08-29 05:16:22 +00002239void
2240Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2241 const CXXConstructorDecl *Tmpl,
2242 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Anders Carlsson09025312009-08-29 05:16:22 +00002244 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002245 bool AnyErrors = false;
2246
Anders Carlsson09025312009-08-29 05:16:22 +00002247 // Instantiate all the initializers.
2248 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002249 InitsEnd = Tmpl->init_end();
2250 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002251 CXXBaseOrMemberInitializer *Init = *Inits;
2252
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002253 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002254 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002255 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002257 // Instantiate the initializer.
2258 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2259 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2260 AnyErrors = true;
2261 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002262 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002263
Anders Carlsson09025312009-08-29 05:16:22 +00002264 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002265 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002266 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002267 TemplateArgs,
2268 Init->getSourceLocation(),
2269 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002270 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002271 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002272 New->setInvalidDecl();
2273 continue;
2274 }
2275
John McCalla93c9342009-12-07 02:54:59 +00002276 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002277 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002278 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002279 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002280 Init->getRParenLoc(),
2281 New->getParent());
2282 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002283 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002285 // Is this an anonymous union?
2286 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002287 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2288 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002289 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002290 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2291 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002292 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002293
2294 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002295 NewArgs.size(),
2296 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002297 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002298 Init->getRParenLoc());
2299 }
2300
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002301 if (NewInit.isInvalid()) {
2302 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002303 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002304 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002305 // FIXME: It would be nice if ASTOwningVector had a release function.
2306 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Anders Carlsson09025312009-08-29 05:16:22 +00002308 NewInits.push_back((MemInitTy *)NewInit.get());
2309 }
2310 }
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Anders Carlsson09025312009-08-29 05:16:22 +00002312 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002313 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002314 /*FIXME: ColonLoc */
2315 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002316 NewInits.data(), NewInits.size(),
2317 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002318}
2319
John McCall52a575a2009-08-29 08:11:13 +00002320// TODO: this could be templated if the various decl types used the
2321// same method name.
2322static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2323 ClassTemplateDecl *Instance) {
2324 Pattern = Pattern->getCanonicalDecl();
2325
2326 do {
2327 Instance = Instance->getCanonicalDecl();
2328 if (Pattern == Instance) return true;
2329 Instance = Instance->getInstantiatedFromMemberTemplate();
2330 } while (Instance);
2331
2332 return false;
2333}
2334
Douglas Gregor0d696532009-09-28 06:34:35 +00002335static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2336 FunctionTemplateDecl *Instance) {
2337 Pattern = Pattern->getCanonicalDecl();
2338
2339 do {
2340 Instance = Instance->getCanonicalDecl();
2341 if (Pattern == Instance) return true;
2342 Instance = Instance->getInstantiatedFromMemberTemplate();
2343 } while (Instance);
2344
2345 return false;
2346}
2347
Douglas Gregored9c0f92009-10-29 00:04:11 +00002348static bool
2349isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2350 ClassTemplatePartialSpecializationDecl *Instance) {
2351 Pattern
2352 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2353 do {
2354 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2355 Instance->getCanonicalDecl());
2356 if (Pattern == Instance)
2357 return true;
2358 Instance = Instance->getInstantiatedFromMember();
2359 } while (Instance);
2360
2361 return false;
2362}
2363
John McCall52a575a2009-08-29 08:11:13 +00002364static bool isInstantiationOf(CXXRecordDecl *Pattern,
2365 CXXRecordDecl *Instance) {
2366 Pattern = Pattern->getCanonicalDecl();
2367
2368 do {
2369 Instance = Instance->getCanonicalDecl();
2370 if (Pattern == Instance) return true;
2371 Instance = Instance->getInstantiatedFromMemberClass();
2372 } while (Instance);
2373
2374 return false;
2375}
2376
2377static bool isInstantiationOf(FunctionDecl *Pattern,
2378 FunctionDecl *Instance) {
2379 Pattern = Pattern->getCanonicalDecl();
2380
2381 do {
2382 Instance = Instance->getCanonicalDecl();
2383 if (Pattern == Instance) return true;
2384 Instance = Instance->getInstantiatedFromMemberFunction();
2385 } while (Instance);
2386
2387 return false;
2388}
2389
2390static bool isInstantiationOf(EnumDecl *Pattern,
2391 EnumDecl *Instance) {
2392 Pattern = Pattern->getCanonicalDecl();
2393
2394 do {
2395 Instance = Instance->getCanonicalDecl();
2396 if (Pattern == Instance) return true;
2397 Instance = Instance->getInstantiatedFromMemberEnum();
2398 } while (Instance);
2399
2400 return false;
2401}
2402
John McCalled976492009-12-04 22:46:56 +00002403static bool isInstantiationOf(UsingShadowDecl *Pattern,
2404 UsingShadowDecl *Instance,
2405 ASTContext &C) {
2406 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2407}
2408
2409static bool isInstantiationOf(UsingDecl *Pattern,
2410 UsingDecl *Instance,
2411 ASTContext &C) {
2412 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2413}
2414
John McCall7ba107a2009-11-18 02:36:19 +00002415static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2416 UsingDecl *Instance,
2417 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002418 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002419}
2420
2421static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002422 UsingDecl *Instance,
2423 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002424 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002425}
2426
John McCall52a575a2009-08-29 08:11:13 +00002427static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2428 VarDecl *Instance) {
2429 assert(Instance->isStaticDataMember());
2430
2431 Pattern = Pattern->getCanonicalDecl();
2432
2433 do {
2434 Instance = Instance->getCanonicalDecl();
2435 if (Pattern == Instance) return true;
2436 Instance = Instance->getInstantiatedFromStaticDataMember();
2437 } while (Instance);
2438
2439 return false;
2440}
2441
John McCalled976492009-12-04 22:46:56 +00002442// Other is the prospective instantiation
2443// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002444static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002445 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002446 if (UnresolvedUsingTypenameDecl *UUD
2447 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2448 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2449 return isInstantiationOf(UUD, UD, Ctx);
2450 }
2451 }
2452
2453 if (UnresolvedUsingValueDecl *UUD
2454 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002455 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2456 return isInstantiationOf(UUD, UD, Ctx);
2457 }
2458 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002459
Anders Carlsson0d8df782009-08-29 19:37:28 +00002460 return false;
2461 }
Mike Stump1eb44332009-09-09 15:08:12 +00002462
John McCall52a575a2009-08-29 08:11:13 +00002463 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2464 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002465
John McCall52a575a2009-08-29 08:11:13 +00002466 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2467 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002468
John McCall52a575a2009-08-29 08:11:13 +00002469 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2470 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002471
Douglas Gregor7caa6822009-07-24 20:34:43 +00002472 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002473 if (Var->isStaticDataMember())
2474 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2475
2476 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2477 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002478
Douglas Gregor0d696532009-09-28 06:34:35 +00002479 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2480 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2481
Douglas Gregored9c0f92009-10-29 00:04:11 +00002482 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2483 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2484 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2485 PartialSpec);
2486
Anders Carlssond8b285f2009-09-01 04:26:58 +00002487 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2488 if (!Field->getDeclName()) {
2489 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002490 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002491 cast<FieldDecl>(D);
2492 }
2493 }
Mike Stump1eb44332009-09-09 15:08:12 +00002494
John McCalled976492009-12-04 22:46:56 +00002495 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2496 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2497
2498 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2499 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2500
Douglas Gregor815215d2009-05-27 05:35:12 +00002501 return D->getDeclName() && isa<NamedDecl>(Other) &&
2502 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2503}
2504
2505template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002506static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002507 NamedDecl *D,
2508 ForwardIterator first,
2509 ForwardIterator last) {
2510 for (; first != last; ++first)
2511 if (isInstantiationOf(Ctx, D, *first))
2512 return cast<NamedDecl>(*first);
2513
2514 return 0;
2515}
2516
John McCall02cace72009-08-28 07:59:38 +00002517/// \brief Finds the instantiation of the given declaration context
2518/// within the current instantiation.
2519///
2520/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002521DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002522 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002523 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002524 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002525 return cast_or_null<DeclContext>(ID);
2526 } else return DC;
2527}
2528
Douglas Gregored961e72009-05-27 17:54:46 +00002529/// \brief Find the instantiation of the given declaration within the
2530/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002531///
2532/// This routine is intended to be used when \p D is a declaration
2533/// referenced from within a template, that needs to mapped into the
2534/// corresponding declaration within an instantiation. For example,
2535/// given:
2536///
2537/// \code
2538/// template<typename T>
2539/// struct X {
2540/// enum Kind {
2541/// KnownValue = sizeof(T)
2542/// };
2543///
2544/// bool getKind() const { return KnownValue; }
2545/// };
2546///
2547/// template struct X<int>;
2548/// \endcode
2549///
2550/// In the instantiation of X<int>::getKind(), we need to map the
2551/// EnumConstantDecl for KnownValue (which refers to
2552/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002553/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2554/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002555NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002556 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002557 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002558 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002559 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002560 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002561 // D is a local of some kind. Look into the map of local
2562 // declarations to their instantiations.
2563 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2564 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002565
Douglas Gregore95b4092009-09-16 18:34:49 +00002566 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2567 if (!Record->isDependentContext())
2568 return D;
2569
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002570 // If the RecordDecl is actually the injected-class-name or a
2571 // "templated" declaration for a class template, class template
2572 // partial specialization, or a member class of a class template,
2573 // substitute into the injected-class-name of the class template
2574 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002575 QualType T;
2576 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2577
2578 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002579 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002580 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2581 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002582 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002583
2584 // If we call SubstType with an InjectedClassNameType here we
2585 // can end up in an infinite loop.
2586 T = Context.getTypeDeclType(Record);
2587 assert(isa<InjectedClassNameType>(T) &&
2588 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002589 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002590 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002591
2592 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002593 // Substitute into the injected-class-name to get the type
2594 // corresponding to the instantiation we want, which may also be
2595 // the current instantiation (if we're in a template
2596 // definition). This substitution should never fail, since we
2597 // know we can instantiate the injected-class-name or we
2598 // wouldn't have gotten to the injected-class-name!
2599
2600 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2601 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002602 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2603 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2604
2605 if (!T->isDependentType()) {
2606 assert(T->isRecordType() && "Instantiation must produce a record type");
2607 return T->getAs<RecordType>()->getDecl();
2608 }
2609
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002610 // We are performing "partial" template instantiation to create
2611 // the member declarations for the members of a class template
2612 // specialization. Therefore, D is actually referring to something
2613 // in the current instantiation. Look through the current
2614 // context, which contains actual instantiations, to find the
2615 // instantiation of the "current instantiation" that D refers
2616 // to.
2617 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002618 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002619 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002620 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002621 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002622 if (isInstantiationOf(ClassTemplate,
2623 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002624 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002625
2626 if (!DC->isDependentContext())
2627 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002628 }
2629
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002630 // We're performing "instantiation" of a member of the current
2631 // instantiation while we are type-checking the
2632 // definition. Compute the declaration context and return that.
2633 assert(!SawNonDependentContext &&
2634 "No dependent context while instantiating record");
2635 DeclContext *DC = computeDeclContext(T);
2636 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002637 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002638 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002639 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002640
Douglas Gregore95b4092009-09-16 18:34:49 +00002641 // Fall through to deal with other dependent record types (e.g.,
2642 // anonymous unions in class templates).
2643 }
John McCall52a575a2009-08-29 08:11:13 +00002644
Douglas Gregore95b4092009-09-16 18:34:49 +00002645 if (!ParentDC->isDependentContext())
2646 return D;
2647
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002648 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002649 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002650 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Douglas Gregor815215d2009-05-27 05:35:12 +00002652 if (ParentDC != D->getDeclContext()) {
2653 // We performed some kind of instantiation in the parent context,
2654 // so now we need to look into the instantiated parent context to
2655 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002656
John McCall3cb0ebd2010-03-10 03:28:59 +00002657 // If our context used to be dependent, we may need to instantiate
2658 // it before performing lookup into that context.
2659 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002660 if (!Spec->isDependentContext()) {
2661 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002662 const RecordType *Tag = T->getAs<RecordType>();
2663 assert(Tag && "type of non-dependent record is not a RecordType");
2664 if (!Tag->isBeingDefined() &&
2665 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2666 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002667 }
2668 }
2669
Douglas Gregor815215d2009-05-27 05:35:12 +00002670 NamedDecl *Result = 0;
2671 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002672 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002673 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2674 } else {
2675 // Since we don't have a name for the entity we're looking for,
2676 // our only option is to walk through all of the declarations to
2677 // find that name. This will occur in a few cases:
2678 //
2679 // - anonymous struct/union within a template
2680 // - unnamed class/struct/union/enum within a template
2681 //
2682 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002683 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002684 ParentDC->decls_begin(),
2685 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002686 }
Mike Stump1eb44332009-09-09 15:08:12 +00002687
John McCall9f54ad42009-12-10 09:41:52 +00002688 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002689 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2690 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002691 && "Unable to find instantiation of declaration!");
2692
Douglas Gregor815215d2009-05-27 05:35:12 +00002693 D = Result;
2694 }
2695
Douglas Gregor815215d2009-05-27 05:35:12 +00002696 return D;
2697}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002698
Mike Stump1eb44332009-09-09 15:08:12 +00002699/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002700/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002701void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2702 while (!PendingLocalImplicitInstantiations.empty() ||
2703 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2704 PendingImplicitInstantiation Inst;
2705
2706 if (PendingLocalImplicitInstantiations.empty()) {
2707 Inst = PendingImplicitInstantiations.front();
2708 PendingImplicitInstantiations.pop_front();
2709 } else {
2710 Inst = PendingLocalImplicitInstantiations.front();
2711 PendingLocalImplicitInstantiations.pop_front();
2712 }
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Douglas Gregor7caa6822009-07-24 20:34:43 +00002714 // Instantiate function definitions
2715 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002716 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002717 Function->getLocation(), *this,
2718 Context.getSourceManager(),
2719 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002720
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002721 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002722 continue;
2723 }
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregor7caa6822009-07-24 20:34:43 +00002725 // Instantiate static data member definitions.
2726 VarDecl *Var = cast<VarDecl>(Inst.first);
2727 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002728
Chandler Carruth291b4412010-02-13 10:17:50 +00002729 // Don't try to instantiate declarations if the most recent redeclaration
2730 // is invalid.
2731 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2732 continue;
2733
2734 // Check if the most recent declaration has changed the specialization kind
2735 // and removed the need for implicit instantiation.
2736 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2737 case TSK_Undeclared:
2738 assert(false && "Cannot instantitiate an undeclared specialization.");
2739 case TSK_ExplicitInstantiationDeclaration:
2740 case TSK_ExplicitInstantiationDefinition:
2741 case TSK_ExplicitSpecialization:
2742 continue; // No longer need implicit instantiation.
2743 case TSK_ImplicitInstantiation:
2744 break;
2745 }
2746
Mike Stump1eb44332009-09-09 15:08:12 +00002747 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002748 Var->getLocation(), *this,
2749 Context.getSourceManager(),
2750 "instantiating static data member "
2751 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Douglas Gregor7caa6822009-07-24 20:34:43 +00002753 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002754 }
2755}
John McCall0c01d182010-03-24 05:22:00 +00002756
2757void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2758 const MultiLevelTemplateArgumentList &TemplateArgs) {
2759 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2760 E = Pattern->ddiag_end(); I != E; ++I) {
2761 DependentDiagnostic *DD = *I;
2762
2763 switch (DD->getKind()) {
2764 case DependentDiagnostic::Access:
2765 HandleDependentAccessCheck(*DD, TemplateArgs);
2766 break;
2767 }
2768 }
2769}