blob: a32c0e1ac32af3eaa658cddd8cf0a1cff6ef9e78 [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 Gregord57a38e2010-04-23 16:25:07 +0000220
John McCall46460a62010-01-20 21:53:11 +0000221 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000222 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000224 return Typedef;
225}
226
Douglas Gregor6eef5192009-12-14 19:27:10 +0000227/// \brief Instantiate the arguments provided as part of initialization.
228///
229/// \returns true if an error occurred, false otherwise.
230static bool InstantiateInitializationArguments(Sema &SemaRef,
231 Expr **Args, unsigned NumArgs,
232 const MultiLevelTemplateArgumentList &TemplateArgs,
233 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
234 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
235 for (unsigned I = 0; I != NumArgs; ++I) {
236 // When we hit the first defaulted argument, break out of the loop:
237 // we don't pass those default arguments on.
238 if (Args[I]->isDefaultArgument())
239 break;
240
241 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
242 if (Arg.isInvalid())
243 return true;
244
245 Expr *ArgExpr = (Expr *)Arg.get();
246 InitArgs.push_back(Arg.release());
247
248 // FIXME: We're faking all of the comma locations. Do we need them?
249 FakeCommaLocs.push_back(
250 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
251 }
252
253 return false;
254}
255
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000256/// \brief Instantiate an initializer, breaking it into separate
257/// initialization arguments.
258///
259/// \param S The semantic analysis object.
260///
261/// \param Init The initializer to instantiate.
262///
263/// \param TemplateArgs Template arguments to be substituted into the
264/// initializer.
265///
266/// \param NewArgs Will be filled in with the instantiation arguments.
267///
268/// \returns true if an error occurred, false otherwise
269static bool InstantiateInitializer(Sema &S, Expr *Init,
270 const MultiLevelTemplateArgumentList &TemplateArgs,
271 SourceLocation &LParenLoc,
272 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
273 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
274 SourceLocation &RParenLoc) {
275 NewArgs.clear();
276 LParenLoc = SourceLocation();
277 RParenLoc = SourceLocation();
278
279 if (!Init)
280 return false;
281
282 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
283 Init = ExprTemp->getSubExpr();
284
285 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
286 Init = Binder->getSubExpr();
287
288 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
289 Init = ICE->getSubExprAsWritten();
290
291 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
292 LParenLoc = ParenList->getLParenLoc();
293 RParenLoc = ParenList->getRParenLoc();
294 return InstantiateInitializationArguments(S, ParenList->getExprs(),
295 ParenList->getNumExprs(),
296 TemplateArgs, CommaLocs,
297 NewArgs);
298 }
299
300 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000301 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
302 if (InstantiateInitializationArguments(S,
303 Construct->getArgs(),
304 Construct->getNumArgs(),
305 TemplateArgs,
306 CommaLocs, NewArgs))
307 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000308
Douglas Gregor28329e52010-03-24 21:22:47 +0000309 // FIXME: Fake locations!
310 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
311 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
312 return false;
313 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000314 }
315
316 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
317 if (Result.isInvalid())
318 return true;
319
320 NewArgs.push_back(Result.takeAs<Expr>());
321 return false;
322}
323
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000324Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000325 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000326 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000327 TemplateArgs,
328 D->getTypeSpecStartLoc(),
329 D->getDeclName());
330 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000331 return 0;
332
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000333 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000334 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
335 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000336 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000337 D->getStorageClass(),
338 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000339 Var->setThreadSpecified(D->isThreadSpecified());
340 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
341 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000342
John McCallb6217662010-03-15 10:12:16 +0000343 // Substitute the nested name specifier, if any.
344 if (SubstQualifier(D, Var))
345 return 0;
346
Mike Stump1eb44332009-09-09 15:08:12 +0000347 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000348 // out-of-line, the instantiation will have the same lexical
349 // context (which will be a namespace scope) as the template.
350 if (D->isOutOfLine())
351 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000352
John McCall46460a62010-01-20 21:53:11 +0000353 Var->setAccess(D->getAccess());
354
Mike Stump390b4cc2009-05-16 07:39:55 +0000355 // FIXME: In theory, we could have a previous declaration for variables that
356 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000357 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000358 // FIXME: having to fake up a LookupResult is dumb.
359 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000360 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000361 if (D->isStaticDataMember())
362 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000363 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregor7caa6822009-07-24 20:34:43 +0000365 if (D->isOutOfLine()) {
366 D->getLexicalDeclContext()->addDecl(Var);
367 Owner->makeDeclVisibleInContext(Var);
368 } else {
369 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000370
371 if (Owner->isFunctionOrMethod())
372 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000375 // Link instantiations of static data members back to the template from
376 // which they were instantiated.
377 if (Var->isStaticDataMember())
378 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000379 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000380
Douglas Gregor60c93c92010-02-09 07:26:29 +0000381 if (Var->getAnyInitializer()) {
382 // We already have an initializer in the class.
383 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000384 if (Var->isStaticDataMember() && !D->isOutOfLine())
385 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
386 else
387 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
388
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000389 // Instantiate the initializer.
390 SourceLocation LParenLoc, RParenLoc;
391 llvm::SmallVector<SourceLocation, 4> CommaLocs;
392 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
393 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
394 CommaLocs, InitArgs, RParenLoc)) {
395 // Attach the initializer to the declaration.
396 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000397 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000398 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000399 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000400 move_arg(InitArgs),
401 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000402 RParenLoc);
403 } else if (InitArgs.size() == 1) {
404 Expr *Init = (Expr*)(InitArgs.take()[0]);
405 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
406 SemaRef.Owned(Init),
407 false);
408 } else {
409 assert(InitArgs.size() == 0);
410 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000411 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000412 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000413 // FIXME: Not too happy about invalidating the declaration
414 // because of a bogus initializer.
415 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000416 }
417
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000418 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000419 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
420 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000421
422 return Var;
423}
424
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000425Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
426 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000427 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000428 if (DI->getType()->isDependentType()) {
429 DI = SemaRef.SubstType(DI, TemplateArgs,
430 D->getLocation(), D->getDeclName());
431 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000432 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000433 Invalid = true;
434 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000435 // C++ [temp.arg.type]p3:
436 // If a declaration acquires a function type through a type
437 // dependent on a template-parameter and this causes a
438 // declaration that does not use the syntactic form of a
439 // function declarator to have function type, the program is
440 // ill-formed.
441 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000442 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000443 Invalid = true;
444 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000445 } else {
446 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000447 }
448
449 Expr *BitWidth = D->getBitWidth();
450 if (Invalid)
451 BitWidth = 0;
452 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000453 // The bit-width expression is not potentially evaluated.
454 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000456 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000457 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000458 if (InstantiatedBitWidth.isInvalid()) {
459 Invalid = true;
460 BitWidth = 0;
461 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000462 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000463 }
464
John McCall07fb6be2009-10-22 23:33:21 +0000465 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
466 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000467 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000468 D->getLocation(),
469 D->isMutable(),
470 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000471 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000472 D->getAccess(),
473 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000474 if (!Field) {
475 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000476 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000479 InstantiateAttrs(D, Field);
480
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000481 if (Invalid)
482 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000484 if (!Field->getDeclName()) {
485 // Keep track of where this decl came from.
486 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000489 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000490 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000491 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000492
493 return Field;
494}
495
John McCall02cace72009-08-28 07:59:38 +0000496Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000497 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000498 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000499 if (TypeSourceInfo *Ty = D->getFriendType()) {
500 TypeSourceInfo *InstTy =
501 SemaRef.SubstType(Ty, TemplateArgs,
502 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000503 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000504 return 0;
John McCall02cace72009-08-28 07:59:38 +0000505
Douglas Gregor06245bf2010-04-07 17:57:12 +0000506 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
507 if (!FD)
508 return 0;
509
510 FD->setAccess(AS_public);
511 Owner->addDecl(FD);
512 return FD;
513 }
514
515 NamedDecl *ND = D->getFriendDecl();
516 assert(ND && "friend decl must be a decl or a type!");
517
John McCallaf2094e2010-04-08 09:05:18 +0000518 // All of the Visit implementations for the various potential friend
519 // declarations have to be carefully written to work for friend
520 // objects, with the most important detail being that the target
521 // decl should almost certainly not be placed in Owner.
522 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000523 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
John McCall02cace72009-08-28 07:59:38 +0000525 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000526 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
527 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000528 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000529 Owner->addDecl(FD);
530 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000531}
532
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000533Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
534 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Douglas Gregorac7610d2009-06-22 20:57:11 +0000536 // The expression in a static assertion is not potentially evaluated.
537 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000539 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000540 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000541 if (InstantiatedAssertExpr.isInvalid())
542 return 0;
543
Douglas Gregor43d9d922009-08-08 01:41:12 +0000544 OwningExprResult Message(SemaRef, D->getMessage());
545 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000546 Decl *StaticAssert
547 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000548 move(InstantiatedAssertExpr),
549 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000550 return StaticAssert;
551}
552
553Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000554 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000555 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000556 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000558 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000559 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000560 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000561 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000562 Enum->startDefinition();
563
Douglas Gregor96084f12010-03-01 19:00:07 +0000564 if (D->getDeclContext()->isFunctionOrMethod())
565 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
566
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000567 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568
569 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000570 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
571 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000572 EC != ECEnd; ++EC) {
573 // The specified value for the enumerator.
574 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000575 if (Expr *UninstValue = EC->getInitExpr()) {
576 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000577 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000578 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
John McCallce3ff2b2009-08-25 22:02:44 +0000580 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000581 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000582
583 // Drop the initial value and continue.
584 bool isInvalid = false;
585 if (Value.isInvalid()) {
586 Value = SemaRef.Owned((Expr *)0);
587 isInvalid = true;
588 }
589
Mike Stump1eb44332009-09-09 15:08:12 +0000590 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000591 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
592 EC->getLocation(), EC->getIdentifier(),
593 move(Value));
594
595 if (isInvalid) {
596 if (EnumConst)
597 EnumConst->setInvalidDecl();
598 Enum->setInvalidDecl();
599 }
600
601 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000602 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000603 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000604 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000605 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000606
607 if (D->getDeclContext()->isFunctionOrMethod()) {
608 // If the enumeration is within a function or method, record the enum
609 // constant as a local.
610 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
611 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000612 }
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000615 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000616 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000617 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
618 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000619 &Enumerators[0], Enumerators.size(),
620 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000621
622 return Enum;
623}
624
Douglas Gregor6477b692009-03-25 15:04:13 +0000625Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
626 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
627 return 0;
628}
629
John McCalle29ba202009-08-20 01:44:21 +0000630Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000631 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
632
Douglas Gregor550d9b22009-10-31 17:21:17 +0000633 // Create a local instantiation scope for this class template, which
634 // will contain the instantiations of the template parameters.
635 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000636 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000637 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000638 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000639 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000640
641 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000642
643 // Instantiate the qualifier. We have to do this first in case
644 // we're a friend declaration, because if we are then we need to put
645 // the new declaration in the appropriate context.
646 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
647 if (Qualifier) {
648 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
649 Pattern->getQualifierRange(),
650 TemplateArgs);
651 if (!Qualifier) return 0;
652 }
653
654 CXXRecordDecl *PrevDecl = 0;
655 ClassTemplateDecl *PrevClassTemplate = 0;
656
657 // If this isn't a friend, then it's a member template, in which
658 // case we just want to build the instantiation in the
659 // specialization. If it is a friend, we want to build it in
660 // the appropriate context.
661 DeclContext *DC = Owner;
662 if (isFriend) {
663 if (Qualifier) {
664 CXXScopeSpec SS;
665 SS.setScopeRep(Qualifier);
666 SS.setRange(Pattern->getQualifierRange());
667 DC = SemaRef.computeDeclContext(SS);
668 if (!DC) return 0;
669 } else {
670 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
671 Pattern->getDeclContext(),
672 TemplateArgs);
673 }
674
675 // Look for a previous declaration of the template in the owning
676 // context.
677 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
678 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
679 SemaRef.LookupQualifiedName(R, DC);
680
681 if (R.isSingleResult()) {
682 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
683 if (PrevClassTemplate)
684 PrevDecl = PrevClassTemplate->getTemplatedDecl();
685 }
686
687 if (!PrevClassTemplate && Qualifier) {
688 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000689 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
690 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000691 return 0;
692 }
693
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000694 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000695 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000696 bool Complain = true;
697
698 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
699 // template for struct std::tr1::__detail::_Map_base, where the
700 // template parameters of the friend declaration don't match the
701 // template parameters of the original declaration. In this one
702 // case, we don't complain about the ill-formed friend
703 // declaration.
704 if (isFriend && Pattern->getIdentifier() &&
705 Pattern->getIdentifier()->isStr("_Map_base") &&
706 DC->isNamespace() &&
707 cast<NamespaceDecl>(DC)->getIdentifier() &&
708 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
709 DeclContext *DCParent = DC->getParent();
710 if (DCParent->isNamespace() &&
711 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
712 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
713 DeclContext *DCParent2 = DCParent->getParent();
714 if (DCParent2->isNamespace() &&
715 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
716 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
717 DCParent2->getParent()->isTranslationUnit())
718 Complain = false;
719 }
720 }
721
John McCall93ba8572010-03-25 06:39:04 +0000722 TemplateParameterList *PrevParams
723 = PrevClassTemplate->getTemplateParameters();
724
725 // Make sure the parameter lists match.
726 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000727 Complain,
728 Sema::TPL_TemplateMatch)) {
729 if (Complain)
730 return 0;
731
732 AdoptedPreviousTemplateParams = true;
733 InstParams = PrevParams;
734 }
John McCall93ba8572010-03-25 06:39:04 +0000735
736 // Do some additional validation, then merge default arguments
737 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000738 if (!AdoptedPreviousTemplateParams &&
739 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000740 Sema::TPC_ClassTemplate))
741 return 0;
742 }
743 }
744
John McCalle29ba202009-08-20 01:44:21 +0000745 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000746 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000747 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000748 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000749 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000750
John McCall93ba8572010-03-25 06:39:04 +0000751 if (Qualifier)
752 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000753
John McCalle29ba202009-08-20 01:44:21 +0000754 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000755 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
756 D->getIdentifier(), InstParams, RecordInst,
757 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000758 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000759
John McCall93ba8572010-03-25 06:39:04 +0000760 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000761 if (PrevClassTemplate)
762 Inst->setAccess(PrevClassTemplate->getAccess());
763 else
764 Inst->setAccess(D->getAccess());
765
John McCall93ba8572010-03-25 06:39:04 +0000766 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
767 // TODO: do we want to track the instantiation progeny of this
768 // friend target decl?
769 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000770 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000771 Inst->setInstantiatedFromMemberTemplate(D);
772 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000773
774 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000775 SemaRef.Context.getInjectedClassNameType(RecordInst,
776 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000777
Douglas Gregor259571e2009-10-30 22:42:42 +0000778 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000779 if (isFriend) {
780 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000781 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000782 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000783
John McCalle29ba202009-08-20 01:44:21 +0000784 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000785
Douglas Gregored9c0f92009-10-29 00:04:11 +0000786 // Instantiate all of the partial specializations of this member class
787 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000788 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
789 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000790 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
791 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
792
John McCalle29ba202009-08-20 01:44:21 +0000793 return Inst;
794}
795
Douglas Gregord60e1052009-08-27 16:57:43 +0000796Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000797TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
798 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000799 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
800
801 // Lookup the already-instantiated declaration in the instantiation
802 // of the class template and return that.
803 DeclContext::lookup_result Found
804 = Owner->lookup(ClassTemplate->getDeclName());
805 if (Found.first == Found.second)
806 return 0;
807
808 ClassTemplateDecl *InstClassTemplate
809 = dyn_cast<ClassTemplateDecl>(*Found.first);
810 if (!InstClassTemplate)
811 return 0;
812
813 Decl *DCanon = D->getCanonicalDecl();
814 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
815 P = InstClassTemplate->getPartialSpecializations().begin(),
816 PEnd = InstClassTemplate->getPartialSpecializations().end();
817 P != PEnd; ++P) {
818 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
819 return &*P;
820 }
821
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000822 return 0;
823}
824
825Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000826TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000827 // Create a local instantiation scope for this function template, which
828 // will contain the instantiations of the template parameters and then get
829 // merged with the local instantiation scope for the function template
830 // itself.
831 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000832
Douglas Gregord60e1052009-08-27 16:57:43 +0000833 TemplateParameterList *TempParams = D->getTemplateParameters();
834 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000835 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000836 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000837
Douglas Gregora735b202009-10-13 14:39:41 +0000838 FunctionDecl *Instantiated = 0;
839 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
840 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
841 InstParams));
842 else
843 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
844 D->getTemplatedDecl(),
845 InstParams));
846
847 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000848 return 0;
849
John McCall46460a62010-01-20 21:53:11 +0000850 Instantiated->setAccess(D->getAccess());
851
Mike Stump1eb44332009-09-09 15:08:12 +0000852 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000853 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000854 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000855 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000856 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000857 assert(InstTemplate &&
858 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000859
John McCallb1a56e72010-03-26 23:10:15 +0000860 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
861
John McCalle976ffe2009-12-14 23:19:40 +0000862 // Link the instantiation back to the pattern *unless* this is a
863 // non-definition friend declaration.
864 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000865 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000866 InstTemplate->setInstantiatedFromMemberTemplate(D);
867
John McCallb1a56e72010-03-26 23:10:15 +0000868 // Make declarations visible in the appropriate context.
869 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000870 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000871
Douglas Gregord60e1052009-08-27 16:57:43 +0000872 return InstTemplate;
873}
874
Douglas Gregord475b8d2009-03-25 21:17:03 +0000875Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
876 CXXRecordDecl *PrevDecl = 0;
877 if (D->isInjectedClassName())
878 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000879 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000880 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
881 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000882 TemplateArgs);
883 if (!Prev) return 0;
884 PrevDecl = cast<CXXRecordDecl>(Prev);
885 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000886
887 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000888 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000889 D->getLocation(), D->getIdentifier(),
890 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000891
892 // Substitute the nested name specifier, if any.
893 if (SubstQualifier(D, Record))
894 return 0;
895
Douglas Gregord475b8d2009-03-25 21:17:03 +0000896 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000897 // FIXME: Check against AS_none is an ugly hack to work around the issue that
898 // the tag decls introduced by friend class declarations don't have an access
899 // specifier. Remove once this area of the code gets sorted out.
900 if (D->getAccess() != AS_none)
901 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000903 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000904
John McCall02cace72009-08-28 07:59:38 +0000905 // If the original function was part of a friend declaration,
906 // inherit its namespace state.
907 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
908 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
909
Anders Carlssond8b285f2009-09-01 04:26:58 +0000910 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
911
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000912 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000913 return Record;
914}
915
John McCall02cace72009-08-28 07:59:38 +0000916/// Normal class members are of more specific types and therefore
917/// don't make it here. This function serves two purposes:
918/// 1) instantiating function templates
919/// 2) substituting friend declarations
920/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000921Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000922 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000923 // Check whether there is already a function template specialization for
924 // this declaration.
925 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
926 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000927 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000928 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000929 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000930 TemplateArgs.getInnermost().getFlatArgumentList(),
931 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000932 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
934 FunctionTemplateSpecializationInfo *Info
935 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000936 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Douglas Gregor127102b2009-06-29 20:59:39 +0000938 // If we already have a function template specialization, return it.
939 if (Info)
940 return Info->Function;
941 }
Mike Stump1eb44332009-09-09 15:08:12 +0000942
John McCallb0cb0222010-03-27 05:57:59 +0000943 bool isFriend;
944 if (FunctionTemplate)
945 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
946 else
947 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
948
Douglas Gregor79c22782010-01-16 20:21:20 +0000949 bool MergeWithParentScope = (TemplateParams != 0) ||
950 !(isa<Decl>(Owner) &&
951 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
952 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregore53060f2009-06-25 22:08:12 +0000954 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000955 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
956 TInfo = SubstFunctionType(D, Params);
957 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000958 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000959 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000960
John McCalld325daa2010-03-26 04:53:08 +0000961 NestedNameSpecifier *Qualifier = D->getQualifier();
962 if (Qualifier) {
963 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
964 D->getQualifierRange(),
965 TemplateArgs);
966 if (!Qualifier) return 0;
967 }
968
John McCall68b6b872010-02-06 01:50:47 +0000969 // If we're instantiating a local function declaration, put the result
970 // in the owner; otherwise we need to find the instantiated context.
971 DeclContext *DC;
972 if (D->getDeclContext()->isFunctionOrMethod())
973 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000974 else if (isFriend && Qualifier) {
975 CXXScopeSpec SS;
976 SS.setScopeRep(Qualifier);
977 SS.setRange(D->getQualifierRange());
978 DC = SemaRef.computeDeclContext(SS);
979 if (!DC) return 0;
980 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000981 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
982 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000983 }
John McCall68b6b872010-02-06 01:50:47 +0000984
John McCall02cace72009-08-28 07:59:38 +0000985 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000986 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000987 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000988 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000989 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000990
John McCalld325daa2010-03-26 04:53:08 +0000991 if (Qualifier)
992 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000993
John McCallb1a56e72010-03-26 23:10:15 +0000994 DeclContext *LexicalDC = Owner;
995 if (!isFriend && D->isOutOfLine()) {
996 assert(D->getDeclContext()->isFileContext());
997 LexicalDC = D->getDeclContext();
998 }
999
1000 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregore53060f2009-06-25 22:08:12 +00001002 // Attach the parameters
1003 for (unsigned P = 0; P < Params.size(); ++P)
1004 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001005 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001006
Douglas Gregora735b202009-10-13 14:39:41 +00001007 if (TemplateParams) {
1008 // Our resulting instantiation is actually a function template, since we
1009 // are substituting only the outer template parameters. For example, given
1010 //
1011 // template<typename T>
1012 // struct X {
1013 // template<typename U> friend void f(T, U);
1014 // };
1015 //
1016 // X<int> x;
1017 //
1018 // We are instantiating the friend function template "f" within X<int>,
1019 // which means substituting int for T, but leaving "f" as a friend function
1020 // template.
1021 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001022 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001023 Function->getLocation(),
1024 Function->getDeclName(),
1025 TemplateParams, Function);
1026 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001027
1028 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001029
1030 if (isFriend && D->isThisDeclarationADefinition()) {
1031 // TODO: should we remember this connection regardless of whether
1032 // the friend declaration provided a body?
1033 FunctionTemplate->setInstantiatedFromMemberTemplate(
1034 D->getDescribedFunctionTemplate());
1035 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001036 } else if (FunctionTemplate) {
1037 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001038 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001039 &TemplateArgs.getInnermost(),
1040 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001041 } else if (isFriend && D->isThisDeclarationADefinition()) {
1042 // TODO: should we remember this connection regardless of whether
1043 // the friend declaration provided a body?
1044 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001045 }
Douglas Gregora735b202009-10-13 14:39:41 +00001046
Douglas Gregore53060f2009-06-25 22:08:12 +00001047 if (InitFunctionInstantiation(Function, D))
1048 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Douglas Gregore53060f2009-06-25 22:08:12 +00001050 bool Redeclaration = false;
1051 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001052 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001053
John McCall68263142009-11-18 22:49:29 +00001054 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1055 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1056
John McCallaf2094e2010-04-08 09:05:18 +00001057 if (DependentFunctionTemplateSpecializationInfo *Info
1058 = D->getDependentSpecializationInfo()) {
1059 assert(isFriend && "non-friend has dependent specialization info?");
1060
1061 // This needs to be set now for future sanity.
1062 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1063
1064 // Instantiate the explicit template arguments.
1065 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1066 Info->getRAngleLoc());
1067 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1068 TemplateArgumentLoc Loc;
1069 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1070 return 0;
1071
1072 ExplicitArgs.addArgument(Loc);
1073 }
1074
1075 // Map the candidate templates to their instantiations.
1076 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1077 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1078 Info->getTemplate(I),
1079 TemplateArgs);
1080 if (!Temp) return 0;
1081
1082 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1083 }
1084
1085 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1086 &ExplicitArgs,
1087 Previous))
1088 Function->setInvalidDecl();
1089
1090 isExplicitSpecialization = true;
1091
1092 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001093 // Look only into the namespace where the friend would be declared to
1094 // find a previous declaration. This is the innermost enclosing namespace,
1095 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001096 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001097
Douglas Gregora735b202009-10-13 14:39:41 +00001098 // In C++, the previous declaration we find might be a tag type
1099 // (class or enum). In this case, the new declaration will hide the
1100 // tag type. Note that this does does not apply if we're declaring a
1101 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001102 if (Previous.isSingleTagDecl())
1103 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001104 }
1105
John McCall9f54ad42009-12-10 09:41:52 +00001106 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001107 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001108 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001109
John McCall76d32642010-04-24 01:30:58 +00001110 NamedDecl *PrincipalDecl = (TemplateParams
1111 ? cast<NamedDecl>(FunctionTemplate)
1112 : Function);
1113
Douglas Gregora735b202009-10-13 14:39:41 +00001114 // If the original function was part of a friend declaration,
1115 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001116 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001117 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001118 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001119 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001120 else
Douglas Gregora735b202009-10-13 14:39:41 +00001121 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001122
1123 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1124 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregora735b202009-10-13 14:39:41 +00001125 }
1126
John McCall76d32642010-04-24 01:30:58 +00001127 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1128 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1129 PrincipalDecl->setNonMemberOperator();
1130
Douglas Gregore53060f2009-06-25 22:08:12 +00001131 return Function;
1132}
1133
Douglas Gregord60e1052009-08-27 16:57:43 +00001134Decl *
1135TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1136 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001137 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1138 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001139 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001140 // We are creating a function template specialization from a function
1141 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001142 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001143 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001144 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001145 TemplateArgs.getInnermost().getFlatArgumentList(),
1146 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001147 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
1149 FunctionTemplateSpecializationInfo *Info
1150 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001151 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor6b906862009-08-21 00:16:32 +00001153 // If we already have a function template specialization, return it.
1154 if (Info)
1155 return Info->Function;
1156 }
1157
John McCallb0cb0222010-03-27 05:57:59 +00001158 bool isFriend;
1159 if (FunctionTemplate)
1160 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1161 else
1162 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1163
Douglas Gregor79c22782010-01-16 20:21:20 +00001164 bool MergeWithParentScope = (TemplateParams != 0) ||
1165 !(isa<Decl>(Owner) &&
1166 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1167 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001168
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001169 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001170 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1171 TInfo = SubstFunctionType(D, Params);
1172 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001173 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001174 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001175
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001176 // \brief If the type of this function is not *directly* a function
1177 // type, then we're instantiating the a function that was declared
1178 // via a typedef, e.g.,
1179 //
1180 // typedef int functype(int, int);
1181 // functype func;
1182 //
1183 // In this case, we'll just go instantiate the ParmVarDecls that we
1184 // synthesized in the method declaration.
1185 if (!isa<FunctionProtoType>(T)) {
1186 assert(!Params.size() && "Instantiating type could not yield parameters");
1187 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1188 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1189 TemplateArgs);
1190 if (!P)
1191 return 0;
1192
1193 Params.push_back(P);
1194 }
1195 }
1196
John McCallb0cb0222010-03-27 05:57:59 +00001197 NestedNameSpecifier *Qualifier = D->getQualifier();
1198 if (Qualifier) {
1199 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1200 D->getQualifierRange(),
1201 TemplateArgs);
1202 if (!Qualifier) return 0;
1203 }
1204
1205 DeclContext *DC = Owner;
1206 if (isFriend) {
1207 if (Qualifier) {
1208 CXXScopeSpec SS;
1209 SS.setScopeRep(Qualifier);
1210 SS.setRange(D->getQualifierRange());
1211 DC = SemaRef.computeDeclContext(SS);
1212 } else {
1213 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1214 D->getDeclContext(),
1215 TemplateArgs);
1216 }
1217 if (!DC) return 0;
1218 }
1219
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001220 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001221 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001222 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregordec06662009-08-21 18:42:58 +00001224 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001225 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001226 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1227 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1228 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001229 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1230 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001231 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001232 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001233 Constructor->isInlineSpecified(),
1234 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001235 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1236 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1237 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1238 SemaRef.Context.getCanonicalType(ClassTy));
1239 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1240 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001241 T, Destructor->isInlineSpecified(),
1242 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001243 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001244 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001245 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001246 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001247 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1248 ConvTy);
1249 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1250 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001251 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001252 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001253 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001254 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001255 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001256 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001257 D->isStatic(),
1258 D->getStorageClassAsWritten(),
1259 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001260 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001261
John McCallb0cb0222010-03-27 05:57:59 +00001262 if (Qualifier)
1263 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001264
Douglas Gregord60e1052009-08-27 16:57:43 +00001265 if (TemplateParams) {
1266 // Our resulting instantiation is actually a function template, since we
1267 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001268 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001269 // template<typename T>
1270 // struct X {
1271 // template<typename U> void f(T, U);
1272 // };
1273 //
1274 // X<int> x;
1275 //
1276 // We are instantiating the member template "f" within X<int>, which means
1277 // substituting int for T, but leaving "f" as a member function template.
1278 // Build the function template itself.
1279 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1280 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001281 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001282 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001283 if (isFriend) {
1284 FunctionTemplate->setLexicalDeclContext(Owner);
1285 FunctionTemplate->setObjectOfFriendDecl(true);
1286 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001287 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001288 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001289 } else if (FunctionTemplate) {
1290 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001291 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001292 &TemplateArgs.getInnermost(),
1293 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001294 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001295 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001296 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001297 }
1298
Mike Stump1eb44332009-09-09 15:08:12 +00001299 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001300 // out-of-line, the instantiation will have the same lexical
1301 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001302 if (isFriend) {
1303 Method->setLexicalDeclContext(Owner);
1304 Method->setObjectOfFriendDecl(true);
1305 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001306 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Douglas Gregor5545e162009-03-24 00:38:23 +00001308 // Attach the parameters
1309 for (unsigned P = 0; P < Params.size(); ++P)
1310 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001311 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001312
1313 if (InitMethodInstantiation(Method, D))
1314 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001315
John McCall68263142009-11-18 22:49:29 +00001316 LookupResult Previous(SemaRef, Name, SourceLocation(),
1317 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
John McCallb0cb0222010-03-27 05:57:59 +00001319 if (!FunctionTemplate || TemplateParams || isFriend) {
1320 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregordec06662009-08-21 18:42:58 +00001322 // In C++, the previous declaration we find might be a tag type
1323 // (class or enum). In this case, the new declaration will hide the
1324 // tag type. Note that this does does not apply if we're declaring a
1325 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001326 if (Previous.isSingleTagDecl())
1327 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001328 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001329
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001330 bool Redeclaration = false;
1331 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001332 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001333 /*FIXME:*/OverloadableAttrRequired);
1334
Douglas Gregor4ba31362009-12-01 17:24:26 +00001335 if (D->isPure())
1336 SemaRef.CheckPureMethod(Method, SourceRange());
1337
John McCall46460a62010-01-20 21:53:11 +00001338 Method->setAccess(D->getAccess());
1339
John McCallb0cb0222010-03-27 05:57:59 +00001340 if (FunctionTemplate) {
1341 // If there's a function template, let our caller handle it.
1342 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1343 // Don't hide a (potentially) valid declaration with an invalid one.
1344 } else {
1345 NamedDecl *DeclToAdd = (TemplateParams
1346 ? cast<NamedDecl>(FunctionTemplate)
1347 : Method);
1348 if (isFriend)
1349 Record->makeDeclVisibleInContext(DeclToAdd);
1350 else
1351 Owner->addDecl(DeclToAdd);
1352 }
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001354 return Method;
1355}
1356
Douglas Gregor615c5d42009-03-24 16:43:20 +00001357Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001358 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001359}
1360
Douglas Gregor03b2b072009-03-24 00:15:49 +00001361Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001362 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001363}
1364
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001365Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001366 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001367}
1368
Douglas Gregor6477b692009-03-25 15:04:13 +00001369ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001370 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001371}
1372
John McCalle29ba202009-08-20 01:44:21 +00001373Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1374 TemplateTypeParmDecl *D) {
1375 // TODO: don't always clone when decls are refcounted.
1376 const Type* T = D->getTypeForDecl();
1377 assert(T->isTemplateTypeParmType());
1378 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001379
John McCalle29ba202009-08-20 01:44:21 +00001380 TemplateTypeParmDecl *Inst =
1381 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001382 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001383 TTPT->getName(),
1384 D->wasDeclaredWithTypename(),
1385 D->isParameterPack());
1386
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001387 if (D->hasDefaultArgument())
1388 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001389
Douglas Gregor550d9b22009-10-31 17:21:17 +00001390 // Introduce this template parameter's instantiation into the instantiation
1391 // scope.
1392 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1393
John McCalle29ba202009-08-20 01:44:21 +00001394 return Inst;
1395}
1396
Douglas Gregor33642df2009-10-23 23:25:44 +00001397Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1398 NonTypeTemplateParmDecl *D) {
1399 // Substitute into the type of the non-type template parameter.
1400 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001401 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001402 if (DI) {
1403 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1404 D->getDeclName());
1405 if (DI) T = DI->getType();
1406 } else {
1407 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1408 D->getDeclName());
1409 DI = 0;
1410 }
1411 if (T.isNull())
1412 return 0;
1413
1414 // Check that this type is acceptable for a non-type template parameter.
1415 bool Invalid = false;
1416 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1417 if (T.isNull()) {
1418 T = SemaRef.Context.IntTy;
1419 Invalid = true;
1420 }
1421
1422 NonTypeTemplateParmDecl *Param
1423 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1424 D->getDepth() - 1, D->getPosition(),
1425 D->getIdentifier(), T, DI);
1426 if (Invalid)
1427 Param->setInvalidDecl();
1428
1429 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001430
1431 // Introduce this template parameter's instantiation into the instantiation
1432 // scope.
1433 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001434 return Param;
1435}
1436
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001437Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001438TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1439 TemplateTemplateParmDecl *D) {
1440 // Instantiate the template parameter list of the template template parameter.
1441 TemplateParameterList *TempParams = D->getTemplateParameters();
1442 TemplateParameterList *InstParams;
1443 {
1444 // Perform the actual substitution of template parameters within a new,
1445 // local instantiation scope.
1446 Sema::LocalInstantiationScope Scope(SemaRef);
1447 InstParams = SubstTemplateParams(TempParams);
1448 if (!InstParams)
1449 return NULL;
1450 }
1451
1452 // Build the template template parameter.
1453 TemplateTemplateParmDecl *Param
1454 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1455 D->getDepth() - 1, D->getPosition(),
1456 D->getIdentifier(), InstParams);
1457 Param->setDefaultArgument(D->getDefaultArgument());
1458
1459 // Introduce this template parameter's instantiation into the instantiation
1460 // scope.
1461 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1462
1463 return Param;
1464}
1465
Douglas Gregor48c32a72009-11-17 06:07:40 +00001466Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1467 // Using directives are never dependent, so they require no explicit
1468
1469 UsingDirectiveDecl *Inst
1470 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1471 D->getNamespaceKeyLocation(),
1472 D->getQualifierRange(), D->getQualifier(),
1473 D->getIdentLocation(),
1474 D->getNominatedNamespace(),
1475 D->getCommonAncestor());
1476 Owner->addDecl(Inst);
1477 return Inst;
1478}
1479
John McCalled976492009-12-04 22:46:56 +00001480Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1481 // The nested name specifier is non-dependent, so no transformation
1482 // is required.
1483
John McCall9f54ad42009-12-10 09:41:52 +00001484 // We only need to do redeclaration lookups if we're in a class
1485 // scope (in fact, it's not really even possible in non-class
1486 // scopes).
1487 bool CheckRedeclaration = Owner->isRecord();
1488
1489 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1490 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1491
John McCalled976492009-12-04 22:46:56 +00001492 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1493 D->getLocation(),
1494 D->getNestedNameRange(),
1495 D->getUsingLocation(),
1496 D->getTargetNestedNameDecl(),
1497 D->getDeclName(),
1498 D->isTypeName());
1499
1500 CXXScopeSpec SS;
1501 SS.setScopeRep(D->getTargetNestedNameDecl());
1502 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001503
1504 if (CheckRedeclaration) {
1505 Prev.setHideTags(false);
1506 SemaRef.LookupQualifiedName(Prev, Owner);
1507
1508 // Check for invalid redeclarations.
1509 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1510 D->isTypeName(), SS,
1511 D->getLocation(), Prev))
1512 NewUD->setInvalidDecl();
1513
1514 }
1515
1516 if (!NewUD->isInvalidDecl() &&
1517 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001518 D->getLocation()))
1519 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001520
John McCalled976492009-12-04 22:46:56 +00001521 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1522 NewUD->setAccess(D->getAccess());
1523 Owner->addDecl(NewUD);
1524
John McCall9f54ad42009-12-10 09:41:52 +00001525 // Don't process the shadow decls for an invalid decl.
1526 if (NewUD->isInvalidDecl())
1527 return NewUD;
1528
John McCall323c3102009-12-22 22:26:37 +00001529 bool isFunctionScope = Owner->isFunctionOrMethod();
1530
John McCall9f54ad42009-12-10 09:41:52 +00001531 // Process the shadow decls.
1532 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1533 I != E; ++I) {
1534 UsingShadowDecl *Shadow = *I;
1535 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001536 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1537 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001538 TemplateArgs));
1539
1540 if (CheckRedeclaration &&
1541 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1542 continue;
1543
1544 UsingShadowDecl *InstShadow
1545 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1546 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001547
1548 if (isFunctionScope)
1549 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001550 }
John McCalled976492009-12-04 22:46:56 +00001551
1552 return NewUD;
1553}
1554
1555Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001556 // Ignore these; we handle them in bulk when processing the UsingDecl.
1557 return 0;
John McCalled976492009-12-04 22:46:56 +00001558}
1559
John McCall7ba107a2009-11-18 02:36:19 +00001560Decl * TemplateDeclInstantiator
1561 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001562 NestedNameSpecifier *NNS =
1563 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1564 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001565 TemplateArgs);
1566 if (!NNS)
1567 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001569 CXXScopeSpec SS;
1570 SS.setRange(D->getTargetNestedNameRange());
1571 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
1573 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001574 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001575 D->getUsingLoc(), SS, D->getLocation(),
1576 D->getDeclName(), 0,
1577 /*instantiation*/ true,
1578 /*typename*/ true, D->getTypenameLoc());
1579 if (UD)
John McCalled976492009-12-04 22:46:56 +00001580 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1581
John McCall7ba107a2009-11-18 02:36:19 +00001582 return UD;
1583}
1584
1585Decl * TemplateDeclInstantiator
1586 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1587 NestedNameSpecifier *NNS =
1588 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1589 D->getTargetNestedNameRange(),
1590 TemplateArgs);
1591 if (!NNS)
1592 return 0;
1593
1594 CXXScopeSpec SS;
1595 SS.setRange(D->getTargetNestedNameRange());
1596 SS.setScopeRep(NNS);
1597
1598 NamedDecl *UD =
1599 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1600 D->getUsingLoc(), SS, D->getLocation(),
1601 D->getDeclName(), 0,
1602 /*instantiation*/ true,
1603 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001604 if (UD)
John McCalled976492009-12-04 22:46:56 +00001605 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1606
Anders Carlsson0d8df782009-08-29 19:37:28 +00001607 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001608}
1609
John McCallce3ff2b2009-08-25 22:02:44 +00001610Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001611 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001612 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001613 if (D->isInvalidDecl())
1614 return 0;
1615
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001616 return Instantiator.Visit(D);
1617}
1618
John McCalle29ba202009-08-20 01:44:21 +00001619/// \brief Instantiates a nested template parameter list in the current
1620/// instantiation context.
1621///
1622/// \param L The parameter list to instantiate
1623///
1624/// \returns NULL if there was an error
1625TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001626TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001627 // Get errors for all the parameters before bailing out.
1628 bool Invalid = false;
1629
1630 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001631 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001632 ParamVector Params;
1633 Params.reserve(N);
1634 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1635 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001636 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001637 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001638 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001639 }
1640
1641 // Clean up if we had an error.
1642 if (Invalid) {
1643 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1644 PI != PE; ++PI)
1645 if (*PI)
1646 (*PI)->Destroy(SemaRef.Context);
1647 return NULL;
1648 }
1649
1650 TemplateParameterList *InstL
1651 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1652 L->getLAngleLoc(), &Params.front(), N,
1653 L->getRAngleLoc());
1654 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001655}
John McCalle29ba202009-08-20 01:44:21 +00001656
Douglas Gregored9c0f92009-10-29 00:04:11 +00001657/// \brief Instantiate the declaration of a class template partial
1658/// specialization.
1659///
1660/// \param ClassTemplate the (instantiated) class template that is partially
1661// specialized by the instantiation of \p PartialSpec.
1662///
1663/// \param PartialSpec the (uninstantiated) class template partial
1664/// specialization that we are instantiating.
1665///
1666/// \returns true if there was an error, false otherwise.
1667bool
1668TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1669 ClassTemplateDecl *ClassTemplate,
1670 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001671 // Create a local instantiation scope for this class template partial
1672 // specialization, which will contain the instantiations of the template
1673 // parameters.
1674 Sema::LocalInstantiationScope Scope(SemaRef);
1675
Douglas Gregored9c0f92009-10-29 00:04:11 +00001676 // Substitute into the template parameters of the class template partial
1677 // specialization.
1678 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1679 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1680 if (!InstParams)
1681 return true;
1682
1683 // Substitute into the template arguments of the class template partial
1684 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001685 const TemplateArgumentLoc *PartialSpecTemplateArgs
1686 = PartialSpec->getTemplateArgsAsWritten();
1687 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1688
John McCalld5532b62009-11-23 01:53:49 +00001689 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001690 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001691 TemplateArgumentLoc Loc;
1692 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001693 return true;
John McCalld5532b62009-11-23 01:53:49 +00001694 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001695 }
1696
1697
1698 // Check that the template argument list is well-formed for this
1699 // class template.
1700 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1701 InstTemplateArgs.size());
1702 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1703 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001704 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001705 false,
1706 Converted))
1707 return true;
1708
1709 // Figure out where to insert this class template partial specialization
1710 // in the member template's set of class template partial specializations.
1711 llvm::FoldingSetNodeID ID;
1712 ClassTemplatePartialSpecializationDecl::Profile(ID,
1713 Converted.getFlatArguments(),
1714 Converted.flatSize(),
1715 SemaRef.Context);
1716 void *InsertPos = 0;
1717 ClassTemplateSpecializationDecl *PrevDecl
1718 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1719 InsertPos);
1720
1721 // Build the canonical type that describes the converted template
1722 // arguments of the class template partial specialization.
1723 QualType CanonType
1724 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1725 Converted.getFlatArguments(),
1726 Converted.flatSize());
1727
1728 // Build the fully-sugared type for this class template
1729 // specialization as the user wrote in the specialization
1730 // itself. This means that we'll pretty-print the type retrieved
1731 // from the specialization's declaration the way that the user
1732 // actually wrote the specialization, rather than formatting the
1733 // name based on the "canonical" representation used to store the
1734 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001735 TypeSourceInfo *WrittenTy
1736 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1737 TemplateName(ClassTemplate),
1738 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001739 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001740 CanonType);
1741
1742 if (PrevDecl) {
1743 // We've already seen a partial specialization with the same template
1744 // parameters and template arguments. This can happen, for example, when
1745 // substituting the outer template arguments ends up causing two
1746 // class template partial specializations of a member class template
1747 // to have identical forms, e.g.,
1748 //
1749 // template<typename T, typename U>
1750 // struct Outer {
1751 // template<typename X, typename Y> struct Inner;
1752 // template<typename Y> struct Inner<T, Y>;
1753 // template<typename Y> struct Inner<U, Y>;
1754 // };
1755 //
1756 // Outer<int, int> outer; // error: the partial specializations of Inner
1757 // // have the same signature.
1758 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1759 << WrittenTy;
1760 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1761 << SemaRef.Context.getTypeDeclType(PrevDecl);
1762 return true;
1763 }
1764
1765
1766 // Create the class template partial specialization declaration.
1767 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001768 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1769 PartialSpec->getTagKind(),
1770 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001771 PartialSpec->getLocation(),
1772 InstParams,
1773 ClassTemplate,
1774 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001775 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001776 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001777 0,
1778 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001779 // Substitute the nested name specifier, if any.
1780 if (SubstQualifier(PartialSpec, InstPartialSpec))
1781 return 0;
1782
Douglas Gregored9c0f92009-10-29 00:04:11 +00001783 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1784 InstPartialSpec->setTypeAsWritten(WrittenTy);
1785
1786 // Add this partial specialization to the set of class template partial
1787 // specializations.
1788 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1789 InsertPos);
1790 return false;
1791}
1792
John McCall21ef0fa2010-03-11 09:03:00 +00001793TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001794TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001795 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001796 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1797 assert(OldTInfo && "substituting function without type source info");
1798 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001799 TypeSourceInfo *NewTInfo
1800 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1801 D->getTypeSpecStartLoc(),
1802 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001803 if (!NewTInfo)
1804 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001805
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001806 if (NewTInfo != OldTInfo) {
1807 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001808 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001809 if (FunctionProtoTypeLoc *OldProtoLoc
1810 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1811 TypeLoc NewTL = NewTInfo->getTypeLoc();
1812 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1813 assert(NewProtoLoc && "Missing prototype?");
1814 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1815 // FIXME: Variadic templates will break this.
1816 Params.push_back(NewProtoLoc->getArg(i));
1817 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001818 OldProtoLoc->getArg(i),
1819 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001820 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001821 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001822 } else {
1823 // The function type itself was not dependent and therefore no
1824 // substitution occurred. However, we still need to instantiate
1825 // the function parameters themselves.
1826 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001827 if (FunctionProtoTypeLoc *OldProtoLoc
1828 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1829 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1830 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1831 if (!Parm)
1832 return 0;
1833 Params.push_back(Parm);
1834 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001835 }
1836 }
John McCall21ef0fa2010-03-11 09:03:00 +00001837 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001838}
1839
Mike Stump1eb44332009-09-09 15:08:12 +00001840/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001841/// declaration (New) from the corresponding fields of its template (Tmpl).
1842///
1843/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001844bool
1845TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001846 FunctionDecl *Tmpl) {
1847 if (Tmpl->isDeleted())
1848 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Douglas Gregorcca9e962009-07-01 22:01:06 +00001850 // If we are performing substituting explicitly-specified template arguments
1851 // or deduced template arguments into a function template and we reach this
1852 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001853 // to keeping the new function template specialization. We therefore
1854 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001855 // into a template instantiation for this specific function template
1856 // specialization, which is not a SFINAE context, so that we diagnose any
1857 // further errors in the declaration itself.
1858 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1859 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1860 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1861 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001862 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001863 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001864 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001865 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001866 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001867 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1868 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001869 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001870 }
1871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001873 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1874 assert(Proto && "Function template without prototype?");
1875
1876 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1877 Proto->getNoReturnAttr()) {
1878 // The function has an exception specification or a "noreturn"
1879 // attribute. Substitute into each of the exception types.
1880 llvm::SmallVector<QualType, 4> Exceptions;
1881 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1882 // FIXME: Poor location information!
1883 QualType T
1884 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1885 New->getLocation(), New->getDeclName());
1886 if (T.isNull() ||
1887 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1888 continue;
1889
1890 Exceptions.push_back(T);
1891 }
1892
1893 // Rebuild the function type
1894
1895 const FunctionProtoType *NewProto
1896 = New->getType()->getAs<FunctionProtoType>();
1897 assert(NewProto && "Template instantiation without function prototype?");
1898 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1899 NewProto->arg_type_begin(),
1900 NewProto->getNumArgs(),
1901 NewProto->isVariadic(),
1902 NewProto->getTypeQuals(),
1903 Proto->hasExceptionSpec(),
1904 Proto->hasAnyExceptionSpec(),
1905 Exceptions.size(),
1906 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001907 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001908 }
1909
Douglas Gregore53060f2009-06-25 22:08:12 +00001910 return false;
1911}
1912
Douglas Gregor5545e162009-03-24 00:38:23 +00001913/// \brief Initializes common fields of an instantiated method
1914/// declaration (New) from the corresponding fields of its template
1915/// (Tmpl).
1916///
1917/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001918bool
1919TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001920 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001921 if (InitFunctionInstantiation(New, Tmpl))
1922 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor5545e162009-03-24 00:38:23 +00001924 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1925 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001926 if (Tmpl->isVirtualAsWritten())
1927 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001928
1929 // FIXME: attributes
1930 // FIXME: New needs a pointer to Tmpl
1931 return false;
1932}
Douglas Gregora58861f2009-05-13 20:28:22 +00001933
1934/// \brief Instantiate the definition of the given function from its
1935/// template.
1936///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001937/// \param PointOfInstantiation the point at which the instantiation was
1938/// required. Note that this is not precisely a "point of instantiation"
1939/// for the function, but it's close.
1940///
Douglas Gregora58861f2009-05-13 20:28:22 +00001941/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001942/// function template specialization or member function of a class template
1943/// specialization.
1944///
1945/// \param Recursive if true, recursively instantiates any functions that
1946/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001947///
1948/// \param DefinitionRequired if true, then we are performing an explicit
1949/// instantiation where the body of the function is required. Complain if
1950/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001951void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001952 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001953 bool Recursive,
1954 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001955 if (Function->isInvalidDecl())
1956 return;
1957
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001958 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001960 // Never instantiate an explicit specialization.
1961 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1962 return;
1963
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001964 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001965 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001966 Stmt *Pattern = 0;
1967 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001968 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001969
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001970 if (!Pattern) {
1971 if (DefinitionRequired) {
1972 if (Function->getPrimaryTemplate())
1973 Diag(PointOfInstantiation,
1974 diag::err_explicit_instantiation_undefined_func_template)
1975 << Function->getPrimaryTemplate();
1976 else
1977 Diag(PointOfInstantiation,
1978 diag::err_explicit_instantiation_undefined_member)
1979 << 1 << Function->getDeclName() << Function->getDeclContext();
1980
1981 if (PatternDecl)
1982 Diag(PatternDecl->getLocation(),
1983 diag::note_explicit_instantiation_here);
1984 }
1985
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001986 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001987 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001988
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001989 // C++0x [temp.explicit]p9:
1990 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001991 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001992 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001993 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001994 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001995 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001996 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001998 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1999 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002000 return;
2001
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002002 // If we're performing recursive template instantiation, create our own
2003 // queue of pending implicit instantiations that we will instantiate later,
2004 // while we're still within our own instantiation context.
2005 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2006 if (Recursive)
2007 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002009 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2010
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002011 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002012 // recorded, unless we're actually a member function within a local
2013 // class, in which case we need to merge our results with the parent
2014 // scope (of the enclosing function).
2015 bool MergeWithParentScope = false;
2016 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2017 MergeWithParentScope = Rec->isLocalClass();
2018
2019 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002021 // Introduce the instantiated function parameters into the local
2022 // instantiation scope.
2023 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2024 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2025 Function->getParamDecl(I));
2026
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002027 // Enter the scope of this instantiation. We don't use
2028 // PushDeclContext because we don't have a scope.
2029 DeclContext *PreviousContext = CurContext;
2030 CurContext = Function;
2031
Mike Stump1eb44332009-09-09 15:08:12 +00002032 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002033 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002034
2035 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002036 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002037 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2038 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2039 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002040 }
2041
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002042 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002043 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002044
Douglas Gregor52604ab2009-09-11 21:19:12 +00002045 if (Body.isInvalid())
2046 Function->setInvalidDecl();
2047
Mike Stump1eb44332009-09-09 15:08:12 +00002048 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002049 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002050
John McCall0c01d182010-03-24 05:22:00 +00002051 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2052
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002053 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002054
2055 DeclGroupRef DG(Function);
2056 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Douglas Gregor60406be2010-01-16 22:29:39 +00002058 // This class may have local implicit instantiations that need to be
2059 // instantiation within this scope.
2060 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2061 Scope.Exit();
2062
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002063 if (Recursive) {
2064 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002065 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002066 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002068 // Restore the set of pending implicit instantiations.
2069 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2070 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002071}
2072
2073/// \brief Instantiate the definition of the given variable from its
2074/// template.
2075///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002076/// \param PointOfInstantiation the point at which the instantiation was
2077/// required. Note that this is not precisely a "point of instantiation"
2078/// for the function, but it's close.
2079///
2080/// \param Var the already-instantiated declaration of a static member
2081/// variable of a class template specialization.
2082///
2083/// \param Recursive if true, recursively instantiates any functions that
2084/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002085///
2086/// \param DefinitionRequired if true, then we are performing an explicit
2087/// instantiation where an out-of-line definition of the member variable
2088/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002089void Sema::InstantiateStaticDataMemberDefinition(
2090 SourceLocation PointOfInstantiation,
2091 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002092 bool Recursive,
2093 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002094 if (Var->isInvalidDecl())
2095 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Douglas Gregor7caa6822009-07-24 20:34:43 +00002097 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002098 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002099 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002100 assert(Def->isStaticDataMember() && "Not a static data member?");
2101 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregor0d035142009-10-27 18:42:08 +00002103 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002104 // We did not find an out-of-line definition of this static data member,
2105 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002106 // instantiate this definition (or provide a specialization for it) in
2107 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002108 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002109 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002110 Diag(PointOfInstantiation,
2111 diag::err_explicit_instantiation_undefined_member)
2112 << 2 << Var->getDeclName() << Var->getDeclContext();
2113 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2114 }
2115
Douglas Gregor7caa6822009-07-24 20:34:43 +00002116 return;
2117 }
2118
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002119 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002120 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002121 return;
2122
2123 // C++0x [temp.explicit]p9:
2124 // Except for inline functions, other explicit instantiation declarations
2125 // have the effect of suppressing the implicit instantiation of the entity
2126 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002127 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002128 == TSK_ExplicitInstantiationDeclaration)
2129 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Douglas Gregor7caa6822009-07-24 20:34:43 +00002131 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2132 if (Inst)
2133 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Douglas Gregor7caa6822009-07-24 20:34:43 +00002135 // If we're performing recursive template instantiation, create our own
2136 // queue of pending implicit instantiations that we will instantiate later,
2137 // while we're still within our own instantiation context.
2138 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2139 if (Recursive)
2140 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Douglas Gregor7caa6822009-07-24 20:34:43 +00002142 // Enter the scope of this instantiation. We don't use
2143 // PushDeclContext because we don't have a scope.
2144 DeclContext *PreviousContext = CurContext;
2145 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002147 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002148 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002149 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002150 CurContext = PreviousContext;
2151
2152 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002153 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2154 assert(MSInfo && "Missing member specialization information?");
2155 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2156 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002157 DeclGroupRef DG(Var);
2158 Consumer.HandleTopLevelDecl(DG);
2159 }
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Douglas Gregor7caa6822009-07-24 20:34:43 +00002161 if (Recursive) {
2162 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002163 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002164 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Douglas Gregor7caa6822009-07-24 20:34:43 +00002166 // Restore the set of pending implicit instantiations.
2167 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002168 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002169}
Douglas Gregor815215d2009-05-27 05:35:12 +00002170
Anders Carlsson09025312009-08-29 05:16:22 +00002171void
2172Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2173 const CXXConstructorDecl *Tmpl,
2174 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Anders Carlsson09025312009-08-29 05:16:22 +00002176 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002177 bool AnyErrors = false;
2178
Anders Carlsson09025312009-08-29 05:16:22 +00002179 // Instantiate all the initializers.
2180 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002181 InitsEnd = Tmpl->init_end();
2182 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002183 CXXBaseOrMemberInitializer *Init = *Inits;
2184
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002185 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002186 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002187 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002189 // Instantiate the initializer.
2190 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2191 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2192 AnyErrors = true;
2193 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002194 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002195
Anders Carlsson09025312009-08-29 05:16:22 +00002196 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002197 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002198 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002199 TemplateArgs,
2200 Init->getSourceLocation(),
2201 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002202 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002203 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002204 New->setInvalidDecl();
2205 continue;
2206 }
2207
John McCalla93c9342009-12-07 02:54:59 +00002208 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002209 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002210 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002211 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002212 Init->getRParenLoc(),
2213 New->getParent());
2214 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002215 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002217 // Is this an anonymous union?
2218 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002219 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2220 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002221 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002222 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2223 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002224 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002225
2226 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002227 NewArgs.size(),
2228 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002229 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002230 Init->getRParenLoc());
2231 }
2232
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002233 if (NewInit.isInvalid()) {
2234 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002235 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002236 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002237 // FIXME: It would be nice if ASTOwningVector had a release function.
2238 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Anders Carlsson09025312009-08-29 05:16:22 +00002240 NewInits.push_back((MemInitTy *)NewInit.get());
2241 }
2242 }
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Anders Carlsson09025312009-08-29 05:16:22 +00002244 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002245 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002246 /*FIXME: ColonLoc */
2247 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002248 NewInits.data(), NewInits.size(),
2249 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002250}
2251
John McCall52a575a2009-08-29 08:11:13 +00002252// TODO: this could be templated if the various decl types used the
2253// same method name.
2254static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2255 ClassTemplateDecl *Instance) {
2256 Pattern = Pattern->getCanonicalDecl();
2257
2258 do {
2259 Instance = Instance->getCanonicalDecl();
2260 if (Pattern == Instance) return true;
2261 Instance = Instance->getInstantiatedFromMemberTemplate();
2262 } while (Instance);
2263
2264 return false;
2265}
2266
Douglas Gregor0d696532009-09-28 06:34:35 +00002267static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2268 FunctionTemplateDecl *Instance) {
2269 Pattern = Pattern->getCanonicalDecl();
2270
2271 do {
2272 Instance = Instance->getCanonicalDecl();
2273 if (Pattern == Instance) return true;
2274 Instance = Instance->getInstantiatedFromMemberTemplate();
2275 } while (Instance);
2276
2277 return false;
2278}
2279
Douglas Gregored9c0f92009-10-29 00:04:11 +00002280static bool
2281isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2282 ClassTemplatePartialSpecializationDecl *Instance) {
2283 Pattern
2284 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2285 do {
2286 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2287 Instance->getCanonicalDecl());
2288 if (Pattern == Instance)
2289 return true;
2290 Instance = Instance->getInstantiatedFromMember();
2291 } while (Instance);
2292
2293 return false;
2294}
2295
John McCall52a575a2009-08-29 08:11:13 +00002296static bool isInstantiationOf(CXXRecordDecl *Pattern,
2297 CXXRecordDecl *Instance) {
2298 Pattern = Pattern->getCanonicalDecl();
2299
2300 do {
2301 Instance = Instance->getCanonicalDecl();
2302 if (Pattern == Instance) return true;
2303 Instance = Instance->getInstantiatedFromMemberClass();
2304 } while (Instance);
2305
2306 return false;
2307}
2308
2309static bool isInstantiationOf(FunctionDecl *Pattern,
2310 FunctionDecl *Instance) {
2311 Pattern = Pattern->getCanonicalDecl();
2312
2313 do {
2314 Instance = Instance->getCanonicalDecl();
2315 if (Pattern == Instance) return true;
2316 Instance = Instance->getInstantiatedFromMemberFunction();
2317 } while (Instance);
2318
2319 return false;
2320}
2321
2322static bool isInstantiationOf(EnumDecl *Pattern,
2323 EnumDecl *Instance) {
2324 Pattern = Pattern->getCanonicalDecl();
2325
2326 do {
2327 Instance = Instance->getCanonicalDecl();
2328 if (Pattern == Instance) return true;
2329 Instance = Instance->getInstantiatedFromMemberEnum();
2330 } while (Instance);
2331
2332 return false;
2333}
2334
John McCalled976492009-12-04 22:46:56 +00002335static bool isInstantiationOf(UsingShadowDecl *Pattern,
2336 UsingShadowDecl *Instance,
2337 ASTContext &C) {
2338 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2339}
2340
2341static bool isInstantiationOf(UsingDecl *Pattern,
2342 UsingDecl *Instance,
2343 ASTContext &C) {
2344 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2345}
2346
John McCall7ba107a2009-11-18 02:36:19 +00002347static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2348 UsingDecl *Instance,
2349 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002350 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002351}
2352
2353static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002354 UsingDecl *Instance,
2355 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002356 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002357}
2358
John McCall52a575a2009-08-29 08:11:13 +00002359static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2360 VarDecl *Instance) {
2361 assert(Instance->isStaticDataMember());
2362
2363 Pattern = Pattern->getCanonicalDecl();
2364
2365 do {
2366 Instance = Instance->getCanonicalDecl();
2367 if (Pattern == Instance) return true;
2368 Instance = Instance->getInstantiatedFromStaticDataMember();
2369 } while (Instance);
2370
2371 return false;
2372}
2373
John McCalled976492009-12-04 22:46:56 +00002374// Other is the prospective instantiation
2375// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002376static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002377 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002378 if (UnresolvedUsingTypenameDecl *UUD
2379 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2380 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2381 return isInstantiationOf(UUD, UD, Ctx);
2382 }
2383 }
2384
2385 if (UnresolvedUsingValueDecl *UUD
2386 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002387 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2388 return isInstantiationOf(UUD, UD, Ctx);
2389 }
2390 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002391
Anders Carlsson0d8df782009-08-29 19:37:28 +00002392 return false;
2393 }
Mike Stump1eb44332009-09-09 15:08:12 +00002394
John McCall52a575a2009-08-29 08:11:13 +00002395 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2396 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002397
John McCall52a575a2009-08-29 08:11:13 +00002398 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2399 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002400
John McCall52a575a2009-08-29 08:11:13 +00002401 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2402 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002403
Douglas Gregor7caa6822009-07-24 20:34:43 +00002404 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002405 if (Var->isStaticDataMember())
2406 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2407
2408 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2409 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002410
Douglas Gregor0d696532009-09-28 06:34:35 +00002411 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2412 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2413
Douglas Gregored9c0f92009-10-29 00:04:11 +00002414 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2415 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2416 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2417 PartialSpec);
2418
Anders Carlssond8b285f2009-09-01 04:26:58 +00002419 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2420 if (!Field->getDeclName()) {
2421 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002422 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002423 cast<FieldDecl>(D);
2424 }
2425 }
Mike Stump1eb44332009-09-09 15:08:12 +00002426
John McCalled976492009-12-04 22:46:56 +00002427 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2428 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2429
2430 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2431 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2432
Douglas Gregor815215d2009-05-27 05:35:12 +00002433 return D->getDeclName() && isa<NamedDecl>(Other) &&
2434 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2435}
2436
2437template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002438static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002439 NamedDecl *D,
2440 ForwardIterator first,
2441 ForwardIterator last) {
2442 for (; first != last; ++first)
2443 if (isInstantiationOf(Ctx, D, *first))
2444 return cast<NamedDecl>(*first);
2445
2446 return 0;
2447}
2448
John McCall02cace72009-08-28 07:59:38 +00002449/// \brief Finds the instantiation of the given declaration context
2450/// within the current instantiation.
2451///
2452/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002453DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002454 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002455 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002456 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002457 return cast_or_null<DeclContext>(ID);
2458 } else return DC;
2459}
2460
Douglas Gregored961e72009-05-27 17:54:46 +00002461/// \brief Find the instantiation of the given declaration within the
2462/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002463///
2464/// This routine is intended to be used when \p D is a declaration
2465/// referenced from within a template, that needs to mapped into the
2466/// corresponding declaration within an instantiation. For example,
2467/// given:
2468///
2469/// \code
2470/// template<typename T>
2471/// struct X {
2472/// enum Kind {
2473/// KnownValue = sizeof(T)
2474/// };
2475///
2476/// bool getKind() const { return KnownValue; }
2477/// };
2478///
2479/// template struct X<int>;
2480/// \endcode
2481///
2482/// In the instantiation of X<int>::getKind(), we need to map the
2483/// EnumConstantDecl for KnownValue (which refers to
2484/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002485/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2486/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002487NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002488 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002489 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002490 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002491 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002492 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002493 // D is a local of some kind. Look into the map of local
2494 // declarations to their instantiations.
2495 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2496 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002497
Douglas Gregore95b4092009-09-16 18:34:49 +00002498 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2499 if (!Record->isDependentContext())
2500 return D;
2501
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002502 // If the RecordDecl is actually the injected-class-name or a
2503 // "templated" declaration for a class template, class template
2504 // partial specialization, or a member class of a class template,
2505 // substitute into the injected-class-name of the class template
2506 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002507 QualType T;
2508 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2509
2510 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002511 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002512 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2513 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002514 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002515
2516 // If we call SubstType with an InjectedClassNameType here we
2517 // can end up in an infinite loop.
2518 T = Context.getTypeDeclType(Record);
2519 assert(isa<InjectedClassNameType>(T) &&
2520 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002521 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002522 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002523
2524 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002525 // Substitute into the injected-class-name to get the type
2526 // corresponding to the instantiation we want, which may also be
2527 // the current instantiation (if we're in a template
2528 // definition). This substitution should never fail, since we
2529 // know we can instantiate the injected-class-name or we
2530 // wouldn't have gotten to the injected-class-name!
2531
2532 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2533 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002534 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2535 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2536
2537 if (!T->isDependentType()) {
2538 assert(T->isRecordType() && "Instantiation must produce a record type");
2539 return T->getAs<RecordType>()->getDecl();
2540 }
2541
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002542 // We are performing "partial" template instantiation to create
2543 // the member declarations for the members of a class template
2544 // specialization. Therefore, D is actually referring to something
2545 // in the current instantiation. Look through the current
2546 // context, which contains actual instantiations, to find the
2547 // instantiation of the "current instantiation" that D refers
2548 // to.
2549 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002550 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002551 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002552 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002553 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002554 if (isInstantiationOf(ClassTemplate,
2555 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002556 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002557
2558 if (!DC->isDependentContext())
2559 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002560 }
2561
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002562 // We're performing "instantiation" of a member of the current
2563 // instantiation while we are type-checking the
2564 // definition. Compute the declaration context and return that.
2565 assert(!SawNonDependentContext &&
2566 "No dependent context while instantiating record");
2567 DeclContext *DC = computeDeclContext(T);
2568 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002569 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002570 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002571 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002572
Douglas Gregore95b4092009-09-16 18:34:49 +00002573 // Fall through to deal with other dependent record types (e.g.,
2574 // anonymous unions in class templates).
2575 }
John McCall52a575a2009-08-29 08:11:13 +00002576
Douglas Gregore95b4092009-09-16 18:34:49 +00002577 if (!ParentDC->isDependentContext())
2578 return D;
2579
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002580 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002581 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002582 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Douglas Gregor815215d2009-05-27 05:35:12 +00002584 if (ParentDC != D->getDeclContext()) {
2585 // We performed some kind of instantiation in the parent context,
2586 // so now we need to look into the instantiated parent context to
2587 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002588
John McCall3cb0ebd2010-03-10 03:28:59 +00002589 // If our context used to be dependent, we may need to instantiate
2590 // it before performing lookup into that context.
2591 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002592 if (!Spec->isDependentContext()) {
2593 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002594 const RecordType *Tag = T->getAs<RecordType>();
2595 assert(Tag && "type of non-dependent record is not a RecordType");
2596 if (!Tag->isBeingDefined() &&
2597 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2598 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002599 }
2600 }
2601
Douglas Gregor815215d2009-05-27 05:35:12 +00002602 NamedDecl *Result = 0;
2603 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002604 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002605 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2606 } else {
2607 // Since we don't have a name for the entity we're looking for,
2608 // our only option is to walk through all of the declarations to
2609 // find that name. This will occur in a few cases:
2610 //
2611 // - anonymous struct/union within a template
2612 // - unnamed class/struct/union/enum within a template
2613 //
2614 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002615 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002616 ParentDC->decls_begin(),
2617 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002618 }
Mike Stump1eb44332009-09-09 15:08:12 +00002619
John McCall9f54ad42009-12-10 09:41:52 +00002620 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002621 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2622 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002623 && "Unable to find instantiation of declaration!");
2624
Douglas Gregor815215d2009-05-27 05:35:12 +00002625 D = Result;
2626 }
2627
Douglas Gregor815215d2009-05-27 05:35:12 +00002628 return D;
2629}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002630
Mike Stump1eb44332009-09-09 15:08:12 +00002631/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002632/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002633void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2634 while (!PendingLocalImplicitInstantiations.empty() ||
2635 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2636 PendingImplicitInstantiation Inst;
2637
2638 if (PendingLocalImplicitInstantiations.empty()) {
2639 Inst = PendingImplicitInstantiations.front();
2640 PendingImplicitInstantiations.pop_front();
2641 } else {
2642 Inst = PendingLocalImplicitInstantiations.front();
2643 PendingLocalImplicitInstantiations.pop_front();
2644 }
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Douglas Gregor7caa6822009-07-24 20:34:43 +00002646 // Instantiate function definitions
2647 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002648 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002649 Function->getLocation(), *this,
2650 Context.getSourceManager(),
2651 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002653 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002654 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002655 continue;
2656 }
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Douglas Gregor7caa6822009-07-24 20:34:43 +00002658 // Instantiate static data member definitions.
2659 VarDecl *Var = cast<VarDecl>(Inst.first);
2660 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002661
Chandler Carruth291b4412010-02-13 10:17:50 +00002662 // Don't try to instantiate declarations if the most recent redeclaration
2663 // is invalid.
2664 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2665 continue;
2666
2667 // Check if the most recent declaration has changed the specialization kind
2668 // and removed the need for implicit instantiation.
2669 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2670 case TSK_Undeclared:
2671 assert(false && "Cannot instantitiate an undeclared specialization.");
2672 case TSK_ExplicitInstantiationDeclaration:
2673 case TSK_ExplicitInstantiationDefinition:
2674 case TSK_ExplicitSpecialization:
2675 continue; // No longer need implicit instantiation.
2676 case TSK_ImplicitInstantiation:
2677 break;
2678 }
2679
Mike Stump1eb44332009-09-09 15:08:12 +00002680 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002681 Var->getLocation(), *this,
2682 Context.getSourceManager(),
2683 "instantiating static data member "
2684 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Douglas Gregor7caa6822009-07-24 20:34:43 +00002686 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002687 }
2688}
John McCall0c01d182010-03-24 05:22:00 +00002689
2690void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2691 const MultiLevelTemplateArgumentList &TemplateArgs) {
2692 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2693 E = Pattern->ddiag_end(); I != E; ++I) {
2694 DependentDiagnostic *DD = *I;
2695
2696 switch (DD->getKind()) {
2697 case DependentDiagnostic::Access:
2698 HandleDependentAccessCheck(*DD, TemplateArgs);
2699 break;
2700 }
2701 }
2702}