blob: 592b474dfb762b5f0cbf80c20a68a492d0c6f6de [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 Gregor4469e8a2010-05-19 17:02:24 +000034 void 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 Gregor4469e8a2010-05-19 17:02:24 +0000146void 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 }
155}
156
Douglas Gregor4f722be2009-03-25 15:45:12 +0000157Decl *
158TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
159 assert(false && "Translation units cannot be instantiated");
160 return D;
161}
162
163Decl *
164TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
165 assert(false && "Namespaces cannot be instantiated");
166 return D;
167}
168
John McCall3dbd3d52010-02-16 06:53:13 +0000169Decl *
170TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
171 NamespaceAliasDecl *Inst
172 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
173 D->getNamespaceLoc(),
174 D->getAliasLoc(),
175 D->getNamespace()->getIdentifier(),
176 D->getQualifierRange(),
177 D->getQualifier(),
178 D->getTargetNameLoc(),
179 D->getNamespace());
180 Owner->addDecl(Inst);
181 return Inst;
182}
183
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000184Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
185 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000186 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000187 if (DI->getType()->isDependentType()) {
188 DI = SemaRef.SubstType(DI, TemplateArgs,
189 D->getLocation(), D->getDeclName());
190 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000191 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000192 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000193 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000194 } else {
195 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000198 // Create the new typedef
199 TypedefDecl *Typedef
200 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000201 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000202 if (Invalid)
203 Typedef->setInvalidDecl();
204
Douglas Gregord57a38e2010-04-23 16:25:07 +0000205 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
206 TagDecl *TD = TT->getDecl();
207
208 // If the TagDecl that the TypedefDecl points to is an anonymous decl
209 // keep track of the TypedefDecl.
210 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
211 TD->setTypedefForAnonDecl(Typedef);
212 }
213
John McCall5126fd02009-12-30 00:31:22 +0000214 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000215 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
216 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000217 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
218 }
219
Douglas Gregora6b09072010-05-17 23:46:49 +0000220 InstantiateAttrs(D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000221
John McCall46460a62010-01-20 21:53:11 +0000222 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000223 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000225 return Typedef;
226}
227
Douglas Gregor6eef5192009-12-14 19:27:10 +0000228/// \brief Instantiate the arguments provided as part of initialization.
229///
230/// \returns true if an error occurred, false otherwise.
231static bool InstantiateInitializationArguments(Sema &SemaRef,
232 Expr **Args, unsigned NumArgs,
233 const MultiLevelTemplateArgumentList &TemplateArgs,
234 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
235 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
236 for (unsigned I = 0; I != NumArgs; ++I) {
237 // When we hit the first defaulted argument, break out of the loop:
238 // we don't pass those default arguments on.
239 if (Args[I]->isDefaultArgument())
240 break;
241
242 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
243 if (Arg.isInvalid())
244 return true;
245
246 Expr *ArgExpr = (Expr *)Arg.get();
247 InitArgs.push_back(Arg.release());
248
249 // FIXME: We're faking all of the comma locations. Do we need them?
250 FakeCommaLocs.push_back(
251 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
252 }
253
254 return false;
255}
256
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000257/// \brief Instantiate an initializer, breaking it into separate
258/// initialization arguments.
259///
260/// \param S The semantic analysis object.
261///
262/// \param Init The initializer to instantiate.
263///
264/// \param TemplateArgs Template arguments to be substituted into the
265/// initializer.
266///
267/// \param NewArgs Will be filled in with the instantiation arguments.
268///
269/// \returns true if an error occurred, false otherwise
270static bool InstantiateInitializer(Sema &S, Expr *Init,
271 const MultiLevelTemplateArgumentList &TemplateArgs,
272 SourceLocation &LParenLoc,
273 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
274 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
275 SourceLocation &RParenLoc) {
276 NewArgs.clear();
277 LParenLoc = SourceLocation();
278 RParenLoc = SourceLocation();
279
280 if (!Init)
281 return false;
282
283 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
284 Init = ExprTemp->getSubExpr();
285
286 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
287 Init = Binder->getSubExpr();
288
289 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
290 Init = ICE->getSubExprAsWritten();
291
292 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
293 LParenLoc = ParenList->getLParenLoc();
294 RParenLoc = ParenList->getRParenLoc();
295 return InstantiateInitializationArguments(S, ParenList->getExprs(),
296 ParenList->getNumExprs(),
297 TemplateArgs, CommaLocs,
298 NewArgs);
299 }
300
301 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000302 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
303 if (InstantiateInitializationArguments(S,
304 Construct->getArgs(),
305 Construct->getNumArgs(),
306 TemplateArgs,
307 CommaLocs, NewArgs))
308 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000309
Douglas Gregor28329e52010-03-24 21:22:47 +0000310 // FIXME: Fake locations!
311 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
312 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
313 return false;
314 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000315 }
316
317 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
318 if (Result.isInvalid())
319 return true;
320
321 NewArgs.push_back(Result.takeAs<Expr>());
322 return false;
323}
324
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000325Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000326 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000327 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000328 TemplateArgs,
329 D->getTypeSpecStartLoc(),
330 D->getDeclName());
331 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000332 return 0;
333
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000334 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000335 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
336 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000337 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000338 D->getStorageClass(),
339 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000340 Var->setThreadSpecified(D->isThreadSpecified());
341 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
342 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000343
John McCallb6217662010-03-15 10:12:16 +0000344 // Substitute the nested name specifier, if any.
345 if (SubstQualifier(D, Var))
346 return 0;
347
Mike Stump1eb44332009-09-09 15:08:12 +0000348 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000349 // out-of-line, the instantiation will have the same lexical
350 // context (which will be a namespace scope) as the template.
351 if (D->isOutOfLine())
352 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000353
John McCall46460a62010-01-20 21:53:11 +0000354 Var->setAccess(D->getAccess());
Douglas Gregor5764f612010-05-08 23:05:03 +0000355 Var->setUsed(D->isUsed());
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000356
Mike Stump390b4cc2009-05-16 07:39:55 +0000357 // FIXME: In theory, we could have a previous declaration for variables that
358 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000359 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000360 // FIXME: having to fake up a LookupResult is dumb.
361 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000362 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000363 if (D->isStaticDataMember())
364 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000365 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Douglas Gregor7caa6822009-07-24 20:34:43 +0000367 if (D->isOutOfLine()) {
368 D->getLexicalDeclContext()->addDecl(Var);
369 Owner->makeDeclVisibleInContext(Var);
370 } else {
371 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000372
373 if (Owner->isFunctionOrMethod())
374 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000377 // Link instantiations of static data members back to the template from
378 // which they were instantiated.
379 if (Var->isStaticDataMember())
380 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000381 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000382
Douglas Gregor60c93c92010-02-09 07:26:29 +0000383 if (Var->getAnyInitializer()) {
384 // We already have an initializer in the class.
385 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000386 if (Var->isStaticDataMember() && !D->isOutOfLine())
387 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
388 else
389 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
390
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000391 // Instantiate the initializer.
392 SourceLocation LParenLoc, RParenLoc;
393 llvm::SmallVector<SourceLocation, 4> CommaLocs;
394 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
395 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
396 CommaLocs, InitArgs, RParenLoc)) {
397 // Attach the initializer to the declaration.
398 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000399 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000400 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000401 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000402 move_arg(InitArgs),
403 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000404 RParenLoc);
405 } else if (InitArgs.size() == 1) {
406 Expr *Init = (Expr*)(InitArgs.take()[0]);
407 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
408 SemaRef.Owned(Init),
409 false);
410 } else {
411 assert(InitArgs.size() == 0);
412 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000413 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000414 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000415 // FIXME: Not too happy about invalidating the declaration
416 // because of a bogus initializer.
417 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000418 }
419
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000420 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000421 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
422 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000423
Douglas Gregor5764f612010-05-08 23:05:03 +0000424 // Diagnose unused local variables.
425 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
426 SemaRef.DiagnoseUnusedDecl(Var);
427
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000428 return Var;
429}
430
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000431Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
432 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000433 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000434 if (DI->getType()->isDependentType()) {
435 DI = SemaRef.SubstType(DI, TemplateArgs,
436 D->getLocation(), D->getDeclName());
437 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000438 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000439 Invalid = true;
440 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000441 // C++ [temp.arg.type]p3:
442 // If a declaration acquires a function type through a type
443 // dependent on a template-parameter and this causes a
444 // declaration that does not use the syntactic form of a
445 // function declarator to have function type, the program is
446 // ill-formed.
447 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000448 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000449 Invalid = true;
450 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000451 } else {
452 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000453 }
454
455 Expr *BitWidth = D->getBitWidth();
456 if (Invalid)
457 BitWidth = 0;
458 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000459 // The bit-width expression is not potentially evaluated.
460 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000462 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000463 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000464 if (InstantiatedBitWidth.isInvalid()) {
465 Invalid = true;
466 BitWidth = 0;
467 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000468 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 }
470
John McCall07fb6be2009-10-22 23:33:21 +0000471 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
472 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000473 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474 D->getLocation(),
475 D->isMutable(),
476 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000477 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000478 D->getAccess(),
479 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000480 if (!Field) {
481 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000482 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000485 InstantiateAttrs(D, Field);
486
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000487 if (Invalid)
488 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000490 if (!Field->getDeclName()) {
491 // Keep track of where this decl came from.
492 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000495 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000496 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000497 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000498
499 return Field;
500}
501
John McCall02cace72009-08-28 07:59:38 +0000502Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000503 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000504 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000505 if (TypeSourceInfo *Ty = D->getFriendType()) {
506 TypeSourceInfo *InstTy =
507 SemaRef.SubstType(Ty, TemplateArgs,
508 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000509 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000510 return 0;
John McCall02cace72009-08-28 07:59:38 +0000511
Douglas Gregor06245bf2010-04-07 17:57:12 +0000512 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
513 if (!FD)
514 return 0;
515
516 FD->setAccess(AS_public);
517 Owner->addDecl(FD);
518 return FD;
519 }
520
521 NamedDecl *ND = D->getFriendDecl();
522 assert(ND && "friend decl must be a decl or a type!");
523
John McCallaf2094e2010-04-08 09:05:18 +0000524 // All of the Visit implementations for the various potential friend
525 // declarations have to be carefully written to work for friend
526 // objects, with the most important detail being that the target
527 // decl should almost certainly not be placed in Owner.
528 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000529 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
John McCall02cace72009-08-28 07:59:38 +0000531 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000532 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
533 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000534 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000535 Owner->addDecl(FD);
536 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000537}
538
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000539Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
540 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregorac7610d2009-06-22 20:57:11 +0000542 // The expression in a static assertion is not potentially evaluated.
543 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000545 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000546 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000547 if (InstantiatedAssertExpr.isInvalid())
548 return 0;
549
Douglas Gregor43d9d922009-08-08 01:41:12 +0000550 OwningExprResult Message(SemaRef, D->getMessage());
551 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000552 Decl *StaticAssert
553 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000554 move(InstantiatedAssertExpr),
555 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000556 return StaticAssert;
557}
558
559Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000560 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000561 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000562 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000563 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000564 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000565 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000566 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000567 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 Enum->startDefinition();
569
Douglas Gregor96084f12010-03-01 19:00:07 +0000570 if (D->getDeclContext()->isFunctionOrMethod())
571 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
572
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000573 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000574
575 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000576 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
577 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578 EC != ECEnd; ++EC) {
579 // The specified value for the enumerator.
580 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000581 if (Expr *UninstValue = EC->getInitExpr()) {
582 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000583 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000584 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
John McCallce3ff2b2009-08-25 22:02:44 +0000586 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000587 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000588
589 // Drop the initial value and continue.
590 bool isInvalid = false;
591 if (Value.isInvalid()) {
592 Value = SemaRef.Owned((Expr *)0);
593 isInvalid = true;
594 }
595
Mike Stump1eb44332009-09-09 15:08:12 +0000596 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000597 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
598 EC->getLocation(), EC->getIdentifier(),
599 move(Value));
600
601 if (isInvalid) {
602 if (EnumConst)
603 EnumConst->setInvalidDecl();
604 Enum->setInvalidDecl();
605 }
606
607 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000608 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000609 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000610 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000611 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000612
613 if (D->getDeclContext()->isFunctionOrMethod()) {
614 // If the enumeration is within a function or method, record the enum
615 // constant as a local.
616 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
617 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000618 }
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000621 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000622 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000623 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
624 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000625 &Enumerators[0], Enumerators.size(),
626 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000627
628 return Enum;
629}
630
Douglas Gregor6477b692009-03-25 15:04:13 +0000631Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
632 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
633 return 0;
634}
635
John McCalle29ba202009-08-20 01:44:21 +0000636Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000637 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
638
Douglas Gregor550d9b22009-10-31 17:21:17 +0000639 // Create a local instantiation scope for this class template, which
640 // will contain the instantiations of the template parameters.
641 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000642 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000643 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000644 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000645 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000646
647 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000648
649 // Instantiate the qualifier. We have to do this first in case
650 // we're a friend declaration, because if we are then we need to put
651 // the new declaration in the appropriate context.
652 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
653 if (Qualifier) {
654 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
655 Pattern->getQualifierRange(),
656 TemplateArgs);
657 if (!Qualifier) return 0;
658 }
659
660 CXXRecordDecl *PrevDecl = 0;
661 ClassTemplateDecl *PrevClassTemplate = 0;
662
663 // If this isn't a friend, then it's a member template, in which
664 // case we just want to build the instantiation in the
665 // specialization. If it is a friend, we want to build it in
666 // the appropriate context.
667 DeclContext *DC = Owner;
668 if (isFriend) {
669 if (Qualifier) {
670 CXXScopeSpec SS;
671 SS.setScopeRep(Qualifier);
672 SS.setRange(Pattern->getQualifierRange());
673 DC = SemaRef.computeDeclContext(SS);
674 if (!DC) return 0;
675 } else {
676 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
677 Pattern->getDeclContext(),
678 TemplateArgs);
679 }
680
681 // Look for a previous declaration of the template in the owning
682 // context.
683 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
684 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
685 SemaRef.LookupQualifiedName(R, DC);
686
687 if (R.isSingleResult()) {
688 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
689 if (PrevClassTemplate)
690 PrevDecl = PrevClassTemplate->getTemplatedDecl();
691 }
692
693 if (!PrevClassTemplate && Qualifier) {
694 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000695 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
696 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000697 return 0;
698 }
699
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000700 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000701 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000702 bool Complain = true;
703
704 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
705 // template for struct std::tr1::__detail::_Map_base, where the
706 // template parameters of the friend declaration don't match the
707 // template parameters of the original declaration. In this one
708 // case, we don't complain about the ill-formed friend
709 // declaration.
710 if (isFriend && Pattern->getIdentifier() &&
711 Pattern->getIdentifier()->isStr("_Map_base") &&
712 DC->isNamespace() &&
713 cast<NamespaceDecl>(DC)->getIdentifier() &&
714 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
715 DeclContext *DCParent = DC->getParent();
716 if (DCParent->isNamespace() &&
717 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
718 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
719 DeclContext *DCParent2 = DCParent->getParent();
720 if (DCParent2->isNamespace() &&
721 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
722 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
723 DCParent2->getParent()->isTranslationUnit())
724 Complain = false;
725 }
726 }
727
John McCall93ba8572010-03-25 06:39:04 +0000728 TemplateParameterList *PrevParams
729 = PrevClassTemplate->getTemplateParameters();
730
731 // Make sure the parameter lists match.
732 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000733 Complain,
734 Sema::TPL_TemplateMatch)) {
735 if (Complain)
736 return 0;
737
738 AdoptedPreviousTemplateParams = true;
739 InstParams = PrevParams;
740 }
John McCall93ba8572010-03-25 06:39:04 +0000741
742 // Do some additional validation, then merge default arguments
743 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000744 if (!AdoptedPreviousTemplateParams &&
745 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000746 Sema::TPC_ClassTemplate))
747 return 0;
748 }
749 }
750
John McCalle29ba202009-08-20 01:44:21 +0000751 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000752 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000753 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000754 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000755 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000756
John McCall93ba8572010-03-25 06:39:04 +0000757 if (Qualifier)
758 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000759
John McCalle29ba202009-08-20 01:44:21 +0000760 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000761 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
762 D->getIdentifier(), InstParams, RecordInst,
763 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000764 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000765
John McCall93ba8572010-03-25 06:39:04 +0000766 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000767 if (PrevClassTemplate)
768 Inst->setAccess(PrevClassTemplate->getAccess());
769 else
770 Inst->setAccess(D->getAccess());
771
John McCall93ba8572010-03-25 06:39:04 +0000772 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
773 // TODO: do we want to track the instantiation progeny of this
774 // friend target decl?
775 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000776 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000777 Inst->setInstantiatedFromMemberTemplate(D);
778 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000779
780 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000781 SemaRef.Context.getInjectedClassNameType(RecordInst,
782 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000783
Douglas Gregor259571e2009-10-30 22:42:42 +0000784 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000785 if (isFriend) {
786 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000787 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000788 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000789
John McCalle29ba202009-08-20 01:44:21 +0000790 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000791
Douglas Gregored9c0f92009-10-29 00:04:11 +0000792 // Instantiate all of the partial specializations of this member class
793 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000794 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
795 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000796 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
797 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
798
John McCalle29ba202009-08-20 01:44:21 +0000799 return Inst;
800}
801
Douglas Gregord60e1052009-08-27 16:57:43 +0000802Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000803TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
804 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000805 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
806
807 // Lookup the already-instantiated declaration in the instantiation
808 // of the class template and return that.
809 DeclContext::lookup_result Found
810 = Owner->lookup(ClassTemplate->getDeclName());
811 if (Found.first == Found.second)
812 return 0;
813
814 ClassTemplateDecl *InstClassTemplate
815 = dyn_cast<ClassTemplateDecl>(*Found.first);
816 if (!InstClassTemplate)
817 return 0;
818
819 Decl *DCanon = D->getCanonicalDecl();
820 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
821 P = InstClassTemplate->getPartialSpecializations().begin(),
822 PEnd = InstClassTemplate->getPartialSpecializations().end();
823 P != PEnd; ++P) {
824 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
825 return &*P;
826 }
827
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000828 return 0;
829}
830
831Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000832TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000833 // Create a local instantiation scope for this function template, which
834 // will contain the instantiations of the template parameters and then get
835 // merged with the local instantiation scope for the function template
836 // itself.
837 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000838
Douglas Gregord60e1052009-08-27 16:57:43 +0000839 TemplateParameterList *TempParams = D->getTemplateParameters();
840 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000841 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000842 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000843
Douglas Gregora735b202009-10-13 14:39:41 +0000844 FunctionDecl *Instantiated = 0;
845 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
846 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
847 InstParams));
848 else
849 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
850 D->getTemplatedDecl(),
851 InstParams));
852
853 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000854 return 0;
855
John McCall46460a62010-01-20 21:53:11 +0000856 Instantiated->setAccess(D->getAccess());
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000859 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000860 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000861 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000862 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000863 assert(InstTemplate &&
864 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000865
John McCallb1a56e72010-03-26 23:10:15 +0000866 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
867
John McCalle976ffe2009-12-14 23:19:40 +0000868 // Link the instantiation back to the pattern *unless* this is a
869 // non-definition friend declaration.
870 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000871 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000872 InstTemplate->setInstantiatedFromMemberTemplate(D);
873
John McCallb1a56e72010-03-26 23:10:15 +0000874 // Make declarations visible in the appropriate context.
875 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000876 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000877
Douglas Gregord60e1052009-08-27 16:57:43 +0000878 return InstTemplate;
879}
880
Douglas Gregord475b8d2009-03-25 21:17:03 +0000881Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
882 CXXRecordDecl *PrevDecl = 0;
883 if (D->isInjectedClassName())
884 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000885 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000886 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
887 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000888 TemplateArgs);
889 if (!Prev) return 0;
890 PrevDecl = cast<CXXRecordDecl>(Prev);
891 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892
893 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000894 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000895 D->getLocation(), D->getIdentifier(),
896 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000897
898 // Substitute the nested name specifier, if any.
899 if (SubstQualifier(D, Record))
900 return 0;
901
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000903 // FIXME: Check against AS_none is an ugly hack to work around the issue that
904 // the tag decls introduced by friend class declarations don't have an access
905 // specifier. Remove once this area of the code gets sorted out.
906 if (D->getAccess() != AS_none)
907 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000908 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000909 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000910
John McCall02cace72009-08-28 07:59:38 +0000911 // If the original function was part of a friend declaration,
912 // inherit its namespace state.
913 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
914 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
915
Anders Carlssond8b285f2009-09-01 04:26:58 +0000916 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
917
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000919 return Record;
920}
921
John McCall02cace72009-08-28 07:59:38 +0000922/// Normal class members are of more specific types and therefore
923/// don't make it here. This function serves two purposes:
924/// 1) instantiating function templates
925/// 2) substituting friend declarations
926/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000927Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000928 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000929 // Check whether there is already a function template specialization for
930 // this declaration.
931 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
932 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000933 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000934 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000935 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000936 TemplateArgs.getInnermost().getFlatArgumentList(),
937 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000938 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
940 FunctionTemplateSpecializationInfo *Info
941 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000942 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregor127102b2009-06-29 20:59:39 +0000944 // If we already have a function template specialization, return it.
945 if (Info)
946 return Info->Function;
947 }
Mike Stump1eb44332009-09-09 15:08:12 +0000948
John McCallb0cb0222010-03-27 05:57:59 +0000949 bool isFriend;
950 if (FunctionTemplate)
951 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
952 else
953 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
954
Douglas Gregor79c22782010-01-16 20:21:20 +0000955 bool MergeWithParentScope = (TemplateParams != 0) ||
956 !(isa<Decl>(Owner) &&
957 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
958 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregore53060f2009-06-25 22:08:12 +0000960 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000961 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
962 TInfo = SubstFunctionType(D, Params);
963 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000964 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000965 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000966
John McCalld325daa2010-03-26 04:53:08 +0000967 NestedNameSpecifier *Qualifier = D->getQualifier();
968 if (Qualifier) {
969 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
970 D->getQualifierRange(),
971 TemplateArgs);
972 if (!Qualifier) return 0;
973 }
974
John McCall68b6b872010-02-06 01:50:47 +0000975 // If we're instantiating a local function declaration, put the result
976 // in the owner; otherwise we need to find the instantiated context.
977 DeclContext *DC;
978 if (D->getDeclContext()->isFunctionOrMethod())
979 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000980 else if (isFriend && Qualifier) {
981 CXXScopeSpec SS;
982 SS.setScopeRep(Qualifier);
983 SS.setRange(D->getQualifierRange());
984 DC = SemaRef.computeDeclContext(SS);
985 if (!DC) return 0;
986 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000987 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
988 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000989 }
John McCall68b6b872010-02-06 01:50:47 +0000990
John McCall02cace72009-08-28 07:59:38 +0000991 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000992 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000993 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000994 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000995 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000996
John McCalld325daa2010-03-26 04:53:08 +0000997 if (Qualifier)
998 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000999
John McCallb1a56e72010-03-26 23:10:15 +00001000 DeclContext *LexicalDC = Owner;
1001 if (!isFriend && D->isOutOfLine()) {
1002 assert(D->getDeclContext()->isFileContext());
1003 LexicalDC = D->getDeclContext();
1004 }
1005
1006 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Douglas Gregore53060f2009-06-25 22:08:12 +00001008 // Attach the parameters
1009 for (unsigned P = 0; P < Params.size(); ++P)
1010 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001011 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001012
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001013 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001014 if (TemplateParams) {
1015 // Our resulting instantiation is actually a function template, since we
1016 // are substituting only the outer template parameters. For example, given
1017 //
1018 // template<typename T>
1019 // struct X {
1020 // template<typename U> friend void f(T, U);
1021 // };
1022 //
1023 // X<int> x;
1024 //
1025 // We are instantiating the friend function template "f" within X<int>,
1026 // which means substituting int for T, but leaving "f" as a friend function
1027 // template.
1028 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001029 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001030 Function->getLocation(),
1031 Function->getDeclName(),
1032 TemplateParams, Function);
1033 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001034
1035 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001036
1037 if (isFriend && D->isThisDeclarationADefinition()) {
1038 // TODO: should we remember this connection regardless of whether
1039 // the friend declaration provided a body?
1040 FunctionTemplate->setInstantiatedFromMemberTemplate(
1041 D->getDescribedFunctionTemplate());
1042 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001043 } else if (FunctionTemplate) {
1044 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001045 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001046 &TemplateArgs.getInnermost(),
1047 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001048 } else if (isFriend && D->isThisDeclarationADefinition()) {
1049 // TODO: should we remember this connection regardless of whether
1050 // the friend declaration provided a body?
1051 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001052 }
Douglas Gregora735b202009-10-13 14:39:41 +00001053
Douglas Gregore53060f2009-06-25 22:08:12 +00001054 if (InitFunctionInstantiation(Function, D))
1055 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Douglas Gregore53060f2009-06-25 22:08:12 +00001057 bool Redeclaration = false;
1058 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001059 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001060
John McCall68263142009-11-18 22:49:29 +00001061 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1062 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1063
John McCallaf2094e2010-04-08 09:05:18 +00001064 if (DependentFunctionTemplateSpecializationInfo *Info
1065 = D->getDependentSpecializationInfo()) {
1066 assert(isFriend && "non-friend has dependent specialization info?");
1067
1068 // This needs to be set now for future sanity.
1069 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1070
1071 // Instantiate the explicit template arguments.
1072 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1073 Info->getRAngleLoc());
1074 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1075 TemplateArgumentLoc Loc;
1076 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1077 return 0;
1078
1079 ExplicitArgs.addArgument(Loc);
1080 }
1081
1082 // Map the candidate templates to their instantiations.
1083 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1084 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1085 Info->getTemplate(I),
1086 TemplateArgs);
1087 if (!Temp) return 0;
1088
1089 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1090 }
1091
1092 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1093 &ExplicitArgs,
1094 Previous))
1095 Function->setInvalidDecl();
1096
1097 isExplicitSpecialization = true;
1098
1099 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001100 // Look only into the namespace where the friend would be declared to
1101 // find a previous declaration. This is the innermost enclosing namespace,
1102 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001103 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001104
Douglas Gregora735b202009-10-13 14:39:41 +00001105 // In C++, the previous declaration we find might be a tag type
1106 // (class or enum). In this case, the new declaration will hide the
1107 // tag type. Note that this does does not apply if we're declaring a
1108 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001109 if (Previous.isSingleTagDecl())
1110 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001111 }
1112
John McCall9f54ad42009-12-10 09:41:52 +00001113 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001114 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001115 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001116
John McCall76d32642010-04-24 01:30:58 +00001117 NamedDecl *PrincipalDecl = (TemplateParams
1118 ? cast<NamedDecl>(FunctionTemplate)
1119 : Function);
1120
Douglas Gregora735b202009-10-13 14:39:41 +00001121 // If the original function was part of a friend declaration,
1122 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001123 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001124 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001125 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001126 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001127 else
Douglas Gregora735b202009-10-13 14:39:41 +00001128 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001129
1130 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1131 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001132
1133 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1134 D->isThisDeclarationADefinition()) {
1135 // Check for a function body.
1136 const FunctionDecl *Definition = 0;
1137 if (Function->getBody(Definition) &&
1138 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1139 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1140 << Function->getDeclName();
1141 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1142 Function->setInvalidDecl();
1143 }
1144 // Check for redefinitions due to other instantiations of this or
1145 // a similar friend function.
1146 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1147 REnd = Function->redecls_end();
1148 R != REnd; ++R) {
1149 if (*R != Function &&
1150 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1151 if (const FunctionDecl *RPattern
1152 = (*R)->getTemplateInstantiationPattern())
1153 if (RPattern->getBody(RPattern)) {
1154 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1155 << Function->getDeclName();
1156 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1157 Function->setInvalidDecl();
1158 break;
1159 }
1160 }
1161 }
1162 }
1163
Douglas Gregora735b202009-10-13 14:39:41 +00001164 }
1165
John McCall76d32642010-04-24 01:30:58 +00001166 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1167 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1168 PrincipalDecl->setNonMemberOperator();
1169
Douglas Gregore53060f2009-06-25 22:08:12 +00001170 return Function;
1171}
1172
Douglas Gregord60e1052009-08-27 16:57:43 +00001173Decl *
1174TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1175 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001176 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1177 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001178 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001179 // We are creating a function template specialization from a function
1180 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001181 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001182 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001183 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001184 TemplateArgs.getInnermost().getFlatArgumentList(),
1185 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001186 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001187
1188 FunctionTemplateSpecializationInfo *Info
1189 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001190 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Douglas Gregor6b906862009-08-21 00:16:32 +00001192 // If we already have a function template specialization, return it.
1193 if (Info)
1194 return Info->Function;
1195 }
1196
John McCallb0cb0222010-03-27 05:57:59 +00001197 bool isFriend;
1198 if (FunctionTemplate)
1199 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1200 else
1201 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1202
Douglas Gregor79c22782010-01-16 20:21:20 +00001203 bool MergeWithParentScope = (TemplateParams != 0) ||
1204 !(isa<Decl>(Owner) &&
1205 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1206 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001207
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001208 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001209 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1210 TInfo = SubstFunctionType(D, Params);
1211 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001212 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001213 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001214
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001215 // \brief If the type of this function is not *directly* a function
1216 // type, then we're instantiating the a function that was declared
1217 // via a typedef, e.g.,
1218 //
1219 // typedef int functype(int, int);
1220 // functype func;
1221 //
1222 // In this case, we'll just go instantiate the ParmVarDecls that we
1223 // synthesized in the method declaration.
1224 if (!isa<FunctionProtoType>(T)) {
1225 assert(!Params.size() && "Instantiating type could not yield parameters");
1226 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1227 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1228 TemplateArgs);
1229 if (!P)
1230 return 0;
1231
1232 Params.push_back(P);
1233 }
1234 }
1235
John McCallb0cb0222010-03-27 05:57:59 +00001236 NestedNameSpecifier *Qualifier = D->getQualifier();
1237 if (Qualifier) {
1238 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1239 D->getQualifierRange(),
1240 TemplateArgs);
1241 if (!Qualifier) return 0;
1242 }
1243
1244 DeclContext *DC = Owner;
1245 if (isFriend) {
1246 if (Qualifier) {
1247 CXXScopeSpec SS;
1248 SS.setScopeRep(Qualifier);
1249 SS.setRange(D->getQualifierRange());
1250 DC = SemaRef.computeDeclContext(SS);
1251 } else {
1252 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1253 D->getDeclContext(),
1254 TemplateArgs);
1255 }
1256 if (!DC) return 0;
1257 }
1258
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001259 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001260 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001261 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Douglas Gregordec06662009-08-21 18:42:58 +00001263 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001264 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001265 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1266 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1267 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001268 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1269 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001270 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001271 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001272 Constructor->isInlineSpecified(),
1273 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001274 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1275 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1276 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1277 SemaRef.Context.getCanonicalType(ClassTy));
1278 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1279 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001280 T, Destructor->isInlineSpecified(),
1281 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001282 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001283 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001284 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001285 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001286 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1287 ConvTy);
1288 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1289 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001290 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001291 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001292 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001293 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001294 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001295 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001296 D->isStatic(),
1297 D->getStorageClassAsWritten(),
1298 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001299 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001300
John McCallb0cb0222010-03-27 05:57:59 +00001301 if (Qualifier)
1302 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001303
Douglas Gregord60e1052009-08-27 16:57:43 +00001304 if (TemplateParams) {
1305 // Our resulting instantiation is actually a function template, since we
1306 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001307 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001308 // template<typename T>
1309 // struct X {
1310 // template<typename U> void f(T, U);
1311 // };
1312 //
1313 // X<int> x;
1314 //
1315 // We are instantiating the member template "f" within X<int>, which means
1316 // substituting int for T, but leaving "f" as a member function template.
1317 // Build the function template itself.
1318 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1319 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001320 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001321 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001322 if (isFriend) {
1323 FunctionTemplate->setLexicalDeclContext(Owner);
1324 FunctionTemplate->setObjectOfFriendDecl(true);
1325 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001326 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001327 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001328 } else if (FunctionTemplate) {
1329 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001330 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001331 &TemplateArgs.getInnermost(),
1332 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001333 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001334 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001335 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001336 }
1337
Mike Stump1eb44332009-09-09 15:08:12 +00001338 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001339 // out-of-line, the instantiation will have the same lexical
1340 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001341 if (isFriend) {
1342 Method->setLexicalDeclContext(Owner);
1343 Method->setObjectOfFriendDecl(true);
1344 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001345 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Douglas Gregor5545e162009-03-24 00:38:23 +00001347 // Attach the parameters
1348 for (unsigned P = 0; P < Params.size(); ++P)
1349 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001350 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001351
1352 if (InitMethodInstantiation(Method, D))
1353 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001354
John McCall68263142009-11-18 22:49:29 +00001355 LookupResult Previous(SemaRef, Name, SourceLocation(),
1356 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001357
John McCallb0cb0222010-03-27 05:57:59 +00001358 if (!FunctionTemplate || TemplateParams || isFriend) {
1359 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Douglas Gregordec06662009-08-21 18:42:58 +00001361 // In C++, the previous declaration we find might be a tag type
1362 // (class or enum). In this case, the new declaration will hide the
1363 // tag type. Note that this does does not apply if we're declaring a
1364 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001365 if (Previous.isSingleTagDecl())
1366 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001367 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001368
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001369 bool Redeclaration = false;
1370 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001371 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001372 /*FIXME:*/OverloadableAttrRequired);
1373
Douglas Gregor4ba31362009-12-01 17:24:26 +00001374 if (D->isPure())
1375 SemaRef.CheckPureMethod(Method, SourceRange());
1376
John McCall46460a62010-01-20 21:53:11 +00001377 Method->setAccess(D->getAccess());
1378
John McCallb0cb0222010-03-27 05:57:59 +00001379 if (FunctionTemplate) {
1380 // If there's a function template, let our caller handle it.
1381 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1382 // Don't hide a (potentially) valid declaration with an invalid one.
1383 } else {
1384 NamedDecl *DeclToAdd = (TemplateParams
1385 ? cast<NamedDecl>(FunctionTemplate)
1386 : Method);
1387 if (isFriend)
1388 Record->makeDeclVisibleInContext(DeclToAdd);
1389 else
1390 Owner->addDecl(DeclToAdd);
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001393 return Method;
1394}
1395
Douglas Gregor615c5d42009-03-24 16:43:20 +00001396Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001397 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001398}
1399
Douglas Gregor03b2b072009-03-24 00:15:49 +00001400Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001401 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001402}
1403
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001404Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001405 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001406}
1407
Douglas Gregor6477b692009-03-25 15:04:13 +00001408ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001409 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001410}
1411
John McCalle29ba202009-08-20 01:44:21 +00001412Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1413 TemplateTypeParmDecl *D) {
1414 // TODO: don't always clone when decls are refcounted.
1415 const Type* T = D->getTypeForDecl();
1416 assert(T->isTemplateTypeParmType());
1417 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001418
John McCalle29ba202009-08-20 01:44:21 +00001419 TemplateTypeParmDecl *Inst =
1420 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001421 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001422 TTPT->getName(),
1423 D->wasDeclaredWithTypename(),
1424 D->isParameterPack());
1425
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001426 if (D->hasDefaultArgument())
1427 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001428
Douglas Gregor550d9b22009-10-31 17:21:17 +00001429 // Introduce this template parameter's instantiation into the instantiation
1430 // scope.
1431 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1432
John McCalle29ba202009-08-20 01:44:21 +00001433 return Inst;
1434}
1435
Douglas Gregor33642df2009-10-23 23:25:44 +00001436Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1437 NonTypeTemplateParmDecl *D) {
1438 // Substitute into the type of the non-type template parameter.
1439 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001440 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001441 if (DI) {
1442 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1443 D->getDeclName());
1444 if (DI) T = DI->getType();
1445 } else {
1446 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1447 D->getDeclName());
1448 DI = 0;
1449 }
1450 if (T.isNull())
1451 return 0;
1452
1453 // Check that this type is acceptable for a non-type template parameter.
1454 bool Invalid = false;
1455 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1456 if (T.isNull()) {
1457 T = SemaRef.Context.IntTy;
1458 Invalid = true;
1459 }
1460
1461 NonTypeTemplateParmDecl *Param
1462 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1463 D->getDepth() - 1, D->getPosition(),
1464 D->getIdentifier(), T, DI);
1465 if (Invalid)
1466 Param->setInvalidDecl();
1467
1468 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001469
1470 // Introduce this template parameter's instantiation into the instantiation
1471 // scope.
1472 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001473 return Param;
1474}
1475
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001476Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001477TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1478 TemplateTemplateParmDecl *D) {
1479 // Instantiate the template parameter list of the template template parameter.
1480 TemplateParameterList *TempParams = D->getTemplateParameters();
1481 TemplateParameterList *InstParams;
1482 {
1483 // Perform the actual substitution of template parameters within a new,
1484 // local instantiation scope.
1485 Sema::LocalInstantiationScope Scope(SemaRef);
1486 InstParams = SubstTemplateParams(TempParams);
1487 if (!InstParams)
1488 return NULL;
1489 }
1490
1491 // Build the template template parameter.
1492 TemplateTemplateParmDecl *Param
1493 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1494 D->getDepth() - 1, D->getPosition(),
1495 D->getIdentifier(), InstParams);
1496 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001497
Douglas Gregor9106ef72009-11-11 16:58:32 +00001498 // Introduce this template parameter's instantiation into the instantiation
1499 // scope.
1500 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1501
1502 return Param;
1503}
1504
Douglas Gregor48c32a72009-11-17 06:07:40 +00001505Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1506 // Using directives are never dependent, so they require no explicit
1507
1508 UsingDirectiveDecl *Inst
1509 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1510 D->getNamespaceKeyLocation(),
1511 D->getQualifierRange(), D->getQualifier(),
1512 D->getIdentLocation(),
1513 D->getNominatedNamespace(),
1514 D->getCommonAncestor());
1515 Owner->addDecl(Inst);
1516 return Inst;
1517}
1518
John McCalled976492009-12-04 22:46:56 +00001519Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1520 // The nested name specifier is non-dependent, so no transformation
1521 // is required.
1522
John McCall9f54ad42009-12-10 09:41:52 +00001523 // We only need to do redeclaration lookups if we're in a class
1524 // scope (in fact, it's not really even possible in non-class
1525 // scopes).
1526 bool CheckRedeclaration = Owner->isRecord();
1527
1528 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1529 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1530
John McCalled976492009-12-04 22:46:56 +00001531 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1532 D->getLocation(),
1533 D->getNestedNameRange(),
1534 D->getUsingLocation(),
1535 D->getTargetNestedNameDecl(),
1536 D->getDeclName(),
1537 D->isTypeName());
1538
1539 CXXScopeSpec SS;
1540 SS.setScopeRep(D->getTargetNestedNameDecl());
1541 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001542
1543 if (CheckRedeclaration) {
1544 Prev.setHideTags(false);
1545 SemaRef.LookupQualifiedName(Prev, Owner);
1546
1547 // Check for invalid redeclarations.
1548 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1549 D->isTypeName(), SS,
1550 D->getLocation(), Prev))
1551 NewUD->setInvalidDecl();
1552
1553 }
1554
1555 if (!NewUD->isInvalidDecl() &&
1556 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001557 D->getLocation()))
1558 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001559
John McCalled976492009-12-04 22:46:56 +00001560 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1561 NewUD->setAccess(D->getAccess());
1562 Owner->addDecl(NewUD);
1563
John McCall9f54ad42009-12-10 09:41:52 +00001564 // Don't process the shadow decls for an invalid decl.
1565 if (NewUD->isInvalidDecl())
1566 return NewUD;
1567
John McCall323c3102009-12-22 22:26:37 +00001568 bool isFunctionScope = Owner->isFunctionOrMethod();
1569
John McCall9f54ad42009-12-10 09:41:52 +00001570 // Process the shadow decls.
1571 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1572 I != E; ++I) {
1573 UsingShadowDecl *Shadow = *I;
1574 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001575 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1576 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001577 TemplateArgs));
1578
1579 if (CheckRedeclaration &&
1580 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1581 continue;
1582
1583 UsingShadowDecl *InstShadow
1584 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1585 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001586
1587 if (isFunctionScope)
1588 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001589 }
John McCalled976492009-12-04 22:46:56 +00001590
1591 return NewUD;
1592}
1593
1594Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001595 // Ignore these; we handle them in bulk when processing the UsingDecl.
1596 return 0;
John McCalled976492009-12-04 22:46:56 +00001597}
1598
John McCall7ba107a2009-11-18 02:36:19 +00001599Decl * TemplateDeclInstantiator
1600 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001601 NestedNameSpecifier *NNS =
1602 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1603 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001604 TemplateArgs);
1605 if (!NNS)
1606 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001608 CXXScopeSpec SS;
1609 SS.setRange(D->getTargetNestedNameRange());
1610 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
1612 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001613 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001614 D->getUsingLoc(), SS, D->getLocation(),
1615 D->getDeclName(), 0,
1616 /*instantiation*/ true,
1617 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001618 if (UD)
John McCalled976492009-12-04 22:46:56 +00001619 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1620
John McCall7ba107a2009-11-18 02:36:19 +00001621 return UD;
1622}
1623
1624Decl * TemplateDeclInstantiator
1625 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1626 NestedNameSpecifier *NNS =
1627 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1628 D->getTargetNestedNameRange(),
1629 TemplateArgs);
1630 if (!NNS)
1631 return 0;
1632
1633 CXXScopeSpec SS;
1634 SS.setRange(D->getTargetNestedNameRange());
1635 SS.setScopeRep(NNS);
1636
1637 NamedDecl *UD =
1638 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1639 D->getUsingLoc(), SS, D->getLocation(),
1640 D->getDeclName(), 0,
1641 /*instantiation*/ true,
1642 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001643 if (UD)
John McCalled976492009-12-04 22:46:56 +00001644 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1645
Anders Carlsson0d8df782009-08-29 19:37:28 +00001646 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001647}
1648
John McCallce3ff2b2009-08-25 22:02:44 +00001649Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001650 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001651 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001652 if (D->isInvalidDecl())
1653 return 0;
1654
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001655 return Instantiator.Visit(D);
1656}
1657
John McCalle29ba202009-08-20 01:44:21 +00001658/// \brief Instantiates a nested template parameter list in the current
1659/// instantiation context.
1660///
1661/// \param L The parameter list to instantiate
1662///
1663/// \returns NULL if there was an error
1664TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001665TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001666 // Get errors for all the parameters before bailing out.
1667 bool Invalid = false;
1668
1669 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001670 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001671 ParamVector Params;
1672 Params.reserve(N);
1673 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1674 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001675 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001676 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001677 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001678 }
1679
1680 // Clean up if we had an error.
1681 if (Invalid) {
1682 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1683 PI != PE; ++PI)
1684 if (*PI)
1685 (*PI)->Destroy(SemaRef.Context);
1686 return NULL;
1687 }
1688
1689 TemplateParameterList *InstL
1690 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1691 L->getLAngleLoc(), &Params.front(), N,
1692 L->getRAngleLoc());
1693 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001694}
John McCalle29ba202009-08-20 01:44:21 +00001695
Douglas Gregored9c0f92009-10-29 00:04:11 +00001696/// \brief Instantiate the declaration of a class template partial
1697/// specialization.
1698///
1699/// \param ClassTemplate the (instantiated) class template that is partially
1700// specialized by the instantiation of \p PartialSpec.
1701///
1702/// \param PartialSpec the (uninstantiated) class template partial
1703/// specialization that we are instantiating.
1704///
1705/// \returns true if there was an error, false otherwise.
1706bool
1707TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1708 ClassTemplateDecl *ClassTemplate,
1709 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001710 // Create a local instantiation scope for this class template partial
1711 // specialization, which will contain the instantiations of the template
1712 // parameters.
1713 Sema::LocalInstantiationScope Scope(SemaRef);
1714
Douglas Gregored9c0f92009-10-29 00:04:11 +00001715 // Substitute into the template parameters of the class template partial
1716 // specialization.
1717 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1718 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1719 if (!InstParams)
1720 return true;
1721
1722 // Substitute into the template arguments of the class template partial
1723 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001724 const TemplateArgumentLoc *PartialSpecTemplateArgs
1725 = PartialSpec->getTemplateArgsAsWritten();
1726 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1727
John McCalld5532b62009-11-23 01:53:49 +00001728 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001729 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001730 TemplateArgumentLoc Loc;
1731 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001732 return true;
John McCalld5532b62009-11-23 01:53:49 +00001733 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001734 }
1735
1736
1737 // Check that the template argument list is well-formed for this
1738 // class template.
1739 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1740 InstTemplateArgs.size());
1741 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1742 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001743 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001744 false,
1745 Converted))
1746 return true;
1747
1748 // Figure out where to insert this class template partial specialization
1749 // in the member template's set of class template partial specializations.
1750 llvm::FoldingSetNodeID ID;
1751 ClassTemplatePartialSpecializationDecl::Profile(ID,
1752 Converted.getFlatArguments(),
1753 Converted.flatSize(),
1754 SemaRef.Context);
1755 void *InsertPos = 0;
1756 ClassTemplateSpecializationDecl *PrevDecl
1757 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1758 InsertPos);
1759
1760 // Build the canonical type that describes the converted template
1761 // arguments of the class template partial specialization.
1762 QualType CanonType
1763 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1764 Converted.getFlatArguments(),
1765 Converted.flatSize());
1766
1767 // Build the fully-sugared type for this class template
1768 // specialization as the user wrote in the specialization
1769 // itself. This means that we'll pretty-print the type retrieved
1770 // from the specialization's declaration the way that the user
1771 // actually wrote the specialization, rather than formatting the
1772 // name based on the "canonical" representation used to store the
1773 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001774 TypeSourceInfo *WrittenTy
1775 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1776 TemplateName(ClassTemplate),
1777 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001778 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001779 CanonType);
1780
1781 if (PrevDecl) {
1782 // We've already seen a partial specialization with the same template
1783 // parameters and template arguments. This can happen, for example, when
1784 // substituting the outer template arguments ends up causing two
1785 // class template partial specializations of a member class template
1786 // to have identical forms, e.g.,
1787 //
1788 // template<typename T, typename U>
1789 // struct Outer {
1790 // template<typename X, typename Y> struct Inner;
1791 // template<typename Y> struct Inner<T, Y>;
1792 // template<typename Y> struct Inner<U, Y>;
1793 // };
1794 //
1795 // Outer<int, int> outer; // error: the partial specializations of Inner
1796 // // have the same signature.
1797 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1798 << WrittenTy;
1799 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1800 << SemaRef.Context.getTypeDeclType(PrevDecl);
1801 return true;
1802 }
1803
1804
1805 // Create the class template partial specialization declaration.
1806 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001807 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1808 PartialSpec->getTagKind(),
1809 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001810 PartialSpec->getLocation(),
1811 InstParams,
1812 ClassTemplate,
1813 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001814 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001815 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001816 0,
1817 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001818 // Substitute the nested name specifier, if any.
1819 if (SubstQualifier(PartialSpec, InstPartialSpec))
1820 return 0;
1821
Douglas Gregored9c0f92009-10-29 00:04:11 +00001822 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001823 InstPartialSpec->setTypeAsWritten(WrittenTy);
1824
Douglas Gregored9c0f92009-10-29 00:04:11 +00001825 // Add this partial specialization to the set of class template partial
1826 // specializations.
1827 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1828 InsertPos);
1829 return false;
1830}
1831
John McCall21ef0fa2010-03-11 09:03:00 +00001832TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001833TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001834 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001835 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1836 assert(OldTInfo && "substituting function without type source info");
1837 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001838 TypeSourceInfo *NewTInfo
1839 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1840 D->getTypeSpecStartLoc(),
1841 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001842 if (!NewTInfo)
1843 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001844
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001845 if (NewTInfo != OldTInfo) {
1846 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001847 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001848 if (FunctionProtoTypeLoc *OldProtoLoc
1849 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1850 TypeLoc NewTL = NewTInfo->getTypeLoc();
1851 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1852 assert(NewProtoLoc && "Missing prototype?");
1853 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1854 // FIXME: Variadic templates will break this.
1855 Params.push_back(NewProtoLoc->getArg(i));
1856 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001857 OldProtoLoc->getArg(i),
1858 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001859 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001860 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001861 } else {
1862 // The function type itself was not dependent and therefore no
1863 // substitution occurred. However, we still need to instantiate
1864 // the function parameters themselves.
1865 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001866 if (FunctionProtoTypeLoc *OldProtoLoc
1867 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1868 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1869 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1870 if (!Parm)
1871 return 0;
1872 Params.push_back(Parm);
1873 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001874 }
1875 }
John McCall21ef0fa2010-03-11 09:03:00 +00001876 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001877}
1878
Mike Stump1eb44332009-09-09 15:08:12 +00001879/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001880/// declaration (New) from the corresponding fields of its template (Tmpl).
1881///
1882/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001883bool
1884TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001885 FunctionDecl *Tmpl) {
1886 if (Tmpl->isDeleted())
1887 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregorcca9e962009-07-01 22:01:06 +00001889 // If we are performing substituting explicitly-specified template arguments
1890 // or deduced template arguments into a function template and we reach this
1891 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001892 // to keeping the new function template specialization. We therefore
1893 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001894 // into a template instantiation for this specific function template
1895 // specialization, which is not a SFINAE context, so that we diagnose any
1896 // further errors in the declaration itself.
1897 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1898 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1899 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1900 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001901 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001902 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001903 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001904 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001905 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001906 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1907 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001908 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001909 }
1910 }
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001912 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1913 assert(Proto && "Function template without prototype?");
1914
1915 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1916 Proto->getNoReturnAttr()) {
1917 // The function has an exception specification or a "noreturn"
1918 // attribute. Substitute into each of the exception types.
1919 llvm::SmallVector<QualType, 4> Exceptions;
1920 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1921 // FIXME: Poor location information!
1922 QualType T
1923 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1924 New->getLocation(), New->getDeclName());
1925 if (T.isNull() ||
1926 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1927 continue;
1928
1929 Exceptions.push_back(T);
1930 }
1931
1932 // Rebuild the function type
1933
1934 const FunctionProtoType *NewProto
1935 = New->getType()->getAs<FunctionProtoType>();
1936 assert(NewProto && "Template instantiation without function prototype?");
1937 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1938 NewProto->arg_type_begin(),
1939 NewProto->getNumArgs(),
1940 NewProto->isVariadic(),
1941 NewProto->getTypeQuals(),
1942 Proto->hasExceptionSpec(),
1943 Proto->hasAnyExceptionSpec(),
1944 Exceptions.size(),
1945 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001946 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001947 }
1948
Douglas Gregore53060f2009-06-25 22:08:12 +00001949 return false;
1950}
1951
Douglas Gregor5545e162009-03-24 00:38:23 +00001952/// \brief Initializes common fields of an instantiated method
1953/// declaration (New) from the corresponding fields of its template
1954/// (Tmpl).
1955///
1956/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001957bool
1958TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001959 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001960 if (InitFunctionInstantiation(New, Tmpl))
1961 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Douglas Gregor5545e162009-03-24 00:38:23 +00001963 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1964 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001965 if (Tmpl->isVirtualAsWritten())
1966 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001967
1968 // FIXME: attributes
1969 // FIXME: New needs a pointer to Tmpl
1970 return false;
1971}
Douglas Gregora58861f2009-05-13 20:28:22 +00001972
1973/// \brief Instantiate the definition of the given function from its
1974/// template.
1975///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001976/// \param PointOfInstantiation the point at which the instantiation was
1977/// required. Note that this is not precisely a "point of instantiation"
1978/// for the function, but it's close.
1979///
Douglas Gregora58861f2009-05-13 20:28:22 +00001980/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001981/// function template specialization or member function of a class template
1982/// specialization.
1983///
1984/// \param Recursive if true, recursively instantiates any functions that
1985/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001986///
1987/// \param DefinitionRequired if true, then we are performing an explicit
1988/// instantiation where the body of the function is required. Complain if
1989/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001990void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001991 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001992 bool Recursive,
1993 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001994 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001995 return;
1996
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001997 // Never instantiate an explicit specialization.
1998 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1999 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002000
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002001 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002002 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002003 Stmt *Pattern = 0;
2004 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002005 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002006
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002007 if (!Pattern) {
2008 if (DefinitionRequired) {
2009 if (Function->getPrimaryTemplate())
2010 Diag(PointOfInstantiation,
2011 diag::err_explicit_instantiation_undefined_func_template)
2012 << Function->getPrimaryTemplate();
2013 else
2014 Diag(PointOfInstantiation,
2015 diag::err_explicit_instantiation_undefined_member)
2016 << 1 << Function->getDeclName() << Function->getDeclContext();
2017
2018 if (PatternDecl)
2019 Diag(PatternDecl->getLocation(),
2020 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002021 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002022 }
2023
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002024 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002025 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002026
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002027 // C++0x [temp.explicit]p9:
2028 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002029 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002030 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002031 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002032 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002033 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002034 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002036 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2037 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002038 return;
2039
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002040 // If we're performing recursive template instantiation, create our own
2041 // queue of pending implicit instantiations that we will instantiate later,
2042 // while we're still within our own instantiation context.
2043 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2044 if (Recursive)
2045 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Douglas Gregor9679caf2010-05-12 17:27:19 +00002047 EnterExpressionEvaluationContext EvalContext(*this,
2048 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002049 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2050
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002051 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002052 // recorded, unless we're actually a member function within a local
2053 // class, in which case we need to merge our results with the parent
2054 // scope (of the enclosing function).
2055 bool MergeWithParentScope = false;
2056 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2057 MergeWithParentScope = Rec->isLocalClass();
2058
2059 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002061 // Introduce the instantiated function parameters into the local
2062 // instantiation scope.
2063 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2064 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2065 Function->getParamDecl(I));
2066
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002067 // Enter the scope of this instantiation. We don't use
2068 // PushDeclContext because we don't have a scope.
2069 DeclContext *PreviousContext = CurContext;
2070 CurContext = Function;
2071
Mike Stump1eb44332009-09-09 15:08:12 +00002072 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002073 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002074
2075 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002076 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002077 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2078 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2079 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002080 }
2081
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002082 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002083 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002084
Douglas Gregor52604ab2009-09-11 21:19:12 +00002085 if (Body.isInvalid())
2086 Function->setInvalidDecl();
2087
Mike Stump1eb44332009-09-09 15:08:12 +00002088 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002089 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002090
John McCall0c01d182010-03-24 05:22:00 +00002091 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2092
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002093 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002094
2095 DeclGroupRef DG(Function);
2096 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Douglas Gregor60406be2010-01-16 22:29:39 +00002098 // This class may have local implicit instantiations that need to be
2099 // instantiation within this scope.
2100 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2101 Scope.Exit();
2102
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002103 if (Recursive) {
2104 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002105 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002106 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002108 // Restore the set of pending implicit instantiations.
2109 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2110 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002111}
2112
2113/// \brief Instantiate the definition of the given variable from its
2114/// template.
2115///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002116/// \param PointOfInstantiation the point at which the instantiation was
2117/// required. Note that this is not precisely a "point of instantiation"
2118/// for the function, but it's close.
2119///
2120/// \param Var the already-instantiated declaration of a static member
2121/// variable of a class template specialization.
2122///
2123/// \param Recursive if true, recursively instantiates any functions that
2124/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002125///
2126/// \param DefinitionRequired if true, then we are performing an explicit
2127/// instantiation where an out-of-line definition of the member variable
2128/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002129void Sema::InstantiateStaticDataMemberDefinition(
2130 SourceLocation PointOfInstantiation,
2131 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002132 bool Recursive,
2133 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002134 if (Var->isInvalidDecl())
2135 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Douglas Gregor7caa6822009-07-24 20:34:43 +00002137 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002138 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002139 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002140 assert(Def->isStaticDataMember() && "Not a static data member?");
2141 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Douglas Gregor0d035142009-10-27 18:42:08 +00002143 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002144 // We did not find an out-of-line definition of this static data member,
2145 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002146 // instantiate this definition (or provide a specialization for it) in
2147 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002148 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002149 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002150 Diag(PointOfInstantiation,
2151 diag::err_explicit_instantiation_undefined_member)
2152 << 2 << Var->getDeclName() << Var->getDeclContext();
2153 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2154 }
2155
Douglas Gregor7caa6822009-07-24 20:34:43 +00002156 return;
2157 }
2158
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002159 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002160 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002161 return;
2162
2163 // C++0x [temp.explicit]p9:
2164 // Except for inline functions, other explicit instantiation declarations
2165 // have the effect of suppressing the implicit instantiation of the entity
2166 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002167 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002168 == TSK_ExplicitInstantiationDeclaration)
2169 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Douglas Gregor7caa6822009-07-24 20:34:43 +00002171 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2172 if (Inst)
2173 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Douglas Gregor7caa6822009-07-24 20:34:43 +00002175 // If we're performing recursive template instantiation, create our own
2176 // queue of pending implicit instantiations that we will instantiate later,
2177 // while we're still within our own instantiation context.
2178 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2179 if (Recursive)
2180 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Douglas Gregor7caa6822009-07-24 20:34:43 +00002182 // Enter the scope of this instantiation. We don't use
2183 // PushDeclContext because we don't have a scope.
2184 DeclContext *PreviousContext = CurContext;
2185 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002186
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002187 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002188 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002189 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002190 CurContext = PreviousContext;
2191
2192 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002193 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2194 assert(MSInfo && "Missing member specialization information?");
2195 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2196 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002197 DeclGroupRef DG(Var);
2198 Consumer.HandleTopLevelDecl(DG);
2199 }
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Douglas Gregor7caa6822009-07-24 20:34:43 +00002201 if (Recursive) {
2202 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002203 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002204 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Douglas Gregor7caa6822009-07-24 20:34:43 +00002206 // Restore the set of pending implicit instantiations.
2207 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002208 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002209}
Douglas Gregor815215d2009-05-27 05:35:12 +00002210
Anders Carlsson09025312009-08-29 05:16:22 +00002211void
2212Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2213 const CXXConstructorDecl *Tmpl,
2214 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002215
Anders Carlsson09025312009-08-29 05:16:22 +00002216 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002217 bool AnyErrors = false;
2218
Anders Carlsson09025312009-08-29 05:16:22 +00002219 // Instantiate all the initializers.
2220 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002221 InitsEnd = Tmpl->init_end();
2222 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002223 CXXBaseOrMemberInitializer *Init = *Inits;
2224
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002225 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002226 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002227 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002229 // Instantiate the initializer.
2230 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2231 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2232 AnyErrors = true;
2233 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002234 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002235
Anders Carlsson09025312009-08-29 05:16:22 +00002236 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002237 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002238 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002239 TemplateArgs,
2240 Init->getSourceLocation(),
2241 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002242 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002243 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002244 New->setInvalidDecl();
2245 continue;
2246 }
2247
John McCalla93c9342009-12-07 02:54:59 +00002248 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002249 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002250 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002251 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002252 Init->getRParenLoc(),
2253 New->getParent());
2254 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002255 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002257 // Is this an anonymous union?
2258 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002259 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2260 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002261 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002262 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2263 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002264 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002265
2266 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002267 NewArgs.size(),
2268 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002269 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002270 Init->getRParenLoc());
2271 }
2272
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002273 if (NewInit.isInvalid()) {
2274 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002275 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002276 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002277 // FIXME: It would be nice if ASTOwningVector had a release function.
2278 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Anders Carlsson09025312009-08-29 05:16:22 +00002280 NewInits.push_back((MemInitTy *)NewInit.get());
2281 }
2282 }
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Anders Carlsson09025312009-08-29 05:16:22 +00002284 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002285 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002286 /*FIXME: ColonLoc */
2287 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002288 NewInits.data(), NewInits.size(),
2289 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002290}
2291
John McCall52a575a2009-08-29 08:11:13 +00002292// TODO: this could be templated if the various decl types used the
2293// same method name.
2294static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2295 ClassTemplateDecl *Instance) {
2296 Pattern = Pattern->getCanonicalDecl();
2297
2298 do {
2299 Instance = Instance->getCanonicalDecl();
2300 if (Pattern == Instance) return true;
2301 Instance = Instance->getInstantiatedFromMemberTemplate();
2302 } while (Instance);
2303
2304 return false;
2305}
2306
Douglas Gregor0d696532009-09-28 06:34:35 +00002307static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2308 FunctionTemplateDecl *Instance) {
2309 Pattern = Pattern->getCanonicalDecl();
2310
2311 do {
2312 Instance = Instance->getCanonicalDecl();
2313 if (Pattern == Instance) return true;
2314 Instance = Instance->getInstantiatedFromMemberTemplate();
2315 } while (Instance);
2316
2317 return false;
2318}
2319
Douglas Gregored9c0f92009-10-29 00:04:11 +00002320static bool
2321isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2322 ClassTemplatePartialSpecializationDecl *Instance) {
2323 Pattern
2324 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2325 do {
2326 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2327 Instance->getCanonicalDecl());
2328 if (Pattern == Instance)
2329 return true;
2330 Instance = Instance->getInstantiatedFromMember();
2331 } while (Instance);
2332
2333 return false;
2334}
2335
John McCall52a575a2009-08-29 08:11:13 +00002336static bool isInstantiationOf(CXXRecordDecl *Pattern,
2337 CXXRecordDecl *Instance) {
2338 Pattern = Pattern->getCanonicalDecl();
2339
2340 do {
2341 Instance = Instance->getCanonicalDecl();
2342 if (Pattern == Instance) return true;
2343 Instance = Instance->getInstantiatedFromMemberClass();
2344 } while (Instance);
2345
2346 return false;
2347}
2348
2349static bool isInstantiationOf(FunctionDecl *Pattern,
2350 FunctionDecl *Instance) {
2351 Pattern = Pattern->getCanonicalDecl();
2352
2353 do {
2354 Instance = Instance->getCanonicalDecl();
2355 if (Pattern == Instance) return true;
2356 Instance = Instance->getInstantiatedFromMemberFunction();
2357 } while (Instance);
2358
2359 return false;
2360}
2361
2362static bool isInstantiationOf(EnumDecl *Pattern,
2363 EnumDecl *Instance) {
2364 Pattern = Pattern->getCanonicalDecl();
2365
2366 do {
2367 Instance = Instance->getCanonicalDecl();
2368 if (Pattern == Instance) return true;
2369 Instance = Instance->getInstantiatedFromMemberEnum();
2370 } while (Instance);
2371
2372 return false;
2373}
2374
John McCalled976492009-12-04 22:46:56 +00002375static bool isInstantiationOf(UsingShadowDecl *Pattern,
2376 UsingShadowDecl *Instance,
2377 ASTContext &C) {
2378 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2379}
2380
2381static bool isInstantiationOf(UsingDecl *Pattern,
2382 UsingDecl *Instance,
2383 ASTContext &C) {
2384 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2385}
2386
John McCall7ba107a2009-11-18 02:36:19 +00002387static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2388 UsingDecl *Instance,
2389 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002390 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002391}
2392
2393static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002394 UsingDecl *Instance,
2395 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002396 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002397}
2398
John McCall52a575a2009-08-29 08:11:13 +00002399static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2400 VarDecl *Instance) {
2401 assert(Instance->isStaticDataMember());
2402
2403 Pattern = Pattern->getCanonicalDecl();
2404
2405 do {
2406 Instance = Instance->getCanonicalDecl();
2407 if (Pattern == Instance) return true;
2408 Instance = Instance->getInstantiatedFromStaticDataMember();
2409 } while (Instance);
2410
2411 return false;
2412}
2413
John McCalled976492009-12-04 22:46:56 +00002414// Other is the prospective instantiation
2415// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002416static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002417 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002418 if (UnresolvedUsingTypenameDecl *UUD
2419 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2420 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2421 return isInstantiationOf(UUD, UD, Ctx);
2422 }
2423 }
2424
2425 if (UnresolvedUsingValueDecl *UUD
2426 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002427 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2428 return isInstantiationOf(UUD, UD, Ctx);
2429 }
2430 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002431
Anders Carlsson0d8df782009-08-29 19:37:28 +00002432 return false;
2433 }
Mike Stump1eb44332009-09-09 15:08:12 +00002434
John McCall52a575a2009-08-29 08:11:13 +00002435 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2436 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002437
John McCall52a575a2009-08-29 08:11:13 +00002438 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2439 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002440
John McCall52a575a2009-08-29 08:11:13 +00002441 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2442 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002443
Douglas Gregor7caa6822009-07-24 20:34:43 +00002444 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002445 if (Var->isStaticDataMember())
2446 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2447
2448 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2449 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002450
Douglas Gregor0d696532009-09-28 06:34:35 +00002451 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2452 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2453
Douglas Gregored9c0f92009-10-29 00:04:11 +00002454 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2455 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2456 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2457 PartialSpec);
2458
Anders Carlssond8b285f2009-09-01 04:26:58 +00002459 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2460 if (!Field->getDeclName()) {
2461 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002462 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002463 cast<FieldDecl>(D);
2464 }
2465 }
Mike Stump1eb44332009-09-09 15:08:12 +00002466
John McCalled976492009-12-04 22:46:56 +00002467 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2468 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2469
2470 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2471 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2472
Douglas Gregor815215d2009-05-27 05:35:12 +00002473 return D->getDeclName() && isa<NamedDecl>(Other) &&
2474 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2475}
2476
2477template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002478static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002479 NamedDecl *D,
2480 ForwardIterator first,
2481 ForwardIterator last) {
2482 for (; first != last; ++first)
2483 if (isInstantiationOf(Ctx, D, *first))
2484 return cast<NamedDecl>(*first);
2485
2486 return 0;
2487}
2488
John McCall02cace72009-08-28 07:59:38 +00002489/// \brief Finds the instantiation of the given declaration context
2490/// within the current instantiation.
2491///
2492/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002493DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002494 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002495 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002496 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002497 return cast_or_null<DeclContext>(ID);
2498 } else return DC;
2499}
2500
Douglas Gregored961e72009-05-27 17:54:46 +00002501/// \brief Find the instantiation of the given declaration within the
2502/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002503///
2504/// This routine is intended to be used when \p D is a declaration
2505/// referenced from within a template, that needs to mapped into the
2506/// corresponding declaration within an instantiation. For example,
2507/// given:
2508///
2509/// \code
2510/// template<typename T>
2511/// struct X {
2512/// enum Kind {
2513/// KnownValue = sizeof(T)
2514/// };
2515///
2516/// bool getKind() const { return KnownValue; }
2517/// };
2518///
2519/// template struct X<int>;
2520/// \endcode
2521///
2522/// In the instantiation of X<int>::getKind(), we need to map the
2523/// EnumConstantDecl for KnownValue (which refers to
2524/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002525/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2526/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002527NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002528 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002529 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002530 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002531 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002532 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002533 // D is a local of some kind. Look into the map of local
2534 // declarations to their instantiations.
2535 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2536 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002537
Douglas Gregore95b4092009-09-16 18:34:49 +00002538 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2539 if (!Record->isDependentContext())
2540 return D;
2541
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002542 // If the RecordDecl is actually the injected-class-name or a
2543 // "templated" declaration for a class template, class template
2544 // partial specialization, or a member class of a class template,
2545 // substitute into the injected-class-name of the class template
2546 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002547 QualType T;
2548 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2549
2550 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002551 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002552 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2553 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002554 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002555
2556 // If we call SubstType with an InjectedClassNameType here we
2557 // can end up in an infinite loop.
2558 T = Context.getTypeDeclType(Record);
2559 assert(isa<InjectedClassNameType>(T) &&
2560 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002561 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002562 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002563
2564 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002565 // Substitute into the injected-class-name to get the type
2566 // corresponding to the instantiation we want, which may also be
2567 // the current instantiation (if we're in a template
2568 // definition). This substitution should never fail, since we
2569 // know we can instantiate the injected-class-name or we
2570 // wouldn't have gotten to the injected-class-name!
2571
2572 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2573 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002574 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2575 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2576
2577 if (!T->isDependentType()) {
2578 assert(T->isRecordType() && "Instantiation must produce a record type");
2579 return T->getAs<RecordType>()->getDecl();
2580 }
2581
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002582 // We are performing "partial" template instantiation to create
2583 // the member declarations for the members of a class template
2584 // specialization. Therefore, D is actually referring to something
2585 // in the current instantiation. Look through the current
2586 // context, which contains actual instantiations, to find the
2587 // instantiation of the "current instantiation" that D refers
2588 // to.
2589 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002590 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002591 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002592 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002593 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002594 if (isInstantiationOf(ClassTemplate,
2595 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002596 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002597
2598 if (!DC->isDependentContext())
2599 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002600 }
2601
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002602 // We're performing "instantiation" of a member of the current
2603 // instantiation while we are type-checking the
2604 // definition. Compute the declaration context and return that.
2605 assert(!SawNonDependentContext &&
2606 "No dependent context while instantiating record");
2607 DeclContext *DC = computeDeclContext(T);
2608 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002609 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002610 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002611 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002612
Douglas Gregore95b4092009-09-16 18:34:49 +00002613 // Fall through to deal with other dependent record types (e.g.,
2614 // anonymous unions in class templates).
2615 }
John McCall52a575a2009-08-29 08:11:13 +00002616
Douglas Gregore95b4092009-09-16 18:34:49 +00002617 if (!ParentDC->isDependentContext())
2618 return D;
2619
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002620 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002621 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002622 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002623
Douglas Gregor815215d2009-05-27 05:35:12 +00002624 if (ParentDC != D->getDeclContext()) {
2625 // We performed some kind of instantiation in the parent context,
2626 // so now we need to look into the instantiated parent context to
2627 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002628
John McCall3cb0ebd2010-03-10 03:28:59 +00002629 // If our context used to be dependent, we may need to instantiate
2630 // it before performing lookup into that context.
2631 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002632 if (!Spec->isDependentContext()) {
2633 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002634 const RecordType *Tag = T->getAs<RecordType>();
2635 assert(Tag && "type of non-dependent record is not a RecordType");
2636 if (!Tag->isBeingDefined() &&
2637 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2638 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002639 }
2640 }
2641
Douglas Gregor815215d2009-05-27 05:35:12 +00002642 NamedDecl *Result = 0;
2643 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002644 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002645 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2646 } else {
2647 // Since we don't have a name for the entity we're looking for,
2648 // our only option is to walk through all of the declarations to
2649 // find that name. This will occur in a few cases:
2650 //
2651 // - anonymous struct/union within a template
2652 // - unnamed class/struct/union/enum within a template
2653 //
2654 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002655 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002656 ParentDC->decls_begin(),
2657 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002658 }
Mike Stump1eb44332009-09-09 15:08:12 +00002659
John McCall9f54ad42009-12-10 09:41:52 +00002660 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002661 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2662 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002663 && "Unable to find instantiation of declaration!");
2664
Douglas Gregor815215d2009-05-27 05:35:12 +00002665 D = Result;
2666 }
2667
Douglas Gregor815215d2009-05-27 05:35:12 +00002668 return D;
2669}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002670
Mike Stump1eb44332009-09-09 15:08:12 +00002671/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002672/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002673void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2674 while (!PendingLocalImplicitInstantiations.empty() ||
2675 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2676 PendingImplicitInstantiation Inst;
2677
2678 if (PendingLocalImplicitInstantiations.empty()) {
2679 Inst = PendingImplicitInstantiations.front();
2680 PendingImplicitInstantiations.pop_front();
2681 } else {
2682 Inst = PendingLocalImplicitInstantiations.front();
2683 PendingLocalImplicitInstantiations.pop_front();
2684 }
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Douglas Gregor7caa6822009-07-24 20:34:43 +00002686 // Instantiate function definitions
2687 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002688 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002689 Function->getLocation(), *this,
2690 Context.getSourceManager(),
2691 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002693 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002694 continue;
2695 }
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Douglas Gregor7caa6822009-07-24 20:34:43 +00002697 // Instantiate static data member definitions.
2698 VarDecl *Var = cast<VarDecl>(Inst.first);
2699 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002700
Chandler Carruth291b4412010-02-13 10:17:50 +00002701 // Don't try to instantiate declarations if the most recent redeclaration
2702 // is invalid.
2703 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2704 continue;
2705
2706 // Check if the most recent declaration has changed the specialization kind
2707 // and removed the need for implicit instantiation.
2708 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2709 case TSK_Undeclared:
2710 assert(false && "Cannot instantitiate an undeclared specialization.");
2711 case TSK_ExplicitInstantiationDeclaration:
2712 case TSK_ExplicitInstantiationDefinition:
2713 case TSK_ExplicitSpecialization:
2714 continue; // No longer need implicit instantiation.
2715 case TSK_ImplicitInstantiation:
2716 break;
2717 }
2718
Mike Stump1eb44332009-09-09 15:08:12 +00002719 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002720 Var->getLocation(), *this,
2721 Context.getSourceManager(),
2722 "instantiating static data member "
2723 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregor7caa6822009-07-24 20:34:43 +00002725 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002726 }
2727}
John McCall0c01d182010-03-24 05:22:00 +00002728
2729void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2730 const MultiLevelTemplateArgumentList &TemplateArgs) {
2731 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2732 E = Pattern->ddiag_end(); I != E; ++I) {
2733 DependentDiagnostic *DD = *I;
2734
2735 switch (DD->getKind()) {
2736 case DependentDiagnostic::Access:
2737 HandleDependentAccessCheck(*DD, TemplateArgs);
2738 break;
2739 }
2740 }
2741}