blob: 1b28579e61d45f703582217047a4de2c8231268d [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
Sean Hunt9a555912010-05-30 07:21:58 +000045 // clang/AST/DeclNodes.inc.
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);
Abramo Bagnara6206d532010-06-05 05:09:32 +000051 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000052 Decl *VisitFieldDecl(FieldDecl *D);
53 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
54 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000055 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000056 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000057 Decl *VisitFunctionDecl(FunctionDecl *D,
58 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000059 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000060 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
61 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000062 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000063 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000064 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000065 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000066 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000067 Decl *VisitClassTemplatePartialSpecializationDecl(
68 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000069 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000070 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000071 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000072 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000073 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000074 Decl *VisitUsingDecl(UsingDecl *D);
75 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000076 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
77 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000078
Douglas Gregor8dbc2692009-03-17 21:15:40 +000079 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000080 Decl *VisitDecl(Decl *D) {
81 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
82 Diagnostic::Error,
83 "cannot instantiate %0 yet");
84 SemaRef.Diag(D->getLocation(), DiagID)
85 << D->getDeclKindName();
86
Douglas Gregor8dbc2692009-03-17 21:15:40 +000087 return 0;
88 }
Douglas Gregor5545e162009-03-24 00:38:23 +000089
John McCallfd810b12009-08-14 02:03:10 +000090 const LangOptions &getLangOptions() {
91 return SemaRef.getLangOptions();
92 }
93
Douglas Gregor5545e162009-03-24 00:38:23 +000094 // Helper functions for instantiating methods.
John McCall21ef0fa2010-03-11 09:03:00 +000095 TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000096 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000097 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000098 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000099
100 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +0000101 SubstTemplateParams(TemplateParameterList *List);
John McCallb6217662010-03-15 10:12:16 +0000102
103 bool SubstQualifier(const DeclaratorDecl *OldDecl,
104 DeclaratorDecl *NewDecl);
105 bool SubstQualifier(const TagDecl *OldDecl,
106 TagDecl *NewDecl);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000107
108 bool InstantiateClassTemplatePartialSpecialization(
109 ClassTemplateDecl *ClassTemplate,
110 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000111 };
112}
113
John McCallb6217662010-03-15 10:12:16 +0000114bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
115 DeclaratorDecl *NewDecl) {
116 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
117 if (!OldQual) return false;
118
119 SourceRange QualRange = OldDecl->getQualifierRange();
120
121 NestedNameSpecifier *NewQual
122 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
123 if (!NewQual)
124 return true;
125
126 NewDecl->setQualifierInfo(NewQual, QualRange);
127 return false;
128}
129
130bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
131 TagDecl *NewDecl) {
132 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
133 if (!OldQual) return false;
134
135 SourceRange QualRange = OldDecl->getQualifierRange();
136
137 NestedNameSpecifier *NewQual
138 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
139 if (!NewQual)
140 return true;
141
142 NewDecl->setQualifierInfo(NewQual, QualRange);
143 return false;
144}
145
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000146// FIXME: Is this too simple?
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000147void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000148 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
149 TmplAttr = TmplAttr->getNext()) {
150
151 // FIXME: Is cloning correct for all attributes?
152 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
153
154 New->addAttr(NewAttr);
155 }
156}
157
Douglas Gregor4f722be2009-03-25 15:45:12 +0000158Decl *
159TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
160 assert(false && "Translation units cannot be instantiated");
161 return D;
162}
163
164Decl *
165TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
166 assert(false && "Namespaces cannot be instantiated");
167 return D;
168}
169
John McCall3dbd3d52010-02-16 06:53:13 +0000170Decl *
171TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
172 NamespaceAliasDecl *Inst
173 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
174 D->getNamespaceLoc(),
175 D->getAliasLoc(),
176 D->getNamespace()->getIdentifier(),
177 D->getQualifierRange(),
178 D->getQualifier(),
179 D->getTargetNameLoc(),
180 D->getNamespace());
181 Owner->addDecl(Inst);
182 return Inst;
183}
184
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000185Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
186 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000187 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000188 if (DI->getType()->isDependentType() ||
189 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000190 DI = SemaRef.SubstType(DI, TemplateArgs,
191 D->getLocation(), D->getDeclName());
192 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000193 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000194 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000195 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000196 } else {
197 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000198 }
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000200 // Create the new typedef
201 TypedefDecl *Typedef
202 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000203 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204 if (Invalid)
205 Typedef->setInvalidDecl();
206
Douglas Gregord57a38e2010-04-23 16:25:07 +0000207 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
208 TagDecl *TD = TT->getDecl();
209
210 // If the TagDecl that the TypedefDecl points to is an anonymous decl
211 // keep track of the TypedefDecl.
212 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
213 TD->setTypedefForAnonDecl(Typedef);
214 }
215
John McCall5126fd02009-12-30 00:31:22 +0000216 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000217 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
218 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000219 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
220 }
221
Douglas Gregora6b09072010-05-17 23:46:49 +0000222 InstantiateAttrs(D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000223
John McCall46460a62010-01-20 21:53:11 +0000224 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000225 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000227 return Typedef;
228}
229
Douglas Gregor6eef5192009-12-14 19:27:10 +0000230/// \brief Instantiate the arguments provided as part of initialization.
231///
232/// \returns true if an error occurred, false otherwise.
233static bool InstantiateInitializationArguments(Sema &SemaRef,
234 Expr **Args, unsigned NumArgs,
235 const MultiLevelTemplateArgumentList &TemplateArgs,
236 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
237 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
238 for (unsigned I = 0; I != NumArgs; ++I) {
239 // When we hit the first defaulted argument, break out of the loop:
240 // we don't pass those default arguments on.
241 if (Args[I]->isDefaultArgument())
242 break;
243
244 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
245 if (Arg.isInvalid())
246 return true;
247
248 Expr *ArgExpr = (Expr *)Arg.get();
249 InitArgs.push_back(Arg.release());
250
251 // FIXME: We're faking all of the comma locations. Do we need them?
252 FakeCommaLocs.push_back(
253 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
254 }
255
256 return false;
257}
258
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000259/// \brief Instantiate an initializer, breaking it into separate
260/// initialization arguments.
261///
262/// \param S The semantic analysis object.
263///
264/// \param Init The initializer to instantiate.
265///
266/// \param TemplateArgs Template arguments to be substituted into the
267/// initializer.
268///
269/// \param NewArgs Will be filled in with the instantiation arguments.
270///
271/// \returns true if an error occurred, false otherwise
272static bool InstantiateInitializer(Sema &S, Expr *Init,
273 const MultiLevelTemplateArgumentList &TemplateArgs,
274 SourceLocation &LParenLoc,
275 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
276 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
277 SourceLocation &RParenLoc) {
278 NewArgs.clear();
279 LParenLoc = SourceLocation();
280 RParenLoc = SourceLocation();
281
282 if (!Init)
283 return false;
284
285 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
286 Init = ExprTemp->getSubExpr();
287
288 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
289 Init = Binder->getSubExpr();
290
291 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
292 Init = ICE->getSubExprAsWritten();
293
294 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
295 LParenLoc = ParenList->getLParenLoc();
296 RParenLoc = ParenList->getRParenLoc();
297 return InstantiateInitializationArguments(S, ParenList->getExprs(),
298 ParenList->getNumExprs(),
299 TemplateArgs, CommaLocs,
300 NewArgs);
301 }
302
303 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000304 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
305 if (InstantiateInitializationArguments(S,
306 Construct->getArgs(),
307 Construct->getNumArgs(),
308 TemplateArgs,
309 CommaLocs, NewArgs))
310 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000311
Douglas Gregor28329e52010-03-24 21:22:47 +0000312 // FIXME: Fake locations!
313 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
314 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
315 return false;
316 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000317 }
318
319 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
320 if (Result.isInvalid())
321 return true;
322
323 NewArgs.push_back(Result.takeAs<Expr>());
324 return false;
325}
326
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000327Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000328 // If this is the variable for an anonymous struct or union,
329 // instantiate the anonymous struct/union type first.
330 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
331 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
332 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
333 return 0;
334
John McCallce3ff2b2009-08-25 22:02:44 +0000335 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000336 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000337 TemplateArgs,
338 D->getTypeSpecStartLoc(),
339 D->getDeclName());
340 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000341 return 0;
342
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000343 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000344 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
345 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000346 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000347 D->getStorageClass(),
348 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000349 Var->setThreadSpecified(D->isThreadSpecified());
350 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
351 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000352
John McCallb6217662010-03-15 10:12:16 +0000353 // Substitute the nested name specifier, if any.
354 if (SubstQualifier(D, Var))
355 return 0;
356
Mike Stump1eb44332009-09-09 15:08:12 +0000357 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000358 // out-of-line, the instantiation will have the same lexical
359 // context (which will be a namespace scope) as the template.
360 if (D->isOutOfLine())
361 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000362
John McCall46460a62010-01-20 21:53:11 +0000363 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000364
365 if (!D->isStaticDataMember())
366 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000367
Mike Stump390b4cc2009-05-16 07:39:55 +0000368 // FIXME: In theory, we could have a previous declaration for variables that
369 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000370 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000371 // FIXME: having to fake up a LookupResult is dumb.
372 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000373 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000374 if (D->isStaticDataMember())
375 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000376 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Douglas Gregor7caa6822009-07-24 20:34:43 +0000378 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000379 if (!D->isStaticDataMember())
380 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000381 Owner->makeDeclVisibleInContext(Var);
382 } else {
383 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000384
385 if (Owner->isFunctionOrMethod())
386 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000389 // Link instantiations of static data members back to the template from
390 // which they were instantiated.
391 if (Var->isStaticDataMember())
392 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000393 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000394
Douglas Gregor60c93c92010-02-09 07:26:29 +0000395 if (Var->getAnyInitializer()) {
396 // We already have an initializer in the class.
397 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000398 if (Var->isStaticDataMember() && !D->isOutOfLine())
399 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
400 else
401 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
402
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000403 // Instantiate the initializer.
404 SourceLocation LParenLoc, RParenLoc;
405 llvm::SmallVector<SourceLocation, 4> CommaLocs;
406 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
407 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
408 CommaLocs, InitArgs, RParenLoc)) {
409 // Attach the initializer to the declaration.
410 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000411 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000412 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000413 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000414 move_arg(InitArgs),
415 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000416 RParenLoc);
417 } else if (InitArgs.size() == 1) {
418 Expr *Init = (Expr*)(InitArgs.take()[0]);
419 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
420 SemaRef.Owned(Init),
421 false);
422 } else {
423 assert(InitArgs.size() == 0);
424 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000425 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000426 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000427 // FIXME: Not too happy about invalidating the declaration
428 // because of a bogus initializer.
429 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000430 }
431
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000432 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000433 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
434 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000435
Douglas Gregor5764f612010-05-08 23:05:03 +0000436 // Diagnose unused local variables.
437 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
438 SemaRef.DiagnoseUnusedDecl(Var);
439
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000440 return Var;
441}
442
Abramo Bagnara6206d532010-06-05 05:09:32 +0000443Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
444 AccessSpecDecl* AD
445 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
446 D->getAccessSpecifierLoc(), D->getColonLoc());
447 Owner->addHiddenDecl(AD);
448 return AD;
449}
450
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000451Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
452 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000453 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000454 if (DI->getType()->isDependentType() ||
455 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000456 DI = SemaRef.SubstType(DI, TemplateArgs,
457 D->getLocation(), D->getDeclName());
458 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000459 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000460 Invalid = true;
461 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000462 // C++ [temp.arg.type]p3:
463 // If a declaration acquires a function type through a type
464 // dependent on a template-parameter and this causes a
465 // declaration that does not use the syntactic form of a
466 // function declarator to have function type, the program is
467 // ill-formed.
468 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000469 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000470 Invalid = true;
471 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000472 } else {
473 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000474 }
475
476 Expr *BitWidth = D->getBitWidth();
477 if (Invalid)
478 BitWidth = 0;
479 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000480 // The bit-width expression is not potentially evaluated.
481 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000484 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000485 if (InstantiatedBitWidth.isInvalid()) {
486 Invalid = true;
487 BitWidth = 0;
488 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000489 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000490 }
491
John McCall07fb6be2009-10-22 23:33:21 +0000492 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
493 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000494 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000495 D->getLocation(),
496 D->isMutable(),
497 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000498 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000499 D->getAccess(),
500 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000501 if (!Field) {
502 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000503 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000506 InstantiateAttrs(D, Field);
507
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000508 if (Invalid)
509 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000511 if (!Field->getDeclName()) {
512 // Keep track of where this decl came from.
513 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000514 }
515 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
516 if (Parent->isAnonymousStructOrUnion() &&
517 Parent->getLookupContext()->isFunctionOrMethod())
518 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000521 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000522 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000523 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000524
525 return Field;
526}
527
John McCall02cace72009-08-28 07:59:38 +0000528Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000529 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000530 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000531 if (TypeSourceInfo *Ty = D->getFriendType()) {
532 TypeSourceInfo *InstTy =
533 SemaRef.SubstType(Ty, TemplateArgs,
534 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000535 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000536 return 0;
John McCall02cace72009-08-28 07:59:38 +0000537
Douglas Gregor06245bf2010-04-07 17:57:12 +0000538 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
539 if (!FD)
540 return 0;
541
542 FD->setAccess(AS_public);
543 Owner->addDecl(FD);
544 return FD;
545 }
546
547 NamedDecl *ND = D->getFriendDecl();
548 assert(ND && "friend decl must be a decl or a type!");
549
John McCallaf2094e2010-04-08 09:05:18 +0000550 // All of the Visit implementations for the various potential friend
551 // declarations have to be carefully written to work for friend
552 // objects, with the most important detail being that the target
553 // decl should almost certainly not be placed in Owner.
554 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000555 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
John McCall02cace72009-08-28 07:59:38 +0000557 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000558 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
559 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000560 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000561 Owner->addDecl(FD);
562 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000563}
564
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000565Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
566 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Douglas Gregorac7610d2009-06-22 20:57:11 +0000568 // The expression in a static assertion is not potentially evaluated.
569 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000571 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000572 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000573 if (InstantiatedAssertExpr.isInvalid())
574 return 0;
575
Douglas Gregor43d9d922009-08-08 01:41:12 +0000576 OwningExprResult Message(SemaRef, D->getMessage());
577 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000578 Decl *StaticAssert
579 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000580 move(InstantiatedAssertExpr),
581 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000582 return StaticAssert;
583}
584
585Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000586 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000588 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000589 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000590 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000591 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000592 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000593 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000594 Enum->startDefinition();
595
Douglas Gregor96084f12010-03-01 19:00:07 +0000596 if (D->getDeclContext()->isFunctionOrMethod())
597 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
598
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000599 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000600
601 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000602 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
603 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000604 EC != ECEnd; ++EC) {
605 // The specified value for the enumerator.
606 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000607 if (Expr *UninstValue = EC->getInitExpr()) {
608 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000609 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000610 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
John McCallce3ff2b2009-08-25 22:02:44 +0000612 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000613 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000614
615 // Drop the initial value and continue.
616 bool isInvalid = false;
617 if (Value.isInvalid()) {
618 Value = SemaRef.Owned((Expr *)0);
619 isInvalid = true;
620 }
621
Mike Stump1eb44332009-09-09 15:08:12 +0000622 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000623 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
624 EC->getLocation(), EC->getIdentifier(),
625 move(Value));
626
627 if (isInvalid) {
628 if (EnumConst)
629 EnumConst->setInvalidDecl();
630 Enum->setInvalidDecl();
631 }
632
633 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000634 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000635 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000636 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000637 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000638
639 if (D->getDeclContext()->isFunctionOrMethod()) {
640 // If the enumeration is within a function or method, record the enum
641 // constant as a local.
642 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
643 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000644 }
645 }
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000647 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000648 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000649 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
650 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000651 &Enumerators[0], Enumerators.size(),
652 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000653
654 return Enum;
655}
656
Douglas Gregor6477b692009-03-25 15:04:13 +0000657Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
658 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
659 return 0;
660}
661
John McCalle29ba202009-08-20 01:44:21 +0000662Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000663 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
664
Douglas Gregor550d9b22009-10-31 17:21:17 +0000665 // Create a local instantiation scope for this class template, which
666 // will contain the instantiations of the template parameters.
667 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000668 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000669 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000670 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000671 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000672
673 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000674
675 // Instantiate the qualifier. We have to do this first in case
676 // we're a friend declaration, because if we are then we need to put
677 // the new declaration in the appropriate context.
678 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
679 if (Qualifier) {
680 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
681 Pattern->getQualifierRange(),
682 TemplateArgs);
683 if (!Qualifier) return 0;
684 }
685
686 CXXRecordDecl *PrevDecl = 0;
687 ClassTemplateDecl *PrevClassTemplate = 0;
688
689 // If this isn't a friend, then it's a member template, in which
690 // case we just want to build the instantiation in the
691 // specialization. If it is a friend, we want to build it in
692 // the appropriate context.
693 DeclContext *DC = Owner;
694 if (isFriend) {
695 if (Qualifier) {
696 CXXScopeSpec SS;
697 SS.setScopeRep(Qualifier);
698 SS.setRange(Pattern->getQualifierRange());
699 DC = SemaRef.computeDeclContext(SS);
700 if (!DC) return 0;
701 } else {
702 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
703 Pattern->getDeclContext(),
704 TemplateArgs);
705 }
706
707 // Look for a previous declaration of the template in the owning
708 // context.
709 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
710 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
711 SemaRef.LookupQualifiedName(R, DC);
712
713 if (R.isSingleResult()) {
714 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
715 if (PrevClassTemplate)
716 PrevDecl = PrevClassTemplate->getTemplatedDecl();
717 }
718
719 if (!PrevClassTemplate && Qualifier) {
720 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000721 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
722 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000723 return 0;
724 }
725
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000726 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000727 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000728 bool Complain = true;
729
730 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
731 // template for struct std::tr1::__detail::_Map_base, where the
732 // template parameters of the friend declaration don't match the
733 // template parameters of the original declaration. In this one
734 // case, we don't complain about the ill-formed friend
735 // declaration.
736 if (isFriend && Pattern->getIdentifier() &&
737 Pattern->getIdentifier()->isStr("_Map_base") &&
738 DC->isNamespace() &&
739 cast<NamespaceDecl>(DC)->getIdentifier() &&
740 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
741 DeclContext *DCParent = DC->getParent();
742 if (DCParent->isNamespace() &&
743 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
744 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
745 DeclContext *DCParent2 = DCParent->getParent();
746 if (DCParent2->isNamespace() &&
747 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
748 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
749 DCParent2->getParent()->isTranslationUnit())
750 Complain = false;
751 }
752 }
753
John McCall93ba8572010-03-25 06:39:04 +0000754 TemplateParameterList *PrevParams
755 = PrevClassTemplate->getTemplateParameters();
756
757 // Make sure the parameter lists match.
758 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000759 Complain,
760 Sema::TPL_TemplateMatch)) {
761 if (Complain)
762 return 0;
763
764 AdoptedPreviousTemplateParams = true;
765 InstParams = PrevParams;
766 }
John McCall93ba8572010-03-25 06:39:04 +0000767
768 // Do some additional validation, then merge default arguments
769 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000770 if (!AdoptedPreviousTemplateParams &&
771 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000772 Sema::TPC_ClassTemplate))
773 return 0;
774 }
775 }
776
John McCalle29ba202009-08-20 01:44:21 +0000777 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000778 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000779 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000780 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000781 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000782
John McCall93ba8572010-03-25 06:39:04 +0000783 if (Qualifier)
784 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000785
John McCalle29ba202009-08-20 01:44:21 +0000786 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000787 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
788 D->getIdentifier(), InstParams, RecordInst,
789 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000790 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000791
John McCall93ba8572010-03-25 06:39:04 +0000792 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000793 if (PrevClassTemplate)
794 Inst->setAccess(PrevClassTemplate->getAccess());
795 else
796 Inst->setAccess(D->getAccess());
797
John McCall93ba8572010-03-25 06:39:04 +0000798 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
799 // TODO: do we want to track the instantiation progeny of this
800 // friend target decl?
801 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000802 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000803 Inst->setInstantiatedFromMemberTemplate(D);
804 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000805
806 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000807 SemaRef.Context.getInjectedClassNameType(RecordInst,
808 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000809
Douglas Gregor259571e2009-10-30 22:42:42 +0000810 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000811 if (isFriend) {
812 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000813 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000814 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000815
John McCalle29ba202009-08-20 01:44:21 +0000816 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000817
Douglas Gregored9c0f92009-10-29 00:04:11 +0000818 // Instantiate all of the partial specializations of this member class
819 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000820 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
821 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000822 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
823 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
824
John McCalle29ba202009-08-20 01:44:21 +0000825 return Inst;
826}
827
Douglas Gregord60e1052009-08-27 16:57:43 +0000828Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000829TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
830 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000831 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
832
833 // Lookup the already-instantiated declaration in the instantiation
834 // of the class template and return that.
835 DeclContext::lookup_result Found
836 = Owner->lookup(ClassTemplate->getDeclName());
837 if (Found.first == Found.second)
838 return 0;
839
840 ClassTemplateDecl *InstClassTemplate
841 = dyn_cast<ClassTemplateDecl>(*Found.first);
842 if (!InstClassTemplate)
843 return 0;
844
845 Decl *DCanon = D->getCanonicalDecl();
846 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
847 P = InstClassTemplate->getPartialSpecializations().begin(),
848 PEnd = InstClassTemplate->getPartialSpecializations().end();
849 P != PEnd; ++P) {
850 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
851 return &*P;
852 }
853
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000854 return 0;
855}
856
857Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000858TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000859 // Create a local instantiation scope for this function template, which
860 // will contain the instantiations of the template parameters and then get
861 // merged with the local instantiation scope for the function template
862 // itself.
863 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000864
Douglas Gregord60e1052009-08-27 16:57:43 +0000865 TemplateParameterList *TempParams = D->getTemplateParameters();
866 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000867 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000868 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000869
Douglas Gregora735b202009-10-13 14:39:41 +0000870 FunctionDecl *Instantiated = 0;
871 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
872 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
873 InstParams));
874 else
875 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
876 D->getTemplatedDecl(),
877 InstParams));
878
879 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000880 return 0;
881
John McCall46460a62010-01-20 21:53:11 +0000882 Instantiated->setAccess(D->getAccess());
883
Mike Stump1eb44332009-09-09 15:08:12 +0000884 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000885 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000886 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000887 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000888 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000889 assert(InstTemplate &&
890 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000891
John McCallb1a56e72010-03-26 23:10:15 +0000892 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
893
John McCalle976ffe2009-12-14 23:19:40 +0000894 // Link the instantiation back to the pattern *unless* this is a
895 // non-definition friend declaration.
896 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000897 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000898 InstTemplate->setInstantiatedFromMemberTemplate(D);
899
John McCallb1a56e72010-03-26 23:10:15 +0000900 // Make declarations visible in the appropriate context.
901 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000902 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000903
Douglas Gregord60e1052009-08-27 16:57:43 +0000904 return InstTemplate;
905}
906
Douglas Gregord475b8d2009-03-25 21:17:03 +0000907Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
908 CXXRecordDecl *PrevDecl = 0;
909 if (D->isInjectedClassName())
910 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000911 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000912 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
913 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000914 TemplateArgs);
915 if (!Prev) return 0;
916 PrevDecl = cast<CXXRecordDecl>(Prev);
917 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000918
919 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000920 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000921 D->getLocation(), D->getIdentifier(),
922 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000923
924 // Substitute the nested name specifier, if any.
925 if (SubstQualifier(D, Record))
926 return 0;
927
Douglas Gregord475b8d2009-03-25 21:17:03 +0000928 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000929 // FIXME: Check against AS_none is an ugly hack to work around the issue that
930 // the tag decls introduced by friend class declarations don't have an access
931 // specifier. Remove once this area of the code gets sorted out.
932 if (D->getAccess() != AS_none)
933 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000934 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000935 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000936
John McCall02cace72009-08-28 07:59:38 +0000937 // If the original function was part of a friend declaration,
938 // inherit its namespace state.
939 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
940 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
941
Douglas Gregor9901c572010-05-21 00:31:19 +0000942 // Make sure that anonymous structs and unions are recorded.
943 if (D->isAnonymousStructOrUnion()) {
944 Record->setAnonymousStructOrUnion(true);
945 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
946 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
947 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000948
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000949 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000950 return Record;
951}
952
John McCall02cace72009-08-28 07:59:38 +0000953/// Normal class members are of more specific types and therefore
954/// don't make it here. This function serves two purposes:
955/// 1) instantiating function templates
956/// 2) substituting friend declarations
957/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000958Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000959 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000960 // Check whether there is already a function template specialization for
961 // this declaration.
962 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
963 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000964 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000965 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000966 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000967 TemplateArgs.getInnermost().getFlatArgumentList(),
968 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000969 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
971 FunctionTemplateSpecializationInfo *Info
972 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000973 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor127102b2009-06-29 20:59:39 +0000975 // If we already have a function template specialization, return it.
976 if (Info)
977 return Info->Function;
978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
John McCallb0cb0222010-03-27 05:57:59 +0000980 bool isFriend;
981 if (FunctionTemplate)
982 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
983 else
984 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
985
Douglas Gregor79c22782010-01-16 20:21:20 +0000986 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000987 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000988 !(isa<Decl>(Owner) &&
989 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
990 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregore53060f2009-06-25 22:08:12 +0000992 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000993 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
994 TInfo = SubstFunctionType(D, Params);
995 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000996 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000997 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000998
John McCalld325daa2010-03-26 04:53:08 +0000999 NestedNameSpecifier *Qualifier = D->getQualifier();
1000 if (Qualifier) {
1001 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1002 D->getQualifierRange(),
1003 TemplateArgs);
1004 if (!Qualifier) return 0;
1005 }
1006
John McCall68b6b872010-02-06 01:50:47 +00001007 // If we're instantiating a local function declaration, put the result
1008 // in the owner; otherwise we need to find the instantiated context.
1009 DeclContext *DC;
1010 if (D->getDeclContext()->isFunctionOrMethod())
1011 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001012 else if (isFriend && Qualifier) {
1013 CXXScopeSpec SS;
1014 SS.setScopeRep(Qualifier);
1015 SS.setRange(D->getQualifierRange());
1016 DC = SemaRef.computeDeclContext(SS);
1017 if (!DC) return 0;
1018 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001019 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1020 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001021 }
John McCall68b6b872010-02-06 01:50:47 +00001022
John McCall02cace72009-08-28 07:59:38 +00001023 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001024 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001025 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001026 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001027 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001028
John McCalld325daa2010-03-26 04:53:08 +00001029 if (Qualifier)
1030 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001031
John McCallb1a56e72010-03-26 23:10:15 +00001032 DeclContext *LexicalDC = Owner;
1033 if (!isFriend && D->isOutOfLine()) {
1034 assert(D->getDeclContext()->isFileContext());
1035 LexicalDC = D->getDeclContext();
1036 }
1037
1038 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Douglas Gregore53060f2009-06-25 22:08:12 +00001040 // Attach the parameters
1041 for (unsigned P = 0; P < Params.size(); ++P)
1042 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001043 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001044
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001045 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001046 if (TemplateParams) {
1047 // Our resulting instantiation is actually a function template, since we
1048 // are substituting only the outer template parameters. For example, given
1049 //
1050 // template<typename T>
1051 // struct X {
1052 // template<typename U> friend void f(T, U);
1053 // };
1054 //
1055 // X<int> x;
1056 //
1057 // We are instantiating the friend function template "f" within X<int>,
1058 // which means substituting int for T, but leaving "f" as a friend function
1059 // template.
1060 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001061 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001062 Function->getLocation(),
1063 Function->getDeclName(),
1064 TemplateParams, Function);
1065 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001066
1067 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001068
1069 if (isFriend && D->isThisDeclarationADefinition()) {
1070 // TODO: should we remember this connection regardless of whether
1071 // the friend declaration provided a body?
1072 FunctionTemplate->setInstantiatedFromMemberTemplate(
1073 D->getDescribedFunctionTemplate());
1074 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001075 } else if (FunctionTemplate) {
1076 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001077 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001078 &TemplateArgs.getInnermost(),
1079 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001080 } else if (isFriend && D->isThisDeclarationADefinition()) {
1081 // TODO: should we remember this connection regardless of whether
1082 // the friend declaration provided a body?
1083 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001084 }
Douglas Gregora735b202009-10-13 14:39:41 +00001085
Douglas Gregore53060f2009-06-25 22:08:12 +00001086 if (InitFunctionInstantiation(Function, D))
1087 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Douglas Gregore53060f2009-06-25 22:08:12 +00001089 bool Redeclaration = false;
1090 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001091 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001092
John McCall68263142009-11-18 22:49:29 +00001093 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1094 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1095
John McCallaf2094e2010-04-08 09:05:18 +00001096 if (DependentFunctionTemplateSpecializationInfo *Info
1097 = D->getDependentSpecializationInfo()) {
1098 assert(isFriend && "non-friend has dependent specialization info?");
1099
1100 // This needs to be set now for future sanity.
1101 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1102
1103 // Instantiate the explicit template arguments.
1104 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1105 Info->getRAngleLoc());
1106 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1107 TemplateArgumentLoc Loc;
1108 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1109 return 0;
1110
1111 ExplicitArgs.addArgument(Loc);
1112 }
1113
1114 // Map the candidate templates to their instantiations.
1115 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1116 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1117 Info->getTemplate(I),
1118 TemplateArgs);
1119 if (!Temp) return 0;
1120
1121 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1122 }
1123
1124 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1125 &ExplicitArgs,
1126 Previous))
1127 Function->setInvalidDecl();
1128
1129 isExplicitSpecialization = true;
1130
1131 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001132 // Look only into the namespace where the friend would be declared to
1133 // find a previous declaration. This is the innermost enclosing namespace,
1134 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001135 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001136
Douglas Gregora735b202009-10-13 14:39:41 +00001137 // In C++, the previous declaration we find might be a tag type
1138 // (class or enum). In this case, the new declaration will hide the
1139 // tag type. Note that this does does not apply if we're declaring a
1140 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001141 if (Previous.isSingleTagDecl())
1142 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001143 }
1144
John McCall9f54ad42009-12-10 09:41:52 +00001145 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001146 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001147 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001148
John McCall76d32642010-04-24 01:30:58 +00001149 NamedDecl *PrincipalDecl = (TemplateParams
1150 ? cast<NamedDecl>(FunctionTemplate)
1151 : Function);
1152
Douglas Gregora735b202009-10-13 14:39:41 +00001153 // If the original function was part of a friend declaration,
1154 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001155 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001156 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001157 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001158 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001159 else
Douglas Gregora735b202009-10-13 14:39:41 +00001160 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001161
1162 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1163 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001164
1165 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1166 D->isThisDeclarationADefinition()) {
1167 // Check for a function body.
1168 const FunctionDecl *Definition = 0;
1169 if (Function->getBody(Definition) &&
1170 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1171 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1172 << Function->getDeclName();
1173 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1174 Function->setInvalidDecl();
1175 }
1176 // Check for redefinitions due to other instantiations of this or
1177 // a similar friend function.
1178 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1179 REnd = Function->redecls_end();
1180 R != REnd; ++R) {
1181 if (*R != Function &&
1182 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1183 if (const FunctionDecl *RPattern
1184 = (*R)->getTemplateInstantiationPattern())
1185 if (RPattern->getBody(RPattern)) {
1186 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1187 << Function->getDeclName();
1188 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1189 Function->setInvalidDecl();
1190 break;
1191 }
1192 }
1193 }
1194 }
1195
Douglas Gregora735b202009-10-13 14:39:41 +00001196 }
1197
John McCall76d32642010-04-24 01:30:58 +00001198 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1199 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1200 PrincipalDecl->setNonMemberOperator();
1201
Douglas Gregore53060f2009-06-25 22:08:12 +00001202 return Function;
1203}
1204
Douglas Gregord60e1052009-08-27 16:57:43 +00001205Decl *
1206TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1207 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001208 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1209 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001210 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001211 // We are creating a function template specialization from a function
1212 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001213 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001214 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001215 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001216 TemplateArgs.getInnermost().getFlatArgumentList(),
1217 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001218 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001219
1220 FunctionTemplateSpecializationInfo *Info
1221 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001222 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor6b906862009-08-21 00:16:32 +00001224 // If we already have a function template specialization, return it.
1225 if (Info)
1226 return Info->Function;
1227 }
1228
John McCallb0cb0222010-03-27 05:57:59 +00001229 bool isFriend;
1230 if (FunctionTemplate)
1231 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1232 else
1233 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1234
Douglas Gregor79c22782010-01-16 20:21:20 +00001235 bool MergeWithParentScope = (TemplateParams != 0) ||
1236 !(isa<Decl>(Owner) &&
1237 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1238 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001239
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001240 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001241 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1242 TInfo = SubstFunctionType(D, Params);
1243 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001244 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001245 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001246
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001247 // \brief If the type of this function is not *directly* a function
1248 // type, then we're instantiating the a function that was declared
1249 // via a typedef, e.g.,
1250 //
1251 // typedef int functype(int, int);
1252 // functype func;
1253 //
1254 // In this case, we'll just go instantiate the ParmVarDecls that we
1255 // synthesized in the method declaration.
1256 if (!isa<FunctionProtoType>(T)) {
1257 assert(!Params.size() && "Instantiating type could not yield parameters");
1258 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1259 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1260 TemplateArgs);
1261 if (!P)
1262 return 0;
1263
1264 Params.push_back(P);
1265 }
1266 }
1267
John McCallb0cb0222010-03-27 05:57:59 +00001268 NestedNameSpecifier *Qualifier = D->getQualifier();
1269 if (Qualifier) {
1270 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1271 D->getQualifierRange(),
1272 TemplateArgs);
1273 if (!Qualifier) return 0;
1274 }
1275
1276 DeclContext *DC = Owner;
1277 if (isFriend) {
1278 if (Qualifier) {
1279 CXXScopeSpec SS;
1280 SS.setScopeRep(Qualifier);
1281 SS.setRange(D->getQualifierRange());
1282 DC = SemaRef.computeDeclContext(SS);
1283 } else {
1284 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1285 D->getDeclContext(),
1286 TemplateArgs);
1287 }
1288 if (!DC) return 0;
1289 }
1290
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001291 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001292 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001293 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregordec06662009-08-21 18:42:58 +00001295 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001296 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001297 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1298 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1299 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001300 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1301 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001302 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001303 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001304 Constructor->isInlineSpecified(),
1305 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001306 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1307 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1308 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1309 SemaRef.Context.getCanonicalType(ClassTy));
1310 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1311 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001312 T, Destructor->isInlineSpecified(),
1313 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001314 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001315 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001316 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001317 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001318 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1319 ConvTy);
1320 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1321 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001322 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001323 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001324 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001325 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001326 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001327 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001328 D->isStatic(),
1329 D->getStorageClassAsWritten(),
1330 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001331 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001332
John McCallb0cb0222010-03-27 05:57:59 +00001333 if (Qualifier)
1334 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001335
Douglas Gregord60e1052009-08-27 16:57:43 +00001336 if (TemplateParams) {
1337 // Our resulting instantiation is actually a function template, since we
1338 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001339 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001340 // template<typename T>
1341 // struct X {
1342 // template<typename U> void f(T, U);
1343 // };
1344 //
1345 // X<int> x;
1346 //
1347 // We are instantiating the member template "f" within X<int>, which means
1348 // substituting int for T, but leaving "f" as a member function template.
1349 // Build the function template itself.
1350 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1351 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001352 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001353 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001354 if (isFriend) {
1355 FunctionTemplate->setLexicalDeclContext(Owner);
1356 FunctionTemplate->setObjectOfFriendDecl(true);
1357 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001358 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001359 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001360 } else if (FunctionTemplate) {
1361 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001362 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001363 &TemplateArgs.getInnermost(),
1364 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001365 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001366 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001367 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001368 }
1369
Mike Stump1eb44332009-09-09 15:08:12 +00001370 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001371 // out-of-line, the instantiation will have the same lexical
1372 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001373 if (isFriend) {
1374 Method->setLexicalDeclContext(Owner);
1375 Method->setObjectOfFriendDecl(true);
1376 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001377 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregor5545e162009-03-24 00:38:23 +00001379 // Attach the parameters
1380 for (unsigned P = 0; P < Params.size(); ++P)
1381 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001382 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001383
1384 if (InitMethodInstantiation(Method, D))
1385 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001386
John McCall68263142009-11-18 22:49:29 +00001387 LookupResult Previous(SemaRef, Name, SourceLocation(),
1388 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
John McCallb0cb0222010-03-27 05:57:59 +00001390 if (!FunctionTemplate || TemplateParams || isFriend) {
1391 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregordec06662009-08-21 18:42:58 +00001393 // In C++, the previous declaration we find might be a tag type
1394 // (class or enum). In this case, the new declaration will hide the
1395 // tag type. Note that this does does not apply if we're declaring a
1396 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001397 if (Previous.isSingleTagDecl())
1398 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001399 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001400
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001401 bool Redeclaration = false;
1402 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001403 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001404 /*FIXME:*/OverloadableAttrRequired);
1405
Douglas Gregor4ba31362009-12-01 17:24:26 +00001406 if (D->isPure())
1407 SemaRef.CheckPureMethod(Method, SourceRange());
1408
John McCall46460a62010-01-20 21:53:11 +00001409 Method->setAccess(D->getAccess());
1410
John McCallb0cb0222010-03-27 05:57:59 +00001411 if (FunctionTemplate) {
1412 // If there's a function template, let our caller handle it.
1413 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1414 // Don't hide a (potentially) valid declaration with an invalid one.
1415 } else {
1416 NamedDecl *DeclToAdd = (TemplateParams
1417 ? cast<NamedDecl>(FunctionTemplate)
1418 : Method);
1419 if (isFriend)
1420 Record->makeDeclVisibleInContext(DeclToAdd);
1421 else
1422 Owner->addDecl(DeclToAdd);
1423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001425 return Method;
1426}
1427
Douglas Gregor615c5d42009-03-24 16:43:20 +00001428Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001429 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001430}
1431
Douglas Gregor03b2b072009-03-24 00:15:49 +00001432Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001433 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001434}
1435
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001436Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001437 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001438}
1439
Douglas Gregor6477b692009-03-25 15:04:13 +00001440ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001441 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001442}
1443
John McCalle29ba202009-08-20 01:44:21 +00001444Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1445 TemplateTypeParmDecl *D) {
1446 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001447 const Type* T = D->getTypeForDecl();
1448 assert(T->isTemplateTypeParmType());
1449 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001450
John McCalle29ba202009-08-20 01:44:21 +00001451 TemplateTypeParmDecl *Inst =
1452 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001453 TTPT->getDepth() - 1, TTPT->getIndex(),
1454 TTPT->getName(),
John McCalle29ba202009-08-20 01:44:21 +00001455 D->wasDeclaredWithTypename(),
1456 D->isParameterPack());
1457
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001458 if (D->hasDefaultArgument())
1459 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001460
Douglas Gregor550d9b22009-10-31 17:21:17 +00001461 // Introduce this template parameter's instantiation into the instantiation
1462 // scope.
1463 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1464
John McCalle29ba202009-08-20 01:44:21 +00001465 return Inst;
1466}
1467
Douglas Gregor33642df2009-10-23 23:25:44 +00001468Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1469 NonTypeTemplateParmDecl *D) {
1470 // Substitute into the type of the non-type template parameter.
1471 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001472 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001473 if (DI) {
1474 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1475 D->getDeclName());
1476 if (DI) T = DI->getType();
1477 } else {
1478 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1479 D->getDeclName());
1480 DI = 0;
1481 }
1482 if (T.isNull())
1483 return 0;
1484
1485 // Check that this type is acceptable for a non-type template parameter.
1486 bool Invalid = false;
1487 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1488 if (T.isNull()) {
1489 T = SemaRef.Context.IntTy;
1490 Invalid = true;
1491 }
1492
1493 NonTypeTemplateParmDecl *Param
1494 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1495 D->getDepth() - 1, D->getPosition(),
1496 D->getIdentifier(), T, DI);
1497 if (Invalid)
1498 Param->setInvalidDecl();
1499
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001500 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001501
1502 // Introduce this template parameter's instantiation into the instantiation
1503 // scope.
1504 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001505 return Param;
1506}
1507
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001508Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001509TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1510 TemplateTemplateParmDecl *D) {
1511 // Instantiate the template parameter list of the template template parameter.
1512 TemplateParameterList *TempParams = D->getTemplateParameters();
1513 TemplateParameterList *InstParams;
1514 {
1515 // Perform the actual substitution of template parameters within a new,
1516 // local instantiation scope.
1517 Sema::LocalInstantiationScope Scope(SemaRef);
1518 InstParams = SubstTemplateParams(TempParams);
1519 if (!InstParams)
1520 return NULL;
1521 }
1522
1523 // Build the template template parameter.
1524 TemplateTemplateParmDecl *Param
1525 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1526 D->getDepth() - 1, D->getPosition(),
1527 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001528 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001529
Douglas Gregor9106ef72009-11-11 16:58:32 +00001530 // Introduce this template parameter's instantiation into the instantiation
1531 // scope.
1532 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1533
1534 return Param;
1535}
1536
Douglas Gregor48c32a72009-11-17 06:07:40 +00001537Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1538 // Using directives are never dependent, so they require no explicit
1539
1540 UsingDirectiveDecl *Inst
1541 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1542 D->getNamespaceKeyLocation(),
1543 D->getQualifierRange(), D->getQualifier(),
1544 D->getIdentLocation(),
1545 D->getNominatedNamespace(),
1546 D->getCommonAncestor());
1547 Owner->addDecl(Inst);
1548 return Inst;
1549}
1550
John McCalled976492009-12-04 22:46:56 +00001551Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1552 // The nested name specifier is non-dependent, so no transformation
1553 // is required.
1554
John McCall9f54ad42009-12-10 09:41:52 +00001555 // We only need to do redeclaration lookups if we're in a class
1556 // scope (in fact, it's not really even possible in non-class
1557 // scopes).
1558 bool CheckRedeclaration = Owner->isRecord();
1559
1560 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1561 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1562
John McCalled976492009-12-04 22:46:56 +00001563 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1564 D->getLocation(),
1565 D->getNestedNameRange(),
1566 D->getUsingLocation(),
1567 D->getTargetNestedNameDecl(),
1568 D->getDeclName(),
1569 D->isTypeName());
1570
1571 CXXScopeSpec SS;
1572 SS.setScopeRep(D->getTargetNestedNameDecl());
1573 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001574
1575 if (CheckRedeclaration) {
1576 Prev.setHideTags(false);
1577 SemaRef.LookupQualifiedName(Prev, Owner);
1578
1579 // Check for invalid redeclarations.
1580 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1581 D->isTypeName(), SS,
1582 D->getLocation(), Prev))
1583 NewUD->setInvalidDecl();
1584
1585 }
1586
1587 if (!NewUD->isInvalidDecl() &&
1588 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001589 D->getLocation()))
1590 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001591
John McCalled976492009-12-04 22:46:56 +00001592 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1593 NewUD->setAccess(D->getAccess());
1594 Owner->addDecl(NewUD);
1595
John McCall9f54ad42009-12-10 09:41:52 +00001596 // Don't process the shadow decls for an invalid decl.
1597 if (NewUD->isInvalidDecl())
1598 return NewUD;
1599
John McCall323c3102009-12-22 22:26:37 +00001600 bool isFunctionScope = Owner->isFunctionOrMethod();
1601
John McCall9f54ad42009-12-10 09:41:52 +00001602 // Process the shadow decls.
1603 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1604 I != E; ++I) {
1605 UsingShadowDecl *Shadow = *I;
1606 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001607 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1608 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001609 TemplateArgs));
1610
1611 if (CheckRedeclaration &&
1612 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1613 continue;
1614
1615 UsingShadowDecl *InstShadow
1616 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1617 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001618
1619 if (isFunctionScope)
1620 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001621 }
John McCalled976492009-12-04 22:46:56 +00001622
1623 return NewUD;
1624}
1625
1626Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001627 // Ignore these; we handle them in bulk when processing the UsingDecl.
1628 return 0;
John McCalled976492009-12-04 22:46:56 +00001629}
1630
John McCall7ba107a2009-11-18 02:36:19 +00001631Decl * TemplateDeclInstantiator
1632 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001633 NestedNameSpecifier *NNS =
1634 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1635 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001636 TemplateArgs);
1637 if (!NNS)
1638 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001640 CXXScopeSpec SS;
1641 SS.setRange(D->getTargetNestedNameRange());
1642 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
1644 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001645 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001646 D->getUsingLoc(), SS, D->getLocation(),
1647 D->getDeclName(), 0,
1648 /*instantiation*/ true,
1649 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001650 if (UD)
John McCalled976492009-12-04 22:46:56 +00001651 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1652
John McCall7ba107a2009-11-18 02:36:19 +00001653 return UD;
1654}
1655
1656Decl * TemplateDeclInstantiator
1657 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1658 NestedNameSpecifier *NNS =
1659 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1660 D->getTargetNestedNameRange(),
1661 TemplateArgs);
1662 if (!NNS)
1663 return 0;
1664
1665 CXXScopeSpec SS;
1666 SS.setRange(D->getTargetNestedNameRange());
1667 SS.setScopeRep(NNS);
1668
1669 NamedDecl *UD =
1670 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1671 D->getUsingLoc(), SS, D->getLocation(),
1672 D->getDeclName(), 0,
1673 /*instantiation*/ true,
1674 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001675 if (UD)
John McCalled976492009-12-04 22:46:56 +00001676 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1677
Anders Carlsson0d8df782009-08-29 19:37:28 +00001678 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001679}
1680
John McCallce3ff2b2009-08-25 22:02:44 +00001681Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001682 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001683 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001684 if (D->isInvalidDecl())
1685 return 0;
1686
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001687 return Instantiator.Visit(D);
1688}
1689
John McCalle29ba202009-08-20 01:44:21 +00001690/// \brief Instantiates a nested template parameter list in the current
1691/// instantiation context.
1692///
1693/// \param L The parameter list to instantiate
1694///
1695/// \returns NULL if there was an error
1696TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001697TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001698 // Get errors for all the parameters before bailing out.
1699 bool Invalid = false;
1700
1701 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001702 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001703 ParamVector Params;
1704 Params.reserve(N);
1705 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1706 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001707 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001708 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001709 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001710 }
1711
1712 // Clean up if we had an error.
1713 if (Invalid) {
1714 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1715 PI != PE; ++PI)
1716 if (*PI)
1717 (*PI)->Destroy(SemaRef.Context);
1718 return NULL;
1719 }
1720
1721 TemplateParameterList *InstL
1722 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1723 L->getLAngleLoc(), &Params.front(), N,
1724 L->getRAngleLoc());
1725 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001726}
John McCalle29ba202009-08-20 01:44:21 +00001727
Douglas Gregored9c0f92009-10-29 00:04:11 +00001728/// \brief Instantiate the declaration of a class template partial
1729/// specialization.
1730///
1731/// \param ClassTemplate the (instantiated) class template that is partially
1732// specialized by the instantiation of \p PartialSpec.
1733///
1734/// \param PartialSpec the (uninstantiated) class template partial
1735/// specialization that we are instantiating.
1736///
1737/// \returns true if there was an error, false otherwise.
1738bool
1739TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1740 ClassTemplateDecl *ClassTemplate,
1741 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001742 // Create a local instantiation scope for this class template partial
1743 // specialization, which will contain the instantiations of the template
1744 // parameters.
1745 Sema::LocalInstantiationScope Scope(SemaRef);
1746
Douglas Gregored9c0f92009-10-29 00:04:11 +00001747 // Substitute into the template parameters of the class template partial
1748 // specialization.
1749 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1750 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1751 if (!InstParams)
1752 return true;
1753
1754 // Substitute into the template arguments of the class template partial
1755 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001756 const TemplateArgumentLoc *PartialSpecTemplateArgs
1757 = PartialSpec->getTemplateArgsAsWritten();
1758 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1759
John McCalld5532b62009-11-23 01:53:49 +00001760 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001761 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001762 TemplateArgumentLoc Loc;
1763 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001764 return true;
John McCalld5532b62009-11-23 01:53:49 +00001765 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001766 }
1767
1768
1769 // Check that the template argument list is well-formed for this
1770 // class template.
1771 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1772 InstTemplateArgs.size());
1773 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1774 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001775 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001776 false,
1777 Converted))
1778 return true;
1779
1780 // Figure out where to insert this class template partial specialization
1781 // in the member template's set of class template partial specializations.
1782 llvm::FoldingSetNodeID ID;
1783 ClassTemplatePartialSpecializationDecl::Profile(ID,
1784 Converted.getFlatArguments(),
1785 Converted.flatSize(),
1786 SemaRef.Context);
1787 void *InsertPos = 0;
1788 ClassTemplateSpecializationDecl *PrevDecl
1789 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1790 InsertPos);
1791
1792 // Build the canonical type that describes the converted template
1793 // arguments of the class template partial specialization.
1794 QualType CanonType
1795 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1796 Converted.getFlatArguments(),
1797 Converted.flatSize());
1798
1799 // Build the fully-sugared type for this class template
1800 // specialization as the user wrote in the specialization
1801 // itself. This means that we'll pretty-print the type retrieved
1802 // from the specialization's declaration the way that the user
1803 // actually wrote the specialization, rather than formatting the
1804 // name based on the "canonical" representation used to store the
1805 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001806 TypeSourceInfo *WrittenTy
1807 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1808 TemplateName(ClassTemplate),
1809 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001810 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001811 CanonType);
1812
1813 if (PrevDecl) {
1814 // We've already seen a partial specialization with the same template
1815 // parameters and template arguments. This can happen, for example, when
1816 // substituting the outer template arguments ends up causing two
1817 // class template partial specializations of a member class template
1818 // to have identical forms, e.g.,
1819 //
1820 // template<typename T, typename U>
1821 // struct Outer {
1822 // template<typename X, typename Y> struct Inner;
1823 // template<typename Y> struct Inner<T, Y>;
1824 // template<typename Y> struct Inner<U, Y>;
1825 // };
1826 //
1827 // Outer<int, int> outer; // error: the partial specializations of Inner
1828 // // have the same signature.
1829 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1830 << WrittenTy;
1831 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1832 << SemaRef.Context.getTypeDeclType(PrevDecl);
1833 return true;
1834 }
1835
1836
1837 // Create the class template partial specialization declaration.
1838 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001839 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1840 PartialSpec->getTagKind(),
1841 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001842 PartialSpec->getLocation(),
1843 InstParams,
1844 ClassTemplate,
1845 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001846 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001847 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001848 0,
1849 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001850 // Substitute the nested name specifier, if any.
1851 if (SubstQualifier(PartialSpec, InstPartialSpec))
1852 return 0;
1853
Douglas Gregored9c0f92009-10-29 00:04:11 +00001854 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001855 InstPartialSpec->setTypeAsWritten(WrittenTy);
1856
Douglas Gregored9c0f92009-10-29 00:04:11 +00001857 // Add this partial specialization to the set of class template partial
1858 // specializations.
1859 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1860 InsertPos);
1861 return false;
1862}
1863
John McCall21ef0fa2010-03-11 09:03:00 +00001864TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001865TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001866 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001867 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1868 assert(OldTInfo && "substituting function without type source info");
1869 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001870 TypeSourceInfo *NewTInfo
1871 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1872 D->getTypeSpecStartLoc(),
1873 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001874 if (!NewTInfo)
1875 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001876
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001877 if (NewTInfo != OldTInfo) {
1878 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001879 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001880 if (FunctionProtoTypeLoc *OldProtoLoc
1881 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1882 TypeLoc NewTL = NewTInfo->getTypeLoc();
1883 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1884 assert(NewProtoLoc && "Missing prototype?");
1885 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1886 // FIXME: Variadic templates will break this.
1887 Params.push_back(NewProtoLoc->getArg(i));
1888 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001889 OldProtoLoc->getArg(i),
1890 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001891 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001892 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001893 } else {
1894 // The function type itself was not dependent and therefore no
1895 // substitution occurred. However, we still need to instantiate
1896 // the function parameters themselves.
1897 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001898 if (FunctionProtoTypeLoc *OldProtoLoc
1899 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1900 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1901 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1902 if (!Parm)
1903 return 0;
1904 Params.push_back(Parm);
1905 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001906 }
1907 }
John McCall21ef0fa2010-03-11 09:03:00 +00001908 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001909}
1910
Mike Stump1eb44332009-09-09 15:08:12 +00001911/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001912/// declaration (New) from the corresponding fields of its template (Tmpl).
1913///
1914/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001915bool
1916TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001917 FunctionDecl *Tmpl) {
1918 if (Tmpl->isDeleted())
1919 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregorcca9e962009-07-01 22:01:06 +00001921 // If we are performing substituting explicitly-specified template arguments
1922 // or deduced template arguments into a function template and we reach this
1923 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001924 // to keeping the new function template specialization. We therefore
1925 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001926 // into a template instantiation for this specific function template
1927 // specialization, which is not a SFINAE context, so that we diagnose any
1928 // further errors in the declaration itself.
1929 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1930 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1931 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1932 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001933 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001934 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001935 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001936 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001937 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001938 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1939 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001940 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001941 }
1942 }
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001944 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1945 assert(Proto && "Function template without prototype?");
1946
1947 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1948 Proto->getNoReturnAttr()) {
1949 // The function has an exception specification or a "noreturn"
1950 // attribute. Substitute into each of the exception types.
1951 llvm::SmallVector<QualType, 4> Exceptions;
1952 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1953 // FIXME: Poor location information!
1954 QualType T
1955 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1956 New->getLocation(), New->getDeclName());
1957 if (T.isNull() ||
1958 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1959 continue;
1960
1961 Exceptions.push_back(T);
1962 }
1963
1964 // Rebuild the function type
1965
1966 const FunctionProtoType *NewProto
1967 = New->getType()->getAs<FunctionProtoType>();
1968 assert(NewProto && "Template instantiation without function prototype?");
1969 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1970 NewProto->arg_type_begin(),
1971 NewProto->getNumArgs(),
1972 NewProto->isVariadic(),
1973 NewProto->getTypeQuals(),
1974 Proto->hasExceptionSpec(),
1975 Proto->hasAnyExceptionSpec(),
1976 Exceptions.size(),
1977 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001978 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001979 }
1980
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001981 InstantiateAttrs(Tmpl, New);
1982
Douglas Gregore53060f2009-06-25 22:08:12 +00001983 return false;
1984}
1985
Douglas Gregor5545e162009-03-24 00:38:23 +00001986/// \brief Initializes common fields of an instantiated method
1987/// declaration (New) from the corresponding fields of its template
1988/// (Tmpl).
1989///
1990/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001991bool
1992TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001993 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001994 if (InitFunctionInstantiation(New, Tmpl))
1995 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Douglas Gregor5545e162009-03-24 00:38:23 +00001997 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1998 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001999 if (Tmpl->isVirtualAsWritten())
2000 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00002001
2002 // FIXME: attributes
2003 // FIXME: New needs a pointer to Tmpl
2004 return false;
2005}
Douglas Gregora58861f2009-05-13 20:28:22 +00002006
2007/// \brief Instantiate the definition of the given function from its
2008/// template.
2009///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002010/// \param PointOfInstantiation the point at which the instantiation was
2011/// required. Note that this is not precisely a "point of instantiation"
2012/// for the function, but it's close.
2013///
Douglas Gregora58861f2009-05-13 20:28:22 +00002014/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002015/// function template specialization or member function of a class template
2016/// specialization.
2017///
2018/// \param Recursive if true, recursively instantiates any functions that
2019/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002020///
2021/// \param DefinitionRequired if true, then we are performing an explicit
2022/// instantiation where the body of the function is required. Complain if
2023/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002024void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002025 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002026 bool Recursive,
2027 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00002028 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002029 return;
2030
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002031 // Never instantiate an explicit specialization.
2032 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2033 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002034
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002035 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002036 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002037 Stmt *Pattern = 0;
2038 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002039 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002040
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002041 if (!Pattern) {
2042 if (DefinitionRequired) {
2043 if (Function->getPrimaryTemplate())
2044 Diag(PointOfInstantiation,
2045 diag::err_explicit_instantiation_undefined_func_template)
2046 << Function->getPrimaryTemplate();
2047 else
2048 Diag(PointOfInstantiation,
2049 diag::err_explicit_instantiation_undefined_member)
2050 << 1 << Function->getDeclName() << Function->getDeclContext();
2051
2052 if (PatternDecl)
2053 Diag(PatternDecl->getLocation(),
2054 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002055 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002056 }
2057
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002058 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002059 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002060
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002061 // C++0x [temp.explicit]p9:
2062 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002063 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002064 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002065 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002066 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002067 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002068 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002070 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2071 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002072 return;
2073
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002074 // If we're performing recursive template instantiation, create our own
2075 // queue of pending implicit instantiations that we will instantiate later,
2076 // while we're still within our own instantiation context.
2077 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2078 if (Recursive)
2079 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Douglas Gregor9679caf2010-05-12 17:27:19 +00002081 EnterExpressionEvaluationContext EvalContext(*this,
2082 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002083 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2084
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002085 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002086 // recorded, unless we're actually a member function within a local
2087 // class, in which case we need to merge our results with the parent
2088 // scope (of the enclosing function).
2089 bool MergeWithParentScope = false;
2090 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2091 MergeWithParentScope = Rec->isLocalClass();
2092
2093 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002095 // Introduce the instantiated function parameters into the local
2096 // instantiation scope.
2097 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2098 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2099 Function->getParamDecl(I));
2100
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002101 // Enter the scope of this instantiation. We don't use
2102 // PushDeclContext because we don't have a scope.
2103 DeclContext *PreviousContext = CurContext;
2104 CurContext = Function;
2105
Mike Stump1eb44332009-09-09 15:08:12 +00002106 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002107 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002108
2109 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002110 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002111 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2112 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2113 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002114 }
2115
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002116 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002117 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002118
Douglas Gregor52604ab2009-09-11 21:19:12 +00002119 if (Body.isInvalid())
2120 Function->setInvalidDecl();
2121
Mike Stump1eb44332009-09-09 15:08:12 +00002122 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002123 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002124
John McCall0c01d182010-03-24 05:22:00 +00002125 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2126
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002127 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002128
2129 DeclGroupRef DG(Function);
2130 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Douglas Gregor60406be2010-01-16 22:29:39 +00002132 // This class may have local implicit instantiations that need to be
2133 // instantiation within this scope.
2134 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2135 Scope.Exit();
2136
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002137 if (Recursive) {
2138 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002139 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002140 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002142 // Restore the set of pending implicit instantiations.
2143 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2144 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002145}
2146
2147/// \brief Instantiate the definition of the given variable from its
2148/// template.
2149///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002150/// \param PointOfInstantiation the point at which the instantiation was
2151/// required. Note that this is not precisely a "point of instantiation"
2152/// for the function, but it's close.
2153///
2154/// \param Var the already-instantiated declaration of a static member
2155/// variable of a class template specialization.
2156///
2157/// \param Recursive if true, recursively instantiates any functions that
2158/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002159///
2160/// \param DefinitionRequired if true, then we are performing an explicit
2161/// instantiation where an out-of-line definition of the member variable
2162/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002163void Sema::InstantiateStaticDataMemberDefinition(
2164 SourceLocation PointOfInstantiation,
2165 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002166 bool Recursive,
2167 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002168 if (Var->isInvalidDecl())
2169 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Douglas Gregor7caa6822009-07-24 20:34:43 +00002171 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002172 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002173 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002174 assert(Def->isStaticDataMember() && "Not a static data member?");
2175 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Douglas Gregor0d035142009-10-27 18:42:08 +00002177 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002178 // We did not find an out-of-line definition of this static data member,
2179 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002180 // instantiate this definition (or provide a specialization for it) in
2181 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002182 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002183 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002184 Diag(PointOfInstantiation,
2185 diag::err_explicit_instantiation_undefined_member)
2186 << 2 << Var->getDeclName() << Var->getDeclContext();
2187 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2188 }
2189
Douglas Gregor7caa6822009-07-24 20:34:43 +00002190 return;
2191 }
2192
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002193 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002194 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002195 return;
2196
2197 // C++0x [temp.explicit]p9:
2198 // Except for inline functions, other explicit instantiation declarations
2199 // have the effect of suppressing the implicit instantiation of the entity
2200 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002201 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002202 == TSK_ExplicitInstantiationDeclaration)
2203 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Douglas Gregor7caa6822009-07-24 20:34:43 +00002205 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2206 if (Inst)
2207 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002208
Douglas Gregor7caa6822009-07-24 20:34:43 +00002209 // If we're performing recursive template instantiation, create our own
2210 // queue of pending implicit instantiations that we will instantiate later,
2211 // while we're still within our own instantiation context.
2212 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2213 if (Recursive)
2214 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002215
Douglas Gregor7caa6822009-07-24 20:34:43 +00002216 // Enter the scope of this instantiation. We don't use
2217 // PushDeclContext because we don't have a scope.
2218 DeclContext *PreviousContext = CurContext;
2219 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002220
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002221 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002222 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002223 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002224 CurContext = PreviousContext;
2225
2226 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002227 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2228 assert(MSInfo && "Missing member specialization information?");
2229 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2230 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002231 DeclGroupRef DG(Var);
2232 Consumer.HandleTopLevelDecl(DG);
2233 }
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Douglas Gregor7caa6822009-07-24 20:34:43 +00002235 if (Recursive) {
2236 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002237 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002238 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Douglas Gregor7caa6822009-07-24 20:34:43 +00002240 // Restore the set of pending implicit instantiations.
2241 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002242 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002243}
Douglas Gregor815215d2009-05-27 05:35:12 +00002244
Anders Carlsson09025312009-08-29 05:16:22 +00002245void
2246Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2247 const CXXConstructorDecl *Tmpl,
2248 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Anders Carlsson09025312009-08-29 05:16:22 +00002250 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002251 bool AnyErrors = false;
2252
Anders Carlsson09025312009-08-29 05:16:22 +00002253 // Instantiate all the initializers.
2254 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002255 InitsEnd = Tmpl->init_end();
2256 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002257 CXXBaseOrMemberInitializer *Init = *Inits;
2258
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002259 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002260 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002261 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002263 // Instantiate the initializer.
2264 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2265 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2266 AnyErrors = true;
2267 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002268 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002269
Anders Carlsson09025312009-08-29 05:16:22 +00002270 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002271 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002272 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002273 TemplateArgs,
2274 Init->getSourceLocation(),
2275 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002276 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002277 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002278 New->setInvalidDecl();
2279 continue;
2280 }
2281
John McCalla93c9342009-12-07 02:54:59 +00002282 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002283 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002284 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002285 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002286 Init->getRParenLoc(),
2287 New->getParent());
2288 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002289 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002290
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002291 // Is this an anonymous union?
2292 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002293 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2294 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002295 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002296 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2297 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002298 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002299
2300 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002301 NewArgs.size(),
2302 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002303 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002304 Init->getRParenLoc());
2305 }
2306
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002307 if (NewInit.isInvalid()) {
2308 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002309 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002310 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002311 // FIXME: It would be nice if ASTOwningVector had a release function.
2312 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Anders Carlsson09025312009-08-29 05:16:22 +00002314 NewInits.push_back((MemInitTy *)NewInit.get());
2315 }
2316 }
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Anders Carlsson09025312009-08-29 05:16:22 +00002318 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002319 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002320 /*FIXME: ColonLoc */
2321 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002322 NewInits.data(), NewInits.size(),
2323 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002324}
2325
John McCall52a575a2009-08-29 08:11:13 +00002326// TODO: this could be templated if the various decl types used the
2327// same method name.
2328static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2329 ClassTemplateDecl *Instance) {
2330 Pattern = Pattern->getCanonicalDecl();
2331
2332 do {
2333 Instance = Instance->getCanonicalDecl();
2334 if (Pattern == Instance) return true;
2335 Instance = Instance->getInstantiatedFromMemberTemplate();
2336 } while (Instance);
2337
2338 return false;
2339}
2340
Douglas Gregor0d696532009-09-28 06:34:35 +00002341static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2342 FunctionTemplateDecl *Instance) {
2343 Pattern = Pattern->getCanonicalDecl();
2344
2345 do {
2346 Instance = Instance->getCanonicalDecl();
2347 if (Pattern == Instance) return true;
2348 Instance = Instance->getInstantiatedFromMemberTemplate();
2349 } while (Instance);
2350
2351 return false;
2352}
2353
Douglas Gregored9c0f92009-10-29 00:04:11 +00002354static bool
2355isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2356 ClassTemplatePartialSpecializationDecl *Instance) {
2357 Pattern
2358 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2359 do {
2360 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2361 Instance->getCanonicalDecl());
2362 if (Pattern == Instance)
2363 return true;
2364 Instance = Instance->getInstantiatedFromMember();
2365 } while (Instance);
2366
2367 return false;
2368}
2369
John McCall52a575a2009-08-29 08:11:13 +00002370static bool isInstantiationOf(CXXRecordDecl *Pattern,
2371 CXXRecordDecl *Instance) {
2372 Pattern = Pattern->getCanonicalDecl();
2373
2374 do {
2375 Instance = Instance->getCanonicalDecl();
2376 if (Pattern == Instance) return true;
2377 Instance = Instance->getInstantiatedFromMemberClass();
2378 } while (Instance);
2379
2380 return false;
2381}
2382
2383static bool isInstantiationOf(FunctionDecl *Pattern,
2384 FunctionDecl *Instance) {
2385 Pattern = Pattern->getCanonicalDecl();
2386
2387 do {
2388 Instance = Instance->getCanonicalDecl();
2389 if (Pattern == Instance) return true;
2390 Instance = Instance->getInstantiatedFromMemberFunction();
2391 } while (Instance);
2392
2393 return false;
2394}
2395
2396static bool isInstantiationOf(EnumDecl *Pattern,
2397 EnumDecl *Instance) {
2398 Pattern = Pattern->getCanonicalDecl();
2399
2400 do {
2401 Instance = Instance->getCanonicalDecl();
2402 if (Pattern == Instance) return true;
2403 Instance = Instance->getInstantiatedFromMemberEnum();
2404 } while (Instance);
2405
2406 return false;
2407}
2408
John McCalled976492009-12-04 22:46:56 +00002409static bool isInstantiationOf(UsingShadowDecl *Pattern,
2410 UsingShadowDecl *Instance,
2411 ASTContext &C) {
2412 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2413}
2414
2415static bool isInstantiationOf(UsingDecl *Pattern,
2416 UsingDecl *Instance,
2417 ASTContext &C) {
2418 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2419}
2420
John McCall7ba107a2009-11-18 02:36:19 +00002421static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2422 UsingDecl *Instance,
2423 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002424 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002425}
2426
2427static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002428 UsingDecl *Instance,
2429 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002430 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002431}
2432
John McCall52a575a2009-08-29 08:11:13 +00002433static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2434 VarDecl *Instance) {
2435 assert(Instance->isStaticDataMember());
2436
2437 Pattern = Pattern->getCanonicalDecl();
2438
2439 do {
2440 Instance = Instance->getCanonicalDecl();
2441 if (Pattern == Instance) return true;
2442 Instance = Instance->getInstantiatedFromStaticDataMember();
2443 } while (Instance);
2444
2445 return false;
2446}
2447
John McCalled976492009-12-04 22:46:56 +00002448// Other is the prospective instantiation
2449// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002450static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002451 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002452 if (UnresolvedUsingTypenameDecl *UUD
2453 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2454 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2455 return isInstantiationOf(UUD, UD, Ctx);
2456 }
2457 }
2458
2459 if (UnresolvedUsingValueDecl *UUD
2460 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002461 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2462 return isInstantiationOf(UUD, UD, Ctx);
2463 }
2464 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002465
Anders Carlsson0d8df782009-08-29 19:37:28 +00002466 return false;
2467 }
Mike Stump1eb44332009-09-09 15:08:12 +00002468
John McCall52a575a2009-08-29 08:11:13 +00002469 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2470 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002471
John McCall52a575a2009-08-29 08:11:13 +00002472 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2473 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002474
John McCall52a575a2009-08-29 08:11:13 +00002475 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2476 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002477
Douglas Gregor7caa6822009-07-24 20:34:43 +00002478 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002479 if (Var->isStaticDataMember())
2480 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2481
2482 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2483 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002484
Douglas Gregor0d696532009-09-28 06:34:35 +00002485 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2486 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2487
Douglas Gregored9c0f92009-10-29 00:04:11 +00002488 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2489 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2490 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2491 PartialSpec);
2492
Anders Carlssond8b285f2009-09-01 04:26:58 +00002493 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2494 if (!Field->getDeclName()) {
2495 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002496 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002497 cast<FieldDecl>(D);
2498 }
2499 }
Mike Stump1eb44332009-09-09 15:08:12 +00002500
John McCalled976492009-12-04 22:46:56 +00002501 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2502 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2503
2504 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2505 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2506
Douglas Gregor815215d2009-05-27 05:35:12 +00002507 return D->getDeclName() && isa<NamedDecl>(Other) &&
2508 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2509}
2510
2511template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002512static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002513 NamedDecl *D,
2514 ForwardIterator first,
2515 ForwardIterator last) {
2516 for (; first != last; ++first)
2517 if (isInstantiationOf(Ctx, D, *first))
2518 return cast<NamedDecl>(*first);
2519
2520 return 0;
2521}
2522
John McCall02cace72009-08-28 07:59:38 +00002523/// \brief Finds the instantiation of the given declaration context
2524/// within the current instantiation.
2525///
2526/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002527DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002528 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002529 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002530 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002531 return cast_or_null<DeclContext>(ID);
2532 } else return DC;
2533}
2534
Douglas Gregored961e72009-05-27 17:54:46 +00002535/// \brief Find the instantiation of the given declaration within the
2536/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002537///
2538/// This routine is intended to be used when \p D is a declaration
2539/// referenced from within a template, that needs to mapped into the
2540/// corresponding declaration within an instantiation. For example,
2541/// given:
2542///
2543/// \code
2544/// template<typename T>
2545/// struct X {
2546/// enum Kind {
2547/// KnownValue = sizeof(T)
2548/// };
2549///
2550/// bool getKind() const { return KnownValue; }
2551/// };
2552///
2553/// template struct X<int>;
2554/// \endcode
2555///
2556/// In the instantiation of X<int>::getKind(), we need to map the
2557/// EnumConstantDecl for KnownValue (which refers to
2558/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002559/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2560/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002561NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002562 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002563 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002564 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002565 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002566 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002567 // D is a local of some kind. Look into the map of local
2568 // declarations to their instantiations.
2569 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2570 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002571
Douglas Gregore95b4092009-09-16 18:34:49 +00002572 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2573 if (!Record->isDependentContext())
2574 return D;
2575
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002576 // If the RecordDecl is actually the injected-class-name or a
2577 // "templated" declaration for a class template, class template
2578 // partial specialization, or a member class of a class template,
2579 // substitute into the injected-class-name of the class template
2580 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002581 QualType T;
2582 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2583
2584 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002585 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002586 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2587 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002588 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002589
2590 // If we call SubstType with an InjectedClassNameType here we
2591 // can end up in an infinite loop.
2592 T = Context.getTypeDeclType(Record);
2593 assert(isa<InjectedClassNameType>(T) &&
2594 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002595 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002596 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002597
2598 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002599 // Substitute into the injected-class-name to get the type
2600 // corresponding to the instantiation we want, which may also be
2601 // the current instantiation (if we're in a template
2602 // definition). This substitution should never fail, since we
2603 // know we can instantiate the injected-class-name or we
2604 // wouldn't have gotten to the injected-class-name!
2605
2606 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2607 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002608 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2609 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2610
2611 if (!T->isDependentType()) {
2612 assert(T->isRecordType() && "Instantiation must produce a record type");
2613 return T->getAs<RecordType>()->getDecl();
2614 }
2615
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002616 // We are performing "partial" template instantiation to create
2617 // the member declarations for the members of a class template
2618 // specialization. Therefore, D is actually referring to something
2619 // in the current instantiation. Look through the current
2620 // context, which contains actual instantiations, to find the
2621 // instantiation of the "current instantiation" that D refers
2622 // to.
2623 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002624 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002625 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002626 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002627 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002628 if (isInstantiationOf(ClassTemplate,
2629 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002630 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002631
2632 if (!DC->isDependentContext())
2633 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002634 }
2635
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002636 // We're performing "instantiation" of a member of the current
2637 // instantiation while we are type-checking the
2638 // definition. Compute the declaration context and return that.
2639 assert(!SawNonDependentContext &&
2640 "No dependent context while instantiating record");
2641 DeclContext *DC = computeDeclContext(T);
2642 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002643 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002644 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002645 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002646
Douglas Gregore95b4092009-09-16 18:34:49 +00002647 // Fall through to deal with other dependent record types (e.g.,
2648 // anonymous unions in class templates).
2649 }
John McCall52a575a2009-08-29 08:11:13 +00002650
Douglas Gregore95b4092009-09-16 18:34:49 +00002651 if (!ParentDC->isDependentContext())
2652 return D;
2653
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002654 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002655 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002656 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Douglas Gregor815215d2009-05-27 05:35:12 +00002658 if (ParentDC != D->getDeclContext()) {
2659 // We performed some kind of instantiation in the parent context,
2660 // so now we need to look into the instantiated parent context to
2661 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002662
John McCall3cb0ebd2010-03-10 03:28:59 +00002663 // If our context used to be dependent, we may need to instantiate
2664 // it before performing lookup into that context.
2665 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002666 if (!Spec->isDependentContext()) {
2667 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002668 const RecordType *Tag = T->getAs<RecordType>();
2669 assert(Tag && "type of non-dependent record is not a RecordType");
2670 if (!Tag->isBeingDefined() &&
2671 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2672 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002673 }
2674 }
2675
Douglas Gregor815215d2009-05-27 05:35:12 +00002676 NamedDecl *Result = 0;
2677 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002678 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002679 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2680 } else {
2681 // Since we don't have a name for the entity we're looking for,
2682 // our only option is to walk through all of the declarations to
2683 // find that name. This will occur in a few cases:
2684 //
2685 // - anonymous struct/union within a template
2686 // - unnamed class/struct/union/enum within a template
2687 //
2688 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002689 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002690 ParentDC->decls_begin(),
2691 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002692 }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
John McCall9f54ad42009-12-10 09:41:52 +00002694 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002695 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2696 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002697 && "Unable to find instantiation of declaration!");
2698
Douglas Gregor815215d2009-05-27 05:35:12 +00002699 D = Result;
2700 }
2701
Douglas Gregor815215d2009-05-27 05:35:12 +00002702 return D;
2703}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002704
Mike Stump1eb44332009-09-09 15:08:12 +00002705/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002706/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002707void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2708 while (!PendingLocalImplicitInstantiations.empty() ||
2709 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2710 PendingImplicitInstantiation Inst;
2711
2712 if (PendingLocalImplicitInstantiations.empty()) {
2713 Inst = PendingImplicitInstantiations.front();
2714 PendingImplicitInstantiations.pop_front();
2715 } else {
2716 Inst = PendingLocalImplicitInstantiations.front();
2717 PendingLocalImplicitInstantiations.pop_front();
2718 }
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Douglas Gregor7caa6822009-07-24 20:34:43 +00002720 // Instantiate function definitions
2721 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002722 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002723 Function->getLocation(), *this,
2724 Context.getSourceManager(),
2725 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002727 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002728 continue;
2729 }
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Douglas Gregor7caa6822009-07-24 20:34:43 +00002731 // Instantiate static data member definitions.
2732 VarDecl *Var = cast<VarDecl>(Inst.first);
2733 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002734
Chandler Carruth291b4412010-02-13 10:17:50 +00002735 // Don't try to instantiate declarations if the most recent redeclaration
2736 // is invalid.
2737 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2738 continue;
2739
2740 // Check if the most recent declaration has changed the specialization kind
2741 // and removed the need for implicit instantiation.
2742 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2743 case TSK_Undeclared:
2744 assert(false && "Cannot instantitiate an undeclared specialization.");
2745 case TSK_ExplicitInstantiationDeclaration:
2746 case TSK_ExplicitInstantiationDefinition:
2747 case TSK_ExplicitSpecialization:
2748 continue; // No longer need implicit instantiation.
2749 case TSK_ImplicitInstantiation:
2750 break;
2751 }
2752
Mike Stump1eb44332009-09-09 15:08:12 +00002753 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002754 Var->getLocation(), *this,
2755 Context.getSourceManager(),
2756 "instantiating static data member "
2757 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Douglas Gregor7caa6822009-07-24 20:34:43 +00002759 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002760 }
2761}
John McCall0c01d182010-03-24 05:22:00 +00002762
2763void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2764 const MultiLevelTemplateArgumentList &TemplateArgs) {
2765 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2766 E = Pattern->ddiag_end(); I != E; ++I) {
2767 DependentDiagnostic *DD = *I;
2768
2769 switch (DD->getKind()) {
2770 case DependentDiagnostic::Access:
2771 HandleDependentAccessCheck(*DD, TemplateArgs);
2772 break;
2773 }
2774 }
2775}