blob: 1280c0cb88e536a703ae33e07d84f35f8c5651a9 [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());
Douglas Gregor5764f612010-05-08 23:05:03 +0000354 Var->setUsed(D->isUsed());
355
Mike Stump390b4cc2009-05-16 07:39:55 +0000356 // FIXME: In theory, we could have a previous declaration for variables that
357 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000358 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000359 // FIXME: having to fake up a LookupResult is dumb.
360 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000361 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000362 if (D->isStaticDataMember())
363 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000364 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Douglas Gregor7caa6822009-07-24 20:34:43 +0000366 if (D->isOutOfLine()) {
367 D->getLexicalDeclContext()->addDecl(Var);
368 Owner->makeDeclVisibleInContext(Var);
369 } else {
370 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000371
372 if (Owner->isFunctionOrMethod())
373 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000376 // Link instantiations of static data members back to the template from
377 // which they were instantiated.
378 if (Var->isStaticDataMember())
379 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000380 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000381
Douglas Gregor60c93c92010-02-09 07:26:29 +0000382 if (Var->getAnyInitializer()) {
383 // We already have an initializer in the class.
384 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000385 if (Var->isStaticDataMember() && !D->isOutOfLine())
386 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
387 else
388 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
389
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000390 // Instantiate the initializer.
391 SourceLocation LParenLoc, RParenLoc;
392 llvm::SmallVector<SourceLocation, 4> CommaLocs;
393 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
394 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
395 CommaLocs, InitArgs, RParenLoc)) {
396 // Attach the initializer to the declaration.
397 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000398 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000399 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000400 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000401 move_arg(InitArgs),
402 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000403 RParenLoc);
404 } else if (InitArgs.size() == 1) {
405 Expr *Init = (Expr*)(InitArgs.take()[0]);
406 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
407 SemaRef.Owned(Init),
408 false);
409 } else {
410 assert(InitArgs.size() == 0);
411 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000412 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000413 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000414 // FIXME: Not too happy about invalidating the declaration
415 // because of a bogus initializer.
416 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000417 }
418
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000419 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000420 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
421 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000422
Douglas Gregor5764f612010-05-08 23:05:03 +0000423 // Diagnose unused local variables.
424 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
425 SemaRef.DiagnoseUnusedDecl(Var);
426
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000427 return Var;
428}
429
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000430Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
431 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000432 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000433 if (DI->getType()->isDependentType()) {
434 DI = SemaRef.SubstType(DI, TemplateArgs,
435 D->getLocation(), D->getDeclName());
436 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000437 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000438 Invalid = true;
439 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000440 // C++ [temp.arg.type]p3:
441 // If a declaration acquires a function type through a type
442 // dependent on a template-parameter and this causes a
443 // declaration that does not use the syntactic form of a
444 // function declarator to have function type, the program is
445 // ill-formed.
446 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000447 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000448 Invalid = true;
449 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000450 } else {
451 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000452 }
453
454 Expr *BitWidth = D->getBitWidth();
455 if (Invalid)
456 BitWidth = 0;
457 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000458 // The bit-width expression is not potentially evaluated.
459 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000461 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000462 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000463 if (InstantiatedBitWidth.isInvalid()) {
464 Invalid = true;
465 BitWidth = 0;
466 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000467 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000468 }
469
John McCall07fb6be2009-10-22 23:33:21 +0000470 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
471 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000472 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000473 D->getLocation(),
474 D->isMutable(),
475 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000476 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000477 D->getAccess(),
478 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000479 if (!Field) {
480 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000481 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000484 InstantiateAttrs(D, Field);
485
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000486 if (Invalid)
487 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000489 if (!Field->getDeclName()) {
490 // Keep track of where this decl came from.
491 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000494 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000495 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000496 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000497
498 return Field;
499}
500
John McCall02cace72009-08-28 07:59:38 +0000501Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000502 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000503 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000504 if (TypeSourceInfo *Ty = D->getFriendType()) {
505 TypeSourceInfo *InstTy =
506 SemaRef.SubstType(Ty, TemplateArgs,
507 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000508 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000509 return 0;
John McCall02cace72009-08-28 07:59:38 +0000510
Douglas Gregor06245bf2010-04-07 17:57:12 +0000511 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
512 if (!FD)
513 return 0;
514
515 FD->setAccess(AS_public);
516 Owner->addDecl(FD);
517 return FD;
518 }
519
520 NamedDecl *ND = D->getFriendDecl();
521 assert(ND && "friend decl must be a decl or a type!");
522
John McCallaf2094e2010-04-08 09:05:18 +0000523 // All of the Visit implementations for the various potential friend
524 // declarations have to be carefully written to work for friend
525 // objects, with the most important detail being that the target
526 // decl should almost certainly not be placed in Owner.
527 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000528 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
John McCall02cace72009-08-28 07:59:38 +0000530 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000531 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
532 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000533 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000534 Owner->addDecl(FD);
535 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000536}
537
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000538Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
539 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Douglas Gregorac7610d2009-06-22 20:57:11 +0000541 // The expression in a static assertion is not potentially evaluated.
542 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000544 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000545 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000546 if (InstantiatedAssertExpr.isInvalid())
547 return 0;
548
Douglas Gregor43d9d922009-08-08 01:41:12 +0000549 OwningExprResult Message(SemaRef, D->getMessage());
550 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000551 Decl *StaticAssert
552 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000553 move(InstantiatedAssertExpr),
554 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000555 return StaticAssert;
556}
557
558Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000559 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000560 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000561 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000562 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000563 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000564 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000565 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000566 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000567 Enum->startDefinition();
568
Douglas Gregor96084f12010-03-01 19:00:07 +0000569 if (D->getDeclContext()->isFunctionOrMethod())
570 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
571
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000572 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000573
574 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000575 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
576 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000577 EC != ECEnd; ++EC) {
578 // The specified value for the enumerator.
579 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000580 if (Expr *UninstValue = EC->getInitExpr()) {
581 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000582 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000583 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000584
John McCallce3ff2b2009-08-25 22:02:44 +0000585 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000586 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587
588 // Drop the initial value and continue.
589 bool isInvalid = false;
590 if (Value.isInvalid()) {
591 Value = SemaRef.Owned((Expr *)0);
592 isInvalid = true;
593 }
594
Mike Stump1eb44332009-09-09 15:08:12 +0000595 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000596 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
597 EC->getLocation(), EC->getIdentifier(),
598 move(Value));
599
600 if (isInvalid) {
601 if (EnumConst)
602 EnumConst->setInvalidDecl();
603 Enum->setInvalidDecl();
604 }
605
606 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000607 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000608 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000609 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000610 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000611
612 if (D->getDeclContext()->isFunctionOrMethod()) {
613 // If the enumeration is within a function or method, record the enum
614 // constant as a local.
615 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
616 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000617 }
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000620 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000621 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000622 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
623 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000624 &Enumerators[0], Enumerators.size(),
625 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000626
627 return Enum;
628}
629
Douglas Gregor6477b692009-03-25 15:04:13 +0000630Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
631 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
632 return 0;
633}
634
John McCalle29ba202009-08-20 01:44:21 +0000635Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000636 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
637
Douglas Gregor550d9b22009-10-31 17:21:17 +0000638 // Create a local instantiation scope for this class template, which
639 // will contain the instantiations of the template parameters.
640 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000641 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000642 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000643 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000644 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000645
646 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000647
648 // Instantiate the qualifier. We have to do this first in case
649 // we're a friend declaration, because if we are then we need to put
650 // the new declaration in the appropriate context.
651 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
652 if (Qualifier) {
653 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
654 Pattern->getQualifierRange(),
655 TemplateArgs);
656 if (!Qualifier) return 0;
657 }
658
659 CXXRecordDecl *PrevDecl = 0;
660 ClassTemplateDecl *PrevClassTemplate = 0;
661
662 // If this isn't a friend, then it's a member template, in which
663 // case we just want to build the instantiation in the
664 // specialization. If it is a friend, we want to build it in
665 // the appropriate context.
666 DeclContext *DC = Owner;
667 if (isFriend) {
668 if (Qualifier) {
669 CXXScopeSpec SS;
670 SS.setScopeRep(Qualifier);
671 SS.setRange(Pattern->getQualifierRange());
672 DC = SemaRef.computeDeclContext(SS);
673 if (!DC) return 0;
674 } else {
675 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
676 Pattern->getDeclContext(),
677 TemplateArgs);
678 }
679
680 // Look for a previous declaration of the template in the owning
681 // context.
682 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
683 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
684 SemaRef.LookupQualifiedName(R, DC);
685
686 if (R.isSingleResult()) {
687 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
688 if (PrevClassTemplate)
689 PrevDecl = PrevClassTemplate->getTemplatedDecl();
690 }
691
692 if (!PrevClassTemplate && Qualifier) {
693 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000694 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
695 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000696 return 0;
697 }
698
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000699 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000700 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000701 bool Complain = true;
702
703 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
704 // template for struct std::tr1::__detail::_Map_base, where the
705 // template parameters of the friend declaration don't match the
706 // template parameters of the original declaration. In this one
707 // case, we don't complain about the ill-formed friend
708 // declaration.
709 if (isFriend && Pattern->getIdentifier() &&
710 Pattern->getIdentifier()->isStr("_Map_base") &&
711 DC->isNamespace() &&
712 cast<NamespaceDecl>(DC)->getIdentifier() &&
713 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
714 DeclContext *DCParent = DC->getParent();
715 if (DCParent->isNamespace() &&
716 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
717 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
718 DeclContext *DCParent2 = DCParent->getParent();
719 if (DCParent2->isNamespace() &&
720 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
721 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
722 DCParent2->getParent()->isTranslationUnit())
723 Complain = false;
724 }
725 }
726
John McCall93ba8572010-03-25 06:39:04 +0000727 TemplateParameterList *PrevParams
728 = PrevClassTemplate->getTemplateParameters();
729
730 // Make sure the parameter lists match.
731 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000732 Complain,
733 Sema::TPL_TemplateMatch)) {
734 if (Complain)
735 return 0;
736
737 AdoptedPreviousTemplateParams = true;
738 InstParams = PrevParams;
739 }
John McCall93ba8572010-03-25 06:39:04 +0000740
741 // Do some additional validation, then merge default arguments
742 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000743 if (!AdoptedPreviousTemplateParams &&
744 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000745 Sema::TPC_ClassTemplate))
746 return 0;
747 }
748 }
749
John McCalle29ba202009-08-20 01:44:21 +0000750 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000751 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000752 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000753 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000754 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000755
John McCall93ba8572010-03-25 06:39:04 +0000756 if (Qualifier)
757 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000758
John McCalle29ba202009-08-20 01:44:21 +0000759 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000760 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
761 D->getIdentifier(), InstParams, RecordInst,
762 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000763 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000764
John McCall93ba8572010-03-25 06:39:04 +0000765 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000766 if (PrevClassTemplate)
767 Inst->setAccess(PrevClassTemplate->getAccess());
768 else
769 Inst->setAccess(D->getAccess());
770
John McCall93ba8572010-03-25 06:39:04 +0000771 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
772 // TODO: do we want to track the instantiation progeny of this
773 // friend target decl?
774 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000775 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000776 Inst->setInstantiatedFromMemberTemplate(D);
777 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000778
779 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000780 SemaRef.Context.getInjectedClassNameType(RecordInst,
781 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000782
Douglas Gregor259571e2009-10-30 22:42:42 +0000783 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000784 if (isFriend) {
785 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000786 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000787 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000788
John McCalle29ba202009-08-20 01:44:21 +0000789 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000790
Douglas Gregored9c0f92009-10-29 00:04:11 +0000791 // Instantiate all of the partial specializations of this member class
792 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000793 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
794 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000795 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
796 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
797
John McCalle29ba202009-08-20 01:44:21 +0000798 return Inst;
799}
800
Douglas Gregord60e1052009-08-27 16:57:43 +0000801Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000802TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
803 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000804 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
805
806 // Lookup the already-instantiated declaration in the instantiation
807 // of the class template and return that.
808 DeclContext::lookup_result Found
809 = Owner->lookup(ClassTemplate->getDeclName());
810 if (Found.first == Found.second)
811 return 0;
812
813 ClassTemplateDecl *InstClassTemplate
814 = dyn_cast<ClassTemplateDecl>(*Found.first);
815 if (!InstClassTemplate)
816 return 0;
817
818 Decl *DCanon = D->getCanonicalDecl();
819 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
820 P = InstClassTemplate->getPartialSpecializations().begin(),
821 PEnd = InstClassTemplate->getPartialSpecializations().end();
822 P != PEnd; ++P) {
823 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
824 return &*P;
825 }
826
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000827 return 0;
828}
829
830Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000831TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000832 // Create a local instantiation scope for this function template, which
833 // will contain the instantiations of the template parameters and then get
834 // merged with the local instantiation scope for the function template
835 // itself.
836 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000837
Douglas Gregord60e1052009-08-27 16:57:43 +0000838 TemplateParameterList *TempParams = D->getTemplateParameters();
839 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000840 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000841 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000842
Douglas Gregora735b202009-10-13 14:39:41 +0000843 FunctionDecl *Instantiated = 0;
844 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
845 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
846 InstParams));
847 else
848 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
849 D->getTemplatedDecl(),
850 InstParams));
851
852 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000853 return 0;
854
John McCall46460a62010-01-20 21:53:11 +0000855 Instantiated->setAccess(D->getAccess());
856
Mike Stump1eb44332009-09-09 15:08:12 +0000857 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000858 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000859 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000860 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000861 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000862 assert(InstTemplate &&
863 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000864
John McCallb1a56e72010-03-26 23:10:15 +0000865 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
866
John McCalle976ffe2009-12-14 23:19:40 +0000867 // Link the instantiation back to the pattern *unless* this is a
868 // non-definition friend declaration.
869 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000870 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000871 InstTemplate->setInstantiatedFromMemberTemplate(D);
872
John McCallb1a56e72010-03-26 23:10:15 +0000873 // Make declarations visible in the appropriate context.
874 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000875 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000876
Douglas Gregord60e1052009-08-27 16:57:43 +0000877 return InstTemplate;
878}
879
Douglas Gregord475b8d2009-03-25 21:17:03 +0000880Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
881 CXXRecordDecl *PrevDecl = 0;
882 if (D->isInjectedClassName())
883 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000884 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000885 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
886 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000887 TemplateArgs);
888 if (!Prev) return 0;
889 PrevDecl = cast<CXXRecordDecl>(Prev);
890 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000891
892 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000893 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000894 D->getLocation(), D->getIdentifier(),
895 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000896
897 // Substitute the nested name specifier, if any.
898 if (SubstQualifier(D, Record))
899 return 0;
900
Douglas Gregord475b8d2009-03-25 21:17:03 +0000901 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000902 // FIXME: Check against AS_none is an ugly hack to work around the issue that
903 // the tag decls introduced by friend class declarations don't have an access
904 // specifier. Remove once this area of the code gets sorted out.
905 if (D->getAccess() != AS_none)
906 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000907 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000908 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000909
John McCall02cace72009-08-28 07:59:38 +0000910 // If the original function was part of a friend declaration,
911 // inherit its namespace state.
912 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
913 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
914
Anders Carlssond8b285f2009-09-01 04:26:58 +0000915 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
916
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000917 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000918 return Record;
919}
920
John McCall02cace72009-08-28 07:59:38 +0000921/// Normal class members are of more specific types and therefore
922/// don't make it here. This function serves two purposes:
923/// 1) instantiating function templates
924/// 2) substituting friend declarations
925/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000926Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000927 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000928 // Check whether there is already a function template specialization for
929 // this declaration.
930 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
931 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000932 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000933 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000934 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000935 TemplateArgs.getInnermost().getFlatArgumentList(),
936 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000937 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000938
939 FunctionTemplateSpecializationInfo *Info
940 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000941 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregor127102b2009-06-29 20:59:39 +0000943 // If we already have a function template specialization, return it.
944 if (Info)
945 return Info->Function;
946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
John McCallb0cb0222010-03-27 05:57:59 +0000948 bool isFriend;
949 if (FunctionTemplate)
950 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
951 else
952 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
953
Douglas Gregor79c22782010-01-16 20:21:20 +0000954 bool MergeWithParentScope = (TemplateParams != 0) ||
955 !(isa<Decl>(Owner) &&
956 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
957 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Douglas Gregore53060f2009-06-25 22:08:12 +0000959 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000960 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
961 TInfo = SubstFunctionType(D, Params);
962 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000963 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000964 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000965
John McCalld325daa2010-03-26 04:53:08 +0000966 NestedNameSpecifier *Qualifier = D->getQualifier();
967 if (Qualifier) {
968 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
969 D->getQualifierRange(),
970 TemplateArgs);
971 if (!Qualifier) return 0;
972 }
973
John McCall68b6b872010-02-06 01:50:47 +0000974 // If we're instantiating a local function declaration, put the result
975 // in the owner; otherwise we need to find the instantiated context.
976 DeclContext *DC;
977 if (D->getDeclContext()->isFunctionOrMethod())
978 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000979 else if (isFriend && Qualifier) {
980 CXXScopeSpec SS;
981 SS.setScopeRep(Qualifier);
982 SS.setRange(D->getQualifierRange());
983 DC = SemaRef.computeDeclContext(SS);
984 if (!DC) return 0;
985 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000986 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
987 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +0000988 }
John McCall68b6b872010-02-06 01:50:47 +0000989
John McCall02cace72009-08-28 07:59:38 +0000990 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000991 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000992 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000993 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000994 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000995
John McCalld325daa2010-03-26 04:53:08 +0000996 if (Qualifier)
997 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000998
John McCallb1a56e72010-03-26 23:10:15 +0000999 DeclContext *LexicalDC = Owner;
1000 if (!isFriend && D->isOutOfLine()) {
1001 assert(D->getDeclContext()->isFileContext());
1002 LexicalDC = D->getDeclContext();
1003 }
1004
1005 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregore53060f2009-06-25 22:08:12 +00001007 // Attach the parameters
1008 for (unsigned P = 0; P < Params.size(); ++P)
1009 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001010 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001011
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001012 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001013 if (TemplateParams) {
1014 // Our resulting instantiation is actually a function template, since we
1015 // are substituting only the outer template parameters. For example, given
1016 //
1017 // template<typename T>
1018 // struct X {
1019 // template<typename U> friend void f(T, U);
1020 // };
1021 //
1022 // X<int> x;
1023 //
1024 // We are instantiating the friend function template "f" within X<int>,
1025 // which means substituting int for T, but leaving "f" as a friend function
1026 // template.
1027 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001028 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001029 Function->getLocation(),
1030 Function->getDeclName(),
1031 TemplateParams, Function);
1032 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001033
1034 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001035
1036 if (isFriend && D->isThisDeclarationADefinition()) {
1037 // TODO: should we remember this connection regardless of whether
1038 // the friend declaration provided a body?
1039 FunctionTemplate->setInstantiatedFromMemberTemplate(
1040 D->getDescribedFunctionTemplate());
1041 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001042 } else if (FunctionTemplate) {
1043 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001044 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001045 &TemplateArgs.getInnermost(),
1046 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001047 } else if (isFriend && D->isThisDeclarationADefinition()) {
1048 // TODO: should we remember this connection regardless of whether
1049 // the friend declaration provided a body?
1050 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001051 if (!SemaRef.getLangOptions().CPlusPlus0x) {
1052 // C++03 [temp.friend]p4:
1053 // When a function is defined in a friend function declaration in a
1054 // class template, the function is defined at each instantiation of the
1055 // class template. The function is defined even if it is never used.
1056 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Owner)) {
1057 if (ClassTemplateSpecializationDecl *Spec
1058 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
1059 InstantiateAtPOI = Spec->getPointOfInstantiation();
1060 else if (MemberSpecializationInfo *MSInfo
1061 = Record->getMemberSpecializationInfo())
1062 InstantiateAtPOI = MSInfo->getPointOfInstantiation();
1063 }
1064
1065 if (InstantiateAtPOI.isInvalid())
1066 InstantiateAtPOI = Function->getLocation();
1067 }
John McCall02cace72009-08-28 07:59:38 +00001068 }
Douglas Gregora735b202009-10-13 14:39:41 +00001069
Douglas Gregore53060f2009-06-25 22:08:12 +00001070 if (InitFunctionInstantiation(Function, D))
1071 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Douglas Gregore53060f2009-06-25 22:08:12 +00001073 bool Redeclaration = false;
1074 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001075 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001076
John McCall68263142009-11-18 22:49:29 +00001077 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1078 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1079
John McCallaf2094e2010-04-08 09:05:18 +00001080 if (DependentFunctionTemplateSpecializationInfo *Info
1081 = D->getDependentSpecializationInfo()) {
1082 assert(isFriend && "non-friend has dependent specialization info?");
1083
1084 // This needs to be set now for future sanity.
1085 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1086
1087 // Instantiate the explicit template arguments.
1088 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1089 Info->getRAngleLoc());
1090 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1091 TemplateArgumentLoc Loc;
1092 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1093 return 0;
1094
1095 ExplicitArgs.addArgument(Loc);
1096 }
1097
1098 // Map the candidate templates to their instantiations.
1099 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1100 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1101 Info->getTemplate(I),
1102 TemplateArgs);
1103 if (!Temp) return 0;
1104
1105 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1106 }
1107
1108 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1109 &ExplicitArgs,
1110 Previous))
1111 Function->setInvalidDecl();
1112
1113 isExplicitSpecialization = true;
1114
1115 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001116 // Look only into the namespace where the friend would be declared to
1117 // find a previous declaration. This is the innermost enclosing namespace,
1118 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001119 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001120
Douglas Gregora735b202009-10-13 14:39:41 +00001121 // In C++, the previous declaration we find might be a tag type
1122 // (class or enum). In this case, the new declaration will hide the
1123 // tag type. Note that this does does not apply if we're declaring a
1124 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001125 if (Previous.isSingleTagDecl())
1126 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001127 }
1128
John McCall9f54ad42009-12-10 09:41:52 +00001129 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001130 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001131 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001132
John McCall76d32642010-04-24 01:30:58 +00001133 NamedDecl *PrincipalDecl = (TemplateParams
1134 ? cast<NamedDecl>(FunctionTemplate)
1135 : Function);
1136
Douglas Gregora735b202009-10-13 14:39:41 +00001137 // If the original function was part of a friend declaration,
1138 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001139 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001140 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001141 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001142 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001143 else
Douglas Gregora735b202009-10-13 14:39:41 +00001144 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001145
1146 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1147 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregora735b202009-10-13 14:39:41 +00001148 }
1149
John McCall76d32642010-04-24 01:30:58 +00001150 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1151 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1152 PrincipalDecl->setNonMemberOperator();
1153
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001154 // If we need to instantiate this function now (because it is a C++98/03
1155 // friend function defined inside a class template), do so.
1156 if (InstantiateAtPOI.isValid())
1157 SemaRef.MarkDeclarationReferenced(InstantiateAtPOI, Function);
1158
Douglas Gregore53060f2009-06-25 22:08:12 +00001159 return Function;
1160}
1161
Douglas Gregord60e1052009-08-27 16:57:43 +00001162Decl *
1163TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1164 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001165 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1166 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001167 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001168 // We are creating a function template specialization from a function
1169 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001170 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001171 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001172 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001173 TemplateArgs.getInnermost().getFlatArgumentList(),
1174 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001175 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001176
1177 FunctionTemplateSpecializationInfo *Info
1178 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001179 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregor6b906862009-08-21 00:16:32 +00001181 // If we already have a function template specialization, return it.
1182 if (Info)
1183 return Info->Function;
1184 }
1185
John McCallb0cb0222010-03-27 05:57:59 +00001186 bool isFriend;
1187 if (FunctionTemplate)
1188 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1189 else
1190 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1191
Douglas Gregor79c22782010-01-16 20:21:20 +00001192 bool MergeWithParentScope = (TemplateParams != 0) ||
1193 !(isa<Decl>(Owner) &&
1194 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1195 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001196
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001197 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001198 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1199 TInfo = SubstFunctionType(D, Params);
1200 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001201 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001202 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001203
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001204 // \brief If the type of this function is not *directly* a function
1205 // type, then we're instantiating the a function that was declared
1206 // via a typedef, e.g.,
1207 //
1208 // typedef int functype(int, int);
1209 // functype func;
1210 //
1211 // In this case, we'll just go instantiate the ParmVarDecls that we
1212 // synthesized in the method declaration.
1213 if (!isa<FunctionProtoType>(T)) {
1214 assert(!Params.size() && "Instantiating type could not yield parameters");
1215 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1216 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1217 TemplateArgs);
1218 if (!P)
1219 return 0;
1220
1221 Params.push_back(P);
1222 }
1223 }
1224
John McCallb0cb0222010-03-27 05:57:59 +00001225 NestedNameSpecifier *Qualifier = D->getQualifier();
1226 if (Qualifier) {
1227 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1228 D->getQualifierRange(),
1229 TemplateArgs);
1230 if (!Qualifier) return 0;
1231 }
1232
1233 DeclContext *DC = Owner;
1234 if (isFriend) {
1235 if (Qualifier) {
1236 CXXScopeSpec SS;
1237 SS.setScopeRep(Qualifier);
1238 SS.setRange(D->getQualifierRange());
1239 DC = SemaRef.computeDeclContext(SS);
1240 } else {
1241 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1242 D->getDeclContext(),
1243 TemplateArgs);
1244 }
1245 if (!DC) return 0;
1246 }
1247
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001248 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001249 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001250 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregordec06662009-08-21 18:42:58 +00001252 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001253 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001254 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1255 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1256 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001257 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1258 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001259 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001260 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001261 Constructor->isInlineSpecified(),
1262 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001263 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1264 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1265 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1266 SemaRef.Context.getCanonicalType(ClassTy));
1267 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1268 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001269 T, Destructor->isInlineSpecified(),
1270 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001271 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001272 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001273 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001274 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001275 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1276 ConvTy);
1277 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1278 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001279 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001280 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001281 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001282 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001283 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001284 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001285 D->isStatic(),
1286 D->getStorageClassAsWritten(),
1287 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001288 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001289
John McCallb0cb0222010-03-27 05:57:59 +00001290 if (Qualifier)
1291 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001292
Douglas Gregord60e1052009-08-27 16:57:43 +00001293 if (TemplateParams) {
1294 // Our resulting instantiation is actually a function template, since we
1295 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001296 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001297 // template<typename T>
1298 // struct X {
1299 // template<typename U> void f(T, U);
1300 // };
1301 //
1302 // X<int> x;
1303 //
1304 // We are instantiating the member template "f" within X<int>, which means
1305 // substituting int for T, but leaving "f" as a member function template.
1306 // Build the function template itself.
1307 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1308 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001309 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001310 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001311 if (isFriend) {
1312 FunctionTemplate->setLexicalDeclContext(Owner);
1313 FunctionTemplate->setObjectOfFriendDecl(true);
1314 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001315 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001316 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001317 } else if (FunctionTemplate) {
1318 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001319 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001320 &TemplateArgs.getInnermost(),
1321 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001322 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001323 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001324 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001325 }
1326
Mike Stump1eb44332009-09-09 15:08:12 +00001327 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001328 // out-of-line, the instantiation will have the same lexical
1329 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001330 if (isFriend) {
1331 Method->setLexicalDeclContext(Owner);
1332 Method->setObjectOfFriendDecl(true);
1333 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001334 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Douglas Gregor5545e162009-03-24 00:38:23 +00001336 // Attach the parameters
1337 for (unsigned P = 0; P < Params.size(); ++P)
1338 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001339 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001340
1341 if (InitMethodInstantiation(Method, D))
1342 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001343
John McCall68263142009-11-18 22:49:29 +00001344 LookupResult Previous(SemaRef, Name, SourceLocation(),
1345 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001346
John McCallb0cb0222010-03-27 05:57:59 +00001347 if (!FunctionTemplate || TemplateParams || isFriend) {
1348 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Douglas Gregordec06662009-08-21 18:42:58 +00001350 // In C++, the previous declaration we find might be a tag type
1351 // (class or enum). In this case, the new declaration will hide the
1352 // tag type. Note that this does does not apply if we're declaring a
1353 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001354 if (Previous.isSingleTagDecl())
1355 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001356 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001357
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001358 bool Redeclaration = false;
1359 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001360 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001361 /*FIXME:*/OverloadableAttrRequired);
1362
Douglas Gregor4ba31362009-12-01 17:24:26 +00001363 if (D->isPure())
1364 SemaRef.CheckPureMethod(Method, SourceRange());
1365
John McCall46460a62010-01-20 21:53:11 +00001366 Method->setAccess(D->getAccess());
1367
John McCallb0cb0222010-03-27 05:57:59 +00001368 if (FunctionTemplate) {
1369 // If there's a function template, let our caller handle it.
1370 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1371 // Don't hide a (potentially) valid declaration with an invalid one.
1372 } else {
1373 NamedDecl *DeclToAdd = (TemplateParams
1374 ? cast<NamedDecl>(FunctionTemplate)
1375 : Method);
1376 if (isFriend)
1377 Record->makeDeclVisibleInContext(DeclToAdd);
1378 else
1379 Owner->addDecl(DeclToAdd);
1380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001382 return Method;
1383}
1384
Douglas Gregor615c5d42009-03-24 16:43:20 +00001385Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001386 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001387}
1388
Douglas Gregor03b2b072009-03-24 00:15:49 +00001389Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001390 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001391}
1392
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001393Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001394 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001395}
1396
Douglas Gregor6477b692009-03-25 15:04:13 +00001397ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001398 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001399}
1400
John McCalle29ba202009-08-20 01:44:21 +00001401Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1402 TemplateTypeParmDecl *D) {
1403 // TODO: don't always clone when decls are refcounted.
1404 const Type* T = D->getTypeForDecl();
1405 assert(T->isTemplateTypeParmType());
1406 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
John McCalle29ba202009-08-20 01:44:21 +00001408 TemplateTypeParmDecl *Inst =
1409 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001410 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001411 TTPT->getName(),
1412 D->wasDeclaredWithTypename(),
1413 D->isParameterPack());
1414
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001415 if (D->hasDefaultArgument())
1416 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001417
Douglas Gregor550d9b22009-10-31 17:21:17 +00001418 // Introduce this template parameter's instantiation into the instantiation
1419 // scope.
1420 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1421
John McCalle29ba202009-08-20 01:44:21 +00001422 return Inst;
1423}
1424
Douglas Gregor33642df2009-10-23 23:25:44 +00001425Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1426 NonTypeTemplateParmDecl *D) {
1427 // Substitute into the type of the non-type template parameter.
1428 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001429 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001430 if (DI) {
1431 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1432 D->getDeclName());
1433 if (DI) T = DI->getType();
1434 } else {
1435 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1436 D->getDeclName());
1437 DI = 0;
1438 }
1439 if (T.isNull())
1440 return 0;
1441
1442 // Check that this type is acceptable for a non-type template parameter.
1443 bool Invalid = false;
1444 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1445 if (T.isNull()) {
1446 T = SemaRef.Context.IntTy;
1447 Invalid = true;
1448 }
1449
1450 NonTypeTemplateParmDecl *Param
1451 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1452 D->getDepth() - 1, D->getPosition(),
1453 D->getIdentifier(), T, DI);
1454 if (Invalid)
1455 Param->setInvalidDecl();
1456
1457 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001458
1459 // Introduce this template parameter's instantiation into the instantiation
1460 // scope.
1461 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001462 return Param;
1463}
1464
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001465Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001466TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1467 TemplateTemplateParmDecl *D) {
1468 // Instantiate the template parameter list of the template template parameter.
1469 TemplateParameterList *TempParams = D->getTemplateParameters();
1470 TemplateParameterList *InstParams;
1471 {
1472 // Perform the actual substitution of template parameters within a new,
1473 // local instantiation scope.
1474 Sema::LocalInstantiationScope Scope(SemaRef);
1475 InstParams = SubstTemplateParams(TempParams);
1476 if (!InstParams)
1477 return NULL;
1478 }
1479
1480 // Build the template template parameter.
1481 TemplateTemplateParmDecl *Param
1482 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1483 D->getDepth() - 1, D->getPosition(),
1484 D->getIdentifier(), InstParams);
1485 Param->setDefaultArgument(D->getDefaultArgument());
1486
1487 // Introduce this template parameter's instantiation into the instantiation
1488 // scope.
1489 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1490
1491 return Param;
1492}
1493
Douglas Gregor48c32a72009-11-17 06:07:40 +00001494Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1495 // Using directives are never dependent, so they require no explicit
1496
1497 UsingDirectiveDecl *Inst
1498 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1499 D->getNamespaceKeyLocation(),
1500 D->getQualifierRange(), D->getQualifier(),
1501 D->getIdentLocation(),
1502 D->getNominatedNamespace(),
1503 D->getCommonAncestor());
1504 Owner->addDecl(Inst);
1505 return Inst;
1506}
1507
John McCalled976492009-12-04 22:46:56 +00001508Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1509 // The nested name specifier is non-dependent, so no transformation
1510 // is required.
1511
John McCall9f54ad42009-12-10 09:41:52 +00001512 // We only need to do redeclaration lookups if we're in a class
1513 // scope (in fact, it's not really even possible in non-class
1514 // scopes).
1515 bool CheckRedeclaration = Owner->isRecord();
1516
1517 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1518 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1519
John McCalled976492009-12-04 22:46:56 +00001520 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1521 D->getLocation(),
1522 D->getNestedNameRange(),
1523 D->getUsingLocation(),
1524 D->getTargetNestedNameDecl(),
1525 D->getDeclName(),
1526 D->isTypeName());
1527
1528 CXXScopeSpec SS;
1529 SS.setScopeRep(D->getTargetNestedNameDecl());
1530 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001531
1532 if (CheckRedeclaration) {
1533 Prev.setHideTags(false);
1534 SemaRef.LookupQualifiedName(Prev, Owner);
1535
1536 // Check for invalid redeclarations.
1537 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1538 D->isTypeName(), SS,
1539 D->getLocation(), Prev))
1540 NewUD->setInvalidDecl();
1541
1542 }
1543
1544 if (!NewUD->isInvalidDecl() &&
1545 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001546 D->getLocation()))
1547 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001548
John McCalled976492009-12-04 22:46:56 +00001549 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1550 NewUD->setAccess(D->getAccess());
1551 Owner->addDecl(NewUD);
1552
John McCall9f54ad42009-12-10 09:41:52 +00001553 // Don't process the shadow decls for an invalid decl.
1554 if (NewUD->isInvalidDecl())
1555 return NewUD;
1556
John McCall323c3102009-12-22 22:26:37 +00001557 bool isFunctionScope = Owner->isFunctionOrMethod();
1558
John McCall9f54ad42009-12-10 09:41:52 +00001559 // Process the shadow decls.
1560 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1561 I != E; ++I) {
1562 UsingShadowDecl *Shadow = *I;
1563 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001564 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1565 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001566 TemplateArgs));
1567
1568 if (CheckRedeclaration &&
1569 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1570 continue;
1571
1572 UsingShadowDecl *InstShadow
1573 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1574 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001575
1576 if (isFunctionScope)
1577 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001578 }
John McCalled976492009-12-04 22:46:56 +00001579
1580 return NewUD;
1581}
1582
1583Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001584 // Ignore these; we handle them in bulk when processing the UsingDecl.
1585 return 0;
John McCalled976492009-12-04 22:46:56 +00001586}
1587
John McCall7ba107a2009-11-18 02:36:19 +00001588Decl * TemplateDeclInstantiator
1589 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001590 NestedNameSpecifier *NNS =
1591 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1592 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001593 TemplateArgs);
1594 if (!NNS)
1595 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001597 CXXScopeSpec SS;
1598 SS.setRange(D->getTargetNestedNameRange());
1599 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001600
1601 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001602 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001603 D->getUsingLoc(), SS, D->getLocation(),
1604 D->getDeclName(), 0,
1605 /*instantiation*/ true,
1606 /*typename*/ true, D->getTypenameLoc());
1607 if (UD)
John McCalled976492009-12-04 22:46:56 +00001608 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1609
John McCall7ba107a2009-11-18 02:36:19 +00001610 return UD;
1611}
1612
1613Decl * TemplateDeclInstantiator
1614 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1615 NestedNameSpecifier *NNS =
1616 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1617 D->getTargetNestedNameRange(),
1618 TemplateArgs);
1619 if (!NNS)
1620 return 0;
1621
1622 CXXScopeSpec SS;
1623 SS.setRange(D->getTargetNestedNameRange());
1624 SS.setScopeRep(NNS);
1625
1626 NamedDecl *UD =
1627 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1628 D->getUsingLoc(), SS, D->getLocation(),
1629 D->getDeclName(), 0,
1630 /*instantiation*/ true,
1631 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001632 if (UD)
John McCalled976492009-12-04 22:46:56 +00001633 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1634
Anders Carlsson0d8df782009-08-29 19:37:28 +00001635 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001636}
1637
John McCallce3ff2b2009-08-25 22:02:44 +00001638Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001639 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001640 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001641 if (D->isInvalidDecl())
1642 return 0;
1643
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001644 return Instantiator.Visit(D);
1645}
1646
John McCalle29ba202009-08-20 01:44:21 +00001647/// \brief Instantiates a nested template parameter list in the current
1648/// instantiation context.
1649///
1650/// \param L The parameter list to instantiate
1651///
1652/// \returns NULL if there was an error
1653TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001654TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001655 // Get errors for all the parameters before bailing out.
1656 bool Invalid = false;
1657
1658 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001659 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001660 ParamVector Params;
1661 Params.reserve(N);
1662 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1663 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001664 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001665 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001666 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001667 }
1668
1669 // Clean up if we had an error.
1670 if (Invalid) {
1671 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1672 PI != PE; ++PI)
1673 if (*PI)
1674 (*PI)->Destroy(SemaRef.Context);
1675 return NULL;
1676 }
1677
1678 TemplateParameterList *InstL
1679 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1680 L->getLAngleLoc(), &Params.front(), N,
1681 L->getRAngleLoc());
1682 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001683}
John McCalle29ba202009-08-20 01:44:21 +00001684
Douglas Gregored9c0f92009-10-29 00:04:11 +00001685/// \brief Instantiate the declaration of a class template partial
1686/// specialization.
1687///
1688/// \param ClassTemplate the (instantiated) class template that is partially
1689// specialized by the instantiation of \p PartialSpec.
1690///
1691/// \param PartialSpec the (uninstantiated) class template partial
1692/// specialization that we are instantiating.
1693///
1694/// \returns true if there was an error, false otherwise.
1695bool
1696TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1697 ClassTemplateDecl *ClassTemplate,
1698 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001699 // Create a local instantiation scope for this class template partial
1700 // specialization, which will contain the instantiations of the template
1701 // parameters.
1702 Sema::LocalInstantiationScope Scope(SemaRef);
1703
Douglas Gregored9c0f92009-10-29 00:04:11 +00001704 // Substitute into the template parameters of the class template partial
1705 // specialization.
1706 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1707 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1708 if (!InstParams)
1709 return true;
1710
1711 // Substitute into the template arguments of the class template partial
1712 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001713 const TemplateArgumentLoc *PartialSpecTemplateArgs
1714 = PartialSpec->getTemplateArgsAsWritten();
1715 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1716
John McCalld5532b62009-11-23 01:53:49 +00001717 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001718 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001719 TemplateArgumentLoc Loc;
1720 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001721 return true;
John McCalld5532b62009-11-23 01:53:49 +00001722 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001723 }
1724
1725
1726 // Check that the template argument list is well-formed for this
1727 // class template.
1728 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1729 InstTemplateArgs.size());
1730 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1731 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001732 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001733 false,
1734 Converted))
1735 return true;
1736
1737 // Figure out where to insert this class template partial specialization
1738 // in the member template's set of class template partial specializations.
1739 llvm::FoldingSetNodeID ID;
1740 ClassTemplatePartialSpecializationDecl::Profile(ID,
1741 Converted.getFlatArguments(),
1742 Converted.flatSize(),
1743 SemaRef.Context);
1744 void *InsertPos = 0;
1745 ClassTemplateSpecializationDecl *PrevDecl
1746 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1747 InsertPos);
1748
1749 // Build the canonical type that describes the converted template
1750 // arguments of the class template partial specialization.
1751 QualType CanonType
1752 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1753 Converted.getFlatArguments(),
1754 Converted.flatSize());
1755
1756 // Build the fully-sugared type for this class template
1757 // specialization as the user wrote in the specialization
1758 // itself. This means that we'll pretty-print the type retrieved
1759 // from the specialization's declaration the way that the user
1760 // actually wrote the specialization, rather than formatting the
1761 // name based on the "canonical" representation used to store the
1762 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001763 TypeSourceInfo *WrittenTy
1764 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1765 TemplateName(ClassTemplate),
1766 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001767 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001768 CanonType);
1769
1770 if (PrevDecl) {
1771 // We've already seen a partial specialization with the same template
1772 // parameters and template arguments. This can happen, for example, when
1773 // substituting the outer template arguments ends up causing two
1774 // class template partial specializations of a member class template
1775 // to have identical forms, e.g.,
1776 //
1777 // template<typename T, typename U>
1778 // struct Outer {
1779 // template<typename X, typename Y> struct Inner;
1780 // template<typename Y> struct Inner<T, Y>;
1781 // template<typename Y> struct Inner<U, Y>;
1782 // };
1783 //
1784 // Outer<int, int> outer; // error: the partial specializations of Inner
1785 // // have the same signature.
1786 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1787 << WrittenTy;
1788 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1789 << SemaRef.Context.getTypeDeclType(PrevDecl);
1790 return true;
1791 }
1792
1793
1794 // Create the class template partial specialization declaration.
1795 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001796 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1797 PartialSpec->getTagKind(),
1798 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001799 PartialSpec->getLocation(),
1800 InstParams,
1801 ClassTemplate,
1802 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001803 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001804 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001805 0,
1806 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001807 // Substitute the nested name specifier, if any.
1808 if (SubstQualifier(PartialSpec, InstPartialSpec))
1809 return 0;
1810
Douglas Gregored9c0f92009-10-29 00:04:11 +00001811 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1812 InstPartialSpec->setTypeAsWritten(WrittenTy);
1813
1814 // Add this partial specialization to the set of class template partial
1815 // specializations.
1816 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1817 InsertPos);
1818 return false;
1819}
1820
John McCall21ef0fa2010-03-11 09:03:00 +00001821TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001822TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001823 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001824 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1825 assert(OldTInfo && "substituting function without type source info");
1826 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001827 TypeSourceInfo *NewTInfo
1828 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1829 D->getTypeSpecStartLoc(),
1830 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001831 if (!NewTInfo)
1832 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001833
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001834 if (NewTInfo != OldTInfo) {
1835 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001836 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001837 if (FunctionProtoTypeLoc *OldProtoLoc
1838 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1839 TypeLoc NewTL = NewTInfo->getTypeLoc();
1840 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1841 assert(NewProtoLoc && "Missing prototype?");
1842 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1843 // FIXME: Variadic templates will break this.
1844 Params.push_back(NewProtoLoc->getArg(i));
1845 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001846 OldProtoLoc->getArg(i),
1847 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001848 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001849 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001850 } else {
1851 // The function type itself was not dependent and therefore no
1852 // substitution occurred. However, we still need to instantiate
1853 // the function parameters themselves.
1854 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001855 if (FunctionProtoTypeLoc *OldProtoLoc
1856 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1857 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1858 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1859 if (!Parm)
1860 return 0;
1861 Params.push_back(Parm);
1862 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001863 }
1864 }
John McCall21ef0fa2010-03-11 09:03:00 +00001865 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001866}
1867
Mike Stump1eb44332009-09-09 15:08:12 +00001868/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001869/// declaration (New) from the corresponding fields of its template (Tmpl).
1870///
1871/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001872bool
1873TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001874 FunctionDecl *Tmpl) {
1875 if (Tmpl->isDeleted())
1876 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Douglas Gregorcca9e962009-07-01 22:01:06 +00001878 // If we are performing substituting explicitly-specified template arguments
1879 // or deduced template arguments into a function template and we reach this
1880 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001881 // to keeping the new function template specialization. We therefore
1882 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001883 // into a template instantiation for this specific function template
1884 // specialization, which is not a SFINAE context, so that we diagnose any
1885 // further errors in the declaration itself.
1886 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1887 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1888 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1889 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001890 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001891 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001892 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001893 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001894 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001895 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1896 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001897 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001898 }
1899 }
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001901 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1902 assert(Proto && "Function template without prototype?");
1903
1904 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1905 Proto->getNoReturnAttr()) {
1906 // The function has an exception specification or a "noreturn"
1907 // attribute. Substitute into each of the exception types.
1908 llvm::SmallVector<QualType, 4> Exceptions;
1909 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1910 // FIXME: Poor location information!
1911 QualType T
1912 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1913 New->getLocation(), New->getDeclName());
1914 if (T.isNull() ||
1915 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1916 continue;
1917
1918 Exceptions.push_back(T);
1919 }
1920
1921 // Rebuild the function type
1922
1923 const FunctionProtoType *NewProto
1924 = New->getType()->getAs<FunctionProtoType>();
1925 assert(NewProto && "Template instantiation without function prototype?");
1926 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1927 NewProto->arg_type_begin(),
1928 NewProto->getNumArgs(),
1929 NewProto->isVariadic(),
1930 NewProto->getTypeQuals(),
1931 Proto->hasExceptionSpec(),
1932 Proto->hasAnyExceptionSpec(),
1933 Exceptions.size(),
1934 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001935 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001936 }
1937
Douglas Gregore53060f2009-06-25 22:08:12 +00001938 return false;
1939}
1940
Douglas Gregor5545e162009-03-24 00:38:23 +00001941/// \brief Initializes common fields of an instantiated method
1942/// declaration (New) from the corresponding fields of its template
1943/// (Tmpl).
1944///
1945/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001946bool
1947TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001948 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001949 if (InitFunctionInstantiation(New, Tmpl))
1950 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Douglas Gregor5545e162009-03-24 00:38:23 +00001952 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1953 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001954 if (Tmpl->isVirtualAsWritten())
1955 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001956
1957 // FIXME: attributes
1958 // FIXME: New needs a pointer to Tmpl
1959 return false;
1960}
Douglas Gregora58861f2009-05-13 20:28:22 +00001961
1962/// \brief Instantiate the definition of the given function from its
1963/// template.
1964///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001965/// \param PointOfInstantiation the point at which the instantiation was
1966/// required. Note that this is not precisely a "point of instantiation"
1967/// for the function, but it's close.
1968///
Douglas Gregora58861f2009-05-13 20:28:22 +00001969/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001970/// function template specialization or member function of a class template
1971/// specialization.
1972///
1973/// \param Recursive if true, recursively instantiates any functions that
1974/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001975///
1976/// \param DefinitionRequired if true, then we are performing an explicit
1977/// instantiation where the body of the function is required. Complain if
1978/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001979void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001980 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001981 bool Recursive,
1982 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001983 if (Function->isInvalidDecl())
1984 return;
1985
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001986 // Never instantiate an explicit specialization.
1987 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1988 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001989
1990 const FunctionDecl *Definition = 0;
1991 if (Function->getBody(Definition)) {
1992 // We are trying to instantiate a friend function specialization inside
1993 // a class template, but there is already another (non-template) definition
1994 // of the same function.
1995 if (Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1996 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1997 if (Inst)
1998 return;
1999
2000 Diag(Function->getLocation(), diag::err_redefinition)
2001 << Function->getDeclName();
2002 Diag(Definition->getLocation(), diag::note_previous_definition);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002003 Function->setInvalidDecl();
2004 }
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002005
2006 // We have an explicit instantiation (which already occurred) and an
2007 // implicit instantiation. Return without complaint.
2008 return;
2009 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002010
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002011 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002012 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002013 Stmt *Pattern = 0;
2014 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002015 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002016
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002017 if (!Pattern) {
2018 if (DefinitionRequired) {
2019 if (Function->getPrimaryTemplate())
2020 Diag(PointOfInstantiation,
2021 diag::err_explicit_instantiation_undefined_func_template)
2022 << Function->getPrimaryTemplate();
2023 else
2024 Diag(PointOfInstantiation,
2025 diag::err_explicit_instantiation_undefined_member)
2026 << 1 << Function->getDeclName() << Function->getDeclContext();
2027
2028 if (PatternDecl)
2029 Diag(PatternDecl->getLocation(),
2030 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002031 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002032 }
2033
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002034 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002035 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002036
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002037 // If this is an instantiation of friend function defined within a class
2038 // template or class template specialization or member class thereof,
2039 // determine whether there were multiple instantiations of its lexical class.
2040 if (PatternDecl->getFriendObjectKind() != Decl::FOK_None) {
2041 for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
2042 REnd = Function->redecls_end();
2043 R != REnd; ++R) {
2044 if (*R != Function &&
2045 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
2046 if (const FunctionDecl *RPattern
2047 = (*R)->getTemplateInstantiationPattern())
2048 if (RPattern->getBody(RPattern)) {
2049 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2050 if (Inst)
2051 return;
2052
2053 Diag(Function->getLocation(), diag::err_redefinition)
2054 << Function->getDeclName();
2055 Diag((*R)->getLocation(), diag::note_previous_definition);
2056 Function->setInvalidDecl();
2057 return;
2058 }
2059 }
2060 }
2061 }
2062
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002063 // C++0x [temp.explicit]p9:
2064 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002065 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002066 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002067 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002068 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002069 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002070 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002072 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2073 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002074 return;
2075
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002076 // If we're performing recursive template instantiation, create our own
2077 // queue of pending implicit instantiations that we will instantiate later,
2078 // while we're still within our own instantiation context.
2079 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2080 if (Recursive)
2081 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Douglas Gregor9679caf2010-05-12 17:27:19 +00002083 EnterExpressionEvaluationContext EvalContext(*this,
2084 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002085 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2086
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002087 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002088 // recorded, unless we're actually a member function within a local
2089 // class, in which case we need to merge our results with the parent
2090 // scope (of the enclosing function).
2091 bool MergeWithParentScope = false;
2092 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2093 MergeWithParentScope = Rec->isLocalClass();
2094
2095 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002097 // Introduce the instantiated function parameters into the local
2098 // instantiation scope.
2099 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2100 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2101 Function->getParamDecl(I));
2102
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002103 // Enter the scope of this instantiation. We don't use
2104 // PushDeclContext because we don't have a scope.
2105 DeclContext *PreviousContext = CurContext;
2106 CurContext = Function;
2107
Mike Stump1eb44332009-09-09 15:08:12 +00002108 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002109 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002110
2111 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002112 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002113 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2114 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2115 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002116 }
2117
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002118 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002119 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002120
Douglas Gregor52604ab2009-09-11 21:19:12 +00002121 if (Body.isInvalid())
2122 Function->setInvalidDecl();
2123
Mike Stump1eb44332009-09-09 15:08:12 +00002124 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002125 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002126
John McCall0c01d182010-03-24 05:22:00 +00002127 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2128
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002129 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002130
2131 DeclGroupRef DG(Function);
2132 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Douglas Gregor60406be2010-01-16 22:29:39 +00002134 // This class may have local implicit instantiations that need to be
2135 // instantiation within this scope.
2136 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2137 Scope.Exit();
2138
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002139 if (Recursive) {
2140 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002141 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002142 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002144 // Restore the set of pending implicit instantiations.
2145 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2146 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002147}
2148
2149/// \brief Instantiate the definition of the given variable from its
2150/// template.
2151///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002152/// \param PointOfInstantiation the point at which the instantiation was
2153/// required. Note that this is not precisely a "point of instantiation"
2154/// for the function, but it's close.
2155///
2156/// \param Var the already-instantiated declaration of a static member
2157/// variable of a class template specialization.
2158///
2159/// \param Recursive if true, recursively instantiates any functions that
2160/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002161///
2162/// \param DefinitionRequired if true, then we are performing an explicit
2163/// instantiation where an out-of-line definition of the member variable
2164/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002165void Sema::InstantiateStaticDataMemberDefinition(
2166 SourceLocation PointOfInstantiation,
2167 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002168 bool Recursive,
2169 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002170 if (Var->isInvalidDecl())
2171 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Douglas Gregor7caa6822009-07-24 20:34:43 +00002173 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002174 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002175 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002176 assert(Def->isStaticDataMember() && "Not a static data member?");
2177 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Douglas Gregor0d035142009-10-27 18:42:08 +00002179 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002180 // We did not find an out-of-line definition of this static data member,
2181 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002182 // instantiate this definition (or provide a specialization for it) in
2183 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002184 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002185 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002186 Diag(PointOfInstantiation,
2187 diag::err_explicit_instantiation_undefined_member)
2188 << 2 << Var->getDeclName() << Var->getDeclContext();
2189 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2190 }
2191
Douglas Gregor7caa6822009-07-24 20:34:43 +00002192 return;
2193 }
2194
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002195 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002196 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002197 return;
2198
2199 // C++0x [temp.explicit]p9:
2200 // Except for inline functions, other explicit instantiation declarations
2201 // have the effect of suppressing the implicit instantiation of the entity
2202 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002203 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002204 == TSK_ExplicitInstantiationDeclaration)
2205 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Douglas Gregor7caa6822009-07-24 20:34:43 +00002207 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2208 if (Inst)
2209 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Douglas Gregor7caa6822009-07-24 20:34:43 +00002211 // If we're performing recursive template instantiation, create our own
2212 // queue of pending implicit instantiations that we will instantiate later,
2213 // while we're still within our own instantiation context.
2214 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2215 if (Recursive)
2216 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Douglas Gregor7caa6822009-07-24 20:34:43 +00002218 // Enter the scope of this instantiation. We don't use
2219 // PushDeclContext because we don't have a scope.
2220 DeclContext *PreviousContext = CurContext;
2221 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002223 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002224 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002225 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002226 CurContext = PreviousContext;
2227
2228 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002229 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2230 assert(MSInfo && "Missing member specialization information?");
2231 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2232 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002233 DeclGroupRef DG(Var);
2234 Consumer.HandleTopLevelDecl(DG);
2235 }
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Douglas Gregor7caa6822009-07-24 20:34:43 +00002237 if (Recursive) {
2238 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002239 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002240 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Douglas Gregor7caa6822009-07-24 20:34:43 +00002242 // Restore the set of pending implicit instantiations.
2243 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002244 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002245}
Douglas Gregor815215d2009-05-27 05:35:12 +00002246
Anders Carlsson09025312009-08-29 05:16:22 +00002247void
2248Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2249 const CXXConstructorDecl *Tmpl,
2250 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Anders Carlsson09025312009-08-29 05:16:22 +00002252 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002253 bool AnyErrors = false;
2254
Anders Carlsson09025312009-08-29 05:16:22 +00002255 // Instantiate all the initializers.
2256 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002257 InitsEnd = Tmpl->init_end();
2258 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002259 CXXBaseOrMemberInitializer *Init = *Inits;
2260
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002261 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002262 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002263 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002265 // Instantiate the initializer.
2266 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2267 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2268 AnyErrors = true;
2269 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002270 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002271
Anders Carlsson09025312009-08-29 05:16:22 +00002272 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002273 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002274 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002275 TemplateArgs,
2276 Init->getSourceLocation(),
2277 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002278 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002279 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002280 New->setInvalidDecl();
2281 continue;
2282 }
2283
John McCalla93c9342009-12-07 02:54:59 +00002284 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002285 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002286 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002287 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002288 Init->getRParenLoc(),
2289 New->getParent());
2290 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002291 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002293 // Is this an anonymous union?
2294 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002295 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2296 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002297 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002298 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2299 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002300 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002301
2302 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002303 NewArgs.size(),
2304 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002305 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002306 Init->getRParenLoc());
2307 }
2308
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002309 if (NewInit.isInvalid()) {
2310 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002311 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002312 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002313 // FIXME: It would be nice if ASTOwningVector had a release function.
2314 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Anders Carlsson09025312009-08-29 05:16:22 +00002316 NewInits.push_back((MemInitTy *)NewInit.get());
2317 }
2318 }
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Anders Carlsson09025312009-08-29 05:16:22 +00002320 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002321 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002322 /*FIXME: ColonLoc */
2323 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002324 NewInits.data(), NewInits.size(),
2325 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002326}
2327
John McCall52a575a2009-08-29 08:11:13 +00002328// TODO: this could be templated if the various decl types used the
2329// same method name.
2330static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2331 ClassTemplateDecl *Instance) {
2332 Pattern = Pattern->getCanonicalDecl();
2333
2334 do {
2335 Instance = Instance->getCanonicalDecl();
2336 if (Pattern == Instance) return true;
2337 Instance = Instance->getInstantiatedFromMemberTemplate();
2338 } while (Instance);
2339
2340 return false;
2341}
2342
Douglas Gregor0d696532009-09-28 06:34:35 +00002343static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2344 FunctionTemplateDecl *Instance) {
2345 Pattern = Pattern->getCanonicalDecl();
2346
2347 do {
2348 Instance = Instance->getCanonicalDecl();
2349 if (Pattern == Instance) return true;
2350 Instance = Instance->getInstantiatedFromMemberTemplate();
2351 } while (Instance);
2352
2353 return false;
2354}
2355
Douglas Gregored9c0f92009-10-29 00:04:11 +00002356static bool
2357isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2358 ClassTemplatePartialSpecializationDecl *Instance) {
2359 Pattern
2360 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2361 do {
2362 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2363 Instance->getCanonicalDecl());
2364 if (Pattern == Instance)
2365 return true;
2366 Instance = Instance->getInstantiatedFromMember();
2367 } while (Instance);
2368
2369 return false;
2370}
2371
John McCall52a575a2009-08-29 08:11:13 +00002372static bool isInstantiationOf(CXXRecordDecl *Pattern,
2373 CXXRecordDecl *Instance) {
2374 Pattern = Pattern->getCanonicalDecl();
2375
2376 do {
2377 Instance = Instance->getCanonicalDecl();
2378 if (Pattern == Instance) return true;
2379 Instance = Instance->getInstantiatedFromMemberClass();
2380 } while (Instance);
2381
2382 return false;
2383}
2384
2385static bool isInstantiationOf(FunctionDecl *Pattern,
2386 FunctionDecl *Instance) {
2387 Pattern = Pattern->getCanonicalDecl();
2388
2389 do {
2390 Instance = Instance->getCanonicalDecl();
2391 if (Pattern == Instance) return true;
2392 Instance = Instance->getInstantiatedFromMemberFunction();
2393 } while (Instance);
2394
2395 return false;
2396}
2397
2398static bool isInstantiationOf(EnumDecl *Pattern,
2399 EnumDecl *Instance) {
2400 Pattern = Pattern->getCanonicalDecl();
2401
2402 do {
2403 Instance = Instance->getCanonicalDecl();
2404 if (Pattern == Instance) return true;
2405 Instance = Instance->getInstantiatedFromMemberEnum();
2406 } while (Instance);
2407
2408 return false;
2409}
2410
John McCalled976492009-12-04 22:46:56 +00002411static bool isInstantiationOf(UsingShadowDecl *Pattern,
2412 UsingShadowDecl *Instance,
2413 ASTContext &C) {
2414 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2415}
2416
2417static bool isInstantiationOf(UsingDecl *Pattern,
2418 UsingDecl *Instance,
2419 ASTContext &C) {
2420 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2421}
2422
John McCall7ba107a2009-11-18 02:36:19 +00002423static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2424 UsingDecl *Instance,
2425 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002426 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002427}
2428
2429static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002430 UsingDecl *Instance,
2431 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002432 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002433}
2434
John McCall52a575a2009-08-29 08:11:13 +00002435static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2436 VarDecl *Instance) {
2437 assert(Instance->isStaticDataMember());
2438
2439 Pattern = Pattern->getCanonicalDecl();
2440
2441 do {
2442 Instance = Instance->getCanonicalDecl();
2443 if (Pattern == Instance) return true;
2444 Instance = Instance->getInstantiatedFromStaticDataMember();
2445 } while (Instance);
2446
2447 return false;
2448}
2449
John McCalled976492009-12-04 22:46:56 +00002450// Other is the prospective instantiation
2451// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002452static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002453 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002454 if (UnresolvedUsingTypenameDecl *UUD
2455 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2456 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2457 return isInstantiationOf(UUD, UD, Ctx);
2458 }
2459 }
2460
2461 if (UnresolvedUsingValueDecl *UUD
2462 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002463 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2464 return isInstantiationOf(UUD, UD, Ctx);
2465 }
2466 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002467
Anders Carlsson0d8df782009-08-29 19:37:28 +00002468 return false;
2469 }
Mike Stump1eb44332009-09-09 15:08:12 +00002470
John McCall52a575a2009-08-29 08:11:13 +00002471 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2472 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002473
John McCall52a575a2009-08-29 08:11:13 +00002474 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2475 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002476
John McCall52a575a2009-08-29 08:11:13 +00002477 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2478 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002479
Douglas Gregor7caa6822009-07-24 20:34:43 +00002480 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002481 if (Var->isStaticDataMember())
2482 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2483
2484 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2485 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002486
Douglas Gregor0d696532009-09-28 06:34:35 +00002487 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2488 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2489
Douglas Gregored9c0f92009-10-29 00:04:11 +00002490 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2491 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2492 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2493 PartialSpec);
2494
Anders Carlssond8b285f2009-09-01 04:26:58 +00002495 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2496 if (!Field->getDeclName()) {
2497 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002498 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002499 cast<FieldDecl>(D);
2500 }
2501 }
Mike Stump1eb44332009-09-09 15:08:12 +00002502
John McCalled976492009-12-04 22:46:56 +00002503 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2504 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2505
2506 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2507 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2508
Douglas Gregor815215d2009-05-27 05:35:12 +00002509 return D->getDeclName() && isa<NamedDecl>(Other) &&
2510 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2511}
2512
2513template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002514static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002515 NamedDecl *D,
2516 ForwardIterator first,
2517 ForwardIterator last) {
2518 for (; first != last; ++first)
2519 if (isInstantiationOf(Ctx, D, *first))
2520 return cast<NamedDecl>(*first);
2521
2522 return 0;
2523}
2524
John McCall02cace72009-08-28 07:59:38 +00002525/// \brief Finds the instantiation of the given declaration context
2526/// within the current instantiation.
2527///
2528/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002529DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002530 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002531 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002532 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002533 return cast_or_null<DeclContext>(ID);
2534 } else return DC;
2535}
2536
Douglas Gregored961e72009-05-27 17:54:46 +00002537/// \brief Find the instantiation of the given declaration within the
2538/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002539///
2540/// This routine is intended to be used when \p D is a declaration
2541/// referenced from within a template, that needs to mapped into the
2542/// corresponding declaration within an instantiation. For example,
2543/// given:
2544///
2545/// \code
2546/// template<typename T>
2547/// struct X {
2548/// enum Kind {
2549/// KnownValue = sizeof(T)
2550/// };
2551///
2552/// bool getKind() const { return KnownValue; }
2553/// };
2554///
2555/// template struct X<int>;
2556/// \endcode
2557///
2558/// In the instantiation of X<int>::getKind(), we need to map the
2559/// EnumConstantDecl for KnownValue (which refers to
2560/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002561/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2562/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002563NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002564 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002565 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002566 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002567 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002568 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002569 // D is a local of some kind. Look into the map of local
2570 // declarations to their instantiations.
2571 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2572 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002573
Douglas Gregore95b4092009-09-16 18:34:49 +00002574 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2575 if (!Record->isDependentContext())
2576 return D;
2577
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002578 // If the RecordDecl is actually the injected-class-name or a
2579 // "templated" declaration for a class template, class template
2580 // partial specialization, or a member class of a class template,
2581 // substitute into the injected-class-name of the class template
2582 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002583 QualType T;
2584 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2585
2586 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002587 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002588 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2589 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002590 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002591
2592 // If we call SubstType with an InjectedClassNameType here we
2593 // can end up in an infinite loop.
2594 T = Context.getTypeDeclType(Record);
2595 assert(isa<InjectedClassNameType>(T) &&
2596 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002597 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002598 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002599
2600 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002601 // Substitute into the injected-class-name to get the type
2602 // corresponding to the instantiation we want, which may also be
2603 // the current instantiation (if we're in a template
2604 // definition). This substitution should never fail, since we
2605 // know we can instantiate the injected-class-name or we
2606 // wouldn't have gotten to the injected-class-name!
2607
2608 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2609 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002610 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2611 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2612
2613 if (!T->isDependentType()) {
2614 assert(T->isRecordType() && "Instantiation must produce a record type");
2615 return T->getAs<RecordType>()->getDecl();
2616 }
2617
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002618 // We are performing "partial" template instantiation to create
2619 // the member declarations for the members of a class template
2620 // specialization. Therefore, D is actually referring to something
2621 // in the current instantiation. Look through the current
2622 // context, which contains actual instantiations, to find the
2623 // instantiation of the "current instantiation" that D refers
2624 // to.
2625 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002626 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002627 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002628 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002629 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002630 if (isInstantiationOf(ClassTemplate,
2631 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002632 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002633
2634 if (!DC->isDependentContext())
2635 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002636 }
2637
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002638 // We're performing "instantiation" of a member of the current
2639 // instantiation while we are type-checking the
2640 // definition. Compute the declaration context and return that.
2641 assert(!SawNonDependentContext &&
2642 "No dependent context while instantiating record");
2643 DeclContext *DC = computeDeclContext(T);
2644 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002645 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002646 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002647 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002648
Douglas Gregore95b4092009-09-16 18:34:49 +00002649 // Fall through to deal with other dependent record types (e.g.,
2650 // anonymous unions in class templates).
2651 }
John McCall52a575a2009-08-29 08:11:13 +00002652
Douglas Gregore95b4092009-09-16 18:34:49 +00002653 if (!ParentDC->isDependentContext())
2654 return D;
2655
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002656 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002657 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002658 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Douglas Gregor815215d2009-05-27 05:35:12 +00002660 if (ParentDC != D->getDeclContext()) {
2661 // We performed some kind of instantiation in the parent context,
2662 // so now we need to look into the instantiated parent context to
2663 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002664
John McCall3cb0ebd2010-03-10 03:28:59 +00002665 // If our context used to be dependent, we may need to instantiate
2666 // it before performing lookup into that context.
2667 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002668 if (!Spec->isDependentContext()) {
2669 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002670 const RecordType *Tag = T->getAs<RecordType>();
2671 assert(Tag && "type of non-dependent record is not a RecordType");
2672 if (!Tag->isBeingDefined() &&
2673 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2674 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002675 }
2676 }
2677
Douglas Gregor815215d2009-05-27 05:35:12 +00002678 NamedDecl *Result = 0;
2679 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002680 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002681 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2682 } else {
2683 // Since we don't have a name for the entity we're looking for,
2684 // our only option is to walk through all of the declarations to
2685 // find that name. This will occur in a few cases:
2686 //
2687 // - anonymous struct/union within a template
2688 // - unnamed class/struct/union/enum within a template
2689 //
2690 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002691 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002692 ParentDC->decls_begin(),
2693 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002694 }
Mike Stump1eb44332009-09-09 15:08:12 +00002695
John McCall9f54ad42009-12-10 09:41:52 +00002696 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002697 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2698 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002699 && "Unable to find instantiation of declaration!");
2700
Douglas Gregor815215d2009-05-27 05:35:12 +00002701 D = Result;
2702 }
2703
Douglas Gregor815215d2009-05-27 05:35:12 +00002704 return D;
2705}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002706
Mike Stump1eb44332009-09-09 15:08:12 +00002707/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002708/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002709void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2710 while (!PendingLocalImplicitInstantiations.empty() ||
2711 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2712 PendingImplicitInstantiation Inst;
2713
2714 if (PendingLocalImplicitInstantiations.empty()) {
2715 Inst = PendingImplicitInstantiations.front();
2716 PendingImplicitInstantiations.pop_front();
2717 } else {
2718 Inst = PendingLocalImplicitInstantiations.front();
2719 PendingLocalImplicitInstantiations.pop_front();
2720 }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Douglas Gregor7caa6822009-07-24 20:34:43 +00002722 // Instantiate function definitions
2723 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002724 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002725 Function->getLocation(), *this,
2726 Context.getSourceManager(),
2727 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002729 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002730 continue;
2731 }
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Douglas Gregor7caa6822009-07-24 20:34:43 +00002733 // Instantiate static data member definitions.
2734 VarDecl *Var = cast<VarDecl>(Inst.first);
2735 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002736
Chandler Carruth291b4412010-02-13 10:17:50 +00002737 // Don't try to instantiate declarations if the most recent redeclaration
2738 // is invalid.
2739 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2740 continue;
2741
2742 // Check if the most recent declaration has changed the specialization kind
2743 // and removed the need for implicit instantiation.
2744 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2745 case TSK_Undeclared:
2746 assert(false && "Cannot instantitiate an undeclared specialization.");
2747 case TSK_ExplicitInstantiationDeclaration:
2748 case TSK_ExplicitInstantiationDefinition:
2749 case TSK_ExplicitSpecialization:
2750 continue; // No longer need implicit instantiation.
2751 case TSK_ImplicitInstantiation:
2752 break;
2753 }
2754
Mike Stump1eb44332009-09-09 15:08:12 +00002755 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002756 Var->getLocation(), *this,
2757 Context.getSourceManager(),
2758 "instantiating static data member "
2759 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002760
Douglas Gregor7caa6822009-07-24 20:34:43 +00002761 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002762 }
2763}
John McCall0c01d182010-03-24 05:22:00 +00002764
2765void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2766 const MultiLevelTemplateArgumentList &TemplateArgs) {
2767 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2768 E = Pattern->ddiag_end(); I != E; ++I) {
2769 DependentDiagnostic *DD = *I;
2770
2771 switch (DD->getKind()) {
2772 case DependentDiagnostic::Access:
2773 HandleDependentAccessCheck(*DD, TemplateArgs);
2774 break;
2775 }
2776 }
2777}