blob: f3d37787f81e34f33e054d0719737256b1aa9955 [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
Anders Carlssond8fe2d52009-11-07 06:07:58 +000034 void InstantiateAttrs(Decl *Tmpl, Decl *New);
35
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?
146void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
147 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());
356
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);
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001052 if (!SemaRef.getLangOptions().CPlusPlus0x) {
1053 // C++03 [temp.friend]p4:
1054 // When a function is defined in a friend function declaration in a
1055 // class template, the function is defined at each instantiation of the
1056 // class template. The function is defined even if it is never used.
1057 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Owner)) {
1058 if (ClassTemplateSpecializationDecl *Spec
1059 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
1060 InstantiateAtPOI = Spec->getPointOfInstantiation();
1061 else if (MemberSpecializationInfo *MSInfo
1062 = Record->getMemberSpecializationInfo())
1063 InstantiateAtPOI = MSInfo->getPointOfInstantiation();
1064 }
1065
1066 if (InstantiateAtPOI.isInvalid())
1067 InstantiateAtPOI = Function->getLocation();
1068 }
John McCall02cace72009-08-28 07:59:38 +00001069 }
Douglas Gregora735b202009-10-13 14:39:41 +00001070
Douglas Gregore53060f2009-06-25 22:08:12 +00001071 if (InitFunctionInstantiation(Function, D))
1072 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Douglas Gregore53060f2009-06-25 22:08:12 +00001074 bool Redeclaration = false;
1075 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001076 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001077
John McCall68263142009-11-18 22:49:29 +00001078 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1079 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1080
John McCallaf2094e2010-04-08 09:05:18 +00001081 if (DependentFunctionTemplateSpecializationInfo *Info
1082 = D->getDependentSpecializationInfo()) {
1083 assert(isFriend && "non-friend has dependent specialization info?");
1084
1085 // This needs to be set now for future sanity.
1086 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1087
1088 // Instantiate the explicit template arguments.
1089 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1090 Info->getRAngleLoc());
1091 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1092 TemplateArgumentLoc Loc;
1093 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1094 return 0;
1095
1096 ExplicitArgs.addArgument(Loc);
1097 }
1098
1099 // Map the candidate templates to their instantiations.
1100 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1101 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1102 Info->getTemplate(I),
1103 TemplateArgs);
1104 if (!Temp) return 0;
1105
1106 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1107 }
1108
1109 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1110 &ExplicitArgs,
1111 Previous))
1112 Function->setInvalidDecl();
1113
1114 isExplicitSpecialization = true;
1115
1116 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001117 // Look only into the namespace where the friend would be declared to
1118 // find a previous declaration. This is the innermost enclosing namespace,
1119 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001120 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001121
Douglas Gregora735b202009-10-13 14:39:41 +00001122 // In C++, the previous declaration we find might be a tag type
1123 // (class or enum). In this case, the new declaration will hide the
1124 // tag type. Note that this does does not apply if we're declaring a
1125 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001126 if (Previous.isSingleTagDecl())
1127 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001128 }
1129
John McCall9f54ad42009-12-10 09:41:52 +00001130 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001131 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001132 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001133
John McCall76d32642010-04-24 01:30:58 +00001134 NamedDecl *PrincipalDecl = (TemplateParams
1135 ? cast<NamedDecl>(FunctionTemplate)
1136 : Function);
1137
Douglas Gregora735b202009-10-13 14:39:41 +00001138 // If the original function was part of a friend declaration,
1139 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001140 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001141 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001142 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001143 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001144 else
Douglas Gregora735b202009-10-13 14:39:41 +00001145 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001146
1147 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1148 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregora735b202009-10-13 14:39:41 +00001149 }
1150
John McCall76d32642010-04-24 01:30:58 +00001151 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1152 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1153 PrincipalDecl->setNonMemberOperator();
1154
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001155 // If we need to instantiate this function now (because it is a C++98/03
1156 // friend function defined inside a class template), do so.
1157 if (InstantiateAtPOI.isValid())
1158 SemaRef.MarkDeclarationReferenced(InstantiateAtPOI, Function);
1159
Douglas Gregore53060f2009-06-25 22:08:12 +00001160 return Function;
1161}
1162
Douglas Gregord60e1052009-08-27 16:57:43 +00001163Decl *
1164TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1165 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001166 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1167 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001168 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001169 // We are creating a function template specialization from a function
1170 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001171 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001172 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001173 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001174 TemplateArgs.getInnermost().getFlatArgumentList(),
1175 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001176 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
1178 FunctionTemplateSpecializationInfo *Info
1179 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001180 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor6b906862009-08-21 00:16:32 +00001182 // If we already have a function template specialization, return it.
1183 if (Info)
1184 return Info->Function;
1185 }
1186
John McCallb0cb0222010-03-27 05:57:59 +00001187 bool isFriend;
1188 if (FunctionTemplate)
1189 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1190 else
1191 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1192
Douglas Gregor79c22782010-01-16 20:21:20 +00001193 bool MergeWithParentScope = (TemplateParams != 0) ||
1194 !(isa<Decl>(Owner) &&
1195 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1196 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001197
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001198 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001199 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1200 TInfo = SubstFunctionType(D, Params);
1201 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001202 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001203 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001204
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001205 // \brief If the type of this function is not *directly* a function
1206 // type, then we're instantiating the a function that was declared
1207 // via a typedef, e.g.,
1208 //
1209 // typedef int functype(int, int);
1210 // functype func;
1211 //
1212 // In this case, we'll just go instantiate the ParmVarDecls that we
1213 // synthesized in the method declaration.
1214 if (!isa<FunctionProtoType>(T)) {
1215 assert(!Params.size() && "Instantiating type could not yield parameters");
1216 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1217 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1218 TemplateArgs);
1219 if (!P)
1220 return 0;
1221
1222 Params.push_back(P);
1223 }
1224 }
1225
John McCallb0cb0222010-03-27 05:57:59 +00001226 NestedNameSpecifier *Qualifier = D->getQualifier();
1227 if (Qualifier) {
1228 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1229 D->getQualifierRange(),
1230 TemplateArgs);
1231 if (!Qualifier) return 0;
1232 }
1233
1234 DeclContext *DC = Owner;
1235 if (isFriend) {
1236 if (Qualifier) {
1237 CXXScopeSpec SS;
1238 SS.setScopeRep(Qualifier);
1239 SS.setRange(D->getQualifierRange());
1240 DC = SemaRef.computeDeclContext(SS);
1241 } else {
1242 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1243 D->getDeclContext(),
1244 TemplateArgs);
1245 }
1246 if (!DC) return 0;
1247 }
1248
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001249 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001250 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001251 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Douglas Gregordec06662009-08-21 18:42:58 +00001253 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001254 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001255 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1256 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1257 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001258 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1259 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001260 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001261 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001262 Constructor->isInlineSpecified(),
1263 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001264 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1265 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1266 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1267 SemaRef.Context.getCanonicalType(ClassTy));
1268 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1269 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001270 T, Destructor->isInlineSpecified(),
1271 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001272 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001273 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001274 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001275 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001276 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1277 ConvTy);
1278 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1279 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001280 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001281 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001282 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001283 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001284 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001285 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001286 D->isStatic(),
1287 D->getStorageClassAsWritten(),
1288 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001289 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001290
John McCallb0cb0222010-03-27 05:57:59 +00001291 if (Qualifier)
1292 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001293
Douglas Gregord60e1052009-08-27 16:57:43 +00001294 if (TemplateParams) {
1295 // Our resulting instantiation is actually a function template, since we
1296 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001297 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001298 // template<typename T>
1299 // struct X {
1300 // template<typename U> void f(T, U);
1301 // };
1302 //
1303 // X<int> x;
1304 //
1305 // We are instantiating the member template "f" within X<int>, which means
1306 // substituting int for T, but leaving "f" as a member function template.
1307 // Build the function template itself.
1308 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1309 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001310 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001311 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001312 if (isFriend) {
1313 FunctionTemplate->setLexicalDeclContext(Owner);
1314 FunctionTemplate->setObjectOfFriendDecl(true);
1315 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001316 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001317 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001318 } else if (FunctionTemplate) {
1319 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001320 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001321 &TemplateArgs.getInnermost(),
1322 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001323 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001324 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001325 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001326 }
1327
Mike Stump1eb44332009-09-09 15:08:12 +00001328 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001329 // out-of-line, the instantiation will have the same lexical
1330 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001331 if (isFriend) {
1332 Method->setLexicalDeclContext(Owner);
1333 Method->setObjectOfFriendDecl(true);
1334 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001335 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Douglas Gregor5545e162009-03-24 00:38:23 +00001337 // Attach the parameters
1338 for (unsigned P = 0; P < Params.size(); ++P)
1339 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001340 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001341
1342 if (InitMethodInstantiation(Method, D))
1343 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001344
John McCall68263142009-11-18 22:49:29 +00001345 LookupResult Previous(SemaRef, Name, SourceLocation(),
1346 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001347
John McCallb0cb0222010-03-27 05:57:59 +00001348 if (!FunctionTemplate || TemplateParams || isFriend) {
1349 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Douglas Gregordec06662009-08-21 18:42:58 +00001351 // In C++, the previous declaration we find might be a tag type
1352 // (class or enum). In this case, the new declaration will hide the
1353 // tag type. Note that this does does not apply if we're declaring a
1354 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001355 if (Previous.isSingleTagDecl())
1356 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001357 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001358
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001359 bool Redeclaration = false;
1360 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001361 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001362 /*FIXME:*/OverloadableAttrRequired);
1363
Douglas Gregor4ba31362009-12-01 17:24:26 +00001364 if (D->isPure())
1365 SemaRef.CheckPureMethod(Method, SourceRange());
1366
John McCall46460a62010-01-20 21:53:11 +00001367 Method->setAccess(D->getAccess());
1368
John McCallb0cb0222010-03-27 05:57:59 +00001369 if (FunctionTemplate) {
1370 // If there's a function template, let our caller handle it.
1371 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1372 // Don't hide a (potentially) valid declaration with an invalid one.
1373 } else {
1374 NamedDecl *DeclToAdd = (TemplateParams
1375 ? cast<NamedDecl>(FunctionTemplate)
1376 : Method);
1377 if (isFriend)
1378 Record->makeDeclVisibleInContext(DeclToAdd);
1379 else
1380 Owner->addDecl(DeclToAdd);
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001383 return Method;
1384}
1385
Douglas Gregor615c5d42009-03-24 16:43:20 +00001386Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001387 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001388}
1389
Douglas Gregor03b2b072009-03-24 00:15:49 +00001390Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001391 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001392}
1393
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001394Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001395 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001396}
1397
Douglas Gregor6477b692009-03-25 15:04:13 +00001398ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001399 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001400}
1401
John McCalle29ba202009-08-20 01:44:21 +00001402Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1403 TemplateTypeParmDecl *D) {
1404 // TODO: don't always clone when decls are refcounted.
1405 const Type* T = D->getTypeForDecl();
1406 assert(T->isTemplateTypeParmType());
1407 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
John McCalle29ba202009-08-20 01:44:21 +00001409 TemplateTypeParmDecl *Inst =
1410 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001411 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001412 TTPT->getName(),
1413 D->wasDeclaredWithTypename(),
1414 D->isParameterPack());
1415
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001416 if (D->hasDefaultArgument())
1417 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001418
Douglas Gregor550d9b22009-10-31 17:21:17 +00001419 // Introduce this template parameter's instantiation into the instantiation
1420 // scope.
1421 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1422
John McCalle29ba202009-08-20 01:44:21 +00001423 return Inst;
1424}
1425
Douglas Gregor33642df2009-10-23 23:25:44 +00001426Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1427 NonTypeTemplateParmDecl *D) {
1428 // Substitute into the type of the non-type template parameter.
1429 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001430 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001431 if (DI) {
1432 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1433 D->getDeclName());
1434 if (DI) T = DI->getType();
1435 } else {
1436 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1437 D->getDeclName());
1438 DI = 0;
1439 }
1440 if (T.isNull())
1441 return 0;
1442
1443 // Check that this type is acceptable for a non-type template parameter.
1444 bool Invalid = false;
1445 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1446 if (T.isNull()) {
1447 T = SemaRef.Context.IntTy;
1448 Invalid = true;
1449 }
1450
1451 NonTypeTemplateParmDecl *Param
1452 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1453 D->getDepth() - 1, D->getPosition(),
1454 D->getIdentifier(), T, DI);
1455 if (Invalid)
1456 Param->setInvalidDecl();
1457
1458 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001459
1460 // Introduce this template parameter's instantiation into the instantiation
1461 // scope.
1462 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001463 return Param;
1464}
1465
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001466Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001467TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1468 TemplateTemplateParmDecl *D) {
1469 // Instantiate the template parameter list of the template template parameter.
1470 TemplateParameterList *TempParams = D->getTemplateParameters();
1471 TemplateParameterList *InstParams;
1472 {
1473 // Perform the actual substitution of template parameters within a new,
1474 // local instantiation scope.
1475 Sema::LocalInstantiationScope Scope(SemaRef);
1476 InstParams = SubstTemplateParams(TempParams);
1477 if (!InstParams)
1478 return NULL;
1479 }
1480
1481 // Build the template template parameter.
1482 TemplateTemplateParmDecl *Param
1483 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1484 D->getDepth() - 1, D->getPosition(),
1485 D->getIdentifier(), InstParams);
1486 Param->setDefaultArgument(D->getDefaultArgument());
1487
1488 // Introduce this template parameter's instantiation into the instantiation
1489 // scope.
1490 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1491
1492 return Param;
1493}
1494
Douglas Gregor48c32a72009-11-17 06:07:40 +00001495Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1496 // Using directives are never dependent, so they require no explicit
1497
1498 UsingDirectiveDecl *Inst
1499 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1500 D->getNamespaceKeyLocation(),
1501 D->getQualifierRange(), D->getQualifier(),
1502 D->getIdentLocation(),
1503 D->getNominatedNamespace(),
1504 D->getCommonAncestor());
1505 Owner->addDecl(Inst);
1506 return Inst;
1507}
1508
John McCalled976492009-12-04 22:46:56 +00001509Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1510 // The nested name specifier is non-dependent, so no transformation
1511 // is required.
1512
John McCall9f54ad42009-12-10 09:41:52 +00001513 // We only need to do redeclaration lookups if we're in a class
1514 // scope (in fact, it's not really even possible in non-class
1515 // scopes).
1516 bool CheckRedeclaration = Owner->isRecord();
1517
1518 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1519 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1520
John McCalled976492009-12-04 22:46:56 +00001521 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1522 D->getLocation(),
1523 D->getNestedNameRange(),
1524 D->getUsingLocation(),
1525 D->getTargetNestedNameDecl(),
1526 D->getDeclName(),
1527 D->isTypeName());
1528
1529 CXXScopeSpec SS;
1530 SS.setScopeRep(D->getTargetNestedNameDecl());
1531 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001532
1533 if (CheckRedeclaration) {
1534 Prev.setHideTags(false);
1535 SemaRef.LookupQualifiedName(Prev, Owner);
1536
1537 // Check for invalid redeclarations.
1538 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1539 D->isTypeName(), SS,
1540 D->getLocation(), Prev))
1541 NewUD->setInvalidDecl();
1542
1543 }
1544
1545 if (!NewUD->isInvalidDecl() &&
1546 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001547 D->getLocation()))
1548 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001549
John McCalled976492009-12-04 22:46:56 +00001550 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1551 NewUD->setAccess(D->getAccess());
1552 Owner->addDecl(NewUD);
1553
John McCall9f54ad42009-12-10 09:41:52 +00001554 // Don't process the shadow decls for an invalid decl.
1555 if (NewUD->isInvalidDecl())
1556 return NewUD;
1557
John McCall323c3102009-12-22 22:26:37 +00001558 bool isFunctionScope = Owner->isFunctionOrMethod();
1559
John McCall9f54ad42009-12-10 09:41:52 +00001560 // Process the shadow decls.
1561 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1562 I != E; ++I) {
1563 UsingShadowDecl *Shadow = *I;
1564 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001565 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1566 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001567 TemplateArgs));
1568
1569 if (CheckRedeclaration &&
1570 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1571 continue;
1572
1573 UsingShadowDecl *InstShadow
1574 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1575 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001576
1577 if (isFunctionScope)
1578 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001579 }
John McCalled976492009-12-04 22:46:56 +00001580
1581 return NewUD;
1582}
1583
1584Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001585 // Ignore these; we handle them in bulk when processing the UsingDecl.
1586 return 0;
John McCalled976492009-12-04 22:46:56 +00001587}
1588
John McCall7ba107a2009-11-18 02:36:19 +00001589Decl * TemplateDeclInstantiator
1590 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001591 NestedNameSpecifier *NNS =
1592 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1593 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001594 TemplateArgs);
1595 if (!NNS)
1596 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001598 CXXScopeSpec SS;
1599 SS.setRange(D->getTargetNestedNameRange());
1600 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
1602 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001603 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001604 D->getUsingLoc(), SS, D->getLocation(),
1605 D->getDeclName(), 0,
1606 /*instantiation*/ true,
1607 /*typename*/ true, D->getTypenameLoc());
1608 if (UD)
John McCalled976492009-12-04 22:46:56 +00001609 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1610
John McCall7ba107a2009-11-18 02:36:19 +00001611 return UD;
1612}
1613
1614Decl * TemplateDeclInstantiator
1615 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1616 NestedNameSpecifier *NNS =
1617 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1618 D->getTargetNestedNameRange(),
1619 TemplateArgs);
1620 if (!NNS)
1621 return 0;
1622
1623 CXXScopeSpec SS;
1624 SS.setRange(D->getTargetNestedNameRange());
1625 SS.setScopeRep(NNS);
1626
1627 NamedDecl *UD =
1628 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1629 D->getUsingLoc(), SS, D->getLocation(),
1630 D->getDeclName(), 0,
1631 /*instantiation*/ true,
1632 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001633 if (UD)
John McCalled976492009-12-04 22:46:56 +00001634 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1635
Anders Carlsson0d8df782009-08-29 19:37:28 +00001636 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001637}
1638
John McCallce3ff2b2009-08-25 22:02:44 +00001639Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001640 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001641 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001642 if (D->isInvalidDecl())
1643 return 0;
1644
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001645 return Instantiator.Visit(D);
1646}
1647
John McCalle29ba202009-08-20 01:44:21 +00001648/// \brief Instantiates a nested template parameter list in the current
1649/// instantiation context.
1650///
1651/// \param L The parameter list to instantiate
1652///
1653/// \returns NULL if there was an error
1654TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001655TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001656 // Get errors for all the parameters before bailing out.
1657 bool Invalid = false;
1658
1659 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001660 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001661 ParamVector Params;
1662 Params.reserve(N);
1663 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1664 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001665 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001666 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001667 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001668 }
1669
1670 // Clean up if we had an error.
1671 if (Invalid) {
1672 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1673 PI != PE; ++PI)
1674 if (*PI)
1675 (*PI)->Destroy(SemaRef.Context);
1676 return NULL;
1677 }
1678
1679 TemplateParameterList *InstL
1680 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1681 L->getLAngleLoc(), &Params.front(), N,
1682 L->getRAngleLoc());
1683 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001684}
John McCalle29ba202009-08-20 01:44:21 +00001685
Douglas Gregored9c0f92009-10-29 00:04:11 +00001686/// \brief Instantiate the declaration of a class template partial
1687/// specialization.
1688///
1689/// \param ClassTemplate the (instantiated) class template that is partially
1690// specialized by the instantiation of \p PartialSpec.
1691///
1692/// \param PartialSpec the (uninstantiated) class template partial
1693/// specialization that we are instantiating.
1694///
1695/// \returns true if there was an error, false otherwise.
1696bool
1697TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1698 ClassTemplateDecl *ClassTemplate,
1699 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001700 // Create a local instantiation scope for this class template partial
1701 // specialization, which will contain the instantiations of the template
1702 // parameters.
1703 Sema::LocalInstantiationScope Scope(SemaRef);
1704
Douglas Gregored9c0f92009-10-29 00:04:11 +00001705 // Substitute into the template parameters of the class template partial
1706 // specialization.
1707 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1708 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1709 if (!InstParams)
1710 return true;
1711
1712 // Substitute into the template arguments of the class template partial
1713 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001714 const TemplateArgumentLoc *PartialSpecTemplateArgs
1715 = PartialSpec->getTemplateArgsAsWritten();
1716 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1717
John McCalld5532b62009-11-23 01:53:49 +00001718 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001719 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001720 TemplateArgumentLoc Loc;
1721 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001722 return true;
John McCalld5532b62009-11-23 01:53:49 +00001723 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001724 }
1725
1726
1727 // Check that the template argument list is well-formed for this
1728 // class template.
1729 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1730 InstTemplateArgs.size());
1731 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1732 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001733 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001734 false,
1735 Converted))
1736 return true;
1737
1738 // Figure out where to insert this class template partial specialization
1739 // in the member template's set of class template partial specializations.
1740 llvm::FoldingSetNodeID ID;
1741 ClassTemplatePartialSpecializationDecl::Profile(ID,
1742 Converted.getFlatArguments(),
1743 Converted.flatSize(),
1744 SemaRef.Context);
1745 void *InsertPos = 0;
1746 ClassTemplateSpecializationDecl *PrevDecl
1747 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1748 InsertPos);
1749
1750 // Build the canonical type that describes the converted template
1751 // arguments of the class template partial specialization.
1752 QualType CanonType
1753 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1754 Converted.getFlatArguments(),
1755 Converted.flatSize());
1756
1757 // Build the fully-sugared type for this class template
1758 // specialization as the user wrote in the specialization
1759 // itself. This means that we'll pretty-print the type retrieved
1760 // from the specialization's declaration the way that the user
1761 // actually wrote the specialization, rather than formatting the
1762 // name based on the "canonical" representation used to store the
1763 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001764 TypeSourceInfo *WrittenTy
1765 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1766 TemplateName(ClassTemplate),
1767 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001768 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001769 CanonType);
1770
1771 if (PrevDecl) {
1772 // We've already seen a partial specialization with the same template
1773 // parameters and template arguments. This can happen, for example, when
1774 // substituting the outer template arguments ends up causing two
1775 // class template partial specializations of a member class template
1776 // to have identical forms, e.g.,
1777 //
1778 // template<typename T, typename U>
1779 // struct Outer {
1780 // template<typename X, typename Y> struct Inner;
1781 // template<typename Y> struct Inner<T, Y>;
1782 // template<typename Y> struct Inner<U, Y>;
1783 // };
1784 //
1785 // Outer<int, int> outer; // error: the partial specializations of Inner
1786 // // have the same signature.
1787 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1788 << WrittenTy;
1789 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1790 << SemaRef.Context.getTypeDeclType(PrevDecl);
1791 return true;
1792 }
1793
1794
1795 // Create the class template partial specialization declaration.
1796 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001797 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1798 PartialSpec->getTagKind(),
1799 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001800 PartialSpec->getLocation(),
1801 InstParams,
1802 ClassTemplate,
1803 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001804 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001805 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001806 0,
1807 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001808 // Substitute the nested name specifier, if any.
1809 if (SubstQualifier(PartialSpec, InstPartialSpec))
1810 return 0;
1811
Douglas Gregored9c0f92009-10-29 00:04:11 +00001812 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1813 InstPartialSpec->setTypeAsWritten(WrittenTy);
1814
1815 // Add this partial specialization to the set of class template partial
1816 // specializations.
1817 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1818 InsertPos);
1819 return false;
1820}
1821
John McCall21ef0fa2010-03-11 09:03:00 +00001822TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001823TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001824 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001825 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1826 assert(OldTInfo && "substituting function without type source info");
1827 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001828 TypeSourceInfo *NewTInfo
1829 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1830 D->getTypeSpecStartLoc(),
1831 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001832 if (!NewTInfo)
1833 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001834
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001835 if (NewTInfo != OldTInfo) {
1836 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001837 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001838 if (FunctionProtoTypeLoc *OldProtoLoc
1839 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1840 TypeLoc NewTL = NewTInfo->getTypeLoc();
1841 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1842 assert(NewProtoLoc && "Missing prototype?");
1843 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1844 // FIXME: Variadic templates will break this.
1845 Params.push_back(NewProtoLoc->getArg(i));
1846 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001847 OldProtoLoc->getArg(i),
1848 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001849 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001850 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001851 } else {
1852 // The function type itself was not dependent and therefore no
1853 // substitution occurred. However, we still need to instantiate
1854 // the function parameters themselves.
1855 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001856 if (FunctionProtoTypeLoc *OldProtoLoc
1857 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1858 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1859 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1860 if (!Parm)
1861 return 0;
1862 Params.push_back(Parm);
1863 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001864 }
1865 }
John McCall21ef0fa2010-03-11 09:03:00 +00001866 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001867}
1868
Mike Stump1eb44332009-09-09 15:08:12 +00001869/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001870/// declaration (New) from the corresponding fields of its template (Tmpl).
1871///
1872/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001873bool
1874TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001875 FunctionDecl *Tmpl) {
1876 if (Tmpl->isDeleted())
1877 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Douglas Gregorcca9e962009-07-01 22:01:06 +00001879 // If we are performing substituting explicitly-specified template arguments
1880 // or deduced template arguments into a function template and we reach this
1881 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001882 // to keeping the new function template specialization. We therefore
1883 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001884 // into a template instantiation for this specific function template
1885 // specialization, which is not a SFINAE context, so that we diagnose any
1886 // further errors in the declaration itself.
1887 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1888 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1889 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1890 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001891 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001892 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001893 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001894 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001895 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001896 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1897 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001898 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001899 }
1900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001902 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1903 assert(Proto && "Function template without prototype?");
1904
1905 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1906 Proto->getNoReturnAttr()) {
1907 // The function has an exception specification or a "noreturn"
1908 // attribute. Substitute into each of the exception types.
1909 llvm::SmallVector<QualType, 4> Exceptions;
1910 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1911 // FIXME: Poor location information!
1912 QualType T
1913 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1914 New->getLocation(), New->getDeclName());
1915 if (T.isNull() ||
1916 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1917 continue;
1918
1919 Exceptions.push_back(T);
1920 }
1921
1922 // Rebuild the function type
1923
1924 const FunctionProtoType *NewProto
1925 = New->getType()->getAs<FunctionProtoType>();
1926 assert(NewProto && "Template instantiation without function prototype?");
1927 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1928 NewProto->arg_type_begin(),
1929 NewProto->getNumArgs(),
1930 NewProto->isVariadic(),
1931 NewProto->getTypeQuals(),
1932 Proto->hasExceptionSpec(),
1933 Proto->hasAnyExceptionSpec(),
1934 Exceptions.size(),
1935 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001936 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001937 }
1938
Douglas Gregore53060f2009-06-25 22:08:12 +00001939 return false;
1940}
1941
Douglas Gregor5545e162009-03-24 00:38:23 +00001942/// \brief Initializes common fields of an instantiated method
1943/// declaration (New) from the corresponding fields of its template
1944/// (Tmpl).
1945///
1946/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001947bool
1948TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001949 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001950 if (InitFunctionInstantiation(New, Tmpl))
1951 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Douglas Gregor5545e162009-03-24 00:38:23 +00001953 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1954 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001955 if (Tmpl->isVirtualAsWritten())
1956 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001957
1958 // FIXME: attributes
1959 // FIXME: New needs a pointer to Tmpl
1960 return false;
1961}
Douglas Gregora58861f2009-05-13 20:28:22 +00001962
1963/// \brief Instantiate the definition of the given function from its
1964/// template.
1965///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001966/// \param PointOfInstantiation the point at which the instantiation was
1967/// required. Note that this is not precisely a "point of instantiation"
1968/// for the function, but it's close.
1969///
Douglas Gregora58861f2009-05-13 20:28:22 +00001970/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001971/// function template specialization or member function of a class template
1972/// specialization.
1973///
1974/// \param Recursive if true, recursively instantiates any functions that
1975/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001976///
1977/// \param DefinitionRequired if true, then we are performing an explicit
1978/// instantiation where the body of the function is required. Complain if
1979/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001980void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001981 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001982 bool Recursive,
1983 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001984 if (Function->isInvalidDecl())
1985 return;
1986
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001987 // Never instantiate an explicit specialization.
1988 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1989 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001990
1991 const FunctionDecl *Definition = 0;
1992 if (Function->getBody(Definition)) {
1993 // We are trying to instantiate a friend function specialization inside
1994 // a class template, but there is already another (non-template) definition
1995 // of the same function.
1996 if (Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1997 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1998 if (Inst)
1999 return;
2000
2001 Diag(Function->getLocation(), diag::err_redefinition)
2002 << Function->getDeclName();
2003 Diag(Definition->getLocation(), diag::note_previous_definition);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002004 Function->setInvalidDecl();
2005 }
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002006
2007 // We have an explicit instantiation (which already occurred) and an
2008 // implicit instantiation. Return without complaint.
2009 return;
2010 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002011
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002012 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002013 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002014 Stmt *Pattern = 0;
2015 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002016 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002017
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002018 if (!Pattern) {
2019 if (DefinitionRequired) {
2020 if (Function->getPrimaryTemplate())
2021 Diag(PointOfInstantiation,
2022 diag::err_explicit_instantiation_undefined_func_template)
2023 << Function->getPrimaryTemplate();
2024 else
2025 Diag(PointOfInstantiation,
2026 diag::err_explicit_instantiation_undefined_member)
2027 << 1 << Function->getDeclName() << Function->getDeclContext();
2028
2029 if (PatternDecl)
2030 Diag(PatternDecl->getLocation(),
2031 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002032 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002033 }
2034
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002035 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002036 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002037
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002038 // If this is an instantiation of friend function defined within a class
2039 // template or class template specialization or member class thereof,
2040 // determine whether there were multiple instantiations of its lexical class.
2041 if (PatternDecl->getFriendObjectKind() != Decl::FOK_None) {
2042 for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
2043 REnd = Function->redecls_end();
2044 R != REnd; ++R) {
2045 if (*R != Function &&
2046 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
2047 if (const FunctionDecl *RPattern
2048 = (*R)->getTemplateInstantiationPattern())
2049 if (RPattern->getBody(RPattern)) {
2050 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2051 if (Inst)
2052 return;
2053
2054 Diag(Function->getLocation(), diag::err_redefinition)
2055 << Function->getDeclName();
2056 Diag((*R)->getLocation(), diag::note_previous_definition);
2057 Function->setInvalidDecl();
2058 return;
2059 }
2060 }
2061 }
2062 }
2063
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002064 // C++0x [temp.explicit]p9:
2065 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002066 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002067 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002068 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002069 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002070 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002071 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002073 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2074 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002075 return;
2076
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002077 // If we're performing recursive template instantiation, create our own
2078 // queue of pending implicit instantiations that we will instantiate later,
2079 // while we're still within our own instantiation context.
2080 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2081 if (Recursive)
2082 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Douglas Gregor9679caf2010-05-12 17:27:19 +00002084 EnterExpressionEvaluationContext EvalContext(*this,
2085 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002086 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2087
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002088 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002089 // recorded, unless we're actually a member function within a local
2090 // class, in which case we need to merge our results with the parent
2091 // scope (of the enclosing function).
2092 bool MergeWithParentScope = false;
2093 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2094 MergeWithParentScope = Rec->isLocalClass();
2095
2096 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002098 // Introduce the instantiated function parameters into the local
2099 // instantiation scope.
2100 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2101 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2102 Function->getParamDecl(I));
2103
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002104 // Enter the scope of this instantiation. We don't use
2105 // PushDeclContext because we don't have a scope.
2106 DeclContext *PreviousContext = CurContext;
2107 CurContext = Function;
2108
Mike Stump1eb44332009-09-09 15:08:12 +00002109 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002110 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002111
2112 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002113 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002114 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2115 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2116 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002117 }
2118
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002119 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002120 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002121
Douglas Gregor52604ab2009-09-11 21:19:12 +00002122 if (Body.isInvalid())
2123 Function->setInvalidDecl();
2124
Mike Stump1eb44332009-09-09 15:08:12 +00002125 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002126 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002127
John McCall0c01d182010-03-24 05:22:00 +00002128 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2129
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002130 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002131
2132 DeclGroupRef DG(Function);
2133 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Douglas Gregor60406be2010-01-16 22:29:39 +00002135 // This class may have local implicit instantiations that need to be
2136 // instantiation within this scope.
2137 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2138 Scope.Exit();
2139
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002140 if (Recursive) {
2141 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002142 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002143 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002145 // Restore the set of pending implicit instantiations.
2146 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2147 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002148}
2149
2150/// \brief Instantiate the definition of the given variable from its
2151/// template.
2152///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002153/// \param PointOfInstantiation the point at which the instantiation was
2154/// required. Note that this is not precisely a "point of instantiation"
2155/// for the function, but it's close.
2156///
2157/// \param Var the already-instantiated declaration of a static member
2158/// variable of a class template specialization.
2159///
2160/// \param Recursive if true, recursively instantiates any functions that
2161/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002162///
2163/// \param DefinitionRequired if true, then we are performing an explicit
2164/// instantiation where an out-of-line definition of the member variable
2165/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002166void Sema::InstantiateStaticDataMemberDefinition(
2167 SourceLocation PointOfInstantiation,
2168 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002169 bool Recursive,
2170 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002171 if (Var->isInvalidDecl())
2172 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Douglas Gregor7caa6822009-07-24 20:34:43 +00002174 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002175 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002176 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002177 assert(Def->isStaticDataMember() && "Not a static data member?");
2178 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Douglas Gregor0d035142009-10-27 18:42:08 +00002180 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002181 // We did not find an out-of-line definition of this static data member,
2182 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002183 // instantiate this definition (or provide a specialization for it) in
2184 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002185 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002186 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002187 Diag(PointOfInstantiation,
2188 diag::err_explicit_instantiation_undefined_member)
2189 << 2 << Var->getDeclName() << Var->getDeclContext();
2190 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2191 }
2192
Douglas Gregor7caa6822009-07-24 20:34:43 +00002193 return;
2194 }
2195
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002196 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002197 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002198 return;
2199
2200 // C++0x [temp.explicit]p9:
2201 // Except for inline functions, other explicit instantiation declarations
2202 // have the effect of suppressing the implicit instantiation of the entity
2203 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002204 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002205 == TSK_ExplicitInstantiationDeclaration)
2206 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Douglas Gregor7caa6822009-07-24 20:34:43 +00002208 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2209 if (Inst)
2210 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor7caa6822009-07-24 20:34:43 +00002212 // If we're performing recursive template instantiation, create our own
2213 // queue of pending implicit instantiations that we will instantiate later,
2214 // while we're still within our own instantiation context.
2215 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2216 if (Recursive)
2217 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Douglas Gregor7caa6822009-07-24 20:34:43 +00002219 // Enter the scope of this instantiation. We don't use
2220 // PushDeclContext because we don't have a scope.
2221 DeclContext *PreviousContext = CurContext;
2222 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002224 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002225 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002226 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002227 CurContext = PreviousContext;
2228
2229 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002230 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2231 assert(MSInfo && "Missing member specialization information?");
2232 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2233 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002234 DeclGroupRef DG(Var);
2235 Consumer.HandleTopLevelDecl(DG);
2236 }
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Douglas Gregor7caa6822009-07-24 20:34:43 +00002238 if (Recursive) {
2239 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002240 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002241 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Douglas Gregor7caa6822009-07-24 20:34:43 +00002243 // Restore the set of pending implicit instantiations.
2244 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002245 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002246}
Douglas Gregor815215d2009-05-27 05:35:12 +00002247
Anders Carlsson09025312009-08-29 05:16:22 +00002248void
2249Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2250 const CXXConstructorDecl *Tmpl,
2251 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Anders Carlsson09025312009-08-29 05:16:22 +00002253 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002254 bool AnyErrors = false;
2255
Anders Carlsson09025312009-08-29 05:16:22 +00002256 // Instantiate all the initializers.
2257 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002258 InitsEnd = Tmpl->init_end();
2259 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002260 CXXBaseOrMemberInitializer *Init = *Inits;
2261
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002262 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002263 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002264 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002266 // Instantiate the initializer.
2267 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2268 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2269 AnyErrors = true;
2270 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002271 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002272
Anders Carlsson09025312009-08-29 05:16:22 +00002273 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002274 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002275 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002276 TemplateArgs,
2277 Init->getSourceLocation(),
2278 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002279 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002280 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002281 New->setInvalidDecl();
2282 continue;
2283 }
2284
John McCalla93c9342009-12-07 02:54:59 +00002285 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002286 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002287 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002288 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002289 Init->getRParenLoc(),
2290 New->getParent());
2291 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002292 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002294 // Is this an anonymous union?
2295 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002296 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2297 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002298 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002299 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2300 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002301 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002302
2303 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002304 NewArgs.size(),
2305 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002306 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002307 Init->getRParenLoc());
2308 }
2309
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002310 if (NewInit.isInvalid()) {
2311 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002312 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002313 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002314 // FIXME: It would be nice if ASTOwningVector had a release function.
2315 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Anders Carlsson09025312009-08-29 05:16:22 +00002317 NewInits.push_back((MemInitTy *)NewInit.get());
2318 }
2319 }
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Anders Carlsson09025312009-08-29 05:16:22 +00002321 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002322 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002323 /*FIXME: ColonLoc */
2324 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002325 NewInits.data(), NewInits.size(),
2326 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002327}
2328
John McCall52a575a2009-08-29 08:11:13 +00002329// TODO: this could be templated if the various decl types used the
2330// same method name.
2331static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2332 ClassTemplateDecl *Instance) {
2333 Pattern = Pattern->getCanonicalDecl();
2334
2335 do {
2336 Instance = Instance->getCanonicalDecl();
2337 if (Pattern == Instance) return true;
2338 Instance = Instance->getInstantiatedFromMemberTemplate();
2339 } while (Instance);
2340
2341 return false;
2342}
2343
Douglas Gregor0d696532009-09-28 06:34:35 +00002344static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2345 FunctionTemplateDecl *Instance) {
2346 Pattern = Pattern->getCanonicalDecl();
2347
2348 do {
2349 Instance = Instance->getCanonicalDecl();
2350 if (Pattern == Instance) return true;
2351 Instance = Instance->getInstantiatedFromMemberTemplate();
2352 } while (Instance);
2353
2354 return false;
2355}
2356
Douglas Gregored9c0f92009-10-29 00:04:11 +00002357static bool
2358isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2359 ClassTemplatePartialSpecializationDecl *Instance) {
2360 Pattern
2361 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2362 do {
2363 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2364 Instance->getCanonicalDecl());
2365 if (Pattern == Instance)
2366 return true;
2367 Instance = Instance->getInstantiatedFromMember();
2368 } while (Instance);
2369
2370 return false;
2371}
2372
John McCall52a575a2009-08-29 08:11:13 +00002373static bool isInstantiationOf(CXXRecordDecl *Pattern,
2374 CXXRecordDecl *Instance) {
2375 Pattern = Pattern->getCanonicalDecl();
2376
2377 do {
2378 Instance = Instance->getCanonicalDecl();
2379 if (Pattern == Instance) return true;
2380 Instance = Instance->getInstantiatedFromMemberClass();
2381 } while (Instance);
2382
2383 return false;
2384}
2385
2386static bool isInstantiationOf(FunctionDecl *Pattern,
2387 FunctionDecl *Instance) {
2388 Pattern = Pattern->getCanonicalDecl();
2389
2390 do {
2391 Instance = Instance->getCanonicalDecl();
2392 if (Pattern == Instance) return true;
2393 Instance = Instance->getInstantiatedFromMemberFunction();
2394 } while (Instance);
2395
2396 return false;
2397}
2398
2399static bool isInstantiationOf(EnumDecl *Pattern,
2400 EnumDecl *Instance) {
2401 Pattern = Pattern->getCanonicalDecl();
2402
2403 do {
2404 Instance = Instance->getCanonicalDecl();
2405 if (Pattern == Instance) return true;
2406 Instance = Instance->getInstantiatedFromMemberEnum();
2407 } while (Instance);
2408
2409 return false;
2410}
2411
John McCalled976492009-12-04 22:46:56 +00002412static bool isInstantiationOf(UsingShadowDecl *Pattern,
2413 UsingShadowDecl *Instance,
2414 ASTContext &C) {
2415 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2416}
2417
2418static bool isInstantiationOf(UsingDecl *Pattern,
2419 UsingDecl *Instance,
2420 ASTContext &C) {
2421 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2422}
2423
John McCall7ba107a2009-11-18 02:36:19 +00002424static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2425 UsingDecl *Instance,
2426 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002427 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002428}
2429
2430static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002431 UsingDecl *Instance,
2432 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002433 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002434}
2435
John McCall52a575a2009-08-29 08:11:13 +00002436static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2437 VarDecl *Instance) {
2438 assert(Instance->isStaticDataMember());
2439
2440 Pattern = Pattern->getCanonicalDecl();
2441
2442 do {
2443 Instance = Instance->getCanonicalDecl();
2444 if (Pattern == Instance) return true;
2445 Instance = Instance->getInstantiatedFromStaticDataMember();
2446 } while (Instance);
2447
2448 return false;
2449}
2450
John McCalled976492009-12-04 22:46:56 +00002451// Other is the prospective instantiation
2452// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002453static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002454 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002455 if (UnresolvedUsingTypenameDecl *UUD
2456 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2457 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2458 return isInstantiationOf(UUD, UD, Ctx);
2459 }
2460 }
2461
2462 if (UnresolvedUsingValueDecl *UUD
2463 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002464 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2465 return isInstantiationOf(UUD, UD, Ctx);
2466 }
2467 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002468
Anders Carlsson0d8df782009-08-29 19:37:28 +00002469 return false;
2470 }
Mike Stump1eb44332009-09-09 15:08:12 +00002471
John McCall52a575a2009-08-29 08:11:13 +00002472 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2473 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002474
John McCall52a575a2009-08-29 08:11:13 +00002475 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2476 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002477
John McCall52a575a2009-08-29 08:11:13 +00002478 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2479 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002480
Douglas Gregor7caa6822009-07-24 20:34:43 +00002481 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002482 if (Var->isStaticDataMember())
2483 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2484
2485 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2486 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002487
Douglas Gregor0d696532009-09-28 06:34:35 +00002488 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2489 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2490
Douglas Gregored9c0f92009-10-29 00:04:11 +00002491 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2492 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2493 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2494 PartialSpec);
2495
Anders Carlssond8b285f2009-09-01 04:26:58 +00002496 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2497 if (!Field->getDeclName()) {
2498 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002499 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002500 cast<FieldDecl>(D);
2501 }
2502 }
Mike Stump1eb44332009-09-09 15:08:12 +00002503
John McCalled976492009-12-04 22:46:56 +00002504 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2505 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2506
2507 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2508 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2509
Douglas Gregor815215d2009-05-27 05:35:12 +00002510 return D->getDeclName() && isa<NamedDecl>(Other) &&
2511 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2512}
2513
2514template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002515static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002516 NamedDecl *D,
2517 ForwardIterator first,
2518 ForwardIterator last) {
2519 for (; first != last; ++first)
2520 if (isInstantiationOf(Ctx, D, *first))
2521 return cast<NamedDecl>(*first);
2522
2523 return 0;
2524}
2525
John McCall02cace72009-08-28 07:59:38 +00002526/// \brief Finds the instantiation of the given declaration context
2527/// within the current instantiation.
2528///
2529/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002530DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002531 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002532 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002533 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002534 return cast_or_null<DeclContext>(ID);
2535 } else return DC;
2536}
2537
Douglas Gregored961e72009-05-27 17:54:46 +00002538/// \brief Find the instantiation of the given declaration within the
2539/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002540///
2541/// This routine is intended to be used when \p D is a declaration
2542/// referenced from within a template, that needs to mapped into the
2543/// corresponding declaration within an instantiation. For example,
2544/// given:
2545///
2546/// \code
2547/// template<typename T>
2548/// struct X {
2549/// enum Kind {
2550/// KnownValue = sizeof(T)
2551/// };
2552///
2553/// bool getKind() const { return KnownValue; }
2554/// };
2555///
2556/// template struct X<int>;
2557/// \endcode
2558///
2559/// In the instantiation of X<int>::getKind(), we need to map the
2560/// EnumConstantDecl for KnownValue (which refers to
2561/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002562/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2563/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002564NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002565 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002566 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002567 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002568 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002569 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002570 // D is a local of some kind. Look into the map of local
2571 // declarations to their instantiations.
2572 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2573 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002574
Douglas Gregore95b4092009-09-16 18:34:49 +00002575 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2576 if (!Record->isDependentContext())
2577 return D;
2578
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002579 // If the RecordDecl is actually the injected-class-name or a
2580 // "templated" declaration for a class template, class template
2581 // partial specialization, or a member class of a class template,
2582 // substitute into the injected-class-name of the class template
2583 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002584 QualType T;
2585 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2586
2587 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002588 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002589 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2590 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002591 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002592
2593 // If we call SubstType with an InjectedClassNameType here we
2594 // can end up in an infinite loop.
2595 T = Context.getTypeDeclType(Record);
2596 assert(isa<InjectedClassNameType>(T) &&
2597 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002598 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002599 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002600
2601 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002602 // Substitute into the injected-class-name to get the type
2603 // corresponding to the instantiation we want, which may also be
2604 // the current instantiation (if we're in a template
2605 // definition). This substitution should never fail, since we
2606 // know we can instantiate the injected-class-name or we
2607 // wouldn't have gotten to the injected-class-name!
2608
2609 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2610 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002611 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2612 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2613
2614 if (!T->isDependentType()) {
2615 assert(T->isRecordType() && "Instantiation must produce a record type");
2616 return T->getAs<RecordType>()->getDecl();
2617 }
2618
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002619 // We are performing "partial" template instantiation to create
2620 // the member declarations for the members of a class template
2621 // specialization. Therefore, D is actually referring to something
2622 // in the current instantiation. Look through the current
2623 // context, which contains actual instantiations, to find the
2624 // instantiation of the "current instantiation" that D refers
2625 // to.
2626 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002627 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002628 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002629 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002630 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002631 if (isInstantiationOf(ClassTemplate,
2632 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002633 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002634
2635 if (!DC->isDependentContext())
2636 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002637 }
2638
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002639 // We're performing "instantiation" of a member of the current
2640 // instantiation while we are type-checking the
2641 // definition. Compute the declaration context and return that.
2642 assert(!SawNonDependentContext &&
2643 "No dependent context while instantiating record");
2644 DeclContext *DC = computeDeclContext(T);
2645 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002646 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002647 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002648 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002649
Douglas Gregore95b4092009-09-16 18:34:49 +00002650 // Fall through to deal with other dependent record types (e.g.,
2651 // anonymous unions in class templates).
2652 }
John McCall52a575a2009-08-29 08:11:13 +00002653
Douglas Gregore95b4092009-09-16 18:34:49 +00002654 if (!ParentDC->isDependentContext())
2655 return D;
2656
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002657 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002658 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002659 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Douglas Gregor815215d2009-05-27 05:35:12 +00002661 if (ParentDC != D->getDeclContext()) {
2662 // We performed some kind of instantiation in the parent context,
2663 // so now we need to look into the instantiated parent context to
2664 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002665
John McCall3cb0ebd2010-03-10 03:28:59 +00002666 // If our context used to be dependent, we may need to instantiate
2667 // it before performing lookup into that context.
2668 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002669 if (!Spec->isDependentContext()) {
2670 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002671 const RecordType *Tag = T->getAs<RecordType>();
2672 assert(Tag && "type of non-dependent record is not a RecordType");
2673 if (!Tag->isBeingDefined() &&
2674 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2675 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002676 }
2677 }
2678
Douglas Gregor815215d2009-05-27 05:35:12 +00002679 NamedDecl *Result = 0;
2680 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002681 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002682 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2683 } else {
2684 // Since we don't have a name for the entity we're looking for,
2685 // our only option is to walk through all of the declarations to
2686 // find that name. This will occur in a few cases:
2687 //
2688 // - anonymous struct/union within a template
2689 // - unnamed class/struct/union/enum within a template
2690 //
2691 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002692 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002693 ParentDC->decls_begin(),
2694 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002695 }
Mike Stump1eb44332009-09-09 15:08:12 +00002696
John McCall9f54ad42009-12-10 09:41:52 +00002697 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002698 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2699 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002700 && "Unable to find instantiation of declaration!");
2701
Douglas Gregor815215d2009-05-27 05:35:12 +00002702 D = Result;
2703 }
2704
Douglas Gregor815215d2009-05-27 05:35:12 +00002705 return D;
2706}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002707
Mike Stump1eb44332009-09-09 15:08:12 +00002708/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002709/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002710void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2711 while (!PendingLocalImplicitInstantiations.empty() ||
2712 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2713 PendingImplicitInstantiation Inst;
2714
2715 if (PendingLocalImplicitInstantiations.empty()) {
2716 Inst = PendingImplicitInstantiations.front();
2717 PendingImplicitInstantiations.pop_front();
2718 } else {
2719 Inst = PendingLocalImplicitInstantiations.front();
2720 PendingLocalImplicitInstantiations.pop_front();
2721 }
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregor7caa6822009-07-24 20:34:43 +00002723 // Instantiate function definitions
2724 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002725 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002726 Function->getLocation(), *this,
2727 Context.getSourceManager(),
2728 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002730 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002731 continue;
2732 }
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Douglas Gregor7caa6822009-07-24 20:34:43 +00002734 // Instantiate static data member definitions.
2735 VarDecl *Var = cast<VarDecl>(Inst.first);
2736 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002737
Chandler Carruth291b4412010-02-13 10:17:50 +00002738 // Don't try to instantiate declarations if the most recent redeclaration
2739 // is invalid.
2740 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2741 continue;
2742
2743 // Check if the most recent declaration has changed the specialization kind
2744 // and removed the need for implicit instantiation.
2745 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2746 case TSK_Undeclared:
2747 assert(false && "Cannot instantitiate an undeclared specialization.");
2748 case TSK_ExplicitInstantiationDeclaration:
2749 case TSK_ExplicitInstantiationDefinition:
2750 case TSK_ExplicitSpecialization:
2751 continue; // No longer need implicit instantiation.
2752 case TSK_ImplicitInstantiation:
2753 break;
2754 }
2755
Mike Stump1eb44332009-09-09 15:08:12 +00002756 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002757 Var->getLocation(), *this,
2758 Context.getSourceManager(),
2759 "instantiating static data member "
2760 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Douglas Gregor7caa6822009-07-24 20:34:43 +00002762 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002763 }
2764}
John McCall0c01d182010-03-24 05:22:00 +00002765
2766void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2767 const MultiLevelTemplateArgumentList &TemplateArgs) {
2768 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2769 E = Pattern->ddiag_end(); I != E; ++I) {
2770 DependentDiagnostic *DD = *I;
2771
2772 switch (DD->getKind()) {
2773 case DependentDiagnostic::Access:
2774 HandleDependentAccessCheck(*DD, TemplateArgs);
2775 break;
2776 }
2777 }
2778}