blob: 834b86da9a0f64bc3e2c52b5dc23cbb205c2cd8e [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();
Douglas Gregor836adf62010-05-24 17:22:01 +0000187 if (DI->getType()->isDependentType() ||
188 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000189 DI = SemaRef.SubstType(DI, TemplateArgs,
190 D->getLocation(), D->getDeclName());
191 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000192 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000193 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000194 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000195 } else {
196 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199 // Create the new typedef
200 TypedefDecl *Typedef
201 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000202 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000203 if (Invalid)
204 Typedef->setInvalidDecl();
205
Douglas Gregord57a38e2010-04-23 16:25:07 +0000206 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
207 TagDecl *TD = TT->getDecl();
208
209 // If the TagDecl that the TypedefDecl points to is an anonymous decl
210 // keep track of the TypedefDecl.
211 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
212 TD->setTypedefForAnonDecl(Typedef);
213 }
214
John McCall5126fd02009-12-30 00:31:22 +0000215 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000216 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
217 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000218 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
219 }
220
Douglas Gregora6b09072010-05-17 23:46:49 +0000221 InstantiateAttrs(D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000222
John McCall46460a62010-01-20 21:53:11 +0000223 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000224 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000226 return Typedef;
227}
228
Douglas Gregor6eef5192009-12-14 19:27:10 +0000229/// \brief Instantiate the arguments provided as part of initialization.
230///
231/// \returns true if an error occurred, false otherwise.
232static bool InstantiateInitializationArguments(Sema &SemaRef,
233 Expr **Args, unsigned NumArgs,
234 const MultiLevelTemplateArgumentList &TemplateArgs,
235 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
236 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
237 for (unsigned I = 0; I != NumArgs; ++I) {
238 // When we hit the first defaulted argument, break out of the loop:
239 // we don't pass those default arguments on.
240 if (Args[I]->isDefaultArgument())
241 break;
242
243 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
244 if (Arg.isInvalid())
245 return true;
246
247 Expr *ArgExpr = (Expr *)Arg.get();
248 InitArgs.push_back(Arg.release());
249
250 // FIXME: We're faking all of the comma locations. Do we need them?
251 FakeCommaLocs.push_back(
252 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
253 }
254
255 return false;
256}
257
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000258/// \brief Instantiate an initializer, breaking it into separate
259/// initialization arguments.
260///
261/// \param S The semantic analysis object.
262///
263/// \param Init The initializer to instantiate.
264///
265/// \param TemplateArgs Template arguments to be substituted into the
266/// initializer.
267///
268/// \param NewArgs Will be filled in with the instantiation arguments.
269///
270/// \returns true if an error occurred, false otherwise
271static bool InstantiateInitializer(Sema &S, Expr *Init,
272 const MultiLevelTemplateArgumentList &TemplateArgs,
273 SourceLocation &LParenLoc,
274 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
275 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
276 SourceLocation &RParenLoc) {
277 NewArgs.clear();
278 LParenLoc = SourceLocation();
279 RParenLoc = SourceLocation();
280
281 if (!Init)
282 return false;
283
284 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
285 Init = ExprTemp->getSubExpr();
286
287 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
288 Init = Binder->getSubExpr();
289
290 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
291 Init = ICE->getSubExprAsWritten();
292
293 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
294 LParenLoc = ParenList->getLParenLoc();
295 RParenLoc = ParenList->getRParenLoc();
296 return InstantiateInitializationArguments(S, ParenList->getExprs(),
297 ParenList->getNumExprs(),
298 TemplateArgs, CommaLocs,
299 NewArgs);
300 }
301
302 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000303 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
304 if (InstantiateInitializationArguments(S,
305 Construct->getArgs(),
306 Construct->getNumArgs(),
307 TemplateArgs,
308 CommaLocs, NewArgs))
309 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000310
Douglas Gregor28329e52010-03-24 21:22:47 +0000311 // FIXME: Fake locations!
312 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
313 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
314 return false;
315 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000316 }
317
318 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
319 if (Result.isInvalid())
320 return true;
321
322 NewArgs.push_back(Result.takeAs<Expr>());
323 return false;
324}
325
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000326Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000327 // If this is the variable for an anonymous struct or union,
328 // instantiate the anonymous struct/union type first.
329 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
330 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
331 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
332 return 0;
333
John McCallce3ff2b2009-08-25 22:02:44 +0000334 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000335 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000336 TemplateArgs,
337 D->getTypeSpecStartLoc(),
338 D->getDeclName());
339 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000340 return 0;
341
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000342 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000343 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
344 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000345 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000346 D->getStorageClass(),
347 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000348 Var->setThreadSpecified(D->isThreadSpecified());
349 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
350 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000351
John McCallb6217662010-03-15 10:12:16 +0000352 // Substitute the nested name specifier, if any.
353 if (SubstQualifier(D, Var))
354 return 0;
355
Mike Stump1eb44332009-09-09 15:08:12 +0000356 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000357 // out-of-line, the instantiation will have the same lexical
358 // context (which will be a namespace scope) as the template.
359 if (D->isOutOfLine())
360 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000361
John McCall46460a62010-01-20 21:53:11 +0000362 Var->setAccess(D->getAccess());
Douglas Gregor5764f612010-05-08 23:05:03 +0000363 Var->setUsed(D->isUsed());
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000364
Mike Stump390b4cc2009-05-16 07:39:55 +0000365 // FIXME: In theory, we could have a previous declaration for variables that
366 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000367 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000368 // FIXME: having to fake up a LookupResult is dumb.
369 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000370 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000371 if (D->isStaticDataMember())
372 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000373 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregor7caa6822009-07-24 20:34:43 +0000375 if (D->isOutOfLine()) {
376 D->getLexicalDeclContext()->addDecl(Var);
377 Owner->makeDeclVisibleInContext(Var);
378 } else {
379 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000380
381 if (Owner->isFunctionOrMethod())
382 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000385 // Link instantiations of static data members back to the template from
386 // which they were instantiated.
387 if (Var->isStaticDataMember())
388 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000389 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000390
Douglas Gregor60c93c92010-02-09 07:26:29 +0000391 if (Var->getAnyInitializer()) {
392 // We already have an initializer in the class.
393 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000394 if (Var->isStaticDataMember() && !D->isOutOfLine())
395 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
396 else
397 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
398
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000399 // Instantiate the initializer.
400 SourceLocation LParenLoc, RParenLoc;
401 llvm::SmallVector<SourceLocation, 4> CommaLocs;
402 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
403 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
404 CommaLocs, InitArgs, RParenLoc)) {
405 // Attach the initializer to the declaration.
406 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000407 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000408 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000409 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000410 move_arg(InitArgs),
411 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000412 RParenLoc);
413 } else if (InitArgs.size() == 1) {
414 Expr *Init = (Expr*)(InitArgs.take()[0]);
415 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
416 SemaRef.Owned(Init),
417 false);
418 } else {
419 assert(InitArgs.size() == 0);
420 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000421 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000422 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000423 // FIXME: Not too happy about invalidating the declaration
424 // because of a bogus initializer.
425 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000426 }
427
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000428 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000429 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
430 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000431
Douglas Gregor5764f612010-05-08 23:05:03 +0000432 // Diagnose unused local variables.
433 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
434 SemaRef.DiagnoseUnusedDecl(Var);
435
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000436 return Var;
437}
438
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000439Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
440 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000441 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000442 if (DI->getType()->isDependentType() ||
443 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000444 DI = SemaRef.SubstType(DI, TemplateArgs,
445 D->getLocation(), D->getDeclName());
446 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000447 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000448 Invalid = true;
449 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000450 // C++ [temp.arg.type]p3:
451 // If a declaration acquires a function type through a type
452 // dependent on a template-parameter and this causes a
453 // declaration that does not use the syntactic form of a
454 // function declarator to have function type, the program is
455 // ill-formed.
456 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000457 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000458 Invalid = true;
459 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000460 } else {
461 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000462 }
463
464 Expr *BitWidth = D->getBitWidth();
465 if (Invalid)
466 BitWidth = 0;
467 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000468 // The bit-width expression is not potentially evaluated.
469 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000471 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000472 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000473 if (InstantiatedBitWidth.isInvalid()) {
474 Invalid = true;
475 BitWidth = 0;
476 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000477 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000478 }
479
John McCall07fb6be2009-10-22 23:33:21 +0000480 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
481 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000482 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483 D->getLocation(),
484 D->isMutable(),
485 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000486 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000487 D->getAccess(),
488 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000489 if (!Field) {
490 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000491 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000494 InstantiateAttrs(D, Field);
495
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000496 if (Invalid)
497 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000499 if (!Field->getDeclName()) {
500 // Keep track of where this decl came from.
501 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000502 }
503 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
504 if (Parent->isAnonymousStructOrUnion() &&
505 Parent->getLookupContext()->isFunctionOrMethod())
506 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000509 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000510 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000511 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000512
513 return Field;
514}
515
John McCall02cace72009-08-28 07:59:38 +0000516Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000517 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000518 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000519 if (TypeSourceInfo *Ty = D->getFriendType()) {
520 TypeSourceInfo *InstTy =
521 SemaRef.SubstType(Ty, TemplateArgs,
522 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000523 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000524 return 0;
John McCall02cace72009-08-28 07:59:38 +0000525
Douglas Gregor06245bf2010-04-07 17:57:12 +0000526 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
527 if (!FD)
528 return 0;
529
530 FD->setAccess(AS_public);
531 Owner->addDecl(FD);
532 return FD;
533 }
534
535 NamedDecl *ND = D->getFriendDecl();
536 assert(ND && "friend decl must be a decl or a type!");
537
John McCallaf2094e2010-04-08 09:05:18 +0000538 // All of the Visit implementations for the various potential friend
539 // declarations have to be carefully written to work for friend
540 // objects, with the most important detail being that the target
541 // decl should almost certainly not be placed in Owner.
542 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000543 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
John McCall02cace72009-08-28 07:59:38 +0000545 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000546 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
547 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000548 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000549 Owner->addDecl(FD);
550 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000551}
552
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000553Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
554 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Douglas Gregorac7610d2009-06-22 20:57:11 +0000556 // The expression in a static assertion is not potentially evaluated.
557 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000559 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000560 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000561 if (InstantiatedAssertExpr.isInvalid())
562 return 0;
563
Douglas Gregor43d9d922009-08-08 01:41:12 +0000564 OwningExprResult Message(SemaRef, D->getMessage());
565 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000566 Decl *StaticAssert
567 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000568 move(InstantiatedAssertExpr),
569 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000570 return StaticAssert;
571}
572
573Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000574 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000575 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000576 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000577 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000578 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000579 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000580 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000581 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000582 Enum->startDefinition();
583
Douglas Gregor96084f12010-03-01 19:00:07 +0000584 if (D->getDeclContext()->isFunctionOrMethod())
585 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
586
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000587 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000588
589 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000590 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
591 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000592 EC != ECEnd; ++EC) {
593 // The specified value for the enumerator.
594 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000595 if (Expr *UninstValue = EC->getInitExpr()) {
596 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000597 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000598 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
John McCallce3ff2b2009-08-25 22:02:44 +0000600 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000601 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602
603 // Drop the initial value and continue.
604 bool isInvalid = false;
605 if (Value.isInvalid()) {
606 Value = SemaRef.Owned((Expr *)0);
607 isInvalid = true;
608 }
609
Mike Stump1eb44332009-09-09 15:08:12 +0000610 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000611 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
612 EC->getLocation(), EC->getIdentifier(),
613 move(Value));
614
615 if (isInvalid) {
616 if (EnumConst)
617 EnumConst->setInvalidDecl();
618 Enum->setInvalidDecl();
619 }
620
621 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000622 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000623 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000624 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000625 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000626
627 if (D->getDeclContext()->isFunctionOrMethod()) {
628 // If the enumeration is within a function or method, record the enum
629 // constant as a local.
630 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
631 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000632 }
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000635 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000636 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000637 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
638 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000639 &Enumerators[0], Enumerators.size(),
640 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000641
642 return Enum;
643}
644
Douglas Gregor6477b692009-03-25 15:04:13 +0000645Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
646 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
647 return 0;
648}
649
John McCalle29ba202009-08-20 01:44:21 +0000650Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000651 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
652
Douglas Gregor550d9b22009-10-31 17:21:17 +0000653 // Create a local instantiation scope for this class template, which
654 // will contain the instantiations of the template parameters.
655 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000656 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000657 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000658 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000659 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000660
661 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000662
663 // Instantiate the qualifier. We have to do this first in case
664 // we're a friend declaration, because if we are then we need to put
665 // the new declaration in the appropriate context.
666 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
667 if (Qualifier) {
668 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
669 Pattern->getQualifierRange(),
670 TemplateArgs);
671 if (!Qualifier) return 0;
672 }
673
674 CXXRecordDecl *PrevDecl = 0;
675 ClassTemplateDecl *PrevClassTemplate = 0;
676
677 // If this isn't a friend, then it's a member template, in which
678 // case we just want to build the instantiation in the
679 // specialization. If it is a friend, we want to build it in
680 // the appropriate context.
681 DeclContext *DC = Owner;
682 if (isFriend) {
683 if (Qualifier) {
684 CXXScopeSpec SS;
685 SS.setScopeRep(Qualifier);
686 SS.setRange(Pattern->getQualifierRange());
687 DC = SemaRef.computeDeclContext(SS);
688 if (!DC) return 0;
689 } else {
690 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
691 Pattern->getDeclContext(),
692 TemplateArgs);
693 }
694
695 // Look for a previous declaration of the template in the owning
696 // context.
697 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
698 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
699 SemaRef.LookupQualifiedName(R, DC);
700
701 if (R.isSingleResult()) {
702 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
703 if (PrevClassTemplate)
704 PrevDecl = PrevClassTemplate->getTemplatedDecl();
705 }
706
707 if (!PrevClassTemplate && Qualifier) {
708 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000709 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
710 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000711 return 0;
712 }
713
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000714 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000715 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000716 bool Complain = true;
717
718 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
719 // template for struct std::tr1::__detail::_Map_base, where the
720 // template parameters of the friend declaration don't match the
721 // template parameters of the original declaration. In this one
722 // case, we don't complain about the ill-formed friend
723 // declaration.
724 if (isFriend && Pattern->getIdentifier() &&
725 Pattern->getIdentifier()->isStr("_Map_base") &&
726 DC->isNamespace() &&
727 cast<NamespaceDecl>(DC)->getIdentifier() &&
728 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
729 DeclContext *DCParent = DC->getParent();
730 if (DCParent->isNamespace() &&
731 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
732 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
733 DeclContext *DCParent2 = DCParent->getParent();
734 if (DCParent2->isNamespace() &&
735 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
736 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
737 DCParent2->getParent()->isTranslationUnit())
738 Complain = false;
739 }
740 }
741
John McCall93ba8572010-03-25 06:39:04 +0000742 TemplateParameterList *PrevParams
743 = PrevClassTemplate->getTemplateParameters();
744
745 // Make sure the parameter lists match.
746 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000747 Complain,
748 Sema::TPL_TemplateMatch)) {
749 if (Complain)
750 return 0;
751
752 AdoptedPreviousTemplateParams = true;
753 InstParams = PrevParams;
754 }
John McCall93ba8572010-03-25 06:39:04 +0000755
756 // Do some additional validation, then merge default arguments
757 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000758 if (!AdoptedPreviousTemplateParams &&
759 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000760 Sema::TPC_ClassTemplate))
761 return 0;
762 }
763 }
764
John McCalle29ba202009-08-20 01:44:21 +0000765 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000766 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000767 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000768 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000769 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000770
John McCall93ba8572010-03-25 06:39:04 +0000771 if (Qualifier)
772 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000773
John McCalle29ba202009-08-20 01:44:21 +0000774 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000775 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
776 D->getIdentifier(), InstParams, RecordInst,
777 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000778 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000779
John McCall93ba8572010-03-25 06:39:04 +0000780 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000781 if (PrevClassTemplate)
782 Inst->setAccess(PrevClassTemplate->getAccess());
783 else
784 Inst->setAccess(D->getAccess());
785
John McCall93ba8572010-03-25 06:39:04 +0000786 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
787 // TODO: do we want to track the instantiation progeny of this
788 // friend target decl?
789 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000790 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000791 Inst->setInstantiatedFromMemberTemplate(D);
792 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000793
794 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000795 SemaRef.Context.getInjectedClassNameType(RecordInst,
796 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000797
Douglas Gregor259571e2009-10-30 22:42:42 +0000798 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000799 if (isFriend) {
800 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000801 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000802 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000803
John McCalle29ba202009-08-20 01:44:21 +0000804 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000805
Douglas Gregored9c0f92009-10-29 00:04:11 +0000806 // Instantiate all of the partial specializations of this member class
807 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000808 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
809 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000810 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
811 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
812
John McCalle29ba202009-08-20 01:44:21 +0000813 return Inst;
814}
815
Douglas Gregord60e1052009-08-27 16:57:43 +0000816Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000817TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
818 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000819 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
820
821 // Lookup the already-instantiated declaration in the instantiation
822 // of the class template and return that.
823 DeclContext::lookup_result Found
824 = Owner->lookup(ClassTemplate->getDeclName());
825 if (Found.first == Found.second)
826 return 0;
827
828 ClassTemplateDecl *InstClassTemplate
829 = dyn_cast<ClassTemplateDecl>(*Found.first);
830 if (!InstClassTemplate)
831 return 0;
832
833 Decl *DCanon = D->getCanonicalDecl();
834 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
835 P = InstClassTemplate->getPartialSpecializations().begin(),
836 PEnd = InstClassTemplate->getPartialSpecializations().end();
837 P != PEnd; ++P) {
838 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
839 return &*P;
840 }
841
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000842 return 0;
843}
844
845Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000846TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000847 // Create a local instantiation scope for this function template, which
848 // will contain the instantiations of the template parameters and then get
849 // merged with the local instantiation scope for the function template
850 // itself.
851 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000852
Douglas Gregord60e1052009-08-27 16:57:43 +0000853 TemplateParameterList *TempParams = D->getTemplateParameters();
854 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000855 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000856 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000857
Douglas Gregora735b202009-10-13 14:39:41 +0000858 FunctionDecl *Instantiated = 0;
859 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
860 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
861 InstParams));
862 else
863 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
864 D->getTemplatedDecl(),
865 InstParams));
866
867 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000868 return 0;
869
John McCall46460a62010-01-20 21:53:11 +0000870 Instantiated->setAccess(D->getAccess());
871
Mike Stump1eb44332009-09-09 15:08:12 +0000872 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000873 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000874 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000875 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000876 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000877 assert(InstTemplate &&
878 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000879
John McCallb1a56e72010-03-26 23:10:15 +0000880 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
881
John McCalle976ffe2009-12-14 23:19:40 +0000882 // Link the instantiation back to the pattern *unless* this is a
883 // non-definition friend declaration.
884 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000885 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000886 InstTemplate->setInstantiatedFromMemberTemplate(D);
887
John McCallb1a56e72010-03-26 23:10:15 +0000888 // Make declarations visible in the appropriate context.
889 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000890 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000891
Douglas Gregord60e1052009-08-27 16:57:43 +0000892 return InstTemplate;
893}
894
Douglas Gregord475b8d2009-03-25 21:17:03 +0000895Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
896 CXXRecordDecl *PrevDecl = 0;
897 if (D->isInjectedClassName())
898 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000899 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000900 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
901 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000902 TemplateArgs);
903 if (!Prev) return 0;
904 PrevDecl = cast<CXXRecordDecl>(Prev);
905 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000906
907 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000908 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000909 D->getLocation(), D->getIdentifier(),
910 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000911
912 // Substitute the nested name specifier, if any.
913 if (SubstQualifier(D, Record))
914 return 0;
915
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000917 // FIXME: Check against AS_none is an ugly hack to work around the issue that
918 // the tag decls introduced by friend class declarations don't have an access
919 // specifier. Remove once this area of the code gets sorted out.
920 if (D->getAccess() != AS_none)
921 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000923 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000924
John McCall02cace72009-08-28 07:59:38 +0000925 // If the original function was part of a friend declaration,
926 // inherit its namespace state.
927 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
928 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
929
Douglas Gregor9901c572010-05-21 00:31:19 +0000930 // Make sure that anonymous structs and unions are recorded.
931 if (D->isAnonymousStructOrUnion()) {
932 Record->setAnonymousStructOrUnion(true);
933 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
934 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
935 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000936
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000937 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000938 return Record;
939}
940
John McCall02cace72009-08-28 07:59:38 +0000941/// Normal class members are of more specific types and therefore
942/// don't make it here. This function serves two purposes:
943/// 1) instantiating function templates
944/// 2) substituting friend declarations
945/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000946Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000947 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000948 // Check whether there is already a function template specialization for
949 // this declaration.
950 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
951 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000952 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000953 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000954 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000955 TemplateArgs.getInnermost().getFlatArgumentList(),
956 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000957 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
959 FunctionTemplateSpecializationInfo *Info
960 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000961 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregor127102b2009-06-29 20:59:39 +0000963 // If we already have a function template specialization, return it.
964 if (Info)
965 return Info->Function;
966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
John McCallb0cb0222010-03-27 05:57:59 +0000968 bool isFriend;
969 if (FunctionTemplate)
970 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
971 else
972 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
973
Douglas Gregor79c22782010-01-16 20:21:20 +0000974 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000975 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000976 !(isa<Decl>(Owner) &&
977 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
978 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregore53060f2009-06-25 22:08:12 +0000980 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000981 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
982 TInfo = SubstFunctionType(D, Params);
983 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000984 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000985 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000986
John McCalld325daa2010-03-26 04:53:08 +0000987 NestedNameSpecifier *Qualifier = D->getQualifier();
988 if (Qualifier) {
989 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
990 D->getQualifierRange(),
991 TemplateArgs);
992 if (!Qualifier) return 0;
993 }
994
John McCall68b6b872010-02-06 01:50:47 +0000995 // If we're instantiating a local function declaration, put the result
996 // in the owner; otherwise we need to find the instantiated context.
997 DeclContext *DC;
998 if (D->getDeclContext()->isFunctionOrMethod())
999 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001000 else if (isFriend && Qualifier) {
1001 CXXScopeSpec SS;
1002 SS.setScopeRep(Qualifier);
1003 SS.setRange(D->getQualifierRange());
1004 DC = SemaRef.computeDeclContext(SS);
1005 if (!DC) return 0;
1006 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001007 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1008 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001009 }
John McCall68b6b872010-02-06 01:50:47 +00001010
John McCall02cace72009-08-28 07:59:38 +00001011 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001012 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001013 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001014 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001015 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001016
John McCalld325daa2010-03-26 04:53:08 +00001017 if (Qualifier)
1018 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001019
John McCallb1a56e72010-03-26 23:10:15 +00001020 DeclContext *LexicalDC = Owner;
1021 if (!isFriend && D->isOutOfLine()) {
1022 assert(D->getDeclContext()->isFileContext());
1023 LexicalDC = D->getDeclContext();
1024 }
1025
1026 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Douglas Gregore53060f2009-06-25 22:08:12 +00001028 // Attach the parameters
1029 for (unsigned P = 0; P < Params.size(); ++P)
1030 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001031 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001032
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001033 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001034 if (TemplateParams) {
1035 // Our resulting instantiation is actually a function template, since we
1036 // are substituting only the outer template parameters. For example, given
1037 //
1038 // template<typename T>
1039 // struct X {
1040 // template<typename U> friend void f(T, U);
1041 // };
1042 //
1043 // X<int> x;
1044 //
1045 // We are instantiating the friend function template "f" within X<int>,
1046 // which means substituting int for T, but leaving "f" as a friend function
1047 // template.
1048 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001049 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001050 Function->getLocation(),
1051 Function->getDeclName(),
1052 TemplateParams, Function);
1053 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001054
1055 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001056
1057 if (isFriend && D->isThisDeclarationADefinition()) {
1058 // TODO: should we remember this connection regardless of whether
1059 // the friend declaration provided a body?
1060 FunctionTemplate->setInstantiatedFromMemberTemplate(
1061 D->getDescribedFunctionTemplate());
1062 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001063 } else if (FunctionTemplate) {
1064 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001065 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001066 &TemplateArgs.getInnermost(),
1067 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001068 } else if (isFriend && D->isThisDeclarationADefinition()) {
1069 // TODO: should we remember this connection regardless of whether
1070 // the friend declaration provided a body?
1071 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001072 }
Douglas Gregora735b202009-10-13 14:39:41 +00001073
Douglas Gregore53060f2009-06-25 22:08:12 +00001074 if (InitFunctionInstantiation(Function, D))
1075 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregore53060f2009-06-25 22:08:12 +00001077 bool Redeclaration = false;
1078 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001079 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001080
John McCall68263142009-11-18 22:49:29 +00001081 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1082 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1083
John McCallaf2094e2010-04-08 09:05:18 +00001084 if (DependentFunctionTemplateSpecializationInfo *Info
1085 = D->getDependentSpecializationInfo()) {
1086 assert(isFriend && "non-friend has dependent specialization info?");
1087
1088 // This needs to be set now for future sanity.
1089 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1090
1091 // Instantiate the explicit template arguments.
1092 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1093 Info->getRAngleLoc());
1094 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1095 TemplateArgumentLoc Loc;
1096 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1097 return 0;
1098
1099 ExplicitArgs.addArgument(Loc);
1100 }
1101
1102 // Map the candidate templates to their instantiations.
1103 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1104 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1105 Info->getTemplate(I),
1106 TemplateArgs);
1107 if (!Temp) return 0;
1108
1109 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1110 }
1111
1112 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1113 &ExplicitArgs,
1114 Previous))
1115 Function->setInvalidDecl();
1116
1117 isExplicitSpecialization = true;
1118
1119 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001120 // Look only into the namespace where the friend would be declared to
1121 // find a previous declaration. This is the innermost enclosing namespace,
1122 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001123 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001124
Douglas Gregora735b202009-10-13 14:39:41 +00001125 // In C++, the previous declaration we find might be a tag type
1126 // (class or enum). In this case, the new declaration will hide the
1127 // tag type. Note that this does does not apply if we're declaring a
1128 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001129 if (Previous.isSingleTagDecl())
1130 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001131 }
1132
John McCall9f54ad42009-12-10 09:41:52 +00001133 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001134 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001135 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001136
John McCall76d32642010-04-24 01:30:58 +00001137 NamedDecl *PrincipalDecl = (TemplateParams
1138 ? cast<NamedDecl>(FunctionTemplate)
1139 : Function);
1140
Douglas Gregora735b202009-10-13 14:39:41 +00001141 // If the original function was part of a friend declaration,
1142 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001143 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001144 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001145 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001146 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001147 else
Douglas Gregora735b202009-10-13 14:39:41 +00001148 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001149
1150 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1151 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001152
1153 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1154 D->isThisDeclarationADefinition()) {
1155 // Check for a function body.
1156 const FunctionDecl *Definition = 0;
1157 if (Function->getBody(Definition) &&
1158 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1159 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1160 << Function->getDeclName();
1161 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1162 Function->setInvalidDecl();
1163 }
1164 // Check for redefinitions due to other instantiations of this or
1165 // a similar friend function.
1166 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1167 REnd = Function->redecls_end();
1168 R != REnd; ++R) {
1169 if (*R != Function &&
1170 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1171 if (const FunctionDecl *RPattern
1172 = (*R)->getTemplateInstantiationPattern())
1173 if (RPattern->getBody(RPattern)) {
1174 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1175 << Function->getDeclName();
1176 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1177 Function->setInvalidDecl();
1178 break;
1179 }
1180 }
1181 }
1182 }
1183
Douglas Gregora735b202009-10-13 14:39:41 +00001184 }
1185
John McCall76d32642010-04-24 01:30:58 +00001186 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1187 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1188 PrincipalDecl->setNonMemberOperator();
1189
Douglas Gregore53060f2009-06-25 22:08:12 +00001190 return Function;
1191}
1192
Douglas Gregord60e1052009-08-27 16:57:43 +00001193Decl *
1194TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1195 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001196 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1197 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001198 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001199 // We are creating a function template specialization from a function
1200 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001201 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001202 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001203 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001204 TemplateArgs.getInnermost().getFlatArgumentList(),
1205 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001206 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
1208 FunctionTemplateSpecializationInfo *Info
1209 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001210 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Douglas Gregor6b906862009-08-21 00:16:32 +00001212 // If we already have a function template specialization, return it.
1213 if (Info)
1214 return Info->Function;
1215 }
1216
John McCallb0cb0222010-03-27 05:57:59 +00001217 bool isFriend;
1218 if (FunctionTemplate)
1219 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1220 else
1221 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1222
Douglas Gregor79c22782010-01-16 20:21:20 +00001223 bool MergeWithParentScope = (TemplateParams != 0) ||
1224 !(isa<Decl>(Owner) &&
1225 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1226 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001227
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001228 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001229 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1230 TInfo = SubstFunctionType(D, Params);
1231 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001232 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001233 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001234
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001235 // \brief If the type of this function is not *directly* a function
1236 // type, then we're instantiating the a function that was declared
1237 // via a typedef, e.g.,
1238 //
1239 // typedef int functype(int, int);
1240 // functype func;
1241 //
1242 // In this case, we'll just go instantiate the ParmVarDecls that we
1243 // synthesized in the method declaration.
1244 if (!isa<FunctionProtoType>(T)) {
1245 assert(!Params.size() && "Instantiating type could not yield parameters");
1246 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1247 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1248 TemplateArgs);
1249 if (!P)
1250 return 0;
1251
1252 Params.push_back(P);
1253 }
1254 }
1255
John McCallb0cb0222010-03-27 05:57:59 +00001256 NestedNameSpecifier *Qualifier = D->getQualifier();
1257 if (Qualifier) {
1258 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1259 D->getQualifierRange(),
1260 TemplateArgs);
1261 if (!Qualifier) return 0;
1262 }
1263
1264 DeclContext *DC = Owner;
1265 if (isFriend) {
1266 if (Qualifier) {
1267 CXXScopeSpec SS;
1268 SS.setScopeRep(Qualifier);
1269 SS.setRange(D->getQualifierRange());
1270 DC = SemaRef.computeDeclContext(SS);
1271 } else {
1272 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1273 D->getDeclContext(),
1274 TemplateArgs);
1275 }
1276 if (!DC) return 0;
1277 }
1278
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001279 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001280 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001281 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Douglas Gregordec06662009-08-21 18:42:58 +00001283 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001284 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001285 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1286 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1287 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001288 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1289 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001290 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001291 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001292 Constructor->isInlineSpecified(),
1293 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001294 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1295 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1296 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1297 SemaRef.Context.getCanonicalType(ClassTy));
1298 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1299 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001300 T, Destructor->isInlineSpecified(),
1301 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001302 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001303 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001304 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001305 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001306 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1307 ConvTy);
1308 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1309 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001310 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001311 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001312 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001313 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001314 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001315 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001316 D->isStatic(),
1317 D->getStorageClassAsWritten(),
1318 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001319 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001320
John McCallb0cb0222010-03-27 05:57:59 +00001321 if (Qualifier)
1322 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001323
Douglas Gregord60e1052009-08-27 16:57:43 +00001324 if (TemplateParams) {
1325 // Our resulting instantiation is actually a function template, since we
1326 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001327 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001328 // template<typename T>
1329 // struct X {
1330 // template<typename U> void f(T, U);
1331 // };
1332 //
1333 // X<int> x;
1334 //
1335 // We are instantiating the member template "f" within X<int>, which means
1336 // substituting int for T, but leaving "f" as a member function template.
1337 // Build the function template itself.
1338 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1339 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001340 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001341 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001342 if (isFriend) {
1343 FunctionTemplate->setLexicalDeclContext(Owner);
1344 FunctionTemplate->setObjectOfFriendDecl(true);
1345 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001346 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001347 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001348 } else if (FunctionTemplate) {
1349 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001350 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001351 &TemplateArgs.getInnermost(),
1352 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001353 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001354 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001355 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001356 }
1357
Mike Stump1eb44332009-09-09 15:08:12 +00001358 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001359 // out-of-line, the instantiation will have the same lexical
1360 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001361 if (isFriend) {
1362 Method->setLexicalDeclContext(Owner);
1363 Method->setObjectOfFriendDecl(true);
1364 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001365 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Douglas Gregor5545e162009-03-24 00:38:23 +00001367 // Attach the parameters
1368 for (unsigned P = 0; P < Params.size(); ++P)
1369 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001370 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001371
1372 if (InitMethodInstantiation(Method, D))
1373 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001374
John McCall68263142009-11-18 22:49:29 +00001375 LookupResult Previous(SemaRef, Name, SourceLocation(),
1376 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001377
John McCallb0cb0222010-03-27 05:57:59 +00001378 if (!FunctionTemplate || TemplateParams || isFriend) {
1379 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregordec06662009-08-21 18:42:58 +00001381 // In C++, the previous declaration we find might be a tag type
1382 // (class or enum). In this case, the new declaration will hide the
1383 // tag type. Note that this does does not apply if we're declaring a
1384 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001385 if (Previous.isSingleTagDecl())
1386 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001387 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001388
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001389 bool Redeclaration = false;
1390 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001391 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001392 /*FIXME:*/OverloadableAttrRequired);
1393
Douglas Gregor4ba31362009-12-01 17:24:26 +00001394 if (D->isPure())
1395 SemaRef.CheckPureMethod(Method, SourceRange());
1396
John McCall46460a62010-01-20 21:53:11 +00001397 Method->setAccess(D->getAccess());
1398
John McCallb0cb0222010-03-27 05:57:59 +00001399 if (FunctionTemplate) {
1400 // If there's a function template, let our caller handle it.
1401 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1402 // Don't hide a (potentially) valid declaration with an invalid one.
1403 } else {
1404 NamedDecl *DeclToAdd = (TemplateParams
1405 ? cast<NamedDecl>(FunctionTemplate)
1406 : Method);
1407 if (isFriend)
1408 Record->makeDeclVisibleInContext(DeclToAdd);
1409 else
1410 Owner->addDecl(DeclToAdd);
1411 }
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001413 return Method;
1414}
1415
Douglas Gregor615c5d42009-03-24 16:43:20 +00001416Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001417 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001418}
1419
Douglas Gregor03b2b072009-03-24 00:15:49 +00001420Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001421 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001422}
1423
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001424Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001425 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001426}
1427
Douglas Gregor6477b692009-03-25 15:04:13 +00001428ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001429 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001430}
1431
John McCalle29ba202009-08-20 01:44:21 +00001432Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1433 TemplateTypeParmDecl *D) {
1434 // TODO: don't always clone when decls are refcounted.
1435 const Type* T = D->getTypeForDecl();
1436 assert(T->isTemplateTypeParmType());
1437 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001438
John McCalle29ba202009-08-20 01:44:21 +00001439 TemplateTypeParmDecl *Inst =
1440 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001441 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001442 TTPT->getName(),
1443 D->wasDeclaredWithTypename(),
1444 D->isParameterPack());
1445
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001446 if (D->hasDefaultArgument())
1447 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001448
Douglas Gregor550d9b22009-10-31 17:21:17 +00001449 // Introduce this template parameter's instantiation into the instantiation
1450 // scope.
1451 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1452
John McCalle29ba202009-08-20 01:44:21 +00001453 return Inst;
1454}
1455
Douglas Gregor33642df2009-10-23 23:25:44 +00001456Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1457 NonTypeTemplateParmDecl *D) {
1458 // Substitute into the type of the non-type template parameter.
1459 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001460 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001461 if (DI) {
1462 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1463 D->getDeclName());
1464 if (DI) T = DI->getType();
1465 } else {
1466 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1467 D->getDeclName());
1468 DI = 0;
1469 }
1470 if (T.isNull())
1471 return 0;
1472
1473 // Check that this type is acceptable for a non-type template parameter.
1474 bool Invalid = false;
1475 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1476 if (T.isNull()) {
1477 T = SemaRef.Context.IntTy;
1478 Invalid = true;
1479 }
1480
1481 NonTypeTemplateParmDecl *Param
1482 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1483 D->getDepth() - 1, D->getPosition(),
1484 D->getIdentifier(), T, DI);
1485 if (Invalid)
1486 Param->setInvalidDecl();
1487
1488 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001489
1490 // Introduce this template parameter's instantiation into the instantiation
1491 // scope.
1492 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001493 return Param;
1494}
1495
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001496Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001497TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1498 TemplateTemplateParmDecl *D) {
1499 // Instantiate the template parameter list of the template template parameter.
1500 TemplateParameterList *TempParams = D->getTemplateParameters();
1501 TemplateParameterList *InstParams;
1502 {
1503 // Perform the actual substitution of template parameters within a new,
1504 // local instantiation scope.
1505 Sema::LocalInstantiationScope Scope(SemaRef);
1506 InstParams = SubstTemplateParams(TempParams);
1507 if (!InstParams)
1508 return NULL;
1509 }
1510
1511 // Build the template template parameter.
1512 TemplateTemplateParmDecl *Param
1513 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1514 D->getDepth() - 1, D->getPosition(),
1515 D->getIdentifier(), InstParams);
1516 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001517
Douglas Gregor9106ef72009-11-11 16:58:32 +00001518 // Introduce this template parameter's instantiation into the instantiation
1519 // scope.
1520 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1521
1522 return Param;
1523}
1524
Douglas Gregor48c32a72009-11-17 06:07:40 +00001525Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1526 // Using directives are never dependent, so they require no explicit
1527
1528 UsingDirectiveDecl *Inst
1529 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1530 D->getNamespaceKeyLocation(),
1531 D->getQualifierRange(), D->getQualifier(),
1532 D->getIdentLocation(),
1533 D->getNominatedNamespace(),
1534 D->getCommonAncestor());
1535 Owner->addDecl(Inst);
1536 return Inst;
1537}
1538
John McCalled976492009-12-04 22:46:56 +00001539Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1540 // The nested name specifier is non-dependent, so no transformation
1541 // is required.
1542
John McCall9f54ad42009-12-10 09:41:52 +00001543 // We only need to do redeclaration lookups if we're in a class
1544 // scope (in fact, it's not really even possible in non-class
1545 // scopes).
1546 bool CheckRedeclaration = Owner->isRecord();
1547
1548 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1549 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1550
John McCalled976492009-12-04 22:46:56 +00001551 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1552 D->getLocation(),
1553 D->getNestedNameRange(),
1554 D->getUsingLocation(),
1555 D->getTargetNestedNameDecl(),
1556 D->getDeclName(),
1557 D->isTypeName());
1558
1559 CXXScopeSpec SS;
1560 SS.setScopeRep(D->getTargetNestedNameDecl());
1561 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001562
1563 if (CheckRedeclaration) {
1564 Prev.setHideTags(false);
1565 SemaRef.LookupQualifiedName(Prev, Owner);
1566
1567 // Check for invalid redeclarations.
1568 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1569 D->isTypeName(), SS,
1570 D->getLocation(), Prev))
1571 NewUD->setInvalidDecl();
1572
1573 }
1574
1575 if (!NewUD->isInvalidDecl() &&
1576 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001577 D->getLocation()))
1578 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001579
John McCalled976492009-12-04 22:46:56 +00001580 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1581 NewUD->setAccess(D->getAccess());
1582 Owner->addDecl(NewUD);
1583
John McCall9f54ad42009-12-10 09:41:52 +00001584 // Don't process the shadow decls for an invalid decl.
1585 if (NewUD->isInvalidDecl())
1586 return NewUD;
1587
John McCall323c3102009-12-22 22:26:37 +00001588 bool isFunctionScope = Owner->isFunctionOrMethod();
1589
John McCall9f54ad42009-12-10 09:41:52 +00001590 // Process the shadow decls.
1591 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1592 I != E; ++I) {
1593 UsingShadowDecl *Shadow = *I;
1594 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001595 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1596 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001597 TemplateArgs));
1598
1599 if (CheckRedeclaration &&
1600 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1601 continue;
1602
1603 UsingShadowDecl *InstShadow
1604 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1605 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001606
1607 if (isFunctionScope)
1608 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001609 }
John McCalled976492009-12-04 22:46:56 +00001610
1611 return NewUD;
1612}
1613
1614Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001615 // Ignore these; we handle them in bulk when processing the UsingDecl.
1616 return 0;
John McCalled976492009-12-04 22:46:56 +00001617}
1618
John McCall7ba107a2009-11-18 02:36:19 +00001619Decl * TemplateDeclInstantiator
1620 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001621 NestedNameSpecifier *NNS =
1622 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1623 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001624 TemplateArgs);
1625 if (!NNS)
1626 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001628 CXXScopeSpec SS;
1629 SS.setRange(D->getTargetNestedNameRange());
1630 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001631
1632 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001633 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001634 D->getUsingLoc(), SS, D->getLocation(),
1635 D->getDeclName(), 0,
1636 /*instantiation*/ true,
1637 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001638 if (UD)
John McCalled976492009-12-04 22:46:56 +00001639 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1640
John McCall7ba107a2009-11-18 02:36:19 +00001641 return UD;
1642}
1643
1644Decl * TemplateDeclInstantiator
1645 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1646 NestedNameSpecifier *NNS =
1647 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1648 D->getTargetNestedNameRange(),
1649 TemplateArgs);
1650 if (!NNS)
1651 return 0;
1652
1653 CXXScopeSpec SS;
1654 SS.setRange(D->getTargetNestedNameRange());
1655 SS.setScopeRep(NNS);
1656
1657 NamedDecl *UD =
1658 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1659 D->getUsingLoc(), SS, D->getLocation(),
1660 D->getDeclName(), 0,
1661 /*instantiation*/ true,
1662 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001663 if (UD)
John McCalled976492009-12-04 22:46:56 +00001664 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1665
Anders Carlsson0d8df782009-08-29 19:37:28 +00001666 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001667}
1668
John McCallce3ff2b2009-08-25 22:02:44 +00001669Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001670 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001671 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001672 if (D->isInvalidDecl())
1673 return 0;
1674
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001675 return Instantiator.Visit(D);
1676}
1677
John McCalle29ba202009-08-20 01:44:21 +00001678/// \brief Instantiates a nested template parameter list in the current
1679/// instantiation context.
1680///
1681/// \param L The parameter list to instantiate
1682///
1683/// \returns NULL if there was an error
1684TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001685TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001686 // Get errors for all the parameters before bailing out.
1687 bool Invalid = false;
1688
1689 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001690 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001691 ParamVector Params;
1692 Params.reserve(N);
1693 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1694 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001695 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001696 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001697 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001698 }
1699
1700 // Clean up if we had an error.
1701 if (Invalid) {
1702 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1703 PI != PE; ++PI)
1704 if (*PI)
1705 (*PI)->Destroy(SemaRef.Context);
1706 return NULL;
1707 }
1708
1709 TemplateParameterList *InstL
1710 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1711 L->getLAngleLoc(), &Params.front(), N,
1712 L->getRAngleLoc());
1713 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001714}
John McCalle29ba202009-08-20 01:44:21 +00001715
Douglas Gregored9c0f92009-10-29 00:04:11 +00001716/// \brief Instantiate the declaration of a class template partial
1717/// specialization.
1718///
1719/// \param ClassTemplate the (instantiated) class template that is partially
1720// specialized by the instantiation of \p PartialSpec.
1721///
1722/// \param PartialSpec the (uninstantiated) class template partial
1723/// specialization that we are instantiating.
1724///
1725/// \returns true if there was an error, false otherwise.
1726bool
1727TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1728 ClassTemplateDecl *ClassTemplate,
1729 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001730 // Create a local instantiation scope for this class template partial
1731 // specialization, which will contain the instantiations of the template
1732 // parameters.
1733 Sema::LocalInstantiationScope Scope(SemaRef);
1734
Douglas Gregored9c0f92009-10-29 00:04:11 +00001735 // Substitute into the template parameters of the class template partial
1736 // specialization.
1737 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1738 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1739 if (!InstParams)
1740 return true;
1741
1742 // Substitute into the template arguments of the class template partial
1743 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001744 const TemplateArgumentLoc *PartialSpecTemplateArgs
1745 = PartialSpec->getTemplateArgsAsWritten();
1746 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1747
John McCalld5532b62009-11-23 01:53:49 +00001748 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001749 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001750 TemplateArgumentLoc Loc;
1751 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001752 return true;
John McCalld5532b62009-11-23 01:53:49 +00001753 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001754 }
1755
1756
1757 // Check that the template argument list is well-formed for this
1758 // class template.
1759 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1760 InstTemplateArgs.size());
1761 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1762 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001763 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001764 false,
1765 Converted))
1766 return true;
1767
1768 // Figure out where to insert this class template partial specialization
1769 // in the member template's set of class template partial specializations.
1770 llvm::FoldingSetNodeID ID;
1771 ClassTemplatePartialSpecializationDecl::Profile(ID,
1772 Converted.getFlatArguments(),
1773 Converted.flatSize(),
1774 SemaRef.Context);
1775 void *InsertPos = 0;
1776 ClassTemplateSpecializationDecl *PrevDecl
1777 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1778 InsertPos);
1779
1780 // Build the canonical type that describes the converted template
1781 // arguments of the class template partial specialization.
1782 QualType CanonType
1783 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1784 Converted.getFlatArguments(),
1785 Converted.flatSize());
1786
1787 // Build the fully-sugared type for this class template
1788 // specialization as the user wrote in the specialization
1789 // itself. This means that we'll pretty-print the type retrieved
1790 // from the specialization's declaration the way that the user
1791 // actually wrote the specialization, rather than formatting the
1792 // name based on the "canonical" representation used to store the
1793 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001794 TypeSourceInfo *WrittenTy
1795 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1796 TemplateName(ClassTemplate),
1797 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001798 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001799 CanonType);
1800
1801 if (PrevDecl) {
1802 // We've already seen a partial specialization with the same template
1803 // parameters and template arguments. This can happen, for example, when
1804 // substituting the outer template arguments ends up causing two
1805 // class template partial specializations of a member class template
1806 // to have identical forms, e.g.,
1807 //
1808 // template<typename T, typename U>
1809 // struct Outer {
1810 // template<typename X, typename Y> struct Inner;
1811 // template<typename Y> struct Inner<T, Y>;
1812 // template<typename Y> struct Inner<U, Y>;
1813 // };
1814 //
1815 // Outer<int, int> outer; // error: the partial specializations of Inner
1816 // // have the same signature.
1817 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1818 << WrittenTy;
1819 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1820 << SemaRef.Context.getTypeDeclType(PrevDecl);
1821 return true;
1822 }
1823
1824
1825 // Create the class template partial specialization declaration.
1826 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001827 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1828 PartialSpec->getTagKind(),
1829 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001830 PartialSpec->getLocation(),
1831 InstParams,
1832 ClassTemplate,
1833 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001834 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001835 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001836 0,
1837 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001838 // Substitute the nested name specifier, if any.
1839 if (SubstQualifier(PartialSpec, InstPartialSpec))
1840 return 0;
1841
Douglas Gregored9c0f92009-10-29 00:04:11 +00001842 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001843 InstPartialSpec->setTypeAsWritten(WrittenTy);
1844
Douglas Gregored9c0f92009-10-29 00:04:11 +00001845 // Add this partial specialization to the set of class template partial
1846 // specializations.
1847 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1848 InsertPos);
1849 return false;
1850}
1851
John McCall21ef0fa2010-03-11 09:03:00 +00001852TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001853TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001854 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001855 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1856 assert(OldTInfo && "substituting function without type source info");
1857 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001858 TypeSourceInfo *NewTInfo
1859 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1860 D->getTypeSpecStartLoc(),
1861 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001862 if (!NewTInfo)
1863 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001864
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001865 if (NewTInfo != OldTInfo) {
1866 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001867 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001868 if (FunctionProtoTypeLoc *OldProtoLoc
1869 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1870 TypeLoc NewTL = NewTInfo->getTypeLoc();
1871 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1872 assert(NewProtoLoc && "Missing prototype?");
1873 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1874 // FIXME: Variadic templates will break this.
1875 Params.push_back(NewProtoLoc->getArg(i));
1876 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001877 OldProtoLoc->getArg(i),
1878 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001879 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001880 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001881 } else {
1882 // The function type itself was not dependent and therefore no
1883 // substitution occurred. However, we still need to instantiate
1884 // the function parameters themselves.
1885 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001886 if (FunctionProtoTypeLoc *OldProtoLoc
1887 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1888 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1889 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1890 if (!Parm)
1891 return 0;
1892 Params.push_back(Parm);
1893 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001894 }
1895 }
John McCall21ef0fa2010-03-11 09:03:00 +00001896 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001897}
1898
Mike Stump1eb44332009-09-09 15:08:12 +00001899/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001900/// declaration (New) from the corresponding fields of its template (Tmpl).
1901///
1902/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001903bool
1904TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001905 FunctionDecl *Tmpl) {
1906 if (Tmpl->isDeleted())
1907 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Douglas Gregorcca9e962009-07-01 22:01:06 +00001909 // If we are performing substituting explicitly-specified template arguments
1910 // or deduced template arguments into a function template and we reach this
1911 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001912 // to keeping the new function template specialization. We therefore
1913 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001914 // into a template instantiation for this specific function template
1915 // specialization, which is not a SFINAE context, so that we diagnose any
1916 // further errors in the declaration itself.
1917 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1918 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1919 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1920 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001922 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001923 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001924 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001925 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001926 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1927 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001928 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001929 }
1930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001932 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1933 assert(Proto && "Function template without prototype?");
1934
1935 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1936 Proto->getNoReturnAttr()) {
1937 // The function has an exception specification or a "noreturn"
1938 // attribute. Substitute into each of the exception types.
1939 llvm::SmallVector<QualType, 4> Exceptions;
1940 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1941 // FIXME: Poor location information!
1942 QualType T
1943 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1944 New->getLocation(), New->getDeclName());
1945 if (T.isNull() ||
1946 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1947 continue;
1948
1949 Exceptions.push_back(T);
1950 }
1951
1952 // Rebuild the function type
1953
1954 const FunctionProtoType *NewProto
1955 = New->getType()->getAs<FunctionProtoType>();
1956 assert(NewProto && "Template instantiation without function prototype?");
1957 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1958 NewProto->arg_type_begin(),
1959 NewProto->getNumArgs(),
1960 NewProto->isVariadic(),
1961 NewProto->getTypeQuals(),
1962 Proto->hasExceptionSpec(),
1963 Proto->hasAnyExceptionSpec(),
1964 Exceptions.size(),
1965 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001966 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001967 }
1968
Douglas Gregore53060f2009-06-25 22:08:12 +00001969 return false;
1970}
1971
Douglas Gregor5545e162009-03-24 00:38:23 +00001972/// \brief Initializes common fields of an instantiated method
1973/// declaration (New) from the corresponding fields of its template
1974/// (Tmpl).
1975///
1976/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001977bool
1978TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001979 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001980 if (InitFunctionInstantiation(New, Tmpl))
1981 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Douglas Gregor5545e162009-03-24 00:38:23 +00001983 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1984 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001985 if (Tmpl->isVirtualAsWritten())
1986 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001987
1988 // FIXME: attributes
1989 // FIXME: New needs a pointer to Tmpl
1990 return false;
1991}
Douglas Gregora58861f2009-05-13 20:28:22 +00001992
1993/// \brief Instantiate the definition of the given function from its
1994/// template.
1995///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001996/// \param PointOfInstantiation the point at which the instantiation was
1997/// required. Note that this is not precisely a "point of instantiation"
1998/// for the function, but it's close.
1999///
Douglas Gregora58861f2009-05-13 20:28:22 +00002000/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002001/// function template specialization or member function of a class template
2002/// specialization.
2003///
2004/// \param Recursive if true, recursively instantiates any functions that
2005/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002006///
2007/// \param DefinitionRequired if true, then we are performing an explicit
2008/// instantiation where the body of the function is required. Complain if
2009/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002010void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002011 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002012 bool Recursive,
2013 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00002014 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002015 return;
2016
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002017 // Never instantiate an explicit specialization.
2018 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2019 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002020
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002021 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002022 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002023 Stmt *Pattern = 0;
2024 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002025 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002026
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002027 if (!Pattern) {
2028 if (DefinitionRequired) {
2029 if (Function->getPrimaryTemplate())
2030 Diag(PointOfInstantiation,
2031 diag::err_explicit_instantiation_undefined_func_template)
2032 << Function->getPrimaryTemplate();
2033 else
2034 Diag(PointOfInstantiation,
2035 diag::err_explicit_instantiation_undefined_member)
2036 << 1 << Function->getDeclName() << Function->getDeclContext();
2037
2038 if (PatternDecl)
2039 Diag(PatternDecl->getLocation(),
2040 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002041 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002042 }
2043
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002044 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002045 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002046
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002047 // C++0x [temp.explicit]p9:
2048 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002049 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002050 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002051 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002052 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002053 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002054 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002056 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2057 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002058 return;
2059
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002060 // If we're performing recursive template instantiation, create our own
2061 // queue of pending implicit instantiations that we will instantiate later,
2062 // while we're still within our own instantiation context.
2063 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2064 if (Recursive)
2065 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Douglas Gregor9679caf2010-05-12 17:27:19 +00002067 EnterExpressionEvaluationContext EvalContext(*this,
2068 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002069 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2070
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002071 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002072 // recorded, unless we're actually a member function within a local
2073 // class, in which case we need to merge our results with the parent
2074 // scope (of the enclosing function).
2075 bool MergeWithParentScope = false;
2076 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2077 MergeWithParentScope = Rec->isLocalClass();
2078
2079 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002081 // Introduce the instantiated function parameters into the local
2082 // instantiation scope.
2083 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2084 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2085 Function->getParamDecl(I));
2086
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002087 // Enter the scope of this instantiation. We don't use
2088 // PushDeclContext because we don't have a scope.
2089 DeclContext *PreviousContext = CurContext;
2090 CurContext = Function;
2091
Mike Stump1eb44332009-09-09 15:08:12 +00002092 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002093 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002094
2095 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002096 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002097 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2098 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2099 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002100 }
2101
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002102 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002103 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002104
Douglas Gregor52604ab2009-09-11 21:19:12 +00002105 if (Body.isInvalid())
2106 Function->setInvalidDecl();
2107
Mike Stump1eb44332009-09-09 15:08:12 +00002108 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002109 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002110
John McCall0c01d182010-03-24 05:22:00 +00002111 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2112
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002113 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002114
2115 DeclGroupRef DG(Function);
2116 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Douglas Gregor60406be2010-01-16 22:29:39 +00002118 // This class may have local implicit instantiations that need to be
2119 // instantiation within this scope.
2120 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2121 Scope.Exit();
2122
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002123 if (Recursive) {
2124 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002125 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002126 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002128 // Restore the set of pending implicit instantiations.
2129 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2130 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002131}
2132
2133/// \brief Instantiate the definition of the given variable from its
2134/// template.
2135///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002136/// \param PointOfInstantiation the point at which the instantiation was
2137/// required. Note that this is not precisely a "point of instantiation"
2138/// for the function, but it's close.
2139///
2140/// \param Var the already-instantiated declaration of a static member
2141/// variable of a class template specialization.
2142///
2143/// \param Recursive if true, recursively instantiates any functions that
2144/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002145///
2146/// \param DefinitionRequired if true, then we are performing an explicit
2147/// instantiation where an out-of-line definition of the member variable
2148/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002149void Sema::InstantiateStaticDataMemberDefinition(
2150 SourceLocation PointOfInstantiation,
2151 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002152 bool Recursive,
2153 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002154 if (Var->isInvalidDecl())
2155 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Douglas Gregor7caa6822009-07-24 20:34:43 +00002157 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002158 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002159 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002160 assert(Def->isStaticDataMember() && "Not a static data member?");
2161 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002162
Douglas Gregor0d035142009-10-27 18:42:08 +00002163 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002164 // We did not find an out-of-line definition of this static data member,
2165 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002166 // instantiate this definition (or provide a specialization for it) in
2167 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002168 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002169 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002170 Diag(PointOfInstantiation,
2171 diag::err_explicit_instantiation_undefined_member)
2172 << 2 << Var->getDeclName() << Var->getDeclContext();
2173 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2174 }
2175
Douglas Gregor7caa6822009-07-24 20:34:43 +00002176 return;
2177 }
2178
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002179 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002180 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002181 return;
2182
2183 // C++0x [temp.explicit]p9:
2184 // Except for inline functions, other explicit instantiation declarations
2185 // have the effect of suppressing the implicit instantiation of the entity
2186 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002187 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002188 == TSK_ExplicitInstantiationDeclaration)
2189 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Douglas Gregor7caa6822009-07-24 20:34:43 +00002191 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2192 if (Inst)
2193 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Douglas Gregor7caa6822009-07-24 20:34:43 +00002195 // If we're performing recursive template instantiation, create our own
2196 // queue of pending implicit instantiations that we will instantiate later,
2197 // while we're still within our own instantiation context.
2198 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2199 if (Recursive)
2200 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Douglas Gregor7caa6822009-07-24 20:34:43 +00002202 // Enter the scope of this instantiation. We don't use
2203 // PushDeclContext because we don't have a scope.
2204 DeclContext *PreviousContext = CurContext;
2205 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002207 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002208 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002209 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002210 CurContext = PreviousContext;
2211
2212 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002213 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2214 assert(MSInfo && "Missing member specialization information?");
2215 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2216 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002217 DeclGroupRef DG(Var);
2218 Consumer.HandleTopLevelDecl(DG);
2219 }
Mike Stump1eb44332009-09-09 15:08:12 +00002220
Douglas Gregor7caa6822009-07-24 20:34:43 +00002221 if (Recursive) {
2222 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002223 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002224 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Douglas Gregor7caa6822009-07-24 20:34:43 +00002226 // Restore the set of pending implicit instantiations.
2227 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002228 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002229}
Douglas Gregor815215d2009-05-27 05:35:12 +00002230
Anders Carlsson09025312009-08-29 05:16:22 +00002231void
2232Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2233 const CXXConstructorDecl *Tmpl,
2234 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Anders Carlsson09025312009-08-29 05:16:22 +00002236 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002237 bool AnyErrors = false;
2238
Anders Carlsson09025312009-08-29 05:16:22 +00002239 // Instantiate all the initializers.
2240 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002241 InitsEnd = Tmpl->init_end();
2242 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002243 CXXBaseOrMemberInitializer *Init = *Inits;
2244
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002245 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002246 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002247 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002249 // Instantiate the initializer.
2250 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2251 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2252 AnyErrors = true;
2253 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002254 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002255
Anders Carlsson09025312009-08-29 05:16:22 +00002256 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002257 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002258 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002259 TemplateArgs,
2260 Init->getSourceLocation(),
2261 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002262 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002263 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002264 New->setInvalidDecl();
2265 continue;
2266 }
2267
John McCalla93c9342009-12-07 02:54:59 +00002268 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002269 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002270 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002271 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002272 Init->getRParenLoc(),
2273 New->getParent());
2274 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002275 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002276
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002277 // Is this an anonymous union?
2278 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002279 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2280 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002281 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002282 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2283 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002284 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002285
2286 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002287 NewArgs.size(),
2288 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002289 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002290 Init->getRParenLoc());
2291 }
2292
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002293 if (NewInit.isInvalid()) {
2294 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002295 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002296 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002297 // FIXME: It would be nice if ASTOwningVector had a release function.
2298 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Anders Carlsson09025312009-08-29 05:16:22 +00002300 NewInits.push_back((MemInitTy *)NewInit.get());
2301 }
2302 }
Mike Stump1eb44332009-09-09 15:08:12 +00002303
Anders Carlsson09025312009-08-29 05:16:22 +00002304 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002305 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002306 /*FIXME: ColonLoc */
2307 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002308 NewInits.data(), NewInits.size(),
2309 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002310}
2311
John McCall52a575a2009-08-29 08:11:13 +00002312// TODO: this could be templated if the various decl types used the
2313// same method name.
2314static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2315 ClassTemplateDecl *Instance) {
2316 Pattern = Pattern->getCanonicalDecl();
2317
2318 do {
2319 Instance = Instance->getCanonicalDecl();
2320 if (Pattern == Instance) return true;
2321 Instance = Instance->getInstantiatedFromMemberTemplate();
2322 } while (Instance);
2323
2324 return false;
2325}
2326
Douglas Gregor0d696532009-09-28 06:34:35 +00002327static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2328 FunctionTemplateDecl *Instance) {
2329 Pattern = Pattern->getCanonicalDecl();
2330
2331 do {
2332 Instance = Instance->getCanonicalDecl();
2333 if (Pattern == Instance) return true;
2334 Instance = Instance->getInstantiatedFromMemberTemplate();
2335 } while (Instance);
2336
2337 return false;
2338}
2339
Douglas Gregored9c0f92009-10-29 00:04:11 +00002340static bool
2341isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2342 ClassTemplatePartialSpecializationDecl *Instance) {
2343 Pattern
2344 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2345 do {
2346 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2347 Instance->getCanonicalDecl());
2348 if (Pattern == Instance)
2349 return true;
2350 Instance = Instance->getInstantiatedFromMember();
2351 } while (Instance);
2352
2353 return false;
2354}
2355
John McCall52a575a2009-08-29 08:11:13 +00002356static bool isInstantiationOf(CXXRecordDecl *Pattern,
2357 CXXRecordDecl *Instance) {
2358 Pattern = Pattern->getCanonicalDecl();
2359
2360 do {
2361 Instance = Instance->getCanonicalDecl();
2362 if (Pattern == Instance) return true;
2363 Instance = Instance->getInstantiatedFromMemberClass();
2364 } while (Instance);
2365
2366 return false;
2367}
2368
2369static bool isInstantiationOf(FunctionDecl *Pattern,
2370 FunctionDecl *Instance) {
2371 Pattern = Pattern->getCanonicalDecl();
2372
2373 do {
2374 Instance = Instance->getCanonicalDecl();
2375 if (Pattern == Instance) return true;
2376 Instance = Instance->getInstantiatedFromMemberFunction();
2377 } while (Instance);
2378
2379 return false;
2380}
2381
2382static bool isInstantiationOf(EnumDecl *Pattern,
2383 EnumDecl *Instance) {
2384 Pattern = Pattern->getCanonicalDecl();
2385
2386 do {
2387 Instance = Instance->getCanonicalDecl();
2388 if (Pattern == Instance) return true;
2389 Instance = Instance->getInstantiatedFromMemberEnum();
2390 } while (Instance);
2391
2392 return false;
2393}
2394
John McCalled976492009-12-04 22:46:56 +00002395static bool isInstantiationOf(UsingShadowDecl *Pattern,
2396 UsingShadowDecl *Instance,
2397 ASTContext &C) {
2398 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2399}
2400
2401static bool isInstantiationOf(UsingDecl *Pattern,
2402 UsingDecl *Instance,
2403 ASTContext &C) {
2404 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2405}
2406
John McCall7ba107a2009-11-18 02:36:19 +00002407static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2408 UsingDecl *Instance,
2409 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002410 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002411}
2412
2413static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002414 UsingDecl *Instance,
2415 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002416 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002417}
2418
John McCall52a575a2009-08-29 08:11:13 +00002419static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2420 VarDecl *Instance) {
2421 assert(Instance->isStaticDataMember());
2422
2423 Pattern = Pattern->getCanonicalDecl();
2424
2425 do {
2426 Instance = Instance->getCanonicalDecl();
2427 if (Pattern == Instance) return true;
2428 Instance = Instance->getInstantiatedFromStaticDataMember();
2429 } while (Instance);
2430
2431 return false;
2432}
2433
John McCalled976492009-12-04 22:46:56 +00002434// Other is the prospective instantiation
2435// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002436static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002437 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002438 if (UnresolvedUsingTypenameDecl *UUD
2439 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2440 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2441 return isInstantiationOf(UUD, UD, Ctx);
2442 }
2443 }
2444
2445 if (UnresolvedUsingValueDecl *UUD
2446 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002447 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2448 return isInstantiationOf(UUD, UD, Ctx);
2449 }
2450 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002451
Anders Carlsson0d8df782009-08-29 19:37:28 +00002452 return false;
2453 }
Mike Stump1eb44332009-09-09 15:08:12 +00002454
John McCall52a575a2009-08-29 08:11:13 +00002455 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2456 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002457
John McCall52a575a2009-08-29 08:11:13 +00002458 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2459 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002460
John McCall52a575a2009-08-29 08:11:13 +00002461 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2462 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002463
Douglas Gregor7caa6822009-07-24 20:34:43 +00002464 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002465 if (Var->isStaticDataMember())
2466 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2467
2468 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2469 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002470
Douglas Gregor0d696532009-09-28 06:34:35 +00002471 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2472 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2473
Douglas Gregored9c0f92009-10-29 00:04:11 +00002474 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2475 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2476 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2477 PartialSpec);
2478
Anders Carlssond8b285f2009-09-01 04:26:58 +00002479 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2480 if (!Field->getDeclName()) {
2481 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002482 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002483 cast<FieldDecl>(D);
2484 }
2485 }
Mike Stump1eb44332009-09-09 15:08:12 +00002486
John McCalled976492009-12-04 22:46:56 +00002487 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2488 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2489
2490 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2491 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2492
Douglas Gregor815215d2009-05-27 05:35:12 +00002493 return D->getDeclName() && isa<NamedDecl>(Other) &&
2494 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2495}
2496
2497template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002498static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002499 NamedDecl *D,
2500 ForwardIterator first,
2501 ForwardIterator last) {
2502 for (; first != last; ++first)
2503 if (isInstantiationOf(Ctx, D, *first))
2504 return cast<NamedDecl>(*first);
2505
2506 return 0;
2507}
2508
John McCall02cace72009-08-28 07:59:38 +00002509/// \brief Finds the instantiation of the given declaration context
2510/// within the current instantiation.
2511///
2512/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002513DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002514 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002515 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002516 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002517 return cast_or_null<DeclContext>(ID);
2518 } else return DC;
2519}
2520
Douglas Gregored961e72009-05-27 17:54:46 +00002521/// \brief Find the instantiation of the given declaration within the
2522/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002523///
2524/// This routine is intended to be used when \p D is a declaration
2525/// referenced from within a template, that needs to mapped into the
2526/// corresponding declaration within an instantiation. For example,
2527/// given:
2528///
2529/// \code
2530/// template<typename T>
2531/// struct X {
2532/// enum Kind {
2533/// KnownValue = sizeof(T)
2534/// };
2535///
2536/// bool getKind() const { return KnownValue; }
2537/// };
2538///
2539/// template struct X<int>;
2540/// \endcode
2541///
2542/// In the instantiation of X<int>::getKind(), we need to map the
2543/// EnumConstantDecl for KnownValue (which refers to
2544/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002545/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2546/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002547NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002548 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002549 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002550 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002551 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002552 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002553 // D is a local of some kind. Look into the map of local
2554 // declarations to their instantiations.
2555 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2556 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002557
Douglas Gregore95b4092009-09-16 18:34:49 +00002558 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2559 if (!Record->isDependentContext())
2560 return D;
2561
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002562 // If the RecordDecl is actually the injected-class-name or a
2563 // "templated" declaration for a class template, class template
2564 // partial specialization, or a member class of a class template,
2565 // substitute into the injected-class-name of the class template
2566 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002567 QualType T;
2568 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2569
2570 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002571 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002572 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2573 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002574 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002575
2576 // If we call SubstType with an InjectedClassNameType here we
2577 // can end up in an infinite loop.
2578 T = Context.getTypeDeclType(Record);
2579 assert(isa<InjectedClassNameType>(T) &&
2580 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002581 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002582 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002583
2584 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002585 // Substitute into the injected-class-name to get the type
2586 // corresponding to the instantiation we want, which may also be
2587 // the current instantiation (if we're in a template
2588 // definition). This substitution should never fail, since we
2589 // know we can instantiate the injected-class-name or we
2590 // wouldn't have gotten to the injected-class-name!
2591
2592 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2593 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002594 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2595 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2596
2597 if (!T->isDependentType()) {
2598 assert(T->isRecordType() && "Instantiation must produce a record type");
2599 return T->getAs<RecordType>()->getDecl();
2600 }
2601
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002602 // We are performing "partial" template instantiation to create
2603 // the member declarations for the members of a class template
2604 // specialization. Therefore, D is actually referring to something
2605 // in the current instantiation. Look through the current
2606 // context, which contains actual instantiations, to find the
2607 // instantiation of the "current instantiation" that D refers
2608 // to.
2609 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002610 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002611 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002612 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002613 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002614 if (isInstantiationOf(ClassTemplate,
2615 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002616 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002617
2618 if (!DC->isDependentContext())
2619 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002620 }
2621
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002622 // We're performing "instantiation" of a member of the current
2623 // instantiation while we are type-checking the
2624 // definition. Compute the declaration context and return that.
2625 assert(!SawNonDependentContext &&
2626 "No dependent context while instantiating record");
2627 DeclContext *DC = computeDeclContext(T);
2628 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002629 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002630 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002631 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002632
Douglas Gregore95b4092009-09-16 18:34:49 +00002633 // Fall through to deal with other dependent record types (e.g.,
2634 // anonymous unions in class templates).
2635 }
John McCall52a575a2009-08-29 08:11:13 +00002636
Douglas Gregore95b4092009-09-16 18:34:49 +00002637 if (!ParentDC->isDependentContext())
2638 return D;
2639
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002640 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002641 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002642 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Douglas Gregor815215d2009-05-27 05:35:12 +00002644 if (ParentDC != D->getDeclContext()) {
2645 // We performed some kind of instantiation in the parent context,
2646 // so now we need to look into the instantiated parent context to
2647 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002648
John McCall3cb0ebd2010-03-10 03:28:59 +00002649 // If our context used to be dependent, we may need to instantiate
2650 // it before performing lookup into that context.
2651 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002652 if (!Spec->isDependentContext()) {
2653 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002654 const RecordType *Tag = T->getAs<RecordType>();
2655 assert(Tag && "type of non-dependent record is not a RecordType");
2656 if (!Tag->isBeingDefined() &&
2657 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2658 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002659 }
2660 }
2661
Douglas Gregor815215d2009-05-27 05:35:12 +00002662 NamedDecl *Result = 0;
2663 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002664 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002665 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2666 } else {
2667 // Since we don't have a name for the entity we're looking for,
2668 // our only option is to walk through all of the declarations to
2669 // find that name. This will occur in a few cases:
2670 //
2671 // - anonymous struct/union within a template
2672 // - unnamed class/struct/union/enum within a template
2673 //
2674 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002675 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002676 ParentDC->decls_begin(),
2677 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
John McCall9f54ad42009-12-10 09:41:52 +00002680 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002681 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2682 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002683 && "Unable to find instantiation of declaration!");
2684
Douglas Gregor815215d2009-05-27 05:35:12 +00002685 D = Result;
2686 }
2687
Douglas Gregor815215d2009-05-27 05:35:12 +00002688 return D;
2689}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002690
Mike Stump1eb44332009-09-09 15:08:12 +00002691/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002692/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002693void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2694 while (!PendingLocalImplicitInstantiations.empty() ||
2695 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2696 PendingImplicitInstantiation Inst;
2697
2698 if (PendingLocalImplicitInstantiations.empty()) {
2699 Inst = PendingImplicitInstantiations.front();
2700 PendingImplicitInstantiations.pop_front();
2701 } else {
2702 Inst = PendingLocalImplicitInstantiations.front();
2703 PendingLocalImplicitInstantiations.pop_front();
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Douglas Gregor7caa6822009-07-24 20:34:43 +00002706 // Instantiate function definitions
2707 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002708 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002709 Function->getLocation(), *this,
2710 Context.getSourceManager(),
2711 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002713 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002714 continue;
2715 }
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Douglas Gregor7caa6822009-07-24 20:34:43 +00002717 // Instantiate static data member definitions.
2718 VarDecl *Var = cast<VarDecl>(Inst.first);
2719 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002720
Chandler Carruth291b4412010-02-13 10:17:50 +00002721 // Don't try to instantiate declarations if the most recent redeclaration
2722 // is invalid.
2723 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2724 continue;
2725
2726 // Check if the most recent declaration has changed the specialization kind
2727 // and removed the need for implicit instantiation.
2728 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2729 case TSK_Undeclared:
2730 assert(false && "Cannot instantitiate an undeclared specialization.");
2731 case TSK_ExplicitInstantiationDeclaration:
2732 case TSK_ExplicitInstantiationDefinition:
2733 case TSK_ExplicitSpecialization:
2734 continue; // No longer need implicit instantiation.
2735 case TSK_ImplicitInstantiation:
2736 break;
2737 }
2738
Mike Stump1eb44332009-09-09 15:08:12 +00002739 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002740 Var->getLocation(), *this,
2741 Context.getSourceManager(),
2742 "instantiating static data member "
2743 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Douglas Gregor7caa6822009-07-24 20:34:43 +00002745 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002746 }
2747}
John McCall0c01d182010-03-24 05:22:00 +00002748
2749void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2750 const MultiLevelTemplateArgumentList &TemplateArgs) {
2751 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2752 E = Pattern->ddiag_end(); I != E; ++I) {
2753 DependentDiagnostic *DD = *I;
2754
2755 switch (DD->getKind()) {
2756 case DependentDiagnostic::Access:
2757 HandleDependentAccessCheck(*DD, TemplateArgs);
2758 break;
2759 }
2760 }
2761}