blob: 10d2bb119d3340125ef6c71d97ee3b100db2e374 [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
Douglas Gregor4469e8a2010-05-19 17:02:24 +000034 void InstantiateAttrs(Decl *Tmpl, Decl *New);
Anders Carlssond8fe2d52009-11-07 06:07:58 +000035
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?
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000146void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000147 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
148 TmplAttr = TmplAttr->getNext()) {
149
150 // FIXME: Is cloning correct for all attributes?
151 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
152
153 New->addAttr(NewAttr);
154 }
155}
156
Douglas Gregor4f722be2009-03-25 15:45:12 +0000157Decl *
158TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
159 assert(false && "Translation units cannot be instantiated");
160 return D;
161}
162
163Decl *
164TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
165 assert(false && "Namespaces cannot be instantiated");
166 return D;
167}
168
John McCall3dbd3d52010-02-16 06:53:13 +0000169Decl *
170TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
171 NamespaceAliasDecl *Inst
172 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
173 D->getNamespaceLoc(),
174 D->getAliasLoc(),
175 D->getNamespace()->getIdentifier(),
176 D->getQualifierRange(),
177 D->getQualifier(),
178 D->getTargetNameLoc(),
179 D->getNamespace());
180 Owner->addDecl(Inst);
181 return Inst;
182}
183
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000184Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
185 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000186 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000187 if (DI->getType()->isDependentType()) {
188 DI = SemaRef.SubstType(DI, TemplateArgs,
189 D->getLocation(), D->getDeclName());
190 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000191 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000192 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000193 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000194 } else {
195 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000198 // Create the new typedef
199 TypedefDecl *Typedef
200 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000201 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000202 if (Invalid)
203 Typedef->setInvalidDecl();
204
Douglas Gregord57a38e2010-04-23 16:25:07 +0000205 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
206 TagDecl *TD = TT->getDecl();
207
208 // If the TagDecl that the TypedefDecl points to is an anonymous decl
209 // keep track of the TypedefDecl.
210 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
211 TD->setTypedefForAnonDecl(Typedef);
212 }
213
John McCall5126fd02009-12-30 00:31:22 +0000214 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000215 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
216 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000217 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
218 }
219
Douglas Gregora6b09072010-05-17 23:46:49 +0000220 InstantiateAttrs(D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000221
John McCall46460a62010-01-20 21:53:11 +0000222 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000223 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000225 return Typedef;
226}
227
Douglas Gregor6eef5192009-12-14 19:27:10 +0000228/// \brief Instantiate the arguments provided as part of initialization.
229///
230/// \returns true if an error occurred, false otherwise.
231static bool InstantiateInitializationArguments(Sema &SemaRef,
232 Expr **Args, unsigned NumArgs,
233 const MultiLevelTemplateArgumentList &TemplateArgs,
234 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
235 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
236 for (unsigned I = 0; I != NumArgs; ++I) {
237 // When we hit the first defaulted argument, break out of the loop:
238 // we don't pass those default arguments on.
239 if (Args[I]->isDefaultArgument())
240 break;
241
242 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
243 if (Arg.isInvalid())
244 return true;
245
246 Expr *ArgExpr = (Expr *)Arg.get();
247 InitArgs.push_back(Arg.release());
248
249 // FIXME: We're faking all of the comma locations. Do we need them?
250 FakeCommaLocs.push_back(
251 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
252 }
253
254 return false;
255}
256
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000257/// \brief Instantiate an initializer, breaking it into separate
258/// initialization arguments.
259///
260/// \param S The semantic analysis object.
261///
262/// \param Init The initializer to instantiate.
263///
264/// \param TemplateArgs Template arguments to be substituted into the
265/// initializer.
266///
267/// \param NewArgs Will be filled in with the instantiation arguments.
268///
269/// \returns true if an error occurred, false otherwise
270static bool InstantiateInitializer(Sema &S, Expr *Init,
271 const MultiLevelTemplateArgumentList &TemplateArgs,
272 SourceLocation &LParenLoc,
273 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
274 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
275 SourceLocation &RParenLoc) {
276 NewArgs.clear();
277 LParenLoc = SourceLocation();
278 RParenLoc = SourceLocation();
279
280 if (!Init)
281 return false;
282
283 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
284 Init = ExprTemp->getSubExpr();
285
286 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
287 Init = Binder->getSubExpr();
288
289 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
290 Init = ICE->getSubExprAsWritten();
291
292 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
293 LParenLoc = ParenList->getLParenLoc();
294 RParenLoc = ParenList->getRParenLoc();
295 return InstantiateInitializationArguments(S, ParenList->getExprs(),
296 ParenList->getNumExprs(),
297 TemplateArgs, CommaLocs,
298 NewArgs);
299 }
300
301 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000302 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
303 if (InstantiateInitializationArguments(S,
304 Construct->getArgs(),
305 Construct->getNumArgs(),
306 TemplateArgs,
307 CommaLocs, NewArgs))
308 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000309
Douglas Gregor28329e52010-03-24 21:22:47 +0000310 // FIXME: Fake locations!
311 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
312 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
313 return false;
314 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000315 }
316
317 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
318 if (Result.isInvalid())
319 return true;
320
321 NewArgs.push_back(Result.takeAs<Expr>());
322 return false;
323}
324
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000325Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000326 // If this is the variable for an anonymous struct or union,
327 // instantiate the anonymous struct/union type first.
328 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
329 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
330 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
331 return 0;
332
John McCallce3ff2b2009-08-25 22:02:44 +0000333 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000334 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000335 TemplateArgs,
336 D->getTypeSpecStartLoc(),
337 D->getDeclName());
338 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000339 return 0;
340
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000341 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000342 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
343 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000344 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000345 D->getStorageClass(),
346 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000347 Var->setThreadSpecified(D->isThreadSpecified());
348 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
349 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000350
John McCallb6217662010-03-15 10:12:16 +0000351 // Substitute the nested name specifier, if any.
352 if (SubstQualifier(D, Var))
353 return 0;
354
Mike Stump1eb44332009-09-09 15:08:12 +0000355 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000356 // out-of-line, the instantiation will have the same lexical
357 // context (which will be a namespace scope) as the template.
358 if (D->isOutOfLine())
359 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000360
John McCall46460a62010-01-20 21:53:11 +0000361 Var->setAccess(D->getAccess());
Douglas Gregor5764f612010-05-08 23:05:03 +0000362 Var->setUsed(D->isUsed());
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000363
Mike Stump390b4cc2009-05-16 07:39:55 +0000364 // FIXME: In theory, we could have a previous declaration for variables that
365 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000366 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000367 // FIXME: having to fake up a LookupResult is dumb.
368 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000369 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000370 if (D->isStaticDataMember())
371 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000372 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor7caa6822009-07-24 20:34:43 +0000374 if (D->isOutOfLine()) {
375 D->getLexicalDeclContext()->addDecl(Var);
376 Owner->makeDeclVisibleInContext(Var);
377 } else {
378 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000379
380 if (Owner->isFunctionOrMethod())
381 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000384 // Link instantiations of static data members back to the template from
385 // which they were instantiated.
386 if (Var->isStaticDataMember())
387 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000388 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000389
Douglas Gregor60c93c92010-02-09 07:26:29 +0000390 if (Var->getAnyInitializer()) {
391 // We already have an initializer in the class.
392 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000393 if (Var->isStaticDataMember() && !D->isOutOfLine())
394 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
395 else
396 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
397
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000398 // Instantiate the initializer.
399 SourceLocation LParenLoc, RParenLoc;
400 llvm::SmallVector<SourceLocation, 4> CommaLocs;
401 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
402 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
403 CommaLocs, InitArgs, RParenLoc)) {
404 // Attach the initializer to the declaration.
405 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000406 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000407 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000408 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000409 move_arg(InitArgs),
410 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000411 RParenLoc);
412 } else if (InitArgs.size() == 1) {
413 Expr *Init = (Expr*)(InitArgs.take()[0]);
414 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
415 SemaRef.Owned(Init),
416 false);
417 } else {
418 assert(InitArgs.size() == 0);
419 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000420 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000421 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000422 // FIXME: Not too happy about invalidating the declaration
423 // because of a bogus initializer.
424 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000425 }
426
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000427 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000428 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
429 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000430
Douglas Gregor5764f612010-05-08 23:05:03 +0000431 // Diagnose unused local variables.
432 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
433 SemaRef.DiagnoseUnusedDecl(Var);
434
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000435 return Var;
436}
437
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000438Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
439 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000440 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000441 if (DI->getType()->isDependentType()) {
442 DI = SemaRef.SubstType(DI, TemplateArgs,
443 D->getLocation(), D->getDeclName());
444 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000445 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000446 Invalid = true;
447 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000448 // C++ [temp.arg.type]p3:
449 // If a declaration acquires a function type through a type
450 // dependent on a template-parameter and this causes a
451 // declaration that does not use the syntactic form of a
452 // function declarator to have function type, the program is
453 // ill-formed.
454 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000455 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000456 Invalid = true;
457 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000458 } else {
459 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000460 }
461
462 Expr *BitWidth = D->getBitWidth();
463 if (Invalid)
464 BitWidth = 0;
465 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000466 // The bit-width expression is not potentially evaluated.
467 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000469 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000470 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000471 if (InstantiatedBitWidth.isInvalid()) {
472 Invalid = true;
473 BitWidth = 0;
474 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000475 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000476 }
477
John McCall07fb6be2009-10-22 23:33:21 +0000478 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
479 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000480 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000481 D->getLocation(),
482 D->isMutable(),
483 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000484 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000485 D->getAccess(),
486 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000487 if (!Field) {
488 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000489 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000492 InstantiateAttrs(D, Field);
493
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000494 if (Invalid)
495 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000497 if (!Field->getDeclName()) {
498 // Keep track of where this decl came from.
499 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000500 }
501 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
502 if (Parent->isAnonymousStructOrUnion() &&
503 Parent->getLookupContext()->isFunctionOrMethod())
504 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000507 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000508 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000509 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000510
511 return Field;
512}
513
John McCall02cace72009-08-28 07:59:38 +0000514Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000515 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000516 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000517 if (TypeSourceInfo *Ty = D->getFriendType()) {
518 TypeSourceInfo *InstTy =
519 SemaRef.SubstType(Ty, TemplateArgs,
520 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000521 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000522 return 0;
John McCall02cace72009-08-28 07:59:38 +0000523
Douglas Gregor06245bf2010-04-07 17:57:12 +0000524 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
525 if (!FD)
526 return 0;
527
528 FD->setAccess(AS_public);
529 Owner->addDecl(FD);
530 return FD;
531 }
532
533 NamedDecl *ND = D->getFriendDecl();
534 assert(ND && "friend decl must be a decl or a type!");
535
John McCallaf2094e2010-04-08 09:05:18 +0000536 // All of the Visit implementations for the various potential friend
537 // declarations have to be carefully written to work for friend
538 // objects, with the most important detail being that the target
539 // decl should almost certainly not be placed in Owner.
540 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000541 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
John McCall02cace72009-08-28 07:59:38 +0000543 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000544 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
545 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000546 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000547 Owner->addDecl(FD);
548 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000549}
550
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000551Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
552 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Douglas Gregorac7610d2009-06-22 20:57:11 +0000554 // The expression in a static assertion is not potentially evaluated.
555 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000558 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559 if (InstantiatedAssertExpr.isInvalid())
560 return 0;
561
Douglas Gregor43d9d922009-08-08 01:41:12 +0000562 OwningExprResult Message(SemaRef, D->getMessage());
563 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000564 Decl *StaticAssert
565 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000566 move(InstantiatedAssertExpr),
567 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000568 return StaticAssert;
569}
570
571Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000572 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000573 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000574 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000575 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000576 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000577 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000578 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000579 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000580 Enum->startDefinition();
581
Douglas Gregor96084f12010-03-01 19:00:07 +0000582 if (D->getDeclContext()->isFunctionOrMethod())
583 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
584
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000585 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000586
587 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000588 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
589 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000590 EC != ECEnd; ++EC) {
591 // The specified value for the enumerator.
592 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000593 if (Expr *UninstValue = EC->getInitExpr()) {
594 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000595 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000596 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
John McCallce3ff2b2009-08-25 22:02:44 +0000598 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000599 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000600
601 // Drop the initial value and continue.
602 bool isInvalid = false;
603 if (Value.isInvalid()) {
604 Value = SemaRef.Owned((Expr *)0);
605 isInvalid = true;
606 }
607
Mike Stump1eb44332009-09-09 15:08:12 +0000608 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000609 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
610 EC->getLocation(), EC->getIdentifier(),
611 move(Value));
612
613 if (isInvalid) {
614 if (EnumConst)
615 EnumConst->setInvalidDecl();
616 Enum->setInvalidDecl();
617 }
618
619 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000620 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000621 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000622 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000623 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000624
625 if (D->getDeclContext()->isFunctionOrMethod()) {
626 // If the enumeration is within a function or method, record the enum
627 // constant as a local.
628 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
629 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000630 }
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000633 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000634 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000635 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
636 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000637 &Enumerators[0], Enumerators.size(),
638 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000639
640 return Enum;
641}
642
Douglas Gregor6477b692009-03-25 15:04:13 +0000643Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
644 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
645 return 0;
646}
647
John McCalle29ba202009-08-20 01:44:21 +0000648Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000649 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
650
Douglas Gregor550d9b22009-10-31 17:21:17 +0000651 // Create a local instantiation scope for this class template, which
652 // will contain the instantiations of the template parameters.
653 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000654 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000655 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000656 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000657 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000658
659 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000660
661 // Instantiate the qualifier. We have to do this first in case
662 // we're a friend declaration, because if we are then we need to put
663 // the new declaration in the appropriate context.
664 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
665 if (Qualifier) {
666 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
667 Pattern->getQualifierRange(),
668 TemplateArgs);
669 if (!Qualifier) return 0;
670 }
671
672 CXXRecordDecl *PrevDecl = 0;
673 ClassTemplateDecl *PrevClassTemplate = 0;
674
675 // If this isn't a friend, then it's a member template, in which
676 // case we just want to build the instantiation in the
677 // specialization. If it is a friend, we want to build it in
678 // the appropriate context.
679 DeclContext *DC = Owner;
680 if (isFriend) {
681 if (Qualifier) {
682 CXXScopeSpec SS;
683 SS.setScopeRep(Qualifier);
684 SS.setRange(Pattern->getQualifierRange());
685 DC = SemaRef.computeDeclContext(SS);
686 if (!DC) return 0;
687 } else {
688 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
689 Pattern->getDeclContext(),
690 TemplateArgs);
691 }
692
693 // Look for a previous declaration of the template in the owning
694 // context.
695 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
696 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
697 SemaRef.LookupQualifiedName(R, DC);
698
699 if (R.isSingleResult()) {
700 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
701 if (PrevClassTemplate)
702 PrevDecl = PrevClassTemplate->getTemplatedDecl();
703 }
704
705 if (!PrevClassTemplate && Qualifier) {
706 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000707 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
708 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000709 return 0;
710 }
711
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000712 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000713 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000714 bool Complain = true;
715
716 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
717 // template for struct std::tr1::__detail::_Map_base, where the
718 // template parameters of the friend declaration don't match the
719 // template parameters of the original declaration. In this one
720 // case, we don't complain about the ill-formed friend
721 // declaration.
722 if (isFriend && Pattern->getIdentifier() &&
723 Pattern->getIdentifier()->isStr("_Map_base") &&
724 DC->isNamespace() &&
725 cast<NamespaceDecl>(DC)->getIdentifier() &&
726 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
727 DeclContext *DCParent = DC->getParent();
728 if (DCParent->isNamespace() &&
729 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
730 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
731 DeclContext *DCParent2 = DCParent->getParent();
732 if (DCParent2->isNamespace() &&
733 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
734 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
735 DCParent2->getParent()->isTranslationUnit())
736 Complain = false;
737 }
738 }
739
John McCall93ba8572010-03-25 06:39:04 +0000740 TemplateParameterList *PrevParams
741 = PrevClassTemplate->getTemplateParameters();
742
743 // Make sure the parameter lists match.
744 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000745 Complain,
746 Sema::TPL_TemplateMatch)) {
747 if (Complain)
748 return 0;
749
750 AdoptedPreviousTemplateParams = true;
751 InstParams = PrevParams;
752 }
John McCall93ba8572010-03-25 06:39:04 +0000753
754 // Do some additional validation, then merge default arguments
755 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000756 if (!AdoptedPreviousTemplateParams &&
757 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000758 Sema::TPC_ClassTemplate))
759 return 0;
760 }
761 }
762
John McCalle29ba202009-08-20 01:44:21 +0000763 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000764 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000765 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000766 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000767 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000768
John McCall93ba8572010-03-25 06:39:04 +0000769 if (Qualifier)
770 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000771
John McCalle29ba202009-08-20 01:44:21 +0000772 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000773 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
774 D->getIdentifier(), InstParams, RecordInst,
775 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000776 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000777
John McCall93ba8572010-03-25 06:39:04 +0000778 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000779 if (PrevClassTemplate)
780 Inst->setAccess(PrevClassTemplate->getAccess());
781 else
782 Inst->setAccess(D->getAccess());
783
John McCall93ba8572010-03-25 06:39:04 +0000784 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
785 // TODO: do we want to track the instantiation progeny of this
786 // friend target decl?
787 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000788 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000789 Inst->setInstantiatedFromMemberTemplate(D);
790 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000791
792 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000793 SemaRef.Context.getInjectedClassNameType(RecordInst,
794 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000795
Douglas Gregor259571e2009-10-30 22:42:42 +0000796 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000797 if (isFriend) {
798 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000799 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000800 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000801
John McCalle29ba202009-08-20 01:44:21 +0000802 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000803
Douglas Gregored9c0f92009-10-29 00:04:11 +0000804 // Instantiate all of the partial specializations of this member class
805 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000806 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
807 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000808 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
809 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
810
John McCalle29ba202009-08-20 01:44:21 +0000811 return Inst;
812}
813
Douglas Gregord60e1052009-08-27 16:57:43 +0000814Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000815TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
816 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000817 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
818
819 // Lookup the already-instantiated declaration in the instantiation
820 // of the class template and return that.
821 DeclContext::lookup_result Found
822 = Owner->lookup(ClassTemplate->getDeclName());
823 if (Found.first == Found.second)
824 return 0;
825
826 ClassTemplateDecl *InstClassTemplate
827 = dyn_cast<ClassTemplateDecl>(*Found.first);
828 if (!InstClassTemplate)
829 return 0;
830
831 Decl *DCanon = D->getCanonicalDecl();
832 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
833 P = InstClassTemplate->getPartialSpecializations().begin(),
834 PEnd = InstClassTemplate->getPartialSpecializations().end();
835 P != PEnd; ++P) {
836 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
837 return &*P;
838 }
839
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000840 return 0;
841}
842
843Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000844TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000845 // Create a local instantiation scope for this function template, which
846 // will contain the instantiations of the template parameters and then get
847 // merged with the local instantiation scope for the function template
848 // itself.
849 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000850
Douglas Gregord60e1052009-08-27 16:57:43 +0000851 TemplateParameterList *TempParams = D->getTemplateParameters();
852 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000853 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000854 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000855
Douglas Gregora735b202009-10-13 14:39:41 +0000856 FunctionDecl *Instantiated = 0;
857 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
858 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
859 InstParams));
860 else
861 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
862 D->getTemplatedDecl(),
863 InstParams));
864
865 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000866 return 0;
867
John McCall46460a62010-01-20 21:53:11 +0000868 Instantiated->setAccess(D->getAccess());
869
Mike Stump1eb44332009-09-09 15:08:12 +0000870 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000871 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000872 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000873 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000874 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000875 assert(InstTemplate &&
876 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000877
John McCallb1a56e72010-03-26 23:10:15 +0000878 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
879
John McCalle976ffe2009-12-14 23:19:40 +0000880 // Link the instantiation back to the pattern *unless* this is a
881 // non-definition friend declaration.
882 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000883 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000884 InstTemplate->setInstantiatedFromMemberTemplate(D);
885
John McCallb1a56e72010-03-26 23:10:15 +0000886 // Make declarations visible in the appropriate context.
887 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000888 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000889
Douglas Gregord60e1052009-08-27 16:57:43 +0000890 return InstTemplate;
891}
892
Douglas Gregord475b8d2009-03-25 21:17:03 +0000893Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
894 CXXRecordDecl *PrevDecl = 0;
895 if (D->isInjectedClassName())
896 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000897 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000898 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
899 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000900 TemplateArgs);
901 if (!Prev) return 0;
902 PrevDecl = cast<CXXRecordDecl>(Prev);
903 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000904
905 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000906 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000907 D->getLocation(), D->getIdentifier(),
908 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000909
910 // Substitute the nested name specifier, if any.
911 if (SubstQualifier(D, Record))
912 return 0;
913
Douglas Gregord475b8d2009-03-25 21:17:03 +0000914 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000915 // FIXME: Check against AS_none is an ugly hack to work around the issue that
916 // the tag decls introduced by friend class declarations don't have an access
917 // specifier. Remove once this area of the code gets sorted out.
918 if (D->getAccess() != AS_none)
919 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000920 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000921 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922
John McCall02cace72009-08-28 07:59:38 +0000923 // If the original function was part of a friend declaration,
924 // inherit its namespace state.
925 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
926 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
927
Douglas Gregor9901c572010-05-21 00:31:19 +0000928 // Make sure that anonymous structs and unions are recorded.
929 if (D->isAnonymousStructOrUnion()) {
930 Record->setAnonymousStructOrUnion(true);
931 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
932 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
933 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000934
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000935 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000936 return Record;
937}
938
John McCall02cace72009-08-28 07:59:38 +0000939/// Normal class members are of more specific types and therefore
940/// don't make it here. This function serves two purposes:
941/// 1) instantiating function templates
942/// 2) substituting friend declarations
943/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000944Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000945 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000946 // Check whether there is already a function template specialization for
947 // this declaration.
948 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
949 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000950 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000951 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000952 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000953 TemplateArgs.getInnermost().getFlatArgumentList(),
954 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000955 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
957 FunctionTemplateSpecializationInfo *Info
958 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000959 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregor127102b2009-06-29 20:59:39 +0000961 // If we already have a function template specialization, return it.
962 if (Info)
963 return Info->Function;
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
John McCallb0cb0222010-03-27 05:57:59 +0000966 bool isFriend;
967 if (FunctionTemplate)
968 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
969 else
970 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
971
Douglas Gregor79c22782010-01-16 20:21:20 +0000972 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000973 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000974 !(isa<Decl>(Owner) &&
975 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
976 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregore53060f2009-06-25 22:08:12 +0000978 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000979 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
980 TInfo = SubstFunctionType(D, Params);
981 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000982 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000983 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000984
John McCalld325daa2010-03-26 04:53:08 +0000985 NestedNameSpecifier *Qualifier = D->getQualifier();
986 if (Qualifier) {
987 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
988 D->getQualifierRange(),
989 TemplateArgs);
990 if (!Qualifier) return 0;
991 }
992
John McCall68b6b872010-02-06 01:50:47 +0000993 // If we're instantiating a local function declaration, put the result
994 // in the owner; otherwise we need to find the instantiated context.
995 DeclContext *DC;
996 if (D->getDeclContext()->isFunctionOrMethod())
997 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +0000998 else if (isFriend && Qualifier) {
999 CXXScopeSpec SS;
1000 SS.setScopeRep(Qualifier);
1001 SS.setRange(D->getQualifierRange());
1002 DC = SemaRef.computeDeclContext(SS);
1003 if (!DC) return 0;
1004 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001005 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1006 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001007 }
John McCall68b6b872010-02-06 01:50:47 +00001008
John McCall02cace72009-08-28 07:59:38 +00001009 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001010 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001011 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001012 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001013 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001014
John McCalld325daa2010-03-26 04:53:08 +00001015 if (Qualifier)
1016 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001017
John McCallb1a56e72010-03-26 23:10:15 +00001018 DeclContext *LexicalDC = Owner;
1019 if (!isFriend && D->isOutOfLine()) {
1020 assert(D->getDeclContext()->isFileContext());
1021 LexicalDC = D->getDeclContext();
1022 }
1023
1024 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregore53060f2009-06-25 22:08:12 +00001026 // Attach the parameters
1027 for (unsigned P = 0; P < Params.size(); ++P)
1028 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001029 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001030
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001031 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001032 if (TemplateParams) {
1033 // Our resulting instantiation is actually a function template, since we
1034 // are substituting only the outer template parameters. For example, given
1035 //
1036 // template<typename T>
1037 // struct X {
1038 // template<typename U> friend void f(T, U);
1039 // };
1040 //
1041 // X<int> x;
1042 //
1043 // We are instantiating the friend function template "f" within X<int>,
1044 // which means substituting int for T, but leaving "f" as a friend function
1045 // template.
1046 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001047 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001048 Function->getLocation(),
1049 Function->getDeclName(),
1050 TemplateParams, Function);
1051 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001052
1053 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001054
1055 if (isFriend && D->isThisDeclarationADefinition()) {
1056 // TODO: should we remember this connection regardless of whether
1057 // the friend declaration provided a body?
1058 FunctionTemplate->setInstantiatedFromMemberTemplate(
1059 D->getDescribedFunctionTemplate());
1060 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001061 } else if (FunctionTemplate) {
1062 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001063 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001064 &TemplateArgs.getInnermost(),
1065 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001066 } else if (isFriend && D->isThisDeclarationADefinition()) {
1067 // TODO: should we remember this connection regardless of whether
1068 // the friend declaration provided a body?
1069 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001070 }
Douglas Gregora735b202009-10-13 14:39:41 +00001071
Douglas Gregore53060f2009-06-25 22:08:12 +00001072 if (InitFunctionInstantiation(Function, D))
1073 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregore53060f2009-06-25 22:08:12 +00001075 bool Redeclaration = false;
1076 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001077 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001078
John McCall68263142009-11-18 22:49:29 +00001079 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1080 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1081
John McCallaf2094e2010-04-08 09:05:18 +00001082 if (DependentFunctionTemplateSpecializationInfo *Info
1083 = D->getDependentSpecializationInfo()) {
1084 assert(isFriend && "non-friend has dependent specialization info?");
1085
1086 // This needs to be set now for future sanity.
1087 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1088
1089 // Instantiate the explicit template arguments.
1090 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1091 Info->getRAngleLoc());
1092 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1093 TemplateArgumentLoc Loc;
1094 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1095 return 0;
1096
1097 ExplicitArgs.addArgument(Loc);
1098 }
1099
1100 // Map the candidate templates to their instantiations.
1101 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1102 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1103 Info->getTemplate(I),
1104 TemplateArgs);
1105 if (!Temp) return 0;
1106
1107 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1108 }
1109
1110 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1111 &ExplicitArgs,
1112 Previous))
1113 Function->setInvalidDecl();
1114
1115 isExplicitSpecialization = true;
1116
1117 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001118 // Look only into the namespace where the friend would be declared to
1119 // find a previous declaration. This is the innermost enclosing namespace,
1120 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001121 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001122
Douglas Gregora735b202009-10-13 14:39:41 +00001123 // In C++, the previous declaration we find might be a tag type
1124 // (class or enum). In this case, the new declaration will hide the
1125 // tag type. Note that this does does not apply if we're declaring a
1126 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001127 if (Previous.isSingleTagDecl())
1128 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001129 }
1130
John McCall9f54ad42009-12-10 09:41:52 +00001131 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001132 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001133 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001134
John McCall76d32642010-04-24 01:30:58 +00001135 NamedDecl *PrincipalDecl = (TemplateParams
1136 ? cast<NamedDecl>(FunctionTemplate)
1137 : Function);
1138
Douglas Gregora735b202009-10-13 14:39:41 +00001139 // If the original function was part of a friend declaration,
1140 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001141 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001142 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001143 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001144 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001145 else
Douglas Gregora735b202009-10-13 14:39:41 +00001146 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001147
1148 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1149 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001150
1151 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1152 D->isThisDeclarationADefinition()) {
1153 // Check for a function body.
1154 const FunctionDecl *Definition = 0;
1155 if (Function->getBody(Definition) &&
1156 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1157 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1158 << Function->getDeclName();
1159 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1160 Function->setInvalidDecl();
1161 }
1162 // Check for redefinitions due to other instantiations of this or
1163 // a similar friend function.
1164 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1165 REnd = Function->redecls_end();
1166 R != REnd; ++R) {
1167 if (*R != Function &&
1168 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1169 if (const FunctionDecl *RPattern
1170 = (*R)->getTemplateInstantiationPattern())
1171 if (RPattern->getBody(RPattern)) {
1172 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1173 << Function->getDeclName();
1174 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1175 Function->setInvalidDecl();
1176 break;
1177 }
1178 }
1179 }
1180 }
1181
Douglas Gregora735b202009-10-13 14:39:41 +00001182 }
1183
John McCall76d32642010-04-24 01:30:58 +00001184 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1185 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1186 PrincipalDecl->setNonMemberOperator();
1187
Douglas Gregore53060f2009-06-25 22:08:12 +00001188 return Function;
1189}
1190
Douglas Gregord60e1052009-08-27 16:57:43 +00001191Decl *
1192TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1193 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001194 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1195 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001196 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001197 // We are creating a function template specialization from a function
1198 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001199 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001200 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001201 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001202 TemplateArgs.getInnermost().getFlatArgumentList(),
1203 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001204 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
1206 FunctionTemplateSpecializationInfo *Info
1207 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001208 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Douglas Gregor6b906862009-08-21 00:16:32 +00001210 // If we already have a function template specialization, return it.
1211 if (Info)
1212 return Info->Function;
1213 }
1214
John McCallb0cb0222010-03-27 05:57:59 +00001215 bool isFriend;
1216 if (FunctionTemplate)
1217 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1218 else
1219 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1220
Douglas Gregor79c22782010-01-16 20:21:20 +00001221 bool MergeWithParentScope = (TemplateParams != 0) ||
1222 !(isa<Decl>(Owner) &&
1223 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1224 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001225
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001226 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001227 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1228 TInfo = SubstFunctionType(D, Params);
1229 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001230 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001231 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001232
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001233 // \brief If the type of this function is not *directly* a function
1234 // type, then we're instantiating the a function that was declared
1235 // via a typedef, e.g.,
1236 //
1237 // typedef int functype(int, int);
1238 // functype func;
1239 //
1240 // In this case, we'll just go instantiate the ParmVarDecls that we
1241 // synthesized in the method declaration.
1242 if (!isa<FunctionProtoType>(T)) {
1243 assert(!Params.size() && "Instantiating type could not yield parameters");
1244 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1245 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1246 TemplateArgs);
1247 if (!P)
1248 return 0;
1249
1250 Params.push_back(P);
1251 }
1252 }
1253
John McCallb0cb0222010-03-27 05:57:59 +00001254 NestedNameSpecifier *Qualifier = D->getQualifier();
1255 if (Qualifier) {
1256 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1257 D->getQualifierRange(),
1258 TemplateArgs);
1259 if (!Qualifier) return 0;
1260 }
1261
1262 DeclContext *DC = Owner;
1263 if (isFriend) {
1264 if (Qualifier) {
1265 CXXScopeSpec SS;
1266 SS.setScopeRep(Qualifier);
1267 SS.setRange(D->getQualifierRange());
1268 DC = SemaRef.computeDeclContext(SS);
1269 } else {
1270 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1271 D->getDeclContext(),
1272 TemplateArgs);
1273 }
1274 if (!DC) return 0;
1275 }
1276
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001277 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001278 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001279 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Douglas Gregordec06662009-08-21 18:42:58 +00001281 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001282 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001283 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1284 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1285 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001286 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1287 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001288 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001289 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001290 Constructor->isInlineSpecified(),
1291 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001292 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1293 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1294 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1295 SemaRef.Context.getCanonicalType(ClassTy));
1296 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1297 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001298 T, Destructor->isInlineSpecified(),
1299 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001300 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001301 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001302 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001303 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001304 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1305 ConvTy);
1306 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1307 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001308 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001309 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001310 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001311 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001312 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001313 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001314 D->isStatic(),
1315 D->getStorageClassAsWritten(),
1316 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001317 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001318
John McCallb0cb0222010-03-27 05:57:59 +00001319 if (Qualifier)
1320 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001321
Douglas Gregord60e1052009-08-27 16:57:43 +00001322 if (TemplateParams) {
1323 // Our resulting instantiation is actually a function template, since we
1324 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001325 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001326 // template<typename T>
1327 // struct X {
1328 // template<typename U> void f(T, U);
1329 // };
1330 //
1331 // X<int> x;
1332 //
1333 // We are instantiating the member template "f" within X<int>, which means
1334 // substituting int for T, but leaving "f" as a member function template.
1335 // Build the function template itself.
1336 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1337 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001338 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001339 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001340 if (isFriend) {
1341 FunctionTemplate->setLexicalDeclContext(Owner);
1342 FunctionTemplate->setObjectOfFriendDecl(true);
1343 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001344 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001345 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001346 } else if (FunctionTemplate) {
1347 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001348 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001349 &TemplateArgs.getInnermost(),
1350 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001351 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001352 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001353 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001354 }
1355
Mike Stump1eb44332009-09-09 15:08:12 +00001356 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001357 // out-of-line, the instantiation will have the same lexical
1358 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001359 if (isFriend) {
1360 Method->setLexicalDeclContext(Owner);
1361 Method->setObjectOfFriendDecl(true);
1362 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001363 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Douglas Gregor5545e162009-03-24 00:38:23 +00001365 // Attach the parameters
1366 for (unsigned P = 0; P < Params.size(); ++P)
1367 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001368 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001369
1370 if (InitMethodInstantiation(Method, D))
1371 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001372
John McCall68263142009-11-18 22:49:29 +00001373 LookupResult Previous(SemaRef, Name, SourceLocation(),
1374 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001375
John McCallb0cb0222010-03-27 05:57:59 +00001376 if (!FunctionTemplate || TemplateParams || isFriend) {
1377 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregordec06662009-08-21 18:42:58 +00001379 // In C++, the previous declaration we find might be a tag type
1380 // (class or enum). In this case, the new declaration will hide the
1381 // tag type. Note that this does does not apply if we're declaring a
1382 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001383 if (Previous.isSingleTagDecl())
1384 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001385 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001386
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001387 bool Redeclaration = false;
1388 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001389 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001390 /*FIXME:*/OverloadableAttrRequired);
1391
Douglas Gregor4ba31362009-12-01 17:24:26 +00001392 if (D->isPure())
1393 SemaRef.CheckPureMethod(Method, SourceRange());
1394
John McCall46460a62010-01-20 21:53:11 +00001395 Method->setAccess(D->getAccess());
1396
John McCallb0cb0222010-03-27 05:57:59 +00001397 if (FunctionTemplate) {
1398 // If there's a function template, let our caller handle it.
1399 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1400 // Don't hide a (potentially) valid declaration with an invalid one.
1401 } else {
1402 NamedDecl *DeclToAdd = (TemplateParams
1403 ? cast<NamedDecl>(FunctionTemplate)
1404 : Method);
1405 if (isFriend)
1406 Record->makeDeclVisibleInContext(DeclToAdd);
1407 else
1408 Owner->addDecl(DeclToAdd);
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001411 return Method;
1412}
1413
Douglas Gregor615c5d42009-03-24 16:43:20 +00001414Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001415 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001416}
1417
Douglas Gregor03b2b072009-03-24 00:15:49 +00001418Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001419 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001420}
1421
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001422Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001423 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001424}
1425
Douglas Gregor6477b692009-03-25 15:04:13 +00001426ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001427 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001428}
1429
John McCalle29ba202009-08-20 01:44:21 +00001430Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1431 TemplateTypeParmDecl *D) {
1432 // TODO: don't always clone when decls are refcounted.
1433 const Type* T = D->getTypeForDecl();
1434 assert(T->isTemplateTypeParmType());
1435 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001436
John McCalle29ba202009-08-20 01:44:21 +00001437 TemplateTypeParmDecl *Inst =
1438 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001439 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001440 TTPT->getName(),
1441 D->wasDeclaredWithTypename(),
1442 D->isParameterPack());
1443
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001444 if (D->hasDefaultArgument())
1445 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001446
Douglas Gregor550d9b22009-10-31 17:21:17 +00001447 // Introduce this template parameter's instantiation into the instantiation
1448 // scope.
1449 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1450
John McCalle29ba202009-08-20 01:44:21 +00001451 return Inst;
1452}
1453
Douglas Gregor33642df2009-10-23 23:25:44 +00001454Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1455 NonTypeTemplateParmDecl *D) {
1456 // Substitute into the type of the non-type template parameter.
1457 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001458 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001459 if (DI) {
1460 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1461 D->getDeclName());
1462 if (DI) T = DI->getType();
1463 } else {
1464 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1465 D->getDeclName());
1466 DI = 0;
1467 }
1468 if (T.isNull())
1469 return 0;
1470
1471 // Check that this type is acceptable for a non-type template parameter.
1472 bool Invalid = false;
1473 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1474 if (T.isNull()) {
1475 T = SemaRef.Context.IntTy;
1476 Invalid = true;
1477 }
1478
1479 NonTypeTemplateParmDecl *Param
1480 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1481 D->getDepth() - 1, D->getPosition(),
1482 D->getIdentifier(), T, DI);
1483 if (Invalid)
1484 Param->setInvalidDecl();
1485
1486 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001487
1488 // Introduce this template parameter's instantiation into the instantiation
1489 // scope.
1490 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001491 return Param;
1492}
1493
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001494Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001495TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1496 TemplateTemplateParmDecl *D) {
1497 // Instantiate the template parameter list of the template template parameter.
1498 TemplateParameterList *TempParams = D->getTemplateParameters();
1499 TemplateParameterList *InstParams;
1500 {
1501 // Perform the actual substitution of template parameters within a new,
1502 // local instantiation scope.
1503 Sema::LocalInstantiationScope Scope(SemaRef);
1504 InstParams = SubstTemplateParams(TempParams);
1505 if (!InstParams)
1506 return NULL;
1507 }
1508
1509 // Build the template template parameter.
1510 TemplateTemplateParmDecl *Param
1511 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1512 D->getDepth() - 1, D->getPosition(),
1513 D->getIdentifier(), InstParams);
1514 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001515
Douglas Gregor9106ef72009-11-11 16:58:32 +00001516 // Introduce this template parameter's instantiation into the instantiation
1517 // scope.
1518 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1519
1520 return Param;
1521}
1522
Douglas Gregor48c32a72009-11-17 06:07:40 +00001523Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1524 // Using directives are never dependent, so they require no explicit
1525
1526 UsingDirectiveDecl *Inst
1527 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1528 D->getNamespaceKeyLocation(),
1529 D->getQualifierRange(), D->getQualifier(),
1530 D->getIdentLocation(),
1531 D->getNominatedNamespace(),
1532 D->getCommonAncestor());
1533 Owner->addDecl(Inst);
1534 return Inst;
1535}
1536
John McCalled976492009-12-04 22:46:56 +00001537Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1538 // The nested name specifier is non-dependent, so no transformation
1539 // is required.
1540
John McCall9f54ad42009-12-10 09:41:52 +00001541 // We only need to do redeclaration lookups if we're in a class
1542 // scope (in fact, it's not really even possible in non-class
1543 // scopes).
1544 bool CheckRedeclaration = Owner->isRecord();
1545
1546 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1547 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1548
John McCalled976492009-12-04 22:46:56 +00001549 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1550 D->getLocation(),
1551 D->getNestedNameRange(),
1552 D->getUsingLocation(),
1553 D->getTargetNestedNameDecl(),
1554 D->getDeclName(),
1555 D->isTypeName());
1556
1557 CXXScopeSpec SS;
1558 SS.setScopeRep(D->getTargetNestedNameDecl());
1559 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001560
1561 if (CheckRedeclaration) {
1562 Prev.setHideTags(false);
1563 SemaRef.LookupQualifiedName(Prev, Owner);
1564
1565 // Check for invalid redeclarations.
1566 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1567 D->isTypeName(), SS,
1568 D->getLocation(), Prev))
1569 NewUD->setInvalidDecl();
1570
1571 }
1572
1573 if (!NewUD->isInvalidDecl() &&
1574 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001575 D->getLocation()))
1576 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001577
John McCalled976492009-12-04 22:46:56 +00001578 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1579 NewUD->setAccess(D->getAccess());
1580 Owner->addDecl(NewUD);
1581
John McCall9f54ad42009-12-10 09:41:52 +00001582 // Don't process the shadow decls for an invalid decl.
1583 if (NewUD->isInvalidDecl())
1584 return NewUD;
1585
John McCall323c3102009-12-22 22:26:37 +00001586 bool isFunctionScope = Owner->isFunctionOrMethod();
1587
John McCall9f54ad42009-12-10 09:41:52 +00001588 // Process the shadow decls.
1589 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1590 I != E; ++I) {
1591 UsingShadowDecl *Shadow = *I;
1592 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001593 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1594 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001595 TemplateArgs));
1596
1597 if (CheckRedeclaration &&
1598 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1599 continue;
1600
1601 UsingShadowDecl *InstShadow
1602 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1603 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001604
1605 if (isFunctionScope)
1606 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001607 }
John McCalled976492009-12-04 22:46:56 +00001608
1609 return NewUD;
1610}
1611
1612Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001613 // Ignore these; we handle them in bulk when processing the UsingDecl.
1614 return 0;
John McCalled976492009-12-04 22:46:56 +00001615}
1616
John McCall7ba107a2009-11-18 02:36:19 +00001617Decl * TemplateDeclInstantiator
1618 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001619 NestedNameSpecifier *NNS =
1620 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1621 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001622 TemplateArgs);
1623 if (!NNS)
1624 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001626 CXXScopeSpec SS;
1627 SS.setRange(D->getTargetNestedNameRange());
1628 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001629
1630 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001631 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001632 D->getUsingLoc(), SS, D->getLocation(),
1633 D->getDeclName(), 0,
1634 /*instantiation*/ true,
1635 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001636 if (UD)
John McCalled976492009-12-04 22:46:56 +00001637 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1638
John McCall7ba107a2009-11-18 02:36:19 +00001639 return UD;
1640}
1641
1642Decl * TemplateDeclInstantiator
1643 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1644 NestedNameSpecifier *NNS =
1645 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1646 D->getTargetNestedNameRange(),
1647 TemplateArgs);
1648 if (!NNS)
1649 return 0;
1650
1651 CXXScopeSpec SS;
1652 SS.setRange(D->getTargetNestedNameRange());
1653 SS.setScopeRep(NNS);
1654
1655 NamedDecl *UD =
1656 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1657 D->getUsingLoc(), SS, D->getLocation(),
1658 D->getDeclName(), 0,
1659 /*instantiation*/ true,
1660 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001661 if (UD)
John McCalled976492009-12-04 22:46:56 +00001662 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1663
Anders Carlsson0d8df782009-08-29 19:37:28 +00001664 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001665}
1666
John McCallce3ff2b2009-08-25 22:02:44 +00001667Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001668 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001669 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001670 if (D->isInvalidDecl())
1671 return 0;
1672
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001673 return Instantiator.Visit(D);
1674}
1675
John McCalle29ba202009-08-20 01:44:21 +00001676/// \brief Instantiates a nested template parameter list in the current
1677/// instantiation context.
1678///
1679/// \param L The parameter list to instantiate
1680///
1681/// \returns NULL if there was an error
1682TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001683TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001684 // Get errors for all the parameters before bailing out.
1685 bool Invalid = false;
1686
1687 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001688 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001689 ParamVector Params;
1690 Params.reserve(N);
1691 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1692 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001693 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001694 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001695 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001696 }
1697
1698 // Clean up if we had an error.
1699 if (Invalid) {
1700 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1701 PI != PE; ++PI)
1702 if (*PI)
1703 (*PI)->Destroy(SemaRef.Context);
1704 return NULL;
1705 }
1706
1707 TemplateParameterList *InstL
1708 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1709 L->getLAngleLoc(), &Params.front(), N,
1710 L->getRAngleLoc());
1711 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001712}
John McCalle29ba202009-08-20 01:44:21 +00001713
Douglas Gregored9c0f92009-10-29 00:04:11 +00001714/// \brief Instantiate the declaration of a class template partial
1715/// specialization.
1716///
1717/// \param ClassTemplate the (instantiated) class template that is partially
1718// specialized by the instantiation of \p PartialSpec.
1719///
1720/// \param PartialSpec the (uninstantiated) class template partial
1721/// specialization that we are instantiating.
1722///
1723/// \returns true if there was an error, false otherwise.
1724bool
1725TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1726 ClassTemplateDecl *ClassTemplate,
1727 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001728 // Create a local instantiation scope for this class template partial
1729 // specialization, which will contain the instantiations of the template
1730 // parameters.
1731 Sema::LocalInstantiationScope Scope(SemaRef);
1732
Douglas Gregored9c0f92009-10-29 00:04:11 +00001733 // Substitute into the template parameters of the class template partial
1734 // specialization.
1735 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1736 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1737 if (!InstParams)
1738 return true;
1739
1740 // Substitute into the template arguments of the class template partial
1741 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001742 const TemplateArgumentLoc *PartialSpecTemplateArgs
1743 = PartialSpec->getTemplateArgsAsWritten();
1744 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1745
John McCalld5532b62009-11-23 01:53:49 +00001746 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001747 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001748 TemplateArgumentLoc Loc;
1749 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001750 return true;
John McCalld5532b62009-11-23 01:53:49 +00001751 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001752 }
1753
1754
1755 // Check that the template argument list is well-formed for this
1756 // class template.
1757 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1758 InstTemplateArgs.size());
1759 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1760 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001761 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001762 false,
1763 Converted))
1764 return true;
1765
1766 // Figure out where to insert this class template partial specialization
1767 // in the member template's set of class template partial specializations.
1768 llvm::FoldingSetNodeID ID;
1769 ClassTemplatePartialSpecializationDecl::Profile(ID,
1770 Converted.getFlatArguments(),
1771 Converted.flatSize(),
1772 SemaRef.Context);
1773 void *InsertPos = 0;
1774 ClassTemplateSpecializationDecl *PrevDecl
1775 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1776 InsertPos);
1777
1778 // Build the canonical type that describes the converted template
1779 // arguments of the class template partial specialization.
1780 QualType CanonType
1781 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1782 Converted.getFlatArguments(),
1783 Converted.flatSize());
1784
1785 // Build the fully-sugared type for this class template
1786 // specialization as the user wrote in the specialization
1787 // itself. This means that we'll pretty-print the type retrieved
1788 // from the specialization's declaration the way that the user
1789 // actually wrote the specialization, rather than formatting the
1790 // name based on the "canonical" representation used to store the
1791 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001792 TypeSourceInfo *WrittenTy
1793 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1794 TemplateName(ClassTemplate),
1795 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001796 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001797 CanonType);
1798
1799 if (PrevDecl) {
1800 // We've already seen a partial specialization with the same template
1801 // parameters and template arguments. This can happen, for example, when
1802 // substituting the outer template arguments ends up causing two
1803 // class template partial specializations of a member class template
1804 // to have identical forms, e.g.,
1805 //
1806 // template<typename T, typename U>
1807 // struct Outer {
1808 // template<typename X, typename Y> struct Inner;
1809 // template<typename Y> struct Inner<T, Y>;
1810 // template<typename Y> struct Inner<U, Y>;
1811 // };
1812 //
1813 // Outer<int, int> outer; // error: the partial specializations of Inner
1814 // // have the same signature.
1815 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1816 << WrittenTy;
1817 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1818 << SemaRef.Context.getTypeDeclType(PrevDecl);
1819 return true;
1820 }
1821
1822
1823 // Create the class template partial specialization declaration.
1824 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001825 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1826 PartialSpec->getTagKind(),
1827 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001828 PartialSpec->getLocation(),
1829 InstParams,
1830 ClassTemplate,
1831 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001832 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001833 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001834 0,
1835 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001836 // Substitute the nested name specifier, if any.
1837 if (SubstQualifier(PartialSpec, InstPartialSpec))
1838 return 0;
1839
Douglas Gregored9c0f92009-10-29 00:04:11 +00001840 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001841 InstPartialSpec->setTypeAsWritten(WrittenTy);
1842
Douglas Gregored9c0f92009-10-29 00:04:11 +00001843 // Add this partial specialization to the set of class template partial
1844 // specializations.
1845 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1846 InsertPos);
1847 return false;
1848}
1849
John McCall21ef0fa2010-03-11 09:03:00 +00001850TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001851TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001852 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001853 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1854 assert(OldTInfo && "substituting function without type source info");
1855 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001856 TypeSourceInfo *NewTInfo
1857 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1858 D->getTypeSpecStartLoc(),
1859 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001860 if (!NewTInfo)
1861 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001862
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001863 if (NewTInfo != OldTInfo) {
1864 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001865 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001866 if (FunctionProtoTypeLoc *OldProtoLoc
1867 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1868 TypeLoc NewTL = NewTInfo->getTypeLoc();
1869 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1870 assert(NewProtoLoc && "Missing prototype?");
1871 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1872 // FIXME: Variadic templates will break this.
1873 Params.push_back(NewProtoLoc->getArg(i));
1874 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001875 OldProtoLoc->getArg(i),
1876 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001877 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001878 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001879 } else {
1880 // The function type itself was not dependent and therefore no
1881 // substitution occurred. However, we still need to instantiate
1882 // the function parameters themselves.
1883 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001884 if (FunctionProtoTypeLoc *OldProtoLoc
1885 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1886 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1887 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1888 if (!Parm)
1889 return 0;
1890 Params.push_back(Parm);
1891 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001892 }
1893 }
John McCall21ef0fa2010-03-11 09:03:00 +00001894 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001895}
1896
Mike Stump1eb44332009-09-09 15:08:12 +00001897/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001898/// declaration (New) from the corresponding fields of its template (Tmpl).
1899///
1900/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001901bool
1902TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001903 FunctionDecl *Tmpl) {
1904 if (Tmpl->isDeleted())
1905 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Douglas Gregorcca9e962009-07-01 22:01:06 +00001907 // If we are performing substituting explicitly-specified template arguments
1908 // or deduced template arguments into a function template and we reach this
1909 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001910 // to keeping the new function template specialization. We therefore
1911 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001912 // into a template instantiation for this specific function template
1913 // specialization, which is not a SFINAE context, so that we diagnose any
1914 // further errors in the declaration itself.
1915 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1916 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1917 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1918 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001919 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001920 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001922 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001923 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001924 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1925 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001926 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001927 }
1928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001930 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1931 assert(Proto && "Function template without prototype?");
1932
1933 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1934 Proto->getNoReturnAttr()) {
1935 // The function has an exception specification or a "noreturn"
1936 // attribute. Substitute into each of the exception types.
1937 llvm::SmallVector<QualType, 4> Exceptions;
1938 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1939 // FIXME: Poor location information!
1940 QualType T
1941 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1942 New->getLocation(), New->getDeclName());
1943 if (T.isNull() ||
1944 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1945 continue;
1946
1947 Exceptions.push_back(T);
1948 }
1949
1950 // Rebuild the function type
1951
1952 const FunctionProtoType *NewProto
1953 = New->getType()->getAs<FunctionProtoType>();
1954 assert(NewProto && "Template instantiation without function prototype?");
1955 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1956 NewProto->arg_type_begin(),
1957 NewProto->getNumArgs(),
1958 NewProto->isVariadic(),
1959 NewProto->getTypeQuals(),
1960 Proto->hasExceptionSpec(),
1961 Proto->hasAnyExceptionSpec(),
1962 Exceptions.size(),
1963 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001964 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001965 }
1966
Douglas Gregore53060f2009-06-25 22:08:12 +00001967 return false;
1968}
1969
Douglas Gregor5545e162009-03-24 00:38:23 +00001970/// \brief Initializes common fields of an instantiated method
1971/// declaration (New) from the corresponding fields of its template
1972/// (Tmpl).
1973///
1974/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001975bool
1976TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001977 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001978 if (InitFunctionInstantiation(New, Tmpl))
1979 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Douglas Gregor5545e162009-03-24 00:38:23 +00001981 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1982 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001983 if (Tmpl->isVirtualAsWritten())
1984 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001985
1986 // FIXME: attributes
1987 // FIXME: New needs a pointer to Tmpl
1988 return false;
1989}
Douglas Gregora58861f2009-05-13 20:28:22 +00001990
1991/// \brief Instantiate the definition of the given function from its
1992/// template.
1993///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001994/// \param PointOfInstantiation the point at which the instantiation was
1995/// required. Note that this is not precisely a "point of instantiation"
1996/// for the function, but it's close.
1997///
Douglas Gregora58861f2009-05-13 20:28:22 +00001998/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001999/// function template specialization or member function of a class template
2000/// specialization.
2001///
2002/// \param Recursive if true, recursively instantiates any functions that
2003/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002004///
2005/// \param DefinitionRequired if true, then we are performing an explicit
2006/// instantiation where the body of the function is required. Complain if
2007/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002008void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002009 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002010 bool Recursive,
2011 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00002012 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002013 return;
2014
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002015 // Never instantiate an explicit specialization.
2016 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2017 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002018
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002019 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002020 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002021 Stmt *Pattern = 0;
2022 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002023 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002024
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002025 if (!Pattern) {
2026 if (DefinitionRequired) {
2027 if (Function->getPrimaryTemplate())
2028 Diag(PointOfInstantiation,
2029 diag::err_explicit_instantiation_undefined_func_template)
2030 << Function->getPrimaryTemplate();
2031 else
2032 Diag(PointOfInstantiation,
2033 diag::err_explicit_instantiation_undefined_member)
2034 << 1 << Function->getDeclName() << Function->getDeclContext();
2035
2036 if (PatternDecl)
2037 Diag(PatternDecl->getLocation(),
2038 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002039 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002040 }
2041
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002042 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002043 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002044
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002045 // C++0x [temp.explicit]p9:
2046 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002047 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002048 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002049 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002050 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002051 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002052 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002054 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2055 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002056 return;
2057
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002058 // If we're performing recursive template instantiation, create our own
2059 // queue of pending implicit instantiations that we will instantiate later,
2060 // while we're still within our own instantiation context.
2061 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2062 if (Recursive)
2063 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Douglas Gregor9679caf2010-05-12 17:27:19 +00002065 EnterExpressionEvaluationContext EvalContext(*this,
2066 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002067 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2068
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002069 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002070 // recorded, unless we're actually a member function within a local
2071 // class, in which case we need to merge our results with the parent
2072 // scope (of the enclosing function).
2073 bool MergeWithParentScope = false;
2074 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2075 MergeWithParentScope = Rec->isLocalClass();
2076
2077 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002079 // Introduce the instantiated function parameters into the local
2080 // instantiation scope.
2081 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2082 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2083 Function->getParamDecl(I));
2084
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002085 // Enter the scope of this instantiation. We don't use
2086 // PushDeclContext because we don't have a scope.
2087 DeclContext *PreviousContext = CurContext;
2088 CurContext = Function;
2089
Mike Stump1eb44332009-09-09 15:08:12 +00002090 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002091 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002092
2093 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002094 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002095 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2096 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2097 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002098 }
2099
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002100 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002101 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002102
Douglas Gregor52604ab2009-09-11 21:19:12 +00002103 if (Body.isInvalid())
2104 Function->setInvalidDecl();
2105
Mike Stump1eb44332009-09-09 15:08:12 +00002106 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002107 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002108
John McCall0c01d182010-03-24 05:22:00 +00002109 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2110
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002111 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002112
2113 DeclGroupRef DG(Function);
2114 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Douglas Gregor60406be2010-01-16 22:29:39 +00002116 // This class may have local implicit instantiations that need to be
2117 // instantiation within this scope.
2118 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2119 Scope.Exit();
2120
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002121 if (Recursive) {
2122 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002123 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002124 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002126 // Restore the set of pending implicit instantiations.
2127 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2128 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002129}
2130
2131/// \brief Instantiate the definition of the given variable from its
2132/// template.
2133///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002134/// \param PointOfInstantiation the point at which the instantiation was
2135/// required. Note that this is not precisely a "point of instantiation"
2136/// for the function, but it's close.
2137///
2138/// \param Var the already-instantiated declaration of a static member
2139/// variable of a class template specialization.
2140///
2141/// \param Recursive if true, recursively instantiates any functions that
2142/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002143///
2144/// \param DefinitionRequired if true, then we are performing an explicit
2145/// instantiation where an out-of-line definition of the member variable
2146/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002147void Sema::InstantiateStaticDataMemberDefinition(
2148 SourceLocation PointOfInstantiation,
2149 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002150 bool Recursive,
2151 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002152 if (Var->isInvalidDecl())
2153 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Douglas Gregor7caa6822009-07-24 20:34:43 +00002155 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002156 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002157 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002158 assert(Def->isStaticDataMember() && "Not a static data member?");
2159 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Douglas Gregor0d035142009-10-27 18:42:08 +00002161 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002162 // We did not find an out-of-line definition of this static data member,
2163 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002164 // instantiate this definition (or provide a specialization for it) in
2165 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002166 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002167 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002168 Diag(PointOfInstantiation,
2169 diag::err_explicit_instantiation_undefined_member)
2170 << 2 << Var->getDeclName() << Var->getDeclContext();
2171 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2172 }
2173
Douglas Gregor7caa6822009-07-24 20:34:43 +00002174 return;
2175 }
2176
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002177 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002178 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002179 return;
2180
2181 // C++0x [temp.explicit]p9:
2182 // Except for inline functions, other explicit instantiation declarations
2183 // have the effect of suppressing the implicit instantiation of the entity
2184 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002185 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002186 == TSK_ExplicitInstantiationDeclaration)
2187 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Douglas Gregor7caa6822009-07-24 20:34:43 +00002189 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2190 if (Inst)
2191 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Douglas Gregor7caa6822009-07-24 20:34:43 +00002193 // If we're performing recursive template instantiation, create our own
2194 // queue of pending implicit instantiations that we will instantiate later,
2195 // while we're still within our own instantiation context.
2196 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2197 if (Recursive)
2198 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Douglas Gregor7caa6822009-07-24 20:34:43 +00002200 // Enter the scope of this instantiation. We don't use
2201 // PushDeclContext because we don't have a scope.
2202 DeclContext *PreviousContext = CurContext;
2203 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002205 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002206 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002207 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002208 CurContext = PreviousContext;
2209
2210 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002211 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2212 assert(MSInfo && "Missing member specialization information?");
2213 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2214 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002215 DeclGroupRef DG(Var);
2216 Consumer.HandleTopLevelDecl(DG);
2217 }
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Douglas Gregor7caa6822009-07-24 20:34:43 +00002219 if (Recursive) {
2220 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002221 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002222 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Douglas Gregor7caa6822009-07-24 20:34:43 +00002224 // Restore the set of pending implicit instantiations.
2225 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002226 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002227}
Douglas Gregor815215d2009-05-27 05:35:12 +00002228
Anders Carlsson09025312009-08-29 05:16:22 +00002229void
2230Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2231 const CXXConstructorDecl *Tmpl,
2232 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Anders Carlsson09025312009-08-29 05:16:22 +00002234 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002235 bool AnyErrors = false;
2236
Anders Carlsson09025312009-08-29 05:16:22 +00002237 // Instantiate all the initializers.
2238 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002239 InitsEnd = Tmpl->init_end();
2240 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002241 CXXBaseOrMemberInitializer *Init = *Inits;
2242
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002243 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002244 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002245 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002247 // Instantiate the initializer.
2248 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2249 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2250 AnyErrors = true;
2251 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002252 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002253
Anders Carlsson09025312009-08-29 05:16:22 +00002254 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002255 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002256 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002257 TemplateArgs,
2258 Init->getSourceLocation(),
2259 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002260 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002261 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002262 New->setInvalidDecl();
2263 continue;
2264 }
2265
John McCalla93c9342009-12-07 02:54:59 +00002266 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002267 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002268 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002269 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002270 Init->getRParenLoc(),
2271 New->getParent());
2272 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002273 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002275 // Is this an anonymous union?
2276 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002277 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2278 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002279 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002280 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2281 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002282 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002283
2284 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002285 NewArgs.size(),
2286 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002287 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002288 Init->getRParenLoc());
2289 }
2290
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002291 if (NewInit.isInvalid()) {
2292 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002293 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002294 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002295 // FIXME: It would be nice if ASTOwningVector had a release function.
2296 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Anders Carlsson09025312009-08-29 05:16:22 +00002298 NewInits.push_back((MemInitTy *)NewInit.get());
2299 }
2300 }
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Anders Carlsson09025312009-08-29 05:16:22 +00002302 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002303 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002304 /*FIXME: ColonLoc */
2305 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002306 NewInits.data(), NewInits.size(),
2307 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002308}
2309
John McCall52a575a2009-08-29 08:11:13 +00002310// TODO: this could be templated if the various decl types used the
2311// same method name.
2312static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2313 ClassTemplateDecl *Instance) {
2314 Pattern = Pattern->getCanonicalDecl();
2315
2316 do {
2317 Instance = Instance->getCanonicalDecl();
2318 if (Pattern == Instance) return true;
2319 Instance = Instance->getInstantiatedFromMemberTemplate();
2320 } while (Instance);
2321
2322 return false;
2323}
2324
Douglas Gregor0d696532009-09-28 06:34:35 +00002325static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2326 FunctionTemplateDecl *Instance) {
2327 Pattern = Pattern->getCanonicalDecl();
2328
2329 do {
2330 Instance = Instance->getCanonicalDecl();
2331 if (Pattern == Instance) return true;
2332 Instance = Instance->getInstantiatedFromMemberTemplate();
2333 } while (Instance);
2334
2335 return false;
2336}
2337
Douglas Gregored9c0f92009-10-29 00:04:11 +00002338static bool
2339isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2340 ClassTemplatePartialSpecializationDecl *Instance) {
2341 Pattern
2342 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2343 do {
2344 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2345 Instance->getCanonicalDecl());
2346 if (Pattern == Instance)
2347 return true;
2348 Instance = Instance->getInstantiatedFromMember();
2349 } while (Instance);
2350
2351 return false;
2352}
2353
John McCall52a575a2009-08-29 08:11:13 +00002354static bool isInstantiationOf(CXXRecordDecl *Pattern,
2355 CXXRecordDecl *Instance) {
2356 Pattern = Pattern->getCanonicalDecl();
2357
2358 do {
2359 Instance = Instance->getCanonicalDecl();
2360 if (Pattern == Instance) return true;
2361 Instance = Instance->getInstantiatedFromMemberClass();
2362 } while (Instance);
2363
2364 return false;
2365}
2366
2367static bool isInstantiationOf(FunctionDecl *Pattern,
2368 FunctionDecl *Instance) {
2369 Pattern = Pattern->getCanonicalDecl();
2370
2371 do {
2372 Instance = Instance->getCanonicalDecl();
2373 if (Pattern == Instance) return true;
2374 Instance = Instance->getInstantiatedFromMemberFunction();
2375 } while (Instance);
2376
2377 return false;
2378}
2379
2380static bool isInstantiationOf(EnumDecl *Pattern,
2381 EnumDecl *Instance) {
2382 Pattern = Pattern->getCanonicalDecl();
2383
2384 do {
2385 Instance = Instance->getCanonicalDecl();
2386 if (Pattern == Instance) return true;
2387 Instance = Instance->getInstantiatedFromMemberEnum();
2388 } while (Instance);
2389
2390 return false;
2391}
2392
John McCalled976492009-12-04 22:46:56 +00002393static bool isInstantiationOf(UsingShadowDecl *Pattern,
2394 UsingShadowDecl *Instance,
2395 ASTContext &C) {
2396 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2397}
2398
2399static bool isInstantiationOf(UsingDecl *Pattern,
2400 UsingDecl *Instance,
2401 ASTContext &C) {
2402 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2403}
2404
John McCall7ba107a2009-11-18 02:36:19 +00002405static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2406 UsingDecl *Instance,
2407 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002408 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002409}
2410
2411static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002412 UsingDecl *Instance,
2413 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002414 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002415}
2416
John McCall52a575a2009-08-29 08:11:13 +00002417static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2418 VarDecl *Instance) {
2419 assert(Instance->isStaticDataMember());
2420
2421 Pattern = Pattern->getCanonicalDecl();
2422
2423 do {
2424 Instance = Instance->getCanonicalDecl();
2425 if (Pattern == Instance) return true;
2426 Instance = Instance->getInstantiatedFromStaticDataMember();
2427 } while (Instance);
2428
2429 return false;
2430}
2431
John McCalled976492009-12-04 22:46:56 +00002432// Other is the prospective instantiation
2433// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002434static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002435 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002436 if (UnresolvedUsingTypenameDecl *UUD
2437 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2438 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2439 return isInstantiationOf(UUD, UD, Ctx);
2440 }
2441 }
2442
2443 if (UnresolvedUsingValueDecl *UUD
2444 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002445 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2446 return isInstantiationOf(UUD, UD, Ctx);
2447 }
2448 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002449
Anders Carlsson0d8df782009-08-29 19:37:28 +00002450 return false;
2451 }
Mike Stump1eb44332009-09-09 15:08:12 +00002452
John McCall52a575a2009-08-29 08:11:13 +00002453 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2454 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002455
John McCall52a575a2009-08-29 08:11:13 +00002456 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2457 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002458
John McCall52a575a2009-08-29 08:11:13 +00002459 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2460 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002461
Douglas Gregor7caa6822009-07-24 20:34:43 +00002462 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002463 if (Var->isStaticDataMember())
2464 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2465
2466 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2467 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002468
Douglas Gregor0d696532009-09-28 06:34:35 +00002469 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2470 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2471
Douglas Gregored9c0f92009-10-29 00:04:11 +00002472 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2473 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2474 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2475 PartialSpec);
2476
Anders Carlssond8b285f2009-09-01 04:26:58 +00002477 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2478 if (!Field->getDeclName()) {
2479 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002480 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002481 cast<FieldDecl>(D);
2482 }
2483 }
Mike Stump1eb44332009-09-09 15:08:12 +00002484
John McCalled976492009-12-04 22:46:56 +00002485 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2486 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2487
2488 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2489 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2490
Douglas Gregor815215d2009-05-27 05:35:12 +00002491 return D->getDeclName() && isa<NamedDecl>(Other) &&
2492 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2493}
2494
2495template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002496static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002497 NamedDecl *D,
2498 ForwardIterator first,
2499 ForwardIterator last) {
2500 for (; first != last; ++first)
2501 if (isInstantiationOf(Ctx, D, *first))
2502 return cast<NamedDecl>(*first);
2503
2504 return 0;
2505}
2506
John McCall02cace72009-08-28 07:59:38 +00002507/// \brief Finds the instantiation of the given declaration context
2508/// within the current instantiation.
2509///
2510/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002511DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002512 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002513 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002514 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002515 return cast_or_null<DeclContext>(ID);
2516 } else return DC;
2517}
2518
Douglas Gregored961e72009-05-27 17:54:46 +00002519/// \brief Find the instantiation of the given declaration within the
2520/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002521///
2522/// This routine is intended to be used when \p D is a declaration
2523/// referenced from within a template, that needs to mapped into the
2524/// corresponding declaration within an instantiation. For example,
2525/// given:
2526///
2527/// \code
2528/// template<typename T>
2529/// struct X {
2530/// enum Kind {
2531/// KnownValue = sizeof(T)
2532/// };
2533///
2534/// bool getKind() const { return KnownValue; }
2535/// };
2536///
2537/// template struct X<int>;
2538/// \endcode
2539///
2540/// In the instantiation of X<int>::getKind(), we need to map the
2541/// EnumConstantDecl for KnownValue (which refers to
2542/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002543/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2544/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002545NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002546 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002547 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002548 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002549 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002550 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002551 // D is a local of some kind. Look into the map of local
2552 // declarations to their instantiations.
2553 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2554 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002555
Douglas Gregore95b4092009-09-16 18:34:49 +00002556 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2557 if (!Record->isDependentContext())
2558 return D;
2559
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002560 // If the RecordDecl is actually the injected-class-name or a
2561 // "templated" declaration for a class template, class template
2562 // partial specialization, or a member class of a class template,
2563 // substitute into the injected-class-name of the class template
2564 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002565 QualType T;
2566 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2567
2568 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002569 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002570 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2571 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002572 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002573
2574 // If we call SubstType with an InjectedClassNameType here we
2575 // can end up in an infinite loop.
2576 T = Context.getTypeDeclType(Record);
2577 assert(isa<InjectedClassNameType>(T) &&
2578 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002579 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002580 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002581
2582 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002583 // Substitute into the injected-class-name to get the type
2584 // corresponding to the instantiation we want, which may also be
2585 // the current instantiation (if we're in a template
2586 // definition). This substitution should never fail, since we
2587 // know we can instantiate the injected-class-name or we
2588 // wouldn't have gotten to the injected-class-name!
2589
2590 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2591 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002592 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2593 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2594
2595 if (!T->isDependentType()) {
2596 assert(T->isRecordType() && "Instantiation must produce a record type");
2597 return T->getAs<RecordType>()->getDecl();
2598 }
2599
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002600 // We are performing "partial" template instantiation to create
2601 // the member declarations for the members of a class template
2602 // specialization. Therefore, D is actually referring to something
2603 // in the current instantiation. Look through the current
2604 // context, which contains actual instantiations, to find the
2605 // instantiation of the "current instantiation" that D refers
2606 // to.
2607 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002608 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002609 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002610 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002611 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002612 if (isInstantiationOf(ClassTemplate,
2613 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002614 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002615
2616 if (!DC->isDependentContext())
2617 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002618 }
2619
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002620 // We're performing "instantiation" of a member of the current
2621 // instantiation while we are type-checking the
2622 // definition. Compute the declaration context and return that.
2623 assert(!SawNonDependentContext &&
2624 "No dependent context while instantiating record");
2625 DeclContext *DC = computeDeclContext(T);
2626 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002627 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002628 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002629 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002630
Douglas Gregore95b4092009-09-16 18:34:49 +00002631 // Fall through to deal with other dependent record types (e.g.,
2632 // anonymous unions in class templates).
2633 }
John McCall52a575a2009-08-29 08:11:13 +00002634
Douglas Gregore95b4092009-09-16 18:34:49 +00002635 if (!ParentDC->isDependentContext())
2636 return D;
2637
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002638 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002639 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002640 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Douglas Gregor815215d2009-05-27 05:35:12 +00002642 if (ParentDC != D->getDeclContext()) {
2643 // We performed some kind of instantiation in the parent context,
2644 // so now we need to look into the instantiated parent context to
2645 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002646
John McCall3cb0ebd2010-03-10 03:28:59 +00002647 // If our context used to be dependent, we may need to instantiate
2648 // it before performing lookup into that context.
2649 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002650 if (!Spec->isDependentContext()) {
2651 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002652 const RecordType *Tag = T->getAs<RecordType>();
2653 assert(Tag && "type of non-dependent record is not a RecordType");
2654 if (!Tag->isBeingDefined() &&
2655 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2656 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002657 }
2658 }
2659
Douglas Gregor815215d2009-05-27 05:35:12 +00002660 NamedDecl *Result = 0;
2661 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002662 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002663 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2664 } else {
2665 // Since we don't have a name for the entity we're looking for,
2666 // our only option is to walk through all of the declarations to
2667 // find that name. This will occur in a few cases:
2668 //
2669 // - anonymous struct/union within a template
2670 // - unnamed class/struct/union/enum within a template
2671 //
2672 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002673 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002674 ParentDC->decls_begin(),
2675 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002676 }
Mike Stump1eb44332009-09-09 15:08:12 +00002677
John McCall9f54ad42009-12-10 09:41:52 +00002678 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002679 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2680 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002681 && "Unable to find instantiation of declaration!");
2682
Douglas Gregor815215d2009-05-27 05:35:12 +00002683 D = Result;
2684 }
2685
Douglas Gregor815215d2009-05-27 05:35:12 +00002686 return D;
2687}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002688
Mike Stump1eb44332009-09-09 15:08:12 +00002689/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002690/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002691void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2692 while (!PendingLocalImplicitInstantiations.empty() ||
2693 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2694 PendingImplicitInstantiation Inst;
2695
2696 if (PendingLocalImplicitInstantiations.empty()) {
2697 Inst = PendingImplicitInstantiations.front();
2698 PendingImplicitInstantiations.pop_front();
2699 } else {
2700 Inst = PendingLocalImplicitInstantiations.front();
2701 PendingLocalImplicitInstantiations.pop_front();
2702 }
Mike Stump1eb44332009-09-09 15:08:12 +00002703
Douglas Gregor7caa6822009-07-24 20:34:43 +00002704 // Instantiate function definitions
2705 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002706 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002707 Function->getLocation(), *this,
2708 Context.getSourceManager(),
2709 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002711 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002712 continue;
2713 }
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Douglas Gregor7caa6822009-07-24 20:34:43 +00002715 // Instantiate static data member definitions.
2716 VarDecl *Var = cast<VarDecl>(Inst.first);
2717 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002718
Chandler Carruth291b4412010-02-13 10:17:50 +00002719 // Don't try to instantiate declarations if the most recent redeclaration
2720 // is invalid.
2721 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2722 continue;
2723
2724 // Check if the most recent declaration has changed the specialization kind
2725 // and removed the need for implicit instantiation.
2726 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2727 case TSK_Undeclared:
2728 assert(false && "Cannot instantitiate an undeclared specialization.");
2729 case TSK_ExplicitInstantiationDeclaration:
2730 case TSK_ExplicitInstantiationDefinition:
2731 case TSK_ExplicitSpecialization:
2732 continue; // No longer need implicit instantiation.
2733 case TSK_ImplicitInstantiation:
2734 break;
2735 }
2736
Mike Stump1eb44332009-09-09 15:08:12 +00002737 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002738 Var->getLocation(), *this,
2739 Context.getSourceManager(),
2740 "instantiating static data member "
2741 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Douglas Gregor7caa6822009-07-24 20:34:43 +00002743 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002744 }
2745}
John McCall0c01d182010-03-24 05:22:00 +00002746
2747void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2748 const MultiLevelTemplateArgumentList &TemplateArgs) {
2749 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2750 E = Pattern->ddiag_end(); I != E; ++I) {
2751 DependentDiagnostic *DD = *I;
2752
2753 switch (DD->getKind()) {
2754 case DependentDiagnostic::Access:
2755 HandleDependentAccessCheck(*DD, TemplateArgs);
2756 break;
2757 }
2758 }
2759}