blob: 7b0e88d16347fcb93c93e7e37dc3566d075c1f42 [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)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000289 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
290 if (InstantiateInitializationArguments(S,
291 Construct->getArgs(),
292 Construct->getNumArgs(),
293 TemplateArgs,
294 CommaLocs, NewArgs))
295 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000296
Douglas Gregor28329e52010-03-24 21:22:47 +0000297 // FIXME: Fake locations!
298 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
299 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
300 return false;
301 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000302 }
303
304 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
305 if (Result.isInvalid())
306 return true;
307
308 NewArgs.push_back(Result.takeAs<Expr>());
309 return false;
310}
311
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000312Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000313 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000314 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000315 TemplateArgs,
316 D->getTypeSpecStartLoc(),
317 D->getDeclName());
318 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000319 return 0;
320
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000321 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000322 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
323 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000324 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000325 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000326 Var->setThreadSpecified(D->isThreadSpecified());
327 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
328 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000329
John McCallb6217662010-03-15 10:12:16 +0000330 // Substitute the nested name specifier, if any.
331 if (SubstQualifier(D, Var))
332 return 0;
333
Mike Stump1eb44332009-09-09 15:08:12 +0000334 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000335 // out-of-line, the instantiation will have the same lexical
336 // context (which will be a namespace scope) as the template.
337 if (D->isOutOfLine())
338 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000339
John McCall46460a62010-01-20 21:53:11 +0000340 Var->setAccess(D->getAccess());
341
Mike Stump390b4cc2009-05-16 07:39:55 +0000342 // FIXME: In theory, we could have a previous declaration for variables that
343 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000344 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000345 // FIXME: having to fake up a LookupResult is dumb.
346 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000347 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000348 if (D->isStaticDataMember())
349 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000350 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Douglas Gregor7caa6822009-07-24 20:34:43 +0000352 if (D->isOutOfLine()) {
353 D->getLexicalDeclContext()->addDecl(Var);
354 Owner->makeDeclVisibleInContext(Var);
355 } else {
356 Owner->addDecl(Var);
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000359 // Link instantiations of static data members back to the template from
360 // which they were instantiated.
361 if (Var->isStaticDataMember())
362 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000363 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000364
Douglas Gregor60c93c92010-02-09 07:26:29 +0000365 if (Var->getAnyInitializer()) {
366 // We already have an initializer in the class.
367 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000368 if (Var->isStaticDataMember() && !D->isOutOfLine())
369 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
370 else
371 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
372
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000373 // Instantiate the initializer.
374 SourceLocation LParenLoc, RParenLoc;
375 llvm::SmallVector<SourceLocation, 4> CommaLocs;
376 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
377 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
378 CommaLocs, InitArgs, RParenLoc)) {
379 // Attach the initializer to the declaration.
380 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000381 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000382 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000383 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000384 move_arg(InitArgs),
385 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000386 RParenLoc);
387 } else if (InitArgs.size() == 1) {
388 Expr *Init = (Expr*)(InitArgs.take()[0]);
389 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
390 SemaRef.Owned(Init),
391 false);
392 } else {
393 assert(InitArgs.size() == 0);
394 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000395 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000396 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000397 // FIXME: Not too happy about invalidating the declaration
398 // because of a bogus initializer.
399 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000400 }
401
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000402 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000403 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
404 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000405
406 return Var;
407}
408
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000409Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
410 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000411 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000412 if (DI->getType()->isDependentType()) {
413 DI = SemaRef.SubstType(DI, TemplateArgs,
414 D->getLocation(), D->getDeclName());
415 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000416 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000417 Invalid = true;
418 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000419 // C++ [temp.arg.type]p3:
420 // If a declaration acquires a function type through a type
421 // dependent on a template-parameter and this causes a
422 // declaration that does not use the syntactic form of a
423 // function declarator to have function type, the program is
424 // ill-formed.
425 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000426 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000427 Invalid = true;
428 }
429 }
430
431 Expr *BitWidth = D->getBitWidth();
432 if (Invalid)
433 BitWidth = 0;
434 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000435 // The bit-width expression is not potentially evaluated.
436 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000438 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000439 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000440 if (InstantiatedBitWidth.isInvalid()) {
441 Invalid = true;
442 BitWidth = 0;
443 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000444 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000445 }
446
John McCall07fb6be2009-10-22 23:33:21 +0000447 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
448 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000449 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000450 D->getLocation(),
451 D->isMutable(),
452 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000453 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000454 D->getAccess(),
455 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000456 if (!Field) {
457 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000458 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000461 InstantiateAttrs(D, Field);
462
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000463 if (Invalid)
464 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000466 if (!Field->getDeclName()) {
467 // Keep track of where this decl came from.
468 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000471 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000472 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000473 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474
475 return Field;
476}
477
John McCall02cace72009-08-28 07:59:38 +0000478Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
479 FriendDecl::FriendUnion FU;
480
481 // Handle friend type expressions by simply substituting template
482 // parameters into the pattern type.
483 if (Type *Ty = D->getFriendType()) {
484 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
485 D->getLocation(), DeclarationName());
486 if (T.isNull()) return 0;
487
488 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
489 FU = T.getTypePtr();
490
491 // Handle everything else by appropriate substitution.
492 } else {
493 NamedDecl *ND = D->getFriendDecl();
494 assert(ND && "friend decl must be a decl or a type!");
495
Douglas Gregora735b202009-10-13 14:39:41 +0000496 // FIXME: We have a problem here, because the nested call to Visit(ND)
497 // will inject the thing that the friend references into the current
498 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000499 Decl *NewND;
500
501 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000502 if (ND->getDeclContext()->isRecord()) {
Bob Wilsone131c572010-03-25 04:40:43 +0000503 if (!ND->getDeclContext()->isDependentContext()) {
504 NewND = SemaRef.FindInstantiatedDecl(D->getLocation(), ND,
505 TemplateArgs);
506 } else {
507 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
508 // templated friend declarations. This doesn't produce a correct AST;
509 // however this is sufficient for some AST analysis. The real solution
510 // must be put in place during the pending rewrite. See PR5848.
511 return 0;
512 }
Douglas Gregor63644fa2010-02-07 10:31:35 +0000513 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000514 // Totally egregious hack to work around PR5866
515 return 0;
Bob Wilsone131c572010-03-25 04:40:43 +0000516 } else
John McCalle129d442009-12-17 23:21:11 +0000517 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000518 if (!NewND) return 0;
519
520 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
John McCall02cace72009-08-28 07:59:38 +0000523 FriendDecl *FD =
524 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
525 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000526 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000527 Owner->addDecl(FD);
528 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000529}
530
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000531Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
532 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregorac7610d2009-06-22 20:57:11 +0000534 // The expression in a static assertion is not potentially evaluated.
535 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000537 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000538 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000539 if (InstantiatedAssertExpr.isInvalid())
540 return 0;
541
Douglas Gregor43d9d922009-08-08 01:41:12 +0000542 OwningExprResult Message(SemaRef, D->getMessage());
543 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000544 Decl *StaticAssert
545 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000546 move(InstantiatedAssertExpr),
547 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000548 return StaticAssert;
549}
550
551Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000552 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000553 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000554 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000555 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000556 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000557 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000558 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000559 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000560 Enum->startDefinition();
561
Douglas Gregor96084f12010-03-01 19:00:07 +0000562 if (D->getDeclContext()->isFunctionOrMethod())
563 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
564
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000565 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000566
567 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000568 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
569 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000570 EC != ECEnd; ++EC) {
571 // The specified value for the enumerator.
572 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000573 if (Expr *UninstValue = EC->getInitExpr()) {
574 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000575 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000576 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000577
John McCallce3ff2b2009-08-25 22:02:44 +0000578 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000579 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000580
581 // Drop the initial value and continue.
582 bool isInvalid = false;
583 if (Value.isInvalid()) {
584 Value = SemaRef.Owned((Expr *)0);
585 isInvalid = true;
586 }
587
Mike Stump1eb44332009-09-09 15:08:12 +0000588 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000589 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
590 EC->getLocation(), EC->getIdentifier(),
591 move(Value));
592
593 if (isInvalid) {
594 if (EnumConst)
595 EnumConst->setInvalidDecl();
596 Enum->setInvalidDecl();
597 }
598
599 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000600 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000602 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000603 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000604
605 if (D->getDeclContext()->isFunctionOrMethod()) {
606 // If the enumeration is within a function or method, record the enum
607 // constant as a local.
608 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
609 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000610 }
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000613 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000614 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000615 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
616 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000617 &Enumerators[0], Enumerators.size(),
618 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000619
620 return Enum;
621}
622
Douglas Gregor6477b692009-03-25 15:04:13 +0000623Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
624 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
625 return 0;
626}
627
Douglas Gregored9c0f92009-10-29 00:04:11 +0000628namespace {
629 class SortDeclByLocation {
630 SourceManager &SourceMgr;
631
632 public:
633 explicit SortDeclByLocation(SourceManager &SourceMgr)
634 : SourceMgr(SourceMgr) { }
635
636 bool operator()(const Decl *X, const Decl *Y) const {
637 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
638 Y->getLocation());
639 }
640 };
641}
642
John McCalle29ba202009-08-20 01:44:21 +0000643Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000644 // Create a local instantiation scope for this class template, which
645 // will contain the instantiations of the template parameters.
646 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000647 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000648 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000649 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000650 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000651
652 CXXRecordDecl *Pattern = D->getTemplatedDecl();
653 CXXRecordDecl *RecordInst
Bob Wilsone131c572010-03-25 04:40:43 +0000654 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
John McCalle29ba202009-08-20 01:44:21 +0000655 Pattern->getLocation(), Pattern->getIdentifier(),
Bob Wilsone131c572010-03-25 04:40:43 +0000656 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000657 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000658
Bob Wilsone131c572010-03-25 04:40:43 +0000659 // Substitute the nested name specifier, if any.
660 if (SubstQualifier(Pattern, RecordInst))
661 return 0;
John McCallb6217662010-03-15 10:12:16 +0000662
John McCalle29ba202009-08-20 01:44:21 +0000663 ClassTemplateDecl *Inst
Bob Wilsone131c572010-03-25 04:40:43 +0000664 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
665 D->getIdentifier(), InstParams, RecordInst, 0);
John McCalle29ba202009-08-20 01:44:21 +0000666 RecordInst->setDescribedClassTemplate(Inst);
Bob Wilsone131c572010-03-25 04:40:43 +0000667 if (D->getFriendObjectKind())
668 Inst->setObjectOfFriendDecl(true);
669 else
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000670 Inst->setAccess(D->getAccess());
Bob Wilsone131c572010-03-25 04:40:43 +0000671 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000672
673 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000674 SemaRef.Context.getInjectedClassNameType(RecordInst,
675 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
Douglas Gregorf0510d42009-10-12 23:11:44 +0000676
Douglas Gregor259571e2009-10-30 22:42:42 +0000677 // Finish handling of friends.
Bob Wilsone131c572010-03-25 04:40:43 +0000678 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000679 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000680 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000681
John McCall46460a62010-01-20 21:53:11 +0000682 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000683 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000684
685 // First, we sort the partial specializations by location, so
686 // that we instantiate them in the order they were declared.
687 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
688 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
689 P = D->getPartialSpecializations().begin(),
690 PEnd = D->getPartialSpecializations().end();
691 P != PEnd; ++P)
692 PartialSpecs.push_back(&*P);
693 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
694 SortDeclByLocation(SemaRef.SourceMgr));
695
696 // Instantiate all of the partial specializations of this member class
697 // template.
698 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
699 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
700
John McCalle29ba202009-08-20 01:44:21 +0000701 return Inst;
702}
703
Douglas Gregord60e1052009-08-27 16:57:43 +0000704Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000705TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
706 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000707 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
708
709 // Lookup the already-instantiated declaration in the instantiation
710 // of the class template and return that.
711 DeclContext::lookup_result Found
712 = Owner->lookup(ClassTemplate->getDeclName());
713 if (Found.first == Found.second)
714 return 0;
715
716 ClassTemplateDecl *InstClassTemplate
717 = dyn_cast<ClassTemplateDecl>(*Found.first);
718 if (!InstClassTemplate)
719 return 0;
720
721 Decl *DCanon = D->getCanonicalDecl();
722 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
723 P = InstClassTemplate->getPartialSpecializations().begin(),
724 PEnd = InstClassTemplate->getPartialSpecializations().end();
725 P != PEnd; ++P) {
726 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
727 return &*P;
728 }
729
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000730 return 0;
731}
732
733Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000734TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000735 // Create a local instantiation scope for this function template, which
736 // will contain the instantiations of the template parameters and then get
737 // merged with the local instantiation scope for the function template
738 // itself.
739 Sema::LocalInstantiationScope Scope(SemaRef);
740
Douglas Gregord60e1052009-08-27 16:57:43 +0000741 TemplateParameterList *TempParams = D->getTemplateParameters();
742 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000743 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000744 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000745
Douglas Gregora735b202009-10-13 14:39:41 +0000746 FunctionDecl *Instantiated = 0;
747 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
748 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
749 InstParams));
750 else
751 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
752 D->getTemplatedDecl(),
753 InstParams));
754
755 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000756 return 0;
757
John McCall46460a62010-01-20 21:53:11 +0000758 Instantiated->setAccess(D->getAccess());
759
Mike Stump1eb44332009-09-09 15:08:12 +0000760 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000761 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000762 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000763 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000764 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000765 assert(InstTemplate &&
766 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000767
768 // Link the instantiation back to the pattern *unless* this is a
769 // non-definition friend declaration.
770 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
771 !(InstTemplate->getFriendObjectKind() &&
772 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000773 InstTemplate->setInstantiatedFromMemberTemplate(D);
774
775 // Add non-friends into the owner.
776 if (!InstTemplate->getFriendObjectKind())
777 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000778 return InstTemplate;
779}
780
Douglas Gregord475b8d2009-03-25 21:17:03 +0000781Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
782 CXXRecordDecl *PrevDecl = 0;
783 if (D->isInjectedClassName())
784 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000785 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000786 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
787 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000788 TemplateArgs);
789 if (!Prev) return 0;
790 PrevDecl = cast<CXXRecordDecl>(Prev);
791 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000792
793 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000794 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000795 D->getLocation(), D->getIdentifier(),
796 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000797
798 // Substitute the nested name specifier, if any.
799 if (SubstQualifier(D, Record))
800 return 0;
801
Douglas Gregord475b8d2009-03-25 21:17:03 +0000802 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000803 // FIXME: Check against AS_none is an ugly hack to work around the issue that
804 // the tag decls introduced by friend class declarations don't have an access
805 // specifier. Remove once this area of the code gets sorted out.
806 if (D->getAccess() != AS_none)
807 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000808 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000809 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000810
John McCall02cace72009-08-28 07:59:38 +0000811 // If the original function was part of a friend declaration,
812 // inherit its namespace state.
813 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
814 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
815
Anders Carlssond8b285f2009-09-01 04:26:58 +0000816 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
817
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000818 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000819 return Record;
820}
821
John McCall02cace72009-08-28 07:59:38 +0000822/// Normal class members are of more specific types and therefore
823/// don't make it here. This function serves two purposes:
824/// 1) instantiating function templates
825/// 2) substituting friend declarations
826/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000827Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000828 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000829 // Check whether there is already a function template specialization for
830 // this declaration.
831 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
832 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000833 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000834 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000835 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000836 TemplateArgs.getInnermost().getFlatArgumentList(),
837 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000838 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
840 FunctionTemplateSpecializationInfo *Info
841 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000842 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor127102b2009-06-29 20:59:39 +0000844 // If we already have a function template specialization, return it.
845 if (Info)
846 return Info->Function;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Douglas Gregor79c22782010-01-16 20:21:20 +0000849 bool MergeWithParentScope = (TemplateParams != 0) ||
850 !(isa<Decl>(Owner) &&
851 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
852 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Douglas Gregore53060f2009-06-25 22:08:12 +0000854 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000855 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
856 TInfo = SubstFunctionType(D, Params);
857 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000858 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000859 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000860
John McCall68b6b872010-02-06 01:50:47 +0000861 // If we're instantiating a local function declaration, put the result
862 // in the owner; otherwise we need to find the instantiated context.
863 DeclContext *DC;
864 if (D->getDeclContext()->isFunctionOrMethod())
865 DC = Owner;
866 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000867 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
868 TemplateArgs);
John McCall68b6b872010-02-06 01:50:47 +0000869
John McCall02cace72009-08-28 07:59:38 +0000870 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000871 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +0000872 D->getDeclName(), T, TInfo,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000873 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000874 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +0000875
876 // Substitute the nested name specifier, if any.
877 if (SubstQualifier(D, Function))
878 return 0;
879
John McCall02cace72009-08-28 07:59:38 +0000880 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregore53060f2009-06-25 22:08:12 +0000882 // Attach the parameters
883 for (unsigned P = 0; P < Params.size(); ++P)
884 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +0000885 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000886
Douglas Gregora735b202009-10-13 14:39:41 +0000887 if (TemplateParams) {
888 // Our resulting instantiation is actually a function template, since we
889 // are substituting only the outer template parameters. For example, given
890 //
891 // template<typename T>
892 // struct X {
893 // template<typename U> friend void f(T, U);
894 // };
895 //
896 // X<int> x;
897 //
898 // We are instantiating the friend function template "f" within X<int>,
899 // which means substituting int for T, but leaving "f" as a friend function
900 // template.
901 // Build the function template itself.
902 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
903 Function->getLocation(),
904 Function->getDeclName(),
905 TemplateParams, Function);
906 Function->setDescribedFunctionTemplate(FunctionTemplate);
907 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000908 } else if (FunctionTemplate) {
909 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +0000910 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +0000911 &TemplateArgs.getInnermost(),
912 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000913 }
Douglas Gregora735b202009-10-13 14:39:41 +0000914
Douglas Gregore53060f2009-06-25 22:08:12 +0000915 if (InitFunctionInstantiation(Function, D))
916 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Douglas Gregore53060f2009-06-25 22:08:12 +0000918 bool Redeclaration = false;
919 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000920
John McCall68263142009-11-18 22:49:29 +0000921 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
922 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
923
Douglas Gregora735b202009-10-13 14:39:41 +0000924 if (TemplateParams || !FunctionTemplate) {
925 // Look only into the namespace where the friend would be declared to
926 // find a previous declaration. This is the innermost enclosing namespace,
927 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000928 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000929
Douglas Gregora735b202009-10-13 14:39:41 +0000930 // In C++, the previous declaration we find might be a tag type
931 // (class or enum). In this case, the new declaration will hide the
932 // tag type. Note that this does does not apply if we're declaring a
933 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000934 if (Previous.isSingleTagDecl())
935 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000936 }
937
John McCall9f54ad42009-12-10 09:41:52 +0000938 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
939 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000940 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000941
Douglas Gregora735b202009-10-13 14:39:41 +0000942 // If the original function was part of a friend declaration,
943 // inherit its namespace state and add it to the owner.
944 NamedDecl *FromFriendD
945 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
946 if (FromFriendD->getFriendObjectKind()) {
947 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000948 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000949 if (TemplateParams) {
950 ToFriendD = cast<NamedDecl>(FunctionTemplate);
951 PrevDecl = FunctionTemplate->getPreviousDeclaration();
952 } else {
953 ToFriendD = Function;
954 PrevDecl = Function->getPreviousDeclaration();
955 }
956 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
957 if (!Owner->isDependentContext() && !PrevDecl)
958 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
959
960 if (!TemplateParams)
961 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
962 }
963
Douglas Gregore53060f2009-06-25 22:08:12 +0000964 return Function;
965}
966
Douglas Gregord60e1052009-08-27 16:57:43 +0000967Decl *
968TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
969 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000970 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
971 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000972 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000973 // We are creating a function template specialization from a function
974 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000975 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000976 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000977 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000978 TemplateArgs.getInnermost().getFlatArgumentList(),
979 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000980 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
982 FunctionTemplateSpecializationInfo *Info
983 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000984 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregor6b906862009-08-21 00:16:32 +0000986 // If we already have a function template specialization, return it.
987 if (Info)
988 return Info->Function;
989 }
990
Douglas Gregor79c22782010-01-16 20:21:20 +0000991 bool MergeWithParentScope = (TemplateParams != 0) ||
992 !(isa<Decl>(Owner) &&
993 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
994 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000995
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000996 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000997 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
998 TInfo = SubstFunctionType(D, Params);
999 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001000 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001001 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001002
1003 // Build the instantiated method declaration.
1004 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +00001005 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregordec06662009-08-21 18:42:58 +00001007 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001008 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001009 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1010 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1011 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001012 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1013 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001014 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001015 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001016 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001017 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1018 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1019 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1020 SemaRef.Context.getCanonicalType(ClassTy));
1021 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1022 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001023 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001024 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001025 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001026 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001027 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001028 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1029 ConvTy);
1030 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1031 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001032 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001033 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001034 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001035 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001036 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001037 D->getDeclName(), T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001038 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001039 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001040
John McCallb6217662010-03-15 10:12:16 +00001041 // Substitute the nested name specifier, if any.
1042 if (SubstQualifier(D, Method))
1043 return 0;
1044
Douglas Gregord60e1052009-08-27 16:57:43 +00001045 if (TemplateParams) {
1046 // Our resulting instantiation is actually a function template, since we
1047 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001048 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001049 // template<typename T>
1050 // struct X {
1051 // template<typename U> void f(T, U);
1052 // };
1053 //
1054 // X<int> x;
1055 //
1056 // We are instantiating the member template "f" within X<int>, which means
1057 // substituting int for T, but leaving "f" as a member function template.
1058 // Build the function template itself.
1059 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1060 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001061 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001062 TemplateParams, Method);
1063 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001064 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001065 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001066 } else if (FunctionTemplate) {
1067 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001068 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001069 &TemplateArgs.getInnermost(),
1070 InsertPos);
1071 } else {
1072 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001073 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001074 }
1075
Mike Stump1eb44332009-09-09 15:08:12 +00001076 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001077 // out-of-line, the instantiation will have the same lexical
1078 // context (which will be a namespace scope) as the template.
1079 if (D->isOutOfLine())
1080 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Douglas Gregor5545e162009-03-24 00:38:23 +00001082 // Attach the parameters
1083 for (unsigned P = 0; P < Params.size(); ++P)
1084 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001085 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001086
1087 if (InitMethodInstantiation(Method, D))
1088 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001089
John McCall68263142009-11-18 22:49:29 +00001090 LookupResult Previous(SemaRef, Name, SourceLocation(),
1091 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregord60e1052009-08-27 16:57:43 +00001093 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +00001094 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregordec06662009-08-21 18:42:58 +00001096 // In C++, the previous declaration we find might be a tag type
1097 // (class or enum). In this case, the new declaration will hide the
1098 // tag type. Note that this does does not apply if we're declaring a
1099 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001100 if (Previous.isSingleTagDecl())
1101 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001102 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001103
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001104 bool Redeclaration = false;
1105 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001106 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001107 /*FIXME:*/OverloadableAttrRequired);
1108
Douglas Gregor4ba31362009-12-01 17:24:26 +00001109 if (D->isPure())
1110 SemaRef.CheckPureMethod(Method, SourceRange());
1111
John McCall46460a62010-01-20 21:53:11 +00001112 Method->setAccess(D->getAccess());
1113
John McCall68263142009-11-18 22:49:29 +00001114 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +00001115 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +00001116 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001118 return Method;
1119}
1120
Douglas Gregor615c5d42009-03-24 16:43:20 +00001121Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001122 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001123}
1124
Douglas Gregor03b2b072009-03-24 00:15:49 +00001125Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001126 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001127}
1128
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001129Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001130 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001131}
1132
Douglas Gregor6477b692009-03-25 15:04:13 +00001133ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001134 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001135 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001136 if (DI) {
1137 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1138 D->getDeclName());
1139 if (DI) T = DI->getType();
1140 } else {
1141 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1142 D->getDeclName());
1143 DI = 0;
1144 }
1145
1146 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001147 return 0;
1148
John McCall58e46772009-10-23 21:48:59 +00001149 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001150
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001151 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001152 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001153 = ParmVarDecl::Create(SemaRef.Context,
1154 SemaRef.Context.getTranslationUnitDecl(),
1155 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001156 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001157
Anders Carlsson9351c172009-08-25 03:18:48 +00001158 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001159 if (D->hasUninstantiatedDefaultArg())
1160 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001161 else if (Expr *Arg = D->getDefaultArg())
1162 Param->setUninstantiatedDefaultArg(Arg);
1163
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001164 // Note: we don't try to instantiate function parameters until after
1165 // we've instantiated the function's type. Therefore, we don't have
1166 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001167 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001168 return Param;
1169}
1170
John McCalle29ba202009-08-20 01:44:21 +00001171Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1172 TemplateTypeParmDecl *D) {
1173 // TODO: don't always clone when decls are refcounted.
1174 const Type* T = D->getTypeForDecl();
1175 assert(T->isTemplateTypeParmType());
1176 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001177
John McCalle29ba202009-08-20 01:44:21 +00001178 TemplateTypeParmDecl *Inst =
1179 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001180 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001181 TTPT->getName(),
1182 D->wasDeclaredWithTypename(),
1183 D->isParameterPack());
1184
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001185 if (D->hasDefaultArgument())
1186 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001187
Douglas Gregor550d9b22009-10-31 17:21:17 +00001188 // Introduce this template parameter's instantiation into the instantiation
1189 // scope.
1190 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1191
John McCalle29ba202009-08-20 01:44:21 +00001192 return Inst;
1193}
1194
Douglas Gregor33642df2009-10-23 23:25:44 +00001195Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1196 NonTypeTemplateParmDecl *D) {
1197 // Substitute into the type of the non-type template parameter.
1198 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001199 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001200 if (DI) {
1201 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1202 D->getDeclName());
1203 if (DI) T = DI->getType();
1204 } else {
1205 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1206 D->getDeclName());
1207 DI = 0;
1208 }
1209 if (T.isNull())
1210 return 0;
1211
1212 // Check that this type is acceptable for a non-type template parameter.
1213 bool Invalid = false;
1214 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1215 if (T.isNull()) {
1216 T = SemaRef.Context.IntTy;
1217 Invalid = true;
1218 }
1219
1220 NonTypeTemplateParmDecl *Param
1221 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1222 D->getDepth() - 1, D->getPosition(),
1223 D->getIdentifier(), T, DI);
1224 if (Invalid)
1225 Param->setInvalidDecl();
1226
1227 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001228
1229 // Introduce this template parameter's instantiation into the instantiation
1230 // scope.
1231 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001232 return Param;
1233}
1234
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001235Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001236TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1237 TemplateTemplateParmDecl *D) {
1238 // Instantiate the template parameter list of the template template parameter.
1239 TemplateParameterList *TempParams = D->getTemplateParameters();
1240 TemplateParameterList *InstParams;
1241 {
1242 // Perform the actual substitution of template parameters within a new,
1243 // local instantiation scope.
1244 Sema::LocalInstantiationScope Scope(SemaRef);
1245 InstParams = SubstTemplateParams(TempParams);
1246 if (!InstParams)
1247 return NULL;
1248 }
1249
1250 // Build the template template parameter.
1251 TemplateTemplateParmDecl *Param
1252 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1253 D->getDepth() - 1, D->getPosition(),
1254 D->getIdentifier(), InstParams);
1255 Param->setDefaultArgument(D->getDefaultArgument());
1256
1257 // Introduce this template parameter's instantiation into the instantiation
1258 // scope.
1259 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1260
1261 return Param;
1262}
1263
Douglas Gregor48c32a72009-11-17 06:07:40 +00001264Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1265 // Using directives are never dependent, so they require no explicit
1266
1267 UsingDirectiveDecl *Inst
1268 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1269 D->getNamespaceKeyLocation(),
1270 D->getQualifierRange(), D->getQualifier(),
1271 D->getIdentLocation(),
1272 D->getNominatedNamespace(),
1273 D->getCommonAncestor());
1274 Owner->addDecl(Inst);
1275 return Inst;
1276}
1277
John McCalled976492009-12-04 22:46:56 +00001278Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1279 // The nested name specifier is non-dependent, so no transformation
1280 // is required.
1281
John McCall9f54ad42009-12-10 09:41:52 +00001282 // We only need to do redeclaration lookups if we're in a class
1283 // scope (in fact, it's not really even possible in non-class
1284 // scopes).
1285 bool CheckRedeclaration = Owner->isRecord();
1286
1287 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1288 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1289
John McCalled976492009-12-04 22:46:56 +00001290 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1291 D->getLocation(),
1292 D->getNestedNameRange(),
1293 D->getUsingLocation(),
1294 D->getTargetNestedNameDecl(),
1295 D->getDeclName(),
1296 D->isTypeName());
1297
1298 CXXScopeSpec SS;
1299 SS.setScopeRep(D->getTargetNestedNameDecl());
1300 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001301
1302 if (CheckRedeclaration) {
1303 Prev.setHideTags(false);
1304 SemaRef.LookupQualifiedName(Prev, Owner);
1305
1306 // Check for invalid redeclarations.
1307 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1308 D->isTypeName(), SS,
1309 D->getLocation(), Prev))
1310 NewUD->setInvalidDecl();
1311
1312 }
1313
1314 if (!NewUD->isInvalidDecl() &&
1315 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001316 D->getLocation()))
1317 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001318
John McCalled976492009-12-04 22:46:56 +00001319 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1320 NewUD->setAccess(D->getAccess());
1321 Owner->addDecl(NewUD);
1322
John McCall9f54ad42009-12-10 09:41:52 +00001323 // Don't process the shadow decls for an invalid decl.
1324 if (NewUD->isInvalidDecl())
1325 return NewUD;
1326
John McCall323c3102009-12-22 22:26:37 +00001327 bool isFunctionScope = Owner->isFunctionOrMethod();
1328
John McCall9f54ad42009-12-10 09:41:52 +00001329 // Process the shadow decls.
1330 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1331 I != E; ++I) {
1332 UsingShadowDecl *Shadow = *I;
1333 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001334 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1335 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001336 TemplateArgs));
1337
1338 if (CheckRedeclaration &&
1339 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1340 continue;
1341
1342 UsingShadowDecl *InstShadow
1343 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1344 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001345
1346 if (isFunctionScope)
1347 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001348 }
John McCalled976492009-12-04 22:46:56 +00001349
1350 return NewUD;
1351}
1352
1353Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001354 // Ignore these; we handle them in bulk when processing the UsingDecl.
1355 return 0;
John McCalled976492009-12-04 22:46:56 +00001356}
1357
John McCall7ba107a2009-11-18 02:36:19 +00001358Decl * TemplateDeclInstantiator
1359 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001360 NestedNameSpecifier *NNS =
1361 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1362 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001363 TemplateArgs);
1364 if (!NNS)
1365 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001367 CXXScopeSpec SS;
1368 SS.setRange(D->getTargetNestedNameRange());
1369 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
1371 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001372 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001373 D->getUsingLoc(), SS, D->getLocation(),
1374 D->getDeclName(), 0,
1375 /*instantiation*/ true,
1376 /*typename*/ true, D->getTypenameLoc());
1377 if (UD)
John McCalled976492009-12-04 22:46:56 +00001378 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1379
John McCall7ba107a2009-11-18 02:36:19 +00001380 return UD;
1381}
1382
1383Decl * TemplateDeclInstantiator
1384 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1385 NestedNameSpecifier *NNS =
1386 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1387 D->getTargetNestedNameRange(),
1388 TemplateArgs);
1389 if (!NNS)
1390 return 0;
1391
1392 CXXScopeSpec SS;
1393 SS.setRange(D->getTargetNestedNameRange());
1394 SS.setScopeRep(NNS);
1395
1396 NamedDecl *UD =
1397 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1398 D->getUsingLoc(), SS, D->getLocation(),
1399 D->getDeclName(), 0,
1400 /*instantiation*/ true,
1401 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001402 if (UD)
John McCalled976492009-12-04 22:46:56 +00001403 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1404
Anders Carlsson0d8df782009-08-29 19:37:28 +00001405 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001406}
1407
John McCallce3ff2b2009-08-25 22:02:44 +00001408Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001409 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001410 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001411 if (D->isInvalidDecl())
1412 return 0;
1413
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001414 return Instantiator.Visit(D);
1415}
1416
John McCalle29ba202009-08-20 01:44:21 +00001417/// \brief Instantiates a nested template parameter list in the current
1418/// instantiation context.
1419///
1420/// \param L The parameter list to instantiate
1421///
1422/// \returns NULL if there was an error
1423TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001424TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001425 // Get errors for all the parameters before bailing out.
1426 bool Invalid = false;
1427
1428 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001429 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001430 ParamVector Params;
1431 Params.reserve(N);
1432 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1433 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001434 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001435 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001436 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001437 }
1438
1439 // Clean up if we had an error.
1440 if (Invalid) {
1441 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1442 PI != PE; ++PI)
1443 if (*PI)
1444 (*PI)->Destroy(SemaRef.Context);
1445 return NULL;
1446 }
1447
1448 TemplateParameterList *InstL
1449 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1450 L->getLAngleLoc(), &Params.front(), N,
1451 L->getRAngleLoc());
1452 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001453}
John McCalle29ba202009-08-20 01:44:21 +00001454
Douglas Gregored9c0f92009-10-29 00:04:11 +00001455/// \brief Instantiate the declaration of a class template partial
1456/// specialization.
1457///
1458/// \param ClassTemplate the (instantiated) class template that is partially
1459// specialized by the instantiation of \p PartialSpec.
1460///
1461/// \param PartialSpec the (uninstantiated) class template partial
1462/// specialization that we are instantiating.
1463///
1464/// \returns true if there was an error, false otherwise.
1465bool
1466TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1467 ClassTemplateDecl *ClassTemplate,
1468 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001469 // Create a local instantiation scope for this class template partial
1470 // specialization, which will contain the instantiations of the template
1471 // parameters.
1472 Sema::LocalInstantiationScope Scope(SemaRef);
1473
Douglas Gregored9c0f92009-10-29 00:04:11 +00001474 // Substitute into the template parameters of the class template partial
1475 // specialization.
1476 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1477 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1478 if (!InstParams)
1479 return true;
1480
1481 // Substitute into the template arguments of the class template partial
1482 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001483 const TemplateArgumentLoc *PartialSpecTemplateArgs
1484 = PartialSpec->getTemplateArgsAsWritten();
1485 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1486
John McCalld5532b62009-11-23 01:53:49 +00001487 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001488 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001489 TemplateArgumentLoc Loc;
1490 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001491 return true;
John McCalld5532b62009-11-23 01:53:49 +00001492 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001493 }
1494
1495
1496 // Check that the template argument list is well-formed for this
1497 // class template.
1498 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1499 InstTemplateArgs.size());
1500 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1501 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001502 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001503 false,
1504 Converted))
1505 return true;
1506
1507 // Figure out where to insert this class template partial specialization
1508 // in the member template's set of class template partial specializations.
1509 llvm::FoldingSetNodeID ID;
1510 ClassTemplatePartialSpecializationDecl::Profile(ID,
1511 Converted.getFlatArguments(),
1512 Converted.flatSize(),
1513 SemaRef.Context);
1514 void *InsertPos = 0;
1515 ClassTemplateSpecializationDecl *PrevDecl
1516 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1517 InsertPos);
1518
1519 // Build the canonical type that describes the converted template
1520 // arguments of the class template partial specialization.
1521 QualType CanonType
1522 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1523 Converted.getFlatArguments(),
1524 Converted.flatSize());
1525
1526 // Build the fully-sugared type for this class template
1527 // specialization as the user wrote in the specialization
1528 // itself. This means that we'll pretty-print the type retrieved
1529 // from the specialization's declaration the way that the user
1530 // actually wrote the specialization, rather than formatting the
1531 // name based on the "canonical" representation used to store the
1532 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001533 TypeSourceInfo *WrittenTy
1534 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1535 TemplateName(ClassTemplate),
1536 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001537 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001538 CanonType);
1539
1540 if (PrevDecl) {
1541 // We've already seen a partial specialization with the same template
1542 // parameters and template arguments. This can happen, for example, when
1543 // substituting the outer template arguments ends up causing two
1544 // class template partial specializations of a member class template
1545 // to have identical forms, e.g.,
1546 //
1547 // template<typename T, typename U>
1548 // struct Outer {
1549 // template<typename X, typename Y> struct Inner;
1550 // template<typename Y> struct Inner<T, Y>;
1551 // template<typename Y> struct Inner<U, Y>;
1552 // };
1553 //
1554 // Outer<int, int> outer; // error: the partial specializations of Inner
1555 // // have the same signature.
1556 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1557 << WrittenTy;
1558 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1559 << SemaRef.Context.getTypeDeclType(PrevDecl);
1560 return true;
1561 }
1562
1563
1564 // Create the class template partial specialization declaration.
1565 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1566 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1567 PartialSpec->getLocation(),
1568 InstParams,
1569 ClassTemplate,
1570 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001571 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001572 CanonType,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001573 0);
John McCallb6217662010-03-15 10:12:16 +00001574 // Substitute the nested name specifier, if any.
1575 if (SubstQualifier(PartialSpec, InstPartialSpec))
1576 return 0;
1577
Douglas Gregored9c0f92009-10-29 00:04:11 +00001578 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1579 InstPartialSpec->setTypeAsWritten(WrittenTy);
1580
1581 // Add this partial specialization to the set of class template partial
1582 // specializations.
1583 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1584 InsertPos);
1585 return false;
1586}
1587
John McCall21ef0fa2010-03-11 09:03:00 +00001588bool
1589Sema::CheckInstantiatedParams(llvm::SmallVectorImpl<ParmVarDecl*> &Params) {
1590 bool Invalid = false;
1591 for (unsigned i = 0, i_end = Params.size(); i != i_end; ++i)
1592 if (ParmVarDecl *PInst = Params[i]) {
1593 if (PInst->isInvalidDecl())
1594 Invalid = true;
1595 else if (PInst->getType()->isVoidType()) {
1596 Diag(PInst->getLocation(), diag::err_param_with_void_type);
1597 PInst->setInvalidDecl();
1598 Invalid = true;
1599 }
1600 else if (RequireNonAbstractType(PInst->getLocation(),
1601 PInst->getType(),
1602 diag::err_abstract_type_in_decl,
1603 Sema::AbstractParamType)) {
1604 PInst->setInvalidDecl();
1605 Invalid = true;
1606 }
1607 }
1608 return Invalid;
1609}
Douglas Gregor5545e162009-03-24 00:38:23 +00001610
John McCall21ef0fa2010-03-11 09:03:00 +00001611TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001612TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001613 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001614 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1615 assert(OldTInfo && "substituting function without type source info");
1616 assert(Params.empty() && "parameter vector is non-empty at start");
1617 TypeSourceInfo *NewTInfo = SemaRef.SubstType(OldTInfo, TemplateArgs,
1618 D->getTypeSpecStartLoc(),
1619 D->getDeclName());
1620 if (!NewTInfo)
1621 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001622
John McCall21ef0fa2010-03-11 09:03:00 +00001623 // Get parameters from the new type info.
1624 TypeLoc NewTL = NewTInfo->getTypeLoc();
1625 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1626 assert(NewProtoLoc && "Missing prototype?");
1627 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i)
1628 Params.push_back(NewProtoLoc->getArg(i));
Douglas Gregor5545e162009-03-24 00:38:23 +00001629
John McCall21ef0fa2010-03-11 09:03:00 +00001630 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001631}
1632
Mike Stump1eb44332009-09-09 15:08:12 +00001633/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001634/// declaration (New) from the corresponding fields of its template (Tmpl).
1635///
1636/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001637bool
1638TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001639 FunctionDecl *Tmpl) {
1640 if (Tmpl->isDeleted())
1641 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregorcca9e962009-07-01 22:01:06 +00001643 // If we are performing substituting explicitly-specified template arguments
1644 // or deduced template arguments into a function template and we reach this
1645 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001646 // to keeping the new function template specialization. We therefore
1647 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001648 // into a template instantiation for this specific function template
1649 // specialization, which is not a SFINAE context, so that we diagnose any
1650 // further errors in the declaration itself.
1651 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1652 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1653 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1654 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001655 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001656 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001657 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001658 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001659 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001660 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1661 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001662 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001663 }
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001666 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1667 assert(Proto && "Function template without prototype?");
1668
1669 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1670 Proto->getNoReturnAttr()) {
1671 // The function has an exception specification or a "noreturn"
1672 // attribute. Substitute into each of the exception types.
1673 llvm::SmallVector<QualType, 4> Exceptions;
1674 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1675 // FIXME: Poor location information!
1676 QualType T
1677 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1678 New->getLocation(), New->getDeclName());
1679 if (T.isNull() ||
1680 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1681 continue;
1682
1683 Exceptions.push_back(T);
1684 }
1685
1686 // Rebuild the function type
1687
1688 const FunctionProtoType *NewProto
1689 = New->getType()->getAs<FunctionProtoType>();
1690 assert(NewProto && "Template instantiation without function prototype?");
1691 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1692 NewProto->arg_type_begin(),
1693 NewProto->getNumArgs(),
1694 NewProto->isVariadic(),
1695 NewProto->getTypeQuals(),
1696 Proto->hasExceptionSpec(),
1697 Proto->hasAnyExceptionSpec(),
1698 Exceptions.size(),
1699 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001700 Proto->getNoReturnAttr(),
1701 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001702 }
1703
Douglas Gregore53060f2009-06-25 22:08:12 +00001704 return false;
1705}
1706
Douglas Gregor5545e162009-03-24 00:38:23 +00001707/// \brief Initializes common fields of an instantiated method
1708/// declaration (New) from the corresponding fields of its template
1709/// (Tmpl).
1710///
1711/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001712bool
1713TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001714 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001715 if (InitFunctionInstantiation(New, Tmpl))
1716 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Douglas Gregor5545e162009-03-24 00:38:23 +00001718 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1719 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001720 if (Tmpl->isVirtualAsWritten())
1721 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001722
1723 // FIXME: attributes
1724 // FIXME: New needs a pointer to Tmpl
1725 return false;
1726}
Douglas Gregora58861f2009-05-13 20:28:22 +00001727
1728/// \brief Instantiate the definition of the given function from its
1729/// template.
1730///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001731/// \param PointOfInstantiation the point at which the instantiation was
1732/// required. Note that this is not precisely a "point of instantiation"
1733/// for the function, but it's close.
1734///
Douglas Gregora58861f2009-05-13 20:28:22 +00001735/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001736/// function template specialization or member function of a class template
1737/// specialization.
1738///
1739/// \param Recursive if true, recursively instantiates any functions that
1740/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001741///
1742/// \param DefinitionRequired if true, then we are performing an explicit
1743/// instantiation where the body of the function is required. Complain if
1744/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001745void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001746 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001747 bool Recursive,
1748 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001749 if (Function->isInvalidDecl())
1750 return;
1751
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001752 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001754 // Never instantiate an explicit specialization.
1755 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1756 return;
1757
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001758 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001759 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001760 Stmt *Pattern = 0;
1761 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001762 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001763
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001764 if (!Pattern) {
1765 if (DefinitionRequired) {
1766 if (Function->getPrimaryTemplate())
1767 Diag(PointOfInstantiation,
1768 diag::err_explicit_instantiation_undefined_func_template)
1769 << Function->getPrimaryTemplate();
1770 else
1771 Diag(PointOfInstantiation,
1772 diag::err_explicit_instantiation_undefined_member)
1773 << 1 << Function->getDeclName() << Function->getDeclContext();
1774
1775 if (PatternDecl)
1776 Diag(PatternDecl->getLocation(),
1777 diag::note_explicit_instantiation_here);
1778 }
1779
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001780 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001781 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001782
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001783 // C++0x [temp.explicit]p9:
1784 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001785 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001786 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001787 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001788 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001789 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001790 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001792 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1793 if (Inst)
1794 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001795
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001796 // If we're performing recursive template instantiation, create our own
1797 // queue of pending implicit instantiations that we will instantiate later,
1798 // while we're still within our own instantiation context.
1799 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1800 if (Recursive)
1801 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001803 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1804
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001805 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001806 // recorded, unless we're actually a member function within a local
1807 // class, in which case we need to merge our results with the parent
1808 // scope (of the enclosing function).
1809 bool MergeWithParentScope = false;
1810 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1811 MergeWithParentScope = Rec->isLocalClass();
1812
1813 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001815 // Introduce the instantiated function parameters into the local
1816 // instantiation scope.
1817 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1818 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1819 Function->getParamDecl(I));
1820
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001821 // Enter the scope of this instantiation. We don't use
1822 // PushDeclContext because we don't have a scope.
1823 DeclContext *PreviousContext = CurContext;
1824 CurContext = Function;
1825
Mike Stump1eb44332009-09-09 15:08:12 +00001826 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001827 getTemplateInstantiationArgs(Function);
1828
1829 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001830 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001831 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1832 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1833 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001834 }
1835
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001836 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001837 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001838
Douglas Gregor52604ab2009-09-11 21:19:12 +00001839 if (Body.isInvalid())
1840 Function->setInvalidDecl();
1841
Mike Stump1eb44332009-09-09 15:08:12 +00001842 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001843 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001844
John McCall0c01d182010-03-24 05:22:00 +00001845 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
1846
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001847 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001848
1849 DeclGroupRef DG(Function);
1850 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Douglas Gregor60406be2010-01-16 22:29:39 +00001852 // This class may have local implicit instantiations that need to be
1853 // instantiation within this scope.
1854 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1855 Scope.Exit();
1856
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001857 if (Recursive) {
1858 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001860 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001862 // Restore the set of pending implicit instantiations.
1863 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1864 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001865}
1866
1867/// \brief Instantiate the definition of the given variable from its
1868/// template.
1869///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001870/// \param PointOfInstantiation the point at which the instantiation was
1871/// required. Note that this is not precisely a "point of instantiation"
1872/// for the function, but it's close.
1873///
1874/// \param Var the already-instantiated declaration of a static member
1875/// variable of a class template specialization.
1876///
1877/// \param Recursive if true, recursively instantiates any functions that
1878/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001879///
1880/// \param DefinitionRequired if true, then we are performing an explicit
1881/// instantiation where an out-of-line definition of the member variable
1882/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001883void Sema::InstantiateStaticDataMemberDefinition(
1884 SourceLocation PointOfInstantiation,
1885 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001886 bool Recursive,
1887 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001888 if (Var->isInvalidDecl())
1889 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Douglas Gregor7caa6822009-07-24 20:34:43 +00001891 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001892 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001893 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001894 assert(Def->isStaticDataMember() && "Not a static data member?");
1895 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Douglas Gregor0d035142009-10-27 18:42:08 +00001897 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001898 // We did not find an out-of-line definition of this static data member,
1899 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001900 // instantiate this definition (or provide a specialization for it) in
1901 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001902 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001903 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001904 Diag(PointOfInstantiation,
1905 diag::err_explicit_instantiation_undefined_member)
1906 << 2 << Var->getDeclName() << Var->getDeclContext();
1907 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1908 }
1909
Douglas Gregor7caa6822009-07-24 20:34:43 +00001910 return;
1911 }
1912
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001913 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001914 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001915 return;
1916
1917 // C++0x [temp.explicit]p9:
1918 // Except for inline functions, other explicit instantiation declarations
1919 // have the effect of suppressing the implicit instantiation of the entity
1920 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001921 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001922 == TSK_ExplicitInstantiationDeclaration)
1923 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Douglas Gregor7caa6822009-07-24 20:34:43 +00001925 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1926 if (Inst)
1927 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Douglas Gregor7caa6822009-07-24 20:34:43 +00001929 // If we're performing recursive template instantiation, create our own
1930 // queue of pending implicit instantiations that we will instantiate later,
1931 // while we're still within our own instantiation context.
1932 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1933 if (Recursive)
1934 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Douglas Gregor7caa6822009-07-24 20:34:43 +00001936 // Enter the scope of this instantiation. We don't use
1937 // PushDeclContext because we don't have a scope.
1938 DeclContext *PreviousContext = CurContext;
1939 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001941 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001942 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001943 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001944 CurContext = PreviousContext;
1945
1946 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001947 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1948 assert(MSInfo && "Missing member specialization information?");
1949 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1950 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001951 DeclGroupRef DG(Var);
1952 Consumer.HandleTopLevelDecl(DG);
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Douglas Gregor7caa6822009-07-24 20:34:43 +00001955 if (Recursive) {
1956 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001957 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001958 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor7caa6822009-07-24 20:34:43 +00001960 // Restore the set of pending implicit instantiations.
1961 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001962 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001963}
Douglas Gregor815215d2009-05-27 05:35:12 +00001964
Anders Carlsson09025312009-08-29 05:16:22 +00001965void
1966Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1967 const CXXConstructorDecl *Tmpl,
1968 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Anders Carlsson09025312009-08-29 05:16:22 +00001970 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001971 bool AnyErrors = false;
1972
Anders Carlsson09025312009-08-29 05:16:22 +00001973 // Instantiate all the initializers.
1974 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001975 InitsEnd = Tmpl->init_end();
1976 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001977 CXXBaseOrMemberInitializer *Init = *Inits;
1978
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001979 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00001980 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001981 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001983 // Instantiate the initializer.
1984 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
1985 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
1986 AnyErrors = true;
1987 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00001988 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001989
Anders Carlsson09025312009-08-29 05:16:22 +00001990 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001991 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001992 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001993 TemplateArgs,
1994 Init->getSourceLocation(),
1995 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001996 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001997 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001998 New->setInvalidDecl();
1999 continue;
2000 }
2001
John McCalla93c9342009-12-07 02:54:59 +00002002 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002003 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002004 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002005 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002006 Init->getRParenLoc(),
2007 New->getParent());
2008 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002009 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002011 // Is this an anonymous union?
2012 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002013 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2014 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002015 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002016 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2017 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002018 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002019
2020 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002021 NewArgs.size(),
2022 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002023 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002024 Init->getRParenLoc());
2025 }
2026
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002027 if (NewInit.isInvalid()) {
2028 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002029 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002030 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002031 // FIXME: It would be nice if ASTOwningVector had a release function.
2032 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Anders Carlsson09025312009-08-29 05:16:22 +00002034 NewInits.push_back((MemInitTy *)NewInit.get());
2035 }
2036 }
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Anders Carlsson09025312009-08-29 05:16:22 +00002038 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002039 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002040 /*FIXME: ColonLoc */
2041 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002042 NewInits.data(), NewInits.size(),
2043 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002044}
2045
John McCall52a575a2009-08-29 08:11:13 +00002046// TODO: this could be templated if the various decl types used the
2047// same method name.
2048static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2049 ClassTemplateDecl *Instance) {
2050 Pattern = Pattern->getCanonicalDecl();
2051
2052 do {
2053 Instance = Instance->getCanonicalDecl();
2054 if (Pattern == Instance) return true;
2055 Instance = Instance->getInstantiatedFromMemberTemplate();
2056 } while (Instance);
2057
2058 return false;
2059}
2060
Douglas Gregor0d696532009-09-28 06:34:35 +00002061static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2062 FunctionTemplateDecl *Instance) {
2063 Pattern = Pattern->getCanonicalDecl();
2064
2065 do {
2066 Instance = Instance->getCanonicalDecl();
2067 if (Pattern == Instance) return true;
2068 Instance = Instance->getInstantiatedFromMemberTemplate();
2069 } while (Instance);
2070
2071 return false;
2072}
2073
Douglas Gregored9c0f92009-10-29 00:04:11 +00002074static bool
2075isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2076 ClassTemplatePartialSpecializationDecl *Instance) {
2077 Pattern
2078 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2079 do {
2080 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2081 Instance->getCanonicalDecl());
2082 if (Pattern == Instance)
2083 return true;
2084 Instance = Instance->getInstantiatedFromMember();
2085 } while (Instance);
2086
2087 return false;
2088}
2089
John McCall52a575a2009-08-29 08:11:13 +00002090static bool isInstantiationOf(CXXRecordDecl *Pattern,
2091 CXXRecordDecl *Instance) {
2092 Pattern = Pattern->getCanonicalDecl();
2093
2094 do {
2095 Instance = Instance->getCanonicalDecl();
2096 if (Pattern == Instance) return true;
2097 Instance = Instance->getInstantiatedFromMemberClass();
2098 } while (Instance);
2099
2100 return false;
2101}
2102
2103static bool isInstantiationOf(FunctionDecl *Pattern,
2104 FunctionDecl *Instance) {
2105 Pattern = Pattern->getCanonicalDecl();
2106
2107 do {
2108 Instance = Instance->getCanonicalDecl();
2109 if (Pattern == Instance) return true;
2110 Instance = Instance->getInstantiatedFromMemberFunction();
2111 } while (Instance);
2112
2113 return false;
2114}
2115
2116static bool isInstantiationOf(EnumDecl *Pattern,
2117 EnumDecl *Instance) {
2118 Pattern = Pattern->getCanonicalDecl();
2119
2120 do {
2121 Instance = Instance->getCanonicalDecl();
2122 if (Pattern == Instance) return true;
2123 Instance = Instance->getInstantiatedFromMemberEnum();
2124 } while (Instance);
2125
2126 return false;
2127}
2128
John McCalled976492009-12-04 22:46:56 +00002129static bool isInstantiationOf(UsingShadowDecl *Pattern,
2130 UsingShadowDecl *Instance,
2131 ASTContext &C) {
2132 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2133}
2134
2135static bool isInstantiationOf(UsingDecl *Pattern,
2136 UsingDecl *Instance,
2137 ASTContext &C) {
2138 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2139}
2140
John McCall7ba107a2009-11-18 02:36:19 +00002141static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2142 UsingDecl *Instance,
2143 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002144 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002145}
2146
2147static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002148 UsingDecl *Instance,
2149 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002150 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002151}
2152
John McCall52a575a2009-08-29 08:11:13 +00002153static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2154 VarDecl *Instance) {
2155 assert(Instance->isStaticDataMember());
2156
2157 Pattern = Pattern->getCanonicalDecl();
2158
2159 do {
2160 Instance = Instance->getCanonicalDecl();
2161 if (Pattern == Instance) return true;
2162 Instance = Instance->getInstantiatedFromStaticDataMember();
2163 } while (Instance);
2164
2165 return false;
2166}
2167
John McCalled976492009-12-04 22:46:56 +00002168// Other is the prospective instantiation
2169// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002170static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002171 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002172 if (UnresolvedUsingTypenameDecl *UUD
2173 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2174 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2175 return isInstantiationOf(UUD, UD, Ctx);
2176 }
2177 }
2178
2179 if (UnresolvedUsingValueDecl *UUD
2180 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002181 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2182 return isInstantiationOf(UUD, UD, Ctx);
2183 }
2184 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002185
Anders Carlsson0d8df782009-08-29 19:37:28 +00002186 return false;
2187 }
Mike Stump1eb44332009-09-09 15:08:12 +00002188
John McCall52a575a2009-08-29 08:11:13 +00002189 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2190 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002191
John McCall52a575a2009-08-29 08:11:13 +00002192 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2193 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002194
John McCall52a575a2009-08-29 08:11:13 +00002195 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2196 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002197
Douglas Gregor7caa6822009-07-24 20:34:43 +00002198 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002199 if (Var->isStaticDataMember())
2200 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2201
2202 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2203 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002204
Douglas Gregor0d696532009-09-28 06:34:35 +00002205 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2206 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2207
Douglas Gregored9c0f92009-10-29 00:04:11 +00002208 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2209 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2210 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2211 PartialSpec);
2212
Anders Carlssond8b285f2009-09-01 04:26:58 +00002213 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2214 if (!Field->getDeclName()) {
2215 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002216 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002217 cast<FieldDecl>(D);
2218 }
2219 }
Mike Stump1eb44332009-09-09 15:08:12 +00002220
John McCalled976492009-12-04 22:46:56 +00002221 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2222 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2223
2224 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2225 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2226
Douglas Gregor815215d2009-05-27 05:35:12 +00002227 return D->getDeclName() && isa<NamedDecl>(Other) &&
2228 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2229}
2230
2231template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002232static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002233 NamedDecl *D,
2234 ForwardIterator first,
2235 ForwardIterator last) {
2236 for (; first != last; ++first)
2237 if (isInstantiationOf(Ctx, D, *first))
2238 return cast<NamedDecl>(*first);
2239
2240 return 0;
2241}
2242
John McCall02cace72009-08-28 07:59:38 +00002243/// \brief Finds the instantiation of the given declaration context
2244/// within the current instantiation.
2245///
2246/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002247DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002248 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002249 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002250 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002251 return cast_or_null<DeclContext>(ID);
2252 } else return DC;
2253}
2254
Douglas Gregored961e72009-05-27 17:54:46 +00002255/// \brief Find the instantiation of the given declaration within the
2256/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002257///
2258/// This routine is intended to be used when \p D is a declaration
2259/// referenced from within a template, that needs to mapped into the
2260/// corresponding declaration within an instantiation. For example,
2261/// given:
2262///
2263/// \code
2264/// template<typename T>
2265/// struct X {
2266/// enum Kind {
2267/// KnownValue = sizeof(T)
2268/// };
2269///
2270/// bool getKind() const { return KnownValue; }
2271/// };
2272///
2273/// template struct X<int>;
2274/// \endcode
2275///
2276/// In the instantiation of X<int>::getKind(), we need to map the
2277/// EnumConstantDecl for KnownValue (which refers to
2278/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002279/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2280/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002281NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002282 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002283 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002284 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002285 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002286 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002287 // D is a local of some kind. Look into the map of local
2288 // declarations to their instantiations.
2289 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2290 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002291
Douglas Gregore95b4092009-09-16 18:34:49 +00002292 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2293 if (!Record->isDependentContext())
2294 return D;
2295
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002296 // If the RecordDecl is actually the injected-class-name or a
2297 // "templated" declaration for a class template, class template
2298 // partial specialization, or a member class of a class template,
2299 // substitute into the injected-class-name of the class template
2300 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002301 QualType T;
2302 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2303
2304 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002305 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002306 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2307 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002308 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002309
2310 // If we call SubstType with an InjectedClassNameType here we
2311 // can end up in an infinite loop.
2312 T = Context.getTypeDeclType(Record);
2313 assert(isa<InjectedClassNameType>(T) &&
2314 "type of partial specialization is not an InjectedClassNameType");
2315 T = cast<InjectedClassNameType>(T)->getUnderlyingType();
2316 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002317
2318 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002319 // Substitute into the injected-class-name to get the type
2320 // corresponding to the instantiation we want, which may also be
2321 // the current instantiation (if we're in a template
2322 // definition). This substitution should never fail, since we
2323 // know we can instantiate the injected-class-name or we
2324 // wouldn't have gotten to the injected-class-name!
2325
2326 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2327 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002328 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2329 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2330
2331 if (!T->isDependentType()) {
2332 assert(T->isRecordType() && "Instantiation must produce a record type");
2333 return T->getAs<RecordType>()->getDecl();
2334 }
2335
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002336 // We are performing "partial" template instantiation to create
2337 // the member declarations for the members of a class template
2338 // specialization. Therefore, D is actually referring to something
2339 // in the current instantiation. Look through the current
2340 // context, which contains actual instantiations, to find the
2341 // instantiation of the "current instantiation" that D refers
2342 // to.
2343 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002344 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002345 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002346 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002347 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002348 if (isInstantiationOf(ClassTemplate,
2349 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002350 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002351
2352 if (!DC->isDependentContext())
2353 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002354 }
2355
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002356 // We're performing "instantiation" of a member of the current
2357 // instantiation while we are type-checking the
2358 // definition. Compute the declaration context and return that.
2359 assert(!SawNonDependentContext &&
2360 "No dependent context while instantiating record");
2361 DeclContext *DC = computeDeclContext(T);
2362 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002363 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002364 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002365 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002366
Douglas Gregore95b4092009-09-16 18:34:49 +00002367 // Fall through to deal with other dependent record types (e.g.,
2368 // anonymous unions in class templates).
2369 }
John McCall52a575a2009-08-29 08:11:13 +00002370
Douglas Gregore95b4092009-09-16 18:34:49 +00002371 if (!ParentDC->isDependentContext())
2372 return D;
2373
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002374 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002375 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002376 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Douglas Gregor815215d2009-05-27 05:35:12 +00002378 if (ParentDC != D->getDeclContext()) {
2379 // We performed some kind of instantiation in the parent context,
2380 // so now we need to look into the instantiated parent context to
2381 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002382
John McCall3cb0ebd2010-03-10 03:28:59 +00002383 // If our context used to be dependent, we may need to instantiate
2384 // it before performing lookup into that context.
2385 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002386 if (!Spec->isDependentContext()) {
2387 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002388 const RecordType *Tag = T->getAs<RecordType>();
2389 assert(Tag && "type of non-dependent record is not a RecordType");
2390 if (!Tag->isBeingDefined() &&
2391 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2392 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002393 }
2394 }
2395
Douglas Gregor815215d2009-05-27 05:35:12 +00002396 NamedDecl *Result = 0;
2397 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002398 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002399 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2400 } else {
2401 // Since we don't have a name for the entity we're looking for,
2402 // our only option is to walk through all of the declarations to
2403 // find that name. This will occur in a few cases:
2404 //
2405 // - anonymous struct/union within a template
2406 // - unnamed class/struct/union/enum within a template
2407 //
2408 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002409 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002410 ParentDC->decls_begin(),
2411 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002412 }
Mike Stump1eb44332009-09-09 15:08:12 +00002413
John McCall9f54ad42009-12-10 09:41:52 +00002414 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002415 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2416 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002417 && "Unable to find instantiation of declaration!");
2418
Douglas Gregor815215d2009-05-27 05:35:12 +00002419 D = Result;
2420 }
2421
Douglas Gregor815215d2009-05-27 05:35:12 +00002422 return D;
2423}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002424
Mike Stump1eb44332009-09-09 15:08:12 +00002425/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002426/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002427void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2428 while (!PendingLocalImplicitInstantiations.empty() ||
2429 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2430 PendingImplicitInstantiation Inst;
2431
2432 if (PendingLocalImplicitInstantiations.empty()) {
2433 Inst = PendingImplicitInstantiations.front();
2434 PendingImplicitInstantiations.pop_front();
2435 } else {
2436 Inst = PendingLocalImplicitInstantiations.front();
2437 PendingLocalImplicitInstantiations.pop_front();
2438 }
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Douglas Gregor7caa6822009-07-24 20:34:43 +00002440 // Instantiate function definitions
2441 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002442 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002443 Function->getLocation(), *this,
2444 Context.getSourceManager(),
2445 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002447 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002448 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002449 continue;
2450 }
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Douglas Gregor7caa6822009-07-24 20:34:43 +00002452 // Instantiate static data member definitions.
2453 VarDecl *Var = cast<VarDecl>(Inst.first);
2454 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002455
Chandler Carruth291b4412010-02-13 10:17:50 +00002456 // Don't try to instantiate declarations if the most recent redeclaration
2457 // is invalid.
2458 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2459 continue;
2460
2461 // Check if the most recent declaration has changed the specialization kind
2462 // and removed the need for implicit instantiation.
2463 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2464 case TSK_Undeclared:
2465 assert(false && "Cannot instantitiate an undeclared specialization.");
2466 case TSK_ExplicitInstantiationDeclaration:
2467 case TSK_ExplicitInstantiationDefinition:
2468 case TSK_ExplicitSpecialization:
2469 continue; // No longer need implicit instantiation.
2470 case TSK_ImplicitInstantiation:
2471 break;
2472 }
2473
Mike Stump1eb44332009-09-09 15:08:12 +00002474 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002475 Var->getLocation(), *this,
2476 Context.getSourceManager(),
2477 "instantiating static data member "
2478 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Douglas Gregor7caa6822009-07-24 20:34:43 +00002480 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002481 }
2482}
John McCall0c01d182010-03-24 05:22:00 +00002483
2484void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2485 const MultiLevelTemplateArgumentList &TemplateArgs) {
2486 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2487 E = Pattern->ddiag_end(); I != E; ++I) {
2488 DependentDiagnostic *DD = *I;
2489
2490 switch (DD->getKind()) {
2491 case DependentDiagnostic::Access:
2492 HandleDependentAccessCheck(*DD, TemplateArgs);
2493 break;
2494 }
2495 }
2496}