blob: 15a79461741ed01f911757bc69202e81d86d7626 [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 }
194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 // Create the new typedef
197 TypedefDecl *Typedef
198 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000199 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000200 if (Invalid)
201 Typedef->setInvalidDecl();
202
John McCall5126fd02009-12-30 00:31:22 +0000203 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000204 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
205 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000206 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
207 }
208
John McCall46460a62010-01-20 21:53:11 +0000209 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000210 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000212 return Typedef;
213}
214
Douglas Gregor6eef5192009-12-14 19:27:10 +0000215/// \brief Instantiate the arguments provided as part of initialization.
216///
217/// \returns true if an error occurred, false otherwise.
218static bool InstantiateInitializationArguments(Sema &SemaRef,
219 Expr **Args, unsigned NumArgs,
220 const MultiLevelTemplateArgumentList &TemplateArgs,
221 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
222 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
223 for (unsigned I = 0; I != NumArgs; ++I) {
224 // When we hit the first defaulted argument, break out of the loop:
225 // we don't pass those default arguments on.
226 if (Args[I]->isDefaultArgument())
227 break;
228
229 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
230 if (Arg.isInvalid())
231 return true;
232
233 Expr *ArgExpr = (Expr *)Arg.get();
234 InitArgs.push_back(Arg.release());
235
236 // FIXME: We're faking all of the comma locations. Do we need them?
237 FakeCommaLocs.push_back(
238 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
239 }
240
241 return false;
242}
243
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000244/// \brief Instantiate an initializer, breaking it into separate
245/// initialization arguments.
246///
247/// \param S The semantic analysis object.
248///
249/// \param Init The initializer to instantiate.
250///
251/// \param TemplateArgs Template arguments to be substituted into the
252/// initializer.
253///
254/// \param NewArgs Will be filled in with the instantiation arguments.
255///
256/// \returns true if an error occurred, false otherwise
257static bool InstantiateInitializer(Sema &S, Expr *Init,
258 const MultiLevelTemplateArgumentList &TemplateArgs,
259 SourceLocation &LParenLoc,
260 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
261 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
262 SourceLocation &RParenLoc) {
263 NewArgs.clear();
264 LParenLoc = SourceLocation();
265 RParenLoc = SourceLocation();
266
267 if (!Init)
268 return false;
269
270 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
271 Init = ExprTemp->getSubExpr();
272
273 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
274 Init = Binder->getSubExpr();
275
276 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
277 Init = ICE->getSubExprAsWritten();
278
279 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
280 LParenLoc = ParenList->getLParenLoc();
281 RParenLoc = ParenList->getRParenLoc();
282 return InstantiateInitializationArguments(S, ParenList->getExprs(),
283 ParenList->getNumExprs(),
284 TemplateArgs, CommaLocs,
285 NewArgs);
286 }
287
288 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
289 if (InstantiateInitializationArguments(S,
290 Construct->getArgs(),
291 Construct->getNumArgs(),
292 TemplateArgs,
293 CommaLocs, NewArgs))
294 return true;
295
296 // FIXME: Fake locations!
297 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
298 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
299 return false;
300 }
301
302 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
303 if (Result.isInvalid())
304 return true;
305
306 NewArgs.push_back(Result.takeAs<Expr>());
307 return false;
308}
309
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000310Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000311 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000312 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000313 TemplateArgs,
314 D->getTypeSpecStartLoc(),
315 D->getDeclName());
316 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000317 return 0;
318
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000319 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000320 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
321 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000322 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000323 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000324 Var->setThreadSpecified(D->isThreadSpecified());
325 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
326 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000327
John McCallb6217662010-03-15 10:12:16 +0000328 // Substitute the nested name specifier, if any.
329 if (SubstQualifier(D, Var))
330 return 0;
331
Mike Stump1eb44332009-09-09 15:08:12 +0000332 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000333 // out-of-line, the instantiation will have the same lexical
334 // context (which will be a namespace scope) as the template.
335 if (D->isOutOfLine())
336 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000337
John McCall46460a62010-01-20 21:53:11 +0000338 Var->setAccess(D->getAccess());
339
Mike Stump390b4cc2009-05-16 07:39:55 +0000340 // FIXME: In theory, we could have a previous declaration for variables that
341 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000342 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000343 // FIXME: having to fake up a LookupResult is dumb.
344 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000345 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000346 if (D->isStaticDataMember())
347 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000348 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregor7caa6822009-07-24 20:34:43 +0000350 if (D->isOutOfLine()) {
351 D->getLexicalDeclContext()->addDecl(Var);
352 Owner->makeDeclVisibleInContext(Var);
353 } else {
354 Owner->addDecl(Var);
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000357 // Link instantiations of static data members back to the template from
358 // which they were instantiated.
359 if (Var->isStaticDataMember())
360 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000361 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000362
Douglas Gregor60c93c92010-02-09 07:26:29 +0000363 if (Var->getAnyInitializer()) {
364 // We already have an initializer in the class.
365 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000366 if (Var->isStaticDataMember() && !D->isOutOfLine())
367 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
368 else
369 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
370
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000371 // Instantiate the initializer.
372 SourceLocation LParenLoc, RParenLoc;
373 llvm::SmallVector<SourceLocation, 4> CommaLocs;
374 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
375 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
376 CommaLocs, InitArgs, RParenLoc)) {
377 // Attach the initializer to the declaration.
378 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000379 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000380 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000381 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000382 move_arg(InitArgs),
383 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000384 RParenLoc);
385 } else if (InitArgs.size() == 1) {
386 Expr *Init = (Expr*)(InitArgs.take()[0]);
387 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
388 SemaRef.Owned(Init),
389 false);
390 } else {
391 assert(InitArgs.size() == 0);
392 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000393 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000394 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000395 // FIXME: Not too happy about invalidating the declaration
396 // because of a bogus initializer.
397 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000398 }
399
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000400 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000401 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
402 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000403
404 return Var;
405}
406
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000407Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
408 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000409 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000410 if (DI->getType()->isDependentType()) {
411 DI = SemaRef.SubstType(DI, TemplateArgs,
412 D->getLocation(), D->getDeclName());
413 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000414 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000415 Invalid = true;
416 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000417 // C++ [temp.arg.type]p3:
418 // If a declaration acquires a function type through a type
419 // dependent on a template-parameter and this causes a
420 // declaration that does not use the syntactic form of a
421 // function declarator to have function type, the program is
422 // ill-formed.
423 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000424 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000425 Invalid = true;
426 }
427 }
428
429 Expr *BitWidth = D->getBitWidth();
430 if (Invalid)
431 BitWidth = 0;
432 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000433 // The bit-width expression is not potentially evaluated.
434 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000436 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000437 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000438 if (InstantiatedBitWidth.isInvalid()) {
439 Invalid = true;
440 BitWidth = 0;
441 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000442 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000443 }
444
John McCall07fb6be2009-10-22 23:33:21 +0000445 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
446 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000447 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000448 D->getLocation(),
449 D->isMutable(),
450 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000451 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000452 D->getAccess(),
453 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000454 if (!Field) {
455 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000456 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000459 InstantiateAttrs(D, Field);
460
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000461 if (Invalid)
462 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000464 if (!Field->getDeclName()) {
465 // Keep track of where this decl came from.
466 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000469 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000470 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000471 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000472
473 return Field;
474}
475
John McCall02cace72009-08-28 07:59:38 +0000476Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
477 FriendDecl::FriendUnion FU;
478
479 // Handle friend type expressions by simply substituting template
480 // parameters into the pattern type.
481 if (Type *Ty = D->getFriendType()) {
482 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
483 D->getLocation(), DeclarationName());
484 if (T.isNull()) return 0;
485
486 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
487 FU = T.getTypePtr();
488
489 // Handle everything else by appropriate substitution.
490 } else {
491 NamedDecl *ND = D->getFriendDecl();
492 assert(ND && "friend decl must be a decl or a type!");
493
Douglas Gregora735b202009-10-13 14:39:41 +0000494 // FIXME: We have a problem here, because the nested call to Visit(ND)
495 // will inject the thing that the friend references into the current
496 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000497 Decl *NewND;
498
499 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000500 if (ND->getDeclContext()->isRecord()) {
501 if (!ND->getDeclContext()->isDependentContext()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000502 NewND = SemaRef.FindInstantiatedDecl(D->getLocation(), ND,
503 TemplateArgs);
Douglas Gregor63644fa2010-02-07 10:31:35 +0000504 } else {
505 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
506 // templated friend declarations. This doesn't produce a correct AST;
507 // however this is sufficient for some AST analysis. The real solution
508 // must be put in place during the pending rewrite. See PR5848.
509 return 0;
510 }
511 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000512 // Totally egregious hack to work around PR5866
513 return 0;
514 } else
John McCalle129d442009-12-17 23:21:11 +0000515 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000516 if (!NewND) return 0;
517
518 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
John McCall02cace72009-08-28 07:59:38 +0000521 FriendDecl *FD =
522 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
523 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000524 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000525 Owner->addDecl(FD);
526 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000527}
528
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000529Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
530 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Douglas Gregorac7610d2009-06-22 20:57:11 +0000532 // The expression in a static assertion is not potentially evaluated.
533 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000535 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000536 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000537 if (InstantiatedAssertExpr.isInvalid())
538 return 0;
539
Douglas Gregor43d9d922009-08-08 01:41:12 +0000540 OwningExprResult Message(SemaRef, D->getMessage());
541 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000542 Decl *StaticAssert
543 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000544 move(InstantiatedAssertExpr),
545 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000546 return StaticAssert;
547}
548
549Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000551 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000552 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000553 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000554 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000555 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000556 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000557 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000558 Enum->startDefinition();
559
Douglas Gregor96084f12010-03-01 19:00:07 +0000560 if (D->getDeclContext()->isFunctionOrMethod())
561 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
562
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000563 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000564
565 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000566 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
567 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 EC != ECEnd; ++EC) {
569 // The specified value for the enumerator.
570 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000571 if (Expr *UninstValue = EC->getInitExpr()) {
572 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000573 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000574 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000575
John McCallce3ff2b2009-08-25 22:02:44 +0000576 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000577 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578
579 // Drop the initial value and continue.
580 bool isInvalid = false;
581 if (Value.isInvalid()) {
582 Value = SemaRef.Owned((Expr *)0);
583 isInvalid = true;
584 }
585
Mike Stump1eb44332009-09-09 15:08:12 +0000586 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
588 EC->getLocation(), EC->getIdentifier(),
589 move(Value));
590
591 if (isInvalid) {
592 if (EnumConst)
593 EnumConst->setInvalidDecl();
594 Enum->setInvalidDecl();
595 }
596
597 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000598 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000599 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000600 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000601 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000602
603 if (D->getDeclContext()->isFunctionOrMethod()) {
604 // If the enumeration is within a function or method, record the enum
605 // constant as a local.
606 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
607 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000608 }
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000611 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000612 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000613 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
614 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000615 &Enumerators[0], Enumerators.size(),
616 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000617
618 return Enum;
619}
620
Douglas Gregor6477b692009-03-25 15:04:13 +0000621Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
622 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
623 return 0;
624}
625
Douglas Gregored9c0f92009-10-29 00:04:11 +0000626namespace {
627 class SortDeclByLocation {
628 SourceManager &SourceMgr;
629
630 public:
631 explicit SortDeclByLocation(SourceManager &SourceMgr)
632 : SourceMgr(SourceMgr) { }
633
634 bool operator()(const Decl *X, const Decl *Y) const {
635 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
636 Y->getLocation());
637 }
638 };
639}
640
John McCalle29ba202009-08-20 01:44:21 +0000641Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000642 // Create a local instantiation scope for this class template, which
643 // will contain the instantiations of the template parameters.
644 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000645 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000646 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000647 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000648 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000649
650 CXXRecordDecl *Pattern = D->getTemplatedDecl();
651 CXXRecordDecl *RecordInst
652 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
653 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000654 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
655 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000656
John McCallb6217662010-03-15 10:12:16 +0000657 // Substitute the nested name specifier, if any.
658 if (SubstQualifier(Pattern, RecordInst))
659 return 0;
660
John McCalle29ba202009-08-20 01:44:21 +0000661 ClassTemplateDecl *Inst
662 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
663 D->getIdentifier(), InstParams, RecordInst, 0);
664 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000665 if (D->getFriendObjectKind())
666 Inst->setObjectOfFriendDecl(true);
667 else
668 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000669 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000670
671 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000672 SemaRef.Context.getInjectedClassNameType(RecordInst,
673 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
Douglas Gregorf0510d42009-10-12 23:11:44 +0000674
Douglas Gregor259571e2009-10-30 22:42:42 +0000675 // Finish handling of friends.
676 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000677 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000678 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000679
John McCall46460a62010-01-20 21:53:11 +0000680 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000681 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000682
683 // First, we sort the partial specializations by location, so
684 // that we instantiate them in the order they were declared.
685 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
686 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
687 P = D->getPartialSpecializations().begin(),
688 PEnd = D->getPartialSpecializations().end();
689 P != PEnd; ++P)
690 PartialSpecs.push_back(&*P);
691 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
692 SortDeclByLocation(SemaRef.SourceMgr));
693
694 // Instantiate all of the partial specializations of this member class
695 // template.
696 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
697 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
698
John McCalle29ba202009-08-20 01:44:21 +0000699 return Inst;
700}
701
Douglas Gregord60e1052009-08-27 16:57:43 +0000702Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000703TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
704 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000705 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
706
707 // Lookup the already-instantiated declaration in the instantiation
708 // of the class template and return that.
709 DeclContext::lookup_result Found
710 = Owner->lookup(ClassTemplate->getDeclName());
711 if (Found.first == Found.second)
712 return 0;
713
714 ClassTemplateDecl *InstClassTemplate
715 = dyn_cast<ClassTemplateDecl>(*Found.first);
716 if (!InstClassTemplate)
717 return 0;
718
719 Decl *DCanon = D->getCanonicalDecl();
720 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
721 P = InstClassTemplate->getPartialSpecializations().begin(),
722 PEnd = InstClassTemplate->getPartialSpecializations().end();
723 P != PEnd; ++P) {
724 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
725 return &*P;
726 }
727
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000728 return 0;
729}
730
731Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000732TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000733 // Create a local instantiation scope for this function template, which
734 // will contain the instantiations of the template parameters and then get
735 // merged with the local instantiation scope for the function template
736 // itself.
737 Sema::LocalInstantiationScope Scope(SemaRef);
738
Douglas Gregord60e1052009-08-27 16:57:43 +0000739 TemplateParameterList *TempParams = D->getTemplateParameters();
740 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000741 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000742 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000743
Douglas Gregora735b202009-10-13 14:39:41 +0000744 FunctionDecl *Instantiated = 0;
745 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
746 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
747 InstParams));
748 else
749 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
750 D->getTemplatedDecl(),
751 InstParams));
752
753 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000754 return 0;
755
John McCall46460a62010-01-20 21:53:11 +0000756 Instantiated->setAccess(D->getAccess());
757
Mike Stump1eb44332009-09-09 15:08:12 +0000758 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000759 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000760 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000761 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000762 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000763 assert(InstTemplate &&
764 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000765
766 // Link the instantiation back to the pattern *unless* this is a
767 // non-definition friend declaration.
768 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
769 !(InstTemplate->getFriendObjectKind() &&
770 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000771 InstTemplate->setInstantiatedFromMemberTemplate(D);
772
773 // Add non-friends into the owner.
774 if (!InstTemplate->getFriendObjectKind())
775 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000776 return InstTemplate;
777}
778
Douglas Gregord475b8d2009-03-25 21:17:03 +0000779Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
780 CXXRecordDecl *PrevDecl = 0;
781 if (D->isInjectedClassName())
782 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000783 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000784 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
785 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000786 TemplateArgs);
787 if (!Prev) return 0;
788 PrevDecl = cast<CXXRecordDecl>(Prev);
789 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000790
791 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000792 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000793 D->getLocation(), D->getIdentifier(),
794 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000795
796 // Substitute the nested name specifier, if any.
797 if (SubstQualifier(D, Record))
798 return 0;
799
Douglas Gregord475b8d2009-03-25 21:17:03 +0000800 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000801 // FIXME: Check against AS_none is an ugly hack to work around the issue that
802 // the tag decls introduced by friend class declarations don't have an access
803 // specifier. Remove once this area of the code gets sorted out.
804 if (D->getAccess() != AS_none)
805 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000806 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000807 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000808
John McCall02cace72009-08-28 07:59:38 +0000809 // If the original function was part of a friend declaration,
810 // inherit its namespace state.
811 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
812 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
813
Anders Carlssond8b285f2009-09-01 04:26:58 +0000814 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
815
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000817 return Record;
818}
819
John McCall02cace72009-08-28 07:59:38 +0000820/// Normal class members are of more specific types and therefore
821/// don't make it here. This function serves two purposes:
822/// 1) instantiating function templates
823/// 2) substituting friend declarations
824/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000825Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000826 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000827 // Check whether there is already a function template specialization for
828 // this declaration.
829 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
830 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000831 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000832 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000833 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000834 TemplateArgs.getInnermost().getFlatArgumentList(),
835 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000836 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
838 FunctionTemplateSpecializationInfo *Info
839 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000840 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Douglas Gregor127102b2009-06-29 20:59:39 +0000842 // If we already have a function template specialization, return it.
843 if (Info)
844 return Info->Function;
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor79c22782010-01-16 20:21:20 +0000847 bool MergeWithParentScope = (TemplateParams != 0) ||
848 !(isa<Decl>(Owner) &&
849 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
850 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregore53060f2009-06-25 22:08:12 +0000852 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000853 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
854 TInfo = SubstFunctionType(D, Params);
855 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000856 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000857 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000858
John McCall68b6b872010-02-06 01:50:47 +0000859 // If we're instantiating a local function declaration, put the result
860 // in the owner; otherwise we need to find the instantiated context.
861 DeclContext *DC;
862 if (D->getDeclContext()->isFunctionOrMethod())
863 DC = Owner;
864 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000865 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
866 TemplateArgs);
John McCall68b6b872010-02-06 01:50:47 +0000867
John McCall02cace72009-08-28 07:59:38 +0000868 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000869 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000870 D->getDeclName(), T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000871 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000872 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000873
874 // Substitute the nested name specifier, if any.
875 if (SubstQualifier(D, Function))
876 return 0;
877
John McCall02cace72009-08-28 07:59:38 +0000878 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregore53060f2009-06-25 22:08:12 +0000880 // Attach the parameters
881 for (unsigned P = 0; P < Params.size(); ++P)
882 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +0000883 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000884
Douglas Gregora735b202009-10-13 14:39:41 +0000885 if (TemplateParams) {
886 // Our resulting instantiation is actually a function template, since we
887 // are substituting only the outer template parameters. For example, given
888 //
889 // template<typename T>
890 // struct X {
891 // template<typename U> friend void f(T, U);
892 // };
893 //
894 // X<int> x;
895 //
896 // We are instantiating the friend function template "f" within X<int>,
897 // which means substituting int for T, but leaving "f" as a friend function
898 // template.
899 // Build the function template itself.
900 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
901 Function->getLocation(),
902 Function->getDeclName(),
903 TemplateParams, Function);
904 Function->setDescribedFunctionTemplate(FunctionTemplate);
905 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000906 } else if (FunctionTemplate) {
907 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +0000908 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +0000909 &TemplateArgs.getInnermost(),
910 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000911 }
Douglas Gregora735b202009-10-13 14:39:41 +0000912
Douglas Gregore53060f2009-06-25 22:08:12 +0000913 if (InitFunctionInstantiation(Function, D))
914 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregore53060f2009-06-25 22:08:12 +0000916 bool Redeclaration = false;
917 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000918
John McCall68263142009-11-18 22:49:29 +0000919 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
920 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
921
Douglas Gregora735b202009-10-13 14:39:41 +0000922 if (TemplateParams || !FunctionTemplate) {
923 // Look only into the namespace where the friend would be declared to
924 // find a previous declaration. This is the innermost enclosing namespace,
925 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000926 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000927
Douglas Gregora735b202009-10-13 14:39:41 +0000928 // In C++, the previous declaration we find might be a tag type
929 // (class or enum). In this case, the new declaration will hide the
930 // tag type. Note that this does does not apply if we're declaring a
931 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000932 if (Previous.isSingleTagDecl())
933 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000934 }
935
John McCall9f54ad42009-12-10 09:41:52 +0000936 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
937 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000938 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000939
Douglas Gregora735b202009-10-13 14:39:41 +0000940 // If the original function was part of a friend declaration,
941 // inherit its namespace state and add it to the owner.
942 NamedDecl *FromFriendD
943 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
944 if (FromFriendD->getFriendObjectKind()) {
945 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000946 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000947 if (TemplateParams) {
948 ToFriendD = cast<NamedDecl>(FunctionTemplate);
949 PrevDecl = FunctionTemplate->getPreviousDeclaration();
950 } else {
951 ToFriendD = Function;
952 PrevDecl = Function->getPreviousDeclaration();
953 }
954 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
955 if (!Owner->isDependentContext() && !PrevDecl)
956 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
957
958 if (!TemplateParams)
959 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
960 }
961
Douglas Gregore53060f2009-06-25 22:08:12 +0000962 return Function;
963}
964
Douglas Gregord60e1052009-08-27 16:57:43 +0000965Decl *
966TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
967 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000968 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
969 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000970 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000971 // We are creating a function template specialization from a function
972 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000973 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000974 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000975 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000976 TemplateArgs.getInnermost().getFlatArgumentList(),
977 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000978 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
980 FunctionTemplateSpecializationInfo *Info
981 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000982 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Douglas Gregor6b906862009-08-21 00:16:32 +0000984 // If we already have a function template specialization, return it.
985 if (Info)
986 return Info->Function;
987 }
988
Douglas Gregor79c22782010-01-16 20:21:20 +0000989 bool MergeWithParentScope = (TemplateParams != 0) ||
990 !(isa<Decl>(Owner) &&
991 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
992 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000993
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000994 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000995 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
996 TInfo = SubstFunctionType(D, Params);
997 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000998 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000999 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001000
1001 // Build the instantiated method declaration.
1002 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +00001003 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Douglas Gregordec06662009-08-21 18:42:58 +00001005 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001006 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001007 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1008 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1009 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001010 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1011 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001012 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001013 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001014 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001015 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1016 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1017 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1018 SemaRef.Context.getCanonicalType(ClassTy));
1019 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1020 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001021 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001022 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001023 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001024 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001025 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001026 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1027 ConvTy);
1028 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1029 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001030 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001031 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001032 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001033 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001034 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001035 D->getDeclName(), T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001036 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001037 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001038
John McCallb6217662010-03-15 10:12:16 +00001039 // Substitute the nested name specifier, if any.
1040 if (SubstQualifier(D, Method))
1041 return 0;
1042
Douglas Gregord60e1052009-08-27 16:57:43 +00001043 if (TemplateParams) {
1044 // Our resulting instantiation is actually a function template, since we
1045 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001046 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001047 // template<typename T>
1048 // struct X {
1049 // template<typename U> void f(T, U);
1050 // };
1051 //
1052 // X<int> x;
1053 //
1054 // We are instantiating the member template "f" within X<int>, which means
1055 // substituting int for T, but leaving "f" as a member function template.
1056 // Build the function template itself.
1057 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1058 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001059 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001060 TemplateParams, Method);
1061 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001062 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001063 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001064 } else if (FunctionTemplate) {
1065 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001066 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001067 &TemplateArgs.getInnermost(),
1068 InsertPos);
1069 } else {
1070 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001071 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001072 }
1073
Mike Stump1eb44332009-09-09 15:08:12 +00001074 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001075 // out-of-line, the instantiation will have the same lexical
1076 // context (which will be a namespace scope) as the template.
1077 if (D->isOutOfLine())
1078 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregor5545e162009-03-24 00:38:23 +00001080 // Attach the parameters
1081 for (unsigned P = 0; P < Params.size(); ++P)
1082 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001083 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001084
1085 if (InitMethodInstantiation(Method, D))
1086 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001087
John McCall68263142009-11-18 22:49:29 +00001088 LookupResult Previous(SemaRef, Name, SourceLocation(),
1089 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregord60e1052009-08-27 16:57:43 +00001091 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +00001092 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregordec06662009-08-21 18:42:58 +00001094 // In C++, the previous declaration we find might be a tag type
1095 // (class or enum). In this case, the new declaration will hide the
1096 // tag type. Note that this does does not apply if we're declaring a
1097 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001098 if (Previous.isSingleTagDecl())
1099 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001100 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001101
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001102 bool Redeclaration = false;
1103 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001104 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001105 /*FIXME:*/OverloadableAttrRequired);
1106
Douglas Gregor4ba31362009-12-01 17:24:26 +00001107 if (D->isPure())
1108 SemaRef.CheckPureMethod(Method, SourceRange());
1109
John McCall46460a62010-01-20 21:53:11 +00001110 Method->setAccess(D->getAccess());
1111
John McCall68263142009-11-18 22:49:29 +00001112 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +00001113 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +00001114 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001116 return Method;
1117}
1118
Douglas Gregor615c5d42009-03-24 16:43:20 +00001119Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001120 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001121}
1122
Douglas Gregor03b2b072009-03-24 00:15:49 +00001123Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001124 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001125}
1126
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001127Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001128 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001129}
1130
Douglas Gregor6477b692009-03-25 15:04:13 +00001131ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001132 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001133 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001134 if (DI) {
1135 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1136 D->getDeclName());
1137 if (DI) T = DI->getType();
1138 } else {
1139 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1140 D->getDeclName());
1141 DI = 0;
1142 }
1143
1144 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001145 return 0;
1146
John McCall58e46772009-10-23 21:48:59 +00001147 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001148
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001149 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001150 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001151 = ParmVarDecl::Create(SemaRef.Context,
1152 SemaRef.Context.getTranslationUnitDecl(),
1153 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001154 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001155
Anders Carlsson9351c172009-08-25 03:18:48 +00001156 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001157 if (D->hasUninstantiatedDefaultArg())
1158 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001159 else if (Expr *Arg = D->getDefaultArg())
1160 Param->setUninstantiatedDefaultArg(Arg);
1161
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001162 // Note: we don't try to instantiate function parameters until after
1163 // we've instantiated the function's type. Therefore, we don't have
1164 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001165 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001166 return Param;
1167}
1168
John McCalle29ba202009-08-20 01:44:21 +00001169Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1170 TemplateTypeParmDecl *D) {
1171 // TODO: don't always clone when decls are refcounted.
1172 const Type* T = D->getTypeForDecl();
1173 assert(T->isTemplateTypeParmType());
1174 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001175
John McCalle29ba202009-08-20 01:44:21 +00001176 TemplateTypeParmDecl *Inst =
1177 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001178 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001179 TTPT->getName(),
1180 D->wasDeclaredWithTypename(),
1181 D->isParameterPack());
1182
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001183 if (D->hasDefaultArgument())
1184 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001185
Douglas Gregor550d9b22009-10-31 17:21:17 +00001186 // Introduce this template parameter's instantiation into the instantiation
1187 // scope.
1188 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1189
John McCalle29ba202009-08-20 01:44:21 +00001190 return Inst;
1191}
1192
Douglas Gregor33642df2009-10-23 23:25:44 +00001193Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1194 NonTypeTemplateParmDecl *D) {
1195 // Substitute into the type of the non-type template parameter.
1196 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001197 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001198 if (DI) {
1199 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1200 D->getDeclName());
1201 if (DI) T = DI->getType();
1202 } else {
1203 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1204 D->getDeclName());
1205 DI = 0;
1206 }
1207 if (T.isNull())
1208 return 0;
1209
1210 // Check that this type is acceptable for a non-type template parameter.
1211 bool Invalid = false;
1212 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1213 if (T.isNull()) {
1214 T = SemaRef.Context.IntTy;
1215 Invalid = true;
1216 }
1217
1218 NonTypeTemplateParmDecl *Param
1219 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1220 D->getDepth() - 1, D->getPosition(),
1221 D->getIdentifier(), T, DI);
1222 if (Invalid)
1223 Param->setInvalidDecl();
1224
1225 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001226
1227 // Introduce this template parameter's instantiation into the instantiation
1228 // scope.
1229 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001230 return Param;
1231}
1232
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001233Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001234TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1235 TemplateTemplateParmDecl *D) {
1236 // Instantiate the template parameter list of the template template parameter.
1237 TemplateParameterList *TempParams = D->getTemplateParameters();
1238 TemplateParameterList *InstParams;
1239 {
1240 // Perform the actual substitution of template parameters within a new,
1241 // local instantiation scope.
1242 Sema::LocalInstantiationScope Scope(SemaRef);
1243 InstParams = SubstTemplateParams(TempParams);
1244 if (!InstParams)
1245 return NULL;
1246 }
1247
1248 // Build the template template parameter.
1249 TemplateTemplateParmDecl *Param
1250 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1251 D->getDepth() - 1, D->getPosition(),
1252 D->getIdentifier(), InstParams);
1253 Param->setDefaultArgument(D->getDefaultArgument());
1254
1255 // Introduce this template parameter's instantiation into the instantiation
1256 // scope.
1257 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1258
1259 return Param;
1260}
1261
Douglas Gregor48c32a72009-11-17 06:07:40 +00001262Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1263 // Using directives are never dependent, so they require no explicit
1264
1265 UsingDirectiveDecl *Inst
1266 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1267 D->getNamespaceKeyLocation(),
1268 D->getQualifierRange(), D->getQualifier(),
1269 D->getIdentLocation(),
1270 D->getNominatedNamespace(),
1271 D->getCommonAncestor());
1272 Owner->addDecl(Inst);
1273 return Inst;
1274}
1275
John McCalled976492009-12-04 22:46:56 +00001276Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1277 // The nested name specifier is non-dependent, so no transformation
1278 // is required.
1279
John McCall9f54ad42009-12-10 09:41:52 +00001280 // We only need to do redeclaration lookups if we're in a class
1281 // scope (in fact, it's not really even possible in non-class
1282 // scopes).
1283 bool CheckRedeclaration = Owner->isRecord();
1284
1285 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1286 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1287
John McCalled976492009-12-04 22:46:56 +00001288 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1289 D->getLocation(),
1290 D->getNestedNameRange(),
1291 D->getUsingLocation(),
1292 D->getTargetNestedNameDecl(),
1293 D->getDeclName(),
1294 D->isTypeName());
1295
1296 CXXScopeSpec SS;
1297 SS.setScopeRep(D->getTargetNestedNameDecl());
1298 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001299
1300 if (CheckRedeclaration) {
1301 Prev.setHideTags(false);
1302 SemaRef.LookupQualifiedName(Prev, Owner);
1303
1304 // Check for invalid redeclarations.
1305 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1306 D->isTypeName(), SS,
1307 D->getLocation(), Prev))
1308 NewUD->setInvalidDecl();
1309
1310 }
1311
1312 if (!NewUD->isInvalidDecl() &&
1313 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001314 D->getLocation()))
1315 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001316
John McCalled976492009-12-04 22:46:56 +00001317 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1318 NewUD->setAccess(D->getAccess());
1319 Owner->addDecl(NewUD);
1320
John McCall9f54ad42009-12-10 09:41:52 +00001321 // Don't process the shadow decls for an invalid decl.
1322 if (NewUD->isInvalidDecl())
1323 return NewUD;
1324
John McCall323c3102009-12-22 22:26:37 +00001325 bool isFunctionScope = Owner->isFunctionOrMethod();
1326
John McCall9f54ad42009-12-10 09:41:52 +00001327 // Process the shadow decls.
1328 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1329 I != E; ++I) {
1330 UsingShadowDecl *Shadow = *I;
1331 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001332 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1333 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001334 TemplateArgs));
1335
1336 if (CheckRedeclaration &&
1337 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1338 continue;
1339
1340 UsingShadowDecl *InstShadow
1341 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1342 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001343
1344 if (isFunctionScope)
1345 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001346 }
John McCalled976492009-12-04 22:46:56 +00001347
1348 return NewUD;
1349}
1350
1351Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001352 // Ignore these; we handle them in bulk when processing the UsingDecl.
1353 return 0;
John McCalled976492009-12-04 22:46:56 +00001354}
1355
John McCall7ba107a2009-11-18 02:36:19 +00001356Decl * TemplateDeclInstantiator
1357 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001358 NestedNameSpecifier *NNS =
1359 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1360 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001361 TemplateArgs);
1362 if (!NNS)
1363 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001365 CXXScopeSpec SS;
1366 SS.setRange(D->getTargetNestedNameRange());
1367 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
1369 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001370 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001371 D->getUsingLoc(), SS, D->getLocation(),
1372 D->getDeclName(), 0,
1373 /*instantiation*/ true,
1374 /*typename*/ true, D->getTypenameLoc());
1375 if (UD)
John McCalled976492009-12-04 22:46:56 +00001376 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1377
John McCall7ba107a2009-11-18 02:36:19 +00001378 return UD;
1379}
1380
1381Decl * TemplateDeclInstantiator
1382 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1383 NestedNameSpecifier *NNS =
1384 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1385 D->getTargetNestedNameRange(),
1386 TemplateArgs);
1387 if (!NNS)
1388 return 0;
1389
1390 CXXScopeSpec SS;
1391 SS.setRange(D->getTargetNestedNameRange());
1392 SS.setScopeRep(NNS);
1393
1394 NamedDecl *UD =
1395 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1396 D->getUsingLoc(), SS, D->getLocation(),
1397 D->getDeclName(), 0,
1398 /*instantiation*/ true,
1399 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001400 if (UD)
John McCalled976492009-12-04 22:46:56 +00001401 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1402
Anders Carlsson0d8df782009-08-29 19:37:28 +00001403 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001404}
1405
John McCallce3ff2b2009-08-25 22:02:44 +00001406Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001407 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001408 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001409 if (D->isInvalidDecl())
1410 return 0;
1411
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001412 return Instantiator.Visit(D);
1413}
1414
John McCalle29ba202009-08-20 01:44:21 +00001415/// \brief Instantiates a nested template parameter list in the current
1416/// instantiation context.
1417///
1418/// \param L The parameter list to instantiate
1419///
1420/// \returns NULL if there was an error
1421TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001422TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001423 // Get errors for all the parameters before bailing out.
1424 bool Invalid = false;
1425
1426 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001427 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001428 ParamVector Params;
1429 Params.reserve(N);
1430 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1431 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001432 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001433 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001434 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001435 }
1436
1437 // Clean up if we had an error.
1438 if (Invalid) {
1439 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1440 PI != PE; ++PI)
1441 if (*PI)
1442 (*PI)->Destroy(SemaRef.Context);
1443 return NULL;
1444 }
1445
1446 TemplateParameterList *InstL
1447 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1448 L->getLAngleLoc(), &Params.front(), N,
1449 L->getRAngleLoc());
1450 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001451}
John McCalle29ba202009-08-20 01:44:21 +00001452
Douglas Gregored9c0f92009-10-29 00:04:11 +00001453/// \brief Instantiate the declaration of a class template partial
1454/// specialization.
1455///
1456/// \param ClassTemplate the (instantiated) class template that is partially
1457// specialized by the instantiation of \p PartialSpec.
1458///
1459/// \param PartialSpec the (uninstantiated) class template partial
1460/// specialization that we are instantiating.
1461///
1462/// \returns true if there was an error, false otherwise.
1463bool
1464TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1465 ClassTemplateDecl *ClassTemplate,
1466 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001467 // Create a local instantiation scope for this class template partial
1468 // specialization, which will contain the instantiations of the template
1469 // parameters.
1470 Sema::LocalInstantiationScope Scope(SemaRef);
1471
Douglas Gregored9c0f92009-10-29 00:04:11 +00001472 // Substitute into the template parameters of the class template partial
1473 // specialization.
1474 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1475 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1476 if (!InstParams)
1477 return true;
1478
1479 // Substitute into the template arguments of the class template partial
1480 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001481 const TemplateArgumentLoc *PartialSpecTemplateArgs
1482 = PartialSpec->getTemplateArgsAsWritten();
1483 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1484
John McCalld5532b62009-11-23 01:53:49 +00001485 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001486 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001487 TemplateArgumentLoc Loc;
1488 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001489 return true;
John McCalld5532b62009-11-23 01:53:49 +00001490 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001491 }
1492
1493
1494 // Check that the template argument list is well-formed for this
1495 // class template.
1496 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1497 InstTemplateArgs.size());
1498 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1499 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001500 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001501 false,
1502 Converted))
1503 return true;
1504
1505 // Figure out where to insert this class template partial specialization
1506 // in the member template's set of class template partial specializations.
1507 llvm::FoldingSetNodeID ID;
1508 ClassTemplatePartialSpecializationDecl::Profile(ID,
1509 Converted.getFlatArguments(),
1510 Converted.flatSize(),
1511 SemaRef.Context);
1512 void *InsertPos = 0;
1513 ClassTemplateSpecializationDecl *PrevDecl
1514 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1515 InsertPos);
1516
1517 // Build the canonical type that describes the converted template
1518 // arguments of the class template partial specialization.
1519 QualType CanonType
1520 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1521 Converted.getFlatArguments(),
1522 Converted.flatSize());
1523
1524 // Build the fully-sugared type for this class template
1525 // specialization as the user wrote in the specialization
1526 // itself. This means that we'll pretty-print the type retrieved
1527 // from the specialization's declaration the way that the user
1528 // actually wrote the specialization, rather than formatting the
1529 // name based on the "canonical" representation used to store the
1530 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001531 TypeSourceInfo *WrittenTy
1532 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1533 TemplateName(ClassTemplate),
1534 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001535 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001536 CanonType);
1537
1538 if (PrevDecl) {
1539 // We've already seen a partial specialization with the same template
1540 // parameters and template arguments. This can happen, for example, when
1541 // substituting the outer template arguments ends up causing two
1542 // class template partial specializations of a member class template
1543 // to have identical forms, e.g.,
1544 //
1545 // template<typename T, typename U>
1546 // struct Outer {
1547 // template<typename X, typename Y> struct Inner;
1548 // template<typename Y> struct Inner<T, Y>;
1549 // template<typename Y> struct Inner<U, Y>;
1550 // };
1551 //
1552 // Outer<int, int> outer; // error: the partial specializations of Inner
1553 // // have the same signature.
1554 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1555 << WrittenTy;
1556 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1557 << SemaRef.Context.getTypeDeclType(PrevDecl);
1558 return true;
1559 }
1560
1561
1562 // Create the class template partial specialization declaration.
1563 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1564 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1565 PartialSpec->getLocation(),
1566 InstParams,
1567 ClassTemplate,
1568 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001569 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001570 CanonType,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001571 0);
John McCallb6217662010-03-15 10:12:16 +00001572 // Substitute the nested name specifier, if any.
1573 if (SubstQualifier(PartialSpec, InstPartialSpec))
1574 return 0;
1575
Douglas Gregored9c0f92009-10-29 00:04:11 +00001576 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1577 InstPartialSpec->setTypeAsWritten(WrittenTy);
1578
1579 // Add this partial specialization to the set of class template partial
1580 // specializations.
1581 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1582 InsertPos);
1583 return false;
1584}
1585
John McCall21ef0fa2010-03-11 09:03:00 +00001586bool
1587Sema::CheckInstantiatedParams(llvm::SmallVectorImpl<ParmVarDecl*> &Params) {
1588 bool Invalid = false;
1589 for (unsigned i = 0, i_end = Params.size(); i != i_end; ++i)
1590 if (ParmVarDecl *PInst = Params[i]) {
1591 if (PInst->isInvalidDecl())
1592 Invalid = true;
1593 else if (PInst->getType()->isVoidType()) {
1594 Diag(PInst->getLocation(), diag::err_param_with_void_type);
1595 PInst->setInvalidDecl();
1596 Invalid = true;
1597 }
1598 else if (RequireNonAbstractType(PInst->getLocation(),
1599 PInst->getType(),
1600 diag::err_abstract_type_in_decl,
1601 Sema::AbstractParamType)) {
1602 PInst->setInvalidDecl();
1603 Invalid = true;
1604 }
1605 }
1606 return Invalid;
1607}
Douglas Gregor5545e162009-03-24 00:38:23 +00001608
John McCall21ef0fa2010-03-11 09:03:00 +00001609TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001610TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001611 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001612 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1613 assert(OldTInfo && "substituting function without type source info");
1614 assert(Params.empty() && "parameter vector is non-empty at start");
1615 TypeSourceInfo *NewTInfo = SemaRef.SubstType(OldTInfo, TemplateArgs,
1616 D->getTypeSpecStartLoc(),
1617 D->getDeclName());
1618 if (!NewTInfo)
1619 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001620
John McCall21ef0fa2010-03-11 09:03:00 +00001621 // Get parameters from the new type info.
1622 TypeLoc NewTL = NewTInfo->getTypeLoc();
1623 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1624 assert(NewProtoLoc && "Missing prototype?");
1625 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i)
1626 Params.push_back(NewProtoLoc->getArg(i));
Douglas Gregor5545e162009-03-24 00:38:23 +00001627
John McCall21ef0fa2010-03-11 09:03:00 +00001628 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001629}
1630
Mike Stump1eb44332009-09-09 15:08:12 +00001631/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001632/// declaration (New) from the corresponding fields of its template (Tmpl).
1633///
1634/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001635bool
1636TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001637 FunctionDecl *Tmpl) {
1638 if (Tmpl->isDeleted())
1639 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Douglas Gregorcca9e962009-07-01 22:01:06 +00001641 // If we are performing substituting explicitly-specified template arguments
1642 // or deduced template arguments into a function template and we reach this
1643 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001644 // to keeping the new function template specialization. We therefore
1645 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001646 // into a template instantiation for this specific function template
1647 // specialization, which is not a SFINAE context, so that we diagnose any
1648 // further errors in the declaration itself.
1649 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1650 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1651 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1652 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001653 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001654 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001656 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001657 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001658 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1659 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001660 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001661 }
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001664 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1665 assert(Proto && "Function template without prototype?");
1666
1667 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1668 Proto->getNoReturnAttr()) {
1669 // The function has an exception specification or a "noreturn"
1670 // attribute. Substitute into each of the exception types.
1671 llvm::SmallVector<QualType, 4> Exceptions;
1672 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1673 // FIXME: Poor location information!
1674 QualType T
1675 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1676 New->getLocation(), New->getDeclName());
1677 if (T.isNull() ||
1678 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1679 continue;
1680
1681 Exceptions.push_back(T);
1682 }
1683
1684 // Rebuild the function type
1685
1686 const FunctionProtoType *NewProto
1687 = New->getType()->getAs<FunctionProtoType>();
1688 assert(NewProto && "Template instantiation without function prototype?");
1689 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1690 NewProto->arg_type_begin(),
1691 NewProto->getNumArgs(),
1692 NewProto->isVariadic(),
1693 NewProto->getTypeQuals(),
1694 Proto->hasExceptionSpec(),
1695 Proto->hasAnyExceptionSpec(),
1696 Exceptions.size(),
1697 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001698 Proto->getNoReturnAttr(),
1699 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001700 }
1701
Douglas Gregore53060f2009-06-25 22:08:12 +00001702 return false;
1703}
1704
Douglas Gregor5545e162009-03-24 00:38:23 +00001705/// \brief Initializes common fields of an instantiated method
1706/// declaration (New) from the corresponding fields of its template
1707/// (Tmpl).
1708///
1709/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001710bool
1711TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001712 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001713 if (InitFunctionInstantiation(New, Tmpl))
1714 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Douglas Gregor5545e162009-03-24 00:38:23 +00001716 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1717 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001718 if (Tmpl->isVirtualAsWritten())
1719 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001720
1721 // FIXME: attributes
1722 // FIXME: New needs a pointer to Tmpl
1723 return false;
1724}
Douglas Gregora58861f2009-05-13 20:28:22 +00001725
1726/// \brief Instantiate the definition of the given function from its
1727/// template.
1728///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001729/// \param PointOfInstantiation the point at which the instantiation was
1730/// required. Note that this is not precisely a "point of instantiation"
1731/// for the function, but it's close.
1732///
Douglas Gregora58861f2009-05-13 20:28:22 +00001733/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001734/// function template specialization or member function of a class template
1735/// specialization.
1736///
1737/// \param Recursive if true, recursively instantiates any functions that
1738/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001739///
1740/// \param DefinitionRequired if true, then we are performing an explicit
1741/// instantiation where the body of the function is required. Complain if
1742/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001743void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001744 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001745 bool Recursive,
1746 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001747 if (Function->isInvalidDecl())
1748 return;
1749
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001750 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001752 // Never instantiate an explicit specialization.
1753 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1754 return;
1755
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001756 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001757 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001758 Stmt *Pattern = 0;
1759 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001760 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001761
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001762 if (!Pattern) {
1763 if (DefinitionRequired) {
1764 if (Function->getPrimaryTemplate())
1765 Diag(PointOfInstantiation,
1766 diag::err_explicit_instantiation_undefined_func_template)
1767 << Function->getPrimaryTemplate();
1768 else
1769 Diag(PointOfInstantiation,
1770 diag::err_explicit_instantiation_undefined_member)
1771 << 1 << Function->getDeclName() << Function->getDeclContext();
1772
1773 if (PatternDecl)
1774 Diag(PatternDecl->getLocation(),
1775 diag::note_explicit_instantiation_here);
1776 }
1777
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001778 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001779 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001780
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001781 // C++0x [temp.explicit]p9:
1782 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001783 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001784 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001785 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001786 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001787 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001788 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001790 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1791 if (Inst)
1792 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001793
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001794 // If we're performing recursive template instantiation, create our own
1795 // queue of pending implicit instantiations that we will instantiate later,
1796 // while we're still within our own instantiation context.
1797 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1798 if (Recursive)
1799 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001801 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1802
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001803 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001804 // recorded, unless we're actually a member function within a local
1805 // class, in which case we need to merge our results with the parent
1806 // scope (of the enclosing function).
1807 bool MergeWithParentScope = false;
1808 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1809 MergeWithParentScope = Rec->isLocalClass();
1810
1811 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001813 // Introduce the instantiated function parameters into the local
1814 // instantiation scope.
1815 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1816 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1817 Function->getParamDecl(I));
1818
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001819 // Enter the scope of this instantiation. We don't use
1820 // PushDeclContext because we don't have a scope.
1821 DeclContext *PreviousContext = CurContext;
1822 CurContext = Function;
1823
Mike Stump1eb44332009-09-09 15:08:12 +00001824 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001825 getTemplateInstantiationArgs(Function);
1826
1827 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001828 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001829 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1830 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1831 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001832 }
1833
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001834 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001835 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001836
Douglas Gregor52604ab2009-09-11 21:19:12 +00001837 if (Body.isInvalid())
1838 Function->setInvalidDecl();
1839
Mike Stump1eb44332009-09-09 15:08:12 +00001840 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001841 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001842
John McCall0c01d182010-03-24 05:22:00 +00001843 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
1844
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001845 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001846
1847 DeclGroupRef DG(Function);
1848 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Douglas Gregor60406be2010-01-16 22:29:39 +00001850 // This class may have local implicit instantiations that need to be
1851 // instantiation within this scope.
1852 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1853 Scope.Exit();
1854
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001855 if (Recursive) {
1856 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001857 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001858 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001860 // Restore the set of pending implicit instantiations.
1861 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1862 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001863}
1864
1865/// \brief Instantiate the definition of the given variable from its
1866/// template.
1867///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001868/// \param PointOfInstantiation the point at which the instantiation was
1869/// required. Note that this is not precisely a "point of instantiation"
1870/// for the function, but it's close.
1871///
1872/// \param Var the already-instantiated declaration of a static member
1873/// variable of a class template specialization.
1874///
1875/// \param Recursive if true, recursively instantiates any functions that
1876/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001877///
1878/// \param DefinitionRequired if true, then we are performing an explicit
1879/// instantiation where an out-of-line definition of the member variable
1880/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001881void Sema::InstantiateStaticDataMemberDefinition(
1882 SourceLocation PointOfInstantiation,
1883 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001884 bool Recursive,
1885 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001886 if (Var->isInvalidDecl())
1887 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor7caa6822009-07-24 20:34:43 +00001889 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001890 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001891 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001892 assert(Def->isStaticDataMember() && "Not a static data member?");
1893 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregor0d035142009-10-27 18:42:08 +00001895 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001896 // We did not find an out-of-line definition of this static data member,
1897 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001898 // instantiate this definition (or provide a specialization for it) in
1899 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001900 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001901 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001902 Diag(PointOfInstantiation,
1903 diag::err_explicit_instantiation_undefined_member)
1904 << 2 << Var->getDeclName() << Var->getDeclContext();
1905 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1906 }
1907
Douglas Gregor7caa6822009-07-24 20:34:43 +00001908 return;
1909 }
1910
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001911 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001912 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001913 return;
1914
1915 // C++0x [temp.explicit]p9:
1916 // Except for inline functions, other explicit instantiation declarations
1917 // have the effect of suppressing the implicit instantiation of the entity
1918 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001919 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001920 == TSK_ExplicitInstantiationDeclaration)
1921 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Douglas Gregor7caa6822009-07-24 20:34:43 +00001923 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1924 if (Inst)
1925 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Douglas Gregor7caa6822009-07-24 20:34:43 +00001927 // If we're performing recursive template instantiation, create our own
1928 // queue of pending implicit instantiations that we will instantiate later,
1929 // while we're still within our own instantiation context.
1930 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1931 if (Recursive)
1932 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Douglas Gregor7caa6822009-07-24 20:34:43 +00001934 // Enter the scope of this instantiation. We don't use
1935 // PushDeclContext because we don't have a scope.
1936 DeclContext *PreviousContext = CurContext;
1937 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001939 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001940 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001941 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001942 CurContext = PreviousContext;
1943
1944 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001945 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1946 assert(MSInfo && "Missing member specialization information?");
1947 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1948 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001949 DeclGroupRef DG(Var);
1950 Consumer.HandleTopLevelDecl(DG);
1951 }
Mike Stump1eb44332009-09-09 15:08:12 +00001952
Douglas Gregor7caa6822009-07-24 20:34:43 +00001953 if (Recursive) {
1954 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001955 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001956 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Douglas Gregor7caa6822009-07-24 20:34:43 +00001958 // Restore the set of pending implicit instantiations.
1959 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001960 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001961}
Douglas Gregor815215d2009-05-27 05:35:12 +00001962
Anders Carlsson09025312009-08-29 05:16:22 +00001963void
1964Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1965 const CXXConstructorDecl *Tmpl,
1966 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Anders Carlsson09025312009-08-29 05:16:22 +00001968 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001969 bool AnyErrors = false;
1970
Anders Carlsson09025312009-08-29 05:16:22 +00001971 // Instantiate all the initializers.
1972 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001973 InitsEnd = Tmpl->init_end();
1974 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001975 CXXBaseOrMemberInitializer *Init = *Inits;
1976
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001977 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00001978 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001979 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001981 // Instantiate the initializer.
1982 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
1983 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
1984 AnyErrors = true;
1985 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00001986 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001987
Anders Carlsson09025312009-08-29 05:16:22 +00001988 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001989 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001990 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001991 TemplateArgs,
1992 Init->getSourceLocation(),
1993 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001994 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001995 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001996 New->setInvalidDecl();
1997 continue;
1998 }
1999
John McCalla93c9342009-12-07 02:54:59 +00002000 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002001 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002002 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002003 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002004 Init->getRParenLoc(),
2005 New->getParent());
2006 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002007 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002009 // Is this an anonymous union?
2010 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002011 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2012 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002013 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002014 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2015 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002016 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002017
2018 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002019 NewArgs.size(),
2020 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002021 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002022 Init->getRParenLoc());
2023 }
2024
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002025 if (NewInit.isInvalid()) {
2026 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002027 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002028 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002029 // FIXME: It would be nice if ASTOwningVector had a release function.
2030 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Anders Carlsson09025312009-08-29 05:16:22 +00002032 NewInits.push_back((MemInitTy *)NewInit.get());
2033 }
2034 }
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Anders Carlsson09025312009-08-29 05:16:22 +00002036 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002037 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002038 /*FIXME: ColonLoc */
2039 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002040 NewInits.data(), NewInits.size(),
2041 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002042}
2043
John McCall52a575a2009-08-29 08:11:13 +00002044// TODO: this could be templated if the various decl types used the
2045// same method name.
2046static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2047 ClassTemplateDecl *Instance) {
2048 Pattern = Pattern->getCanonicalDecl();
2049
2050 do {
2051 Instance = Instance->getCanonicalDecl();
2052 if (Pattern == Instance) return true;
2053 Instance = Instance->getInstantiatedFromMemberTemplate();
2054 } while (Instance);
2055
2056 return false;
2057}
2058
Douglas Gregor0d696532009-09-28 06:34:35 +00002059static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2060 FunctionTemplateDecl *Instance) {
2061 Pattern = Pattern->getCanonicalDecl();
2062
2063 do {
2064 Instance = Instance->getCanonicalDecl();
2065 if (Pattern == Instance) return true;
2066 Instance = Instance->getInstantiatedFromMemberTemplate();
2067 } while (Instance);
2068
2069 return false;
2070}
2071
Douglas Gregored9c0f92009-10-29 00:04:11 +00002072static bool
2073isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2074 ClassTemplatePartialSpecializationDecl *Instance) {
2075 Pattern
2076 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2077 do {
2078 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2079 Instance->getCanonicalDecl());
2080 if (Pattern == Instance)
2081 return true;
2082 Instance = Instance->getInstantiatedFromMember();
2083 } while (Instance);
2084
2085 return false;
2086}
2087
John McCall52a575a2009-08-29 08:11:13 +00002088static bool isInstantiationOf(CXXRecordDecl *Pattern,
2089 CXXRecordDecl *Instance) {
2090 Pattern = Pattern->getCanonicalDecl();
2091
2092 do {
2093 Instance = Instance->getCanonicalDecl();
2094 if (Pattern == Instance) return true;
2095 Instance = Instance->getInstantiatedFromMemberClass();
2096 } while (Instance);
2097
2098 return false;
2099}
2100
2101static bool isInstantiationOf(FunctionDecl *Pattern,
2102 FunctionDecl *Instance) {
2103 Pattern = Pattern->getCanonicalDecl();
2104
2105 do {
2106 Instance = Instance->getCanonicalDecl();
2107 if (Pattern == Instance) return true;
2108 Instance = Instance->getInstantiatedFromMemberFunction();
2109 } while (Instance);
2110
2111 return false;
2112}
2113
2114static bool isInstantiationOf(EnumDecl *Pattern,
2115 EnumDecl *Instance) {
2116 Pattern = Pattern->getCanonicalDecl();
2117
2118 do {
2119 Instance = Instance->getCanonicalDecl();
2120 if (Pattern == Instance) return true;
2121 Instance = Instance->getInstantiatedFromMemberEnum();
2122 } while (Instance);
2123
2124 return false;
2125}
2126
John McCalled976492009-12-04 22:46:56 +00002127static bool isInstantiationOf(UsingShadowDecl *Pattern,
2128 UsingShadowDecl *Instance,
2129 ASTContext &C) {
2130 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2131}
2132
2133static bool isInstantiationOf(UsingDecl *Pattern,
2134 UsingDecl *Instance,
2135 ASTContext &C) {
2136 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2137}
2138
John McCall7ba107a2009-11-18 02:36:19 +00002139static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2140 UsingDecl *Instance,
2141 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002142 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002143}
2144
2145static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002146 UsingDecl *Instance,
2147 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002148 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002149}
2150
John McCall52a575a2009-08-29 08:11:13 +00002151static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2152 VarDecl *Instance) {
2153 assert(Instance->isStaticDataMember());
2154
2155 Pattern = Pattern->getCanonicalDecl();
2156
2157 do {
2158 Instance = Instance->getCanonicalDecl();
2159 if (Pattern == Instance) return true;
2160 Instance = Instance->getInstantiatedFromStaticDataMember();
2161 } while (Instance);
2162
2163 return false;
2164}
2165
John McCalled976492009-12-04 22:46:56 +00002166// Other is the prospective instantiation
2167// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002168static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002169 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002170 if (UnresolvedUsingTypenameDecl *UUD
2171 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2172 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2173 return isInstantiationOf(UUD, UD, Ctx);
2174 }
2175 }
2176
2177 if (UnresolvedUsingValueDecl *UUD
2178 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002179 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2180 return isInstantiationOf(UUD, UD, Ctx);
2181 }
2182 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002183
Anders Carlsson0d8df782009-08-29 19:37:28 +00002184 return false;
2185 }
Mike Stump1eb44332009-09-09 15:08:12 +00002186
John McCall52a575a2009-08-29 08:11:13 +00002187 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2188 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002189
John McCall52a575a2009-08-29 08:11:13 +00002190 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2191 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002192
John McCall52a575a2009-08-29 08:11:13 +00002193 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2194 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002195
Douglas Gregor7caa6822009-07-24 20:34:43 +00002196 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002197 if (Var->isStaticDataMember())
2198 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2199
2200 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2201 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002202
Douglas Gregor0d696532009-09-28 06:34:35 +00002203 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2204 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2205
Douglas Gregored9c0f92009-10-29 00:04:11 +00002206 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2207 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2208 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2209 PartialSpec);
2210
Anders Carlssond8b285f2009-09-01 04:26:58 +00002211 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2212 if (!Field->getDeclName()) {
2213 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002214 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002215 cast<FieldDecl>(D);
2216 }
2217 }
Mike Stump1eb44332009-09-09 15:08:12 +00002218
John McCalled976492009-12-04 22:46:56 +00002219 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2220 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2221
2222 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2223 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2224
Douglas Gregor815215d2009-05-27 05:35:12 +00002225 return D->getDeclName() && isa<NamedDecl>(Other) &&
2226 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2227}
2228
2229template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002230static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002231 NamedDecl *D,
2232 ForwardIterator first,
2233 ForwardIterator last) {
2234 for (; first != last; ++first)
2235 if (isInstantiationOf(Ctx, D, *first))
2236 return cast<NamedDecl>(*first);
2237
2238 return 0;
2239}
2240
John McCall02cace72009-08-28 07:59:38 +00002241/// \brief Finds the instantiation of the given declaration context
2242/// within the current instantiation.
2243///
2244/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002245DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002246 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002247 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002248 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002249 return cast_or_null<DeclContext>(ID);
2250 } else return DC;
2251}
2252
Douglas Gregored961e72009-05-27 17:54:46 +00002253/// \brief Find the instantiation of the given declaration within the
2254/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002255///
2256/// This routine is intended to be used when \p D is a declaration
2257/// referenced from within a template, that needs to mapped into the
2258/// corresponding declaration within an instantiation. For example,
2259/// given:
2260///
2261/// \code
2262/// template<typename T>
2263/// struct X {
2264/// enum Kind {
2265/// KnownValue = sizeof(T)
2266/// };
2267///
2268/// bool getKind() const { return KnownValue; }
2269/// };
2270///
2271/// template struct X<int>;
2272/// \endcode
2273///
2274/// In the instantiation of X<int>::getKind(), we need to map the
2275/// EnumConstantDecl for KnownValue (which refers to
2276/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002277/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2278/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002279NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002280 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002281 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002282 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002283 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002284 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002285 // D is a local of some kind. Look into the map of local
2286 // declarations to their instantiations.
2287 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2288 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002289
Douglas Gregore95b4092009-09-16 18:34:49 +00002290 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2291 if (!Record->isDependentContext())
2292 return D;
2293
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002294 // If the RecordDecl is actually the injected-class-name or a
2295 // "templated" declaration for a class template, class template
2296 // partial specialization, or a member class of a class template,
2297 // substitute into the injected-class-name of the class template
2298 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002299 QualType T;
2300 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2301
2302 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002303 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002304 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2305 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002306 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002307
2308 // If we call SubstType with an InjectedClassNameType here we
2309 // can end up in an infinite loop.
2310 T = Context.getTypeDeclType(Record);
2311 assert(isa<InjectedClassNameType>(T) &&
2312 "type of partial specialization is not an InjectedClassNameType");
2313 T = cast<InjectedClassNameType>(T)->getUnderlyingType();
2314 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002315
2316 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002317 // Substitute into the injected-class-name to get the type
2318 // corresponding to the instantiation we want, which may also be
2319 // the current instantiation (if we're in a template
2320 // definition). This substitution should never fail, since we
2321 // know we can instantiate the injected-class-name or we
2322 // wouldn't have gotten to the injected-class-name!
2323
2324 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2325 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002326 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2327 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2328
2329 if (!T->isDependentType()) {
2330 assert(T->isRecordType() && "Instantiation must produce a record type");
2331 return T->getAs<RecordType>()->getDecl();
2332 }
2333
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002334 // We are performing "partial" template instantiation to create
2335 // the member declarations for the members of a class template
2336 // specialization. Therefore, D is actually referring to something
2337 // in the current instantiation. Look through the current
2338 // context, which contains actual instantiations, to find the
2339 // instantiation of the "current instantiation" that D refers
2340 // to.
2341 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002342 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002343 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002344 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002345 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002346 if (isInstantiationOf(ClassTemplate,
2347 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002348 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002349
2350 if (!DC->isDependentContext())
2351 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002352 }
2353
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002354 // We're performing "instantiation" of a member of the current
2355 // instantiation while we are type-checking the
2356 // definition. Compute the declaration context and return that.
2357 assert(!SawNonDependentContext &&
2358 "No dependent context while instantiating record");
2359 DeclContext *DC = computeDeclContext(T);
2360 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002361 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002362 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002363 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002364
Douglas Gregore95b4092009-09-16 18:34:49 +00002365 // Fall through to deal with other dependent record types (e.g.,
2366 // anonymous unions in class templates).
2367 }
John McCall52a575a2009-08-29 08:11:13 +00002368
Douglas Gregore95b4092009-09-16 18:34:49 +00002369 if (!ParentDC->isDependentContext())
2370 return D;
2371
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002372 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002373 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002374 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Douglas Gregor815215d2009-05-27 05:35:12 +00002376 if (ParentDC != D->getDeclContext()) {
2377 // We performed some kind of instantiation in the parent context,
2378 // so now we need to look into the instantiated parent context to
2379 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002380
John McCall3cb0ebd2010-03-10 03:28:59 +00002381 // If our context used to be dependent, we may need to instantiate
2382 // it before performing lookup into that context.
2383 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002384 if (!Spec->isDependentContext()) {
2385 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002386 const RecordType *Tag = T->getAs<RecordType>();
2387 assert(Tag && "type of non-dependent record is not a RecordType");
2388 if (!Tag->isBeingDefined() &&
2389 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2390 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002391 }
2392 }
2393
Douglas Gregor815215d2009-05-27 05:35:12 +00002394 NamedDecl *Result = 0;
2395 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002396 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002397 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2398 } else {
2399 // Since we don't have a name for the entity we're looking for,
2400 // our only option is to walk through all of the declarations to
2401 // find that name. This will occur in a few cases:
2402 //
2403 // - anonymous struct/union within a template
2404 // - unnamed class/struct/union/enum within a template
2405 //
2406 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002407 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002408 ParentDC->decls_begin(),
2409 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
John McCall9f54ad42009-12-10 09:41:52 +00002412 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002413 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2414 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002415 && "Unable to find instantiation of declaration!");
2416
Douglas Gregor815215d2009-05-27 05:35:12 +00002417 D = Result;
2418 }
2419
Douglas Gregor815215d2009-05-27 05:35:12 +00002420 return D;
2421}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002422
Mike Stump1eb44332009-09-09 15:08:12 +00002423/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002424/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002425void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2426 while (!PendingLocalImplicitInstantiations.empty() ||
2427 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2428 PendingImplicitInstantiation Inst;
2429
2430 if (PendingLocalImplicitInstantiations.empty()) {
2431 Inst = PendingImplicitInstantiations.front();
2432 PendingImplicitInstantiations.pop_front();
2433 } else {
2434 Inst = PendingLocalImplicitInstantiations.front();
2435 PendingLocalImplicitInstantiations.pop_front();
2436 }
Mike Stump1eb44332009-09-09 15:08:12 +00002437
Douglas Gregor7caa6822009-07-24 20:34:43 +00002438 // Instantiate function definitions
2439 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002440 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002441 Function->getLocation(), *this,
2442 Context.getSourceManager(),
2443 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002445 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002446 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002447 continue;
2448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Douglas Gregor7caa6822009-07-24 20:34:43 +00002450 // Instantiate static data member definitions.
2451 VarDecl *Var = cast<VarDecl>(Inst.first);
2452 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002453
Chandler Carruth291b4412010-02-13 10:17:50 +00002454 // Don't try to instantiate declarations if the most recent redeclaration
2455 // is invalid.
2456 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2457 continue;
2458
2459 // Check if the most recent declaration has changed the specialization kind
2460 // and removed the need for implicit instantiation.
2461 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2462 case TSK_Undeclared:
2463 assert(false && "Cannot instantitiate an undeclared specialization.");
2464 case TSK_ExplicitInstantiationDeclaration:
2465 case TSK_ExplicitInstantiationDefinition:
2466 case TSK_ExplicitSpecialization:
2467 continue; // No longer need implicit instantiation.
2468 case TSK_ImplicitInstantiation:
2469 break;
2470 }
2471
Mike Stump1eb44332009-09-09 15:08:12 +00002472 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002473 Var->getLocation(), *this,
2474 Context.getSourceManager(),
2475 "instantiating static data member "
2476 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002477
Douglas Gregor7caa6822009-07-24 20:34:43 +00002478 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002479 }
2480}
John McCall0c01d182010-03-24 05:22:00 +00002481
2482void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2483 const MultiLevelTemplateArgumentList &TemplateArgs) {
2484 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2485 E = Pattern->ddiag_end(); I != E; ++I) {
2486 DependentDiagnostic *DD = *I;
2487
2488 switch (DD->getKind()) {
2489 case DependentDiagnostic::Access:
2490 HandleDependentAccessCheck(*DD, TemplateArgs);
2491 break;
2492 }
2493 }
2494}