blob: efd503f1e9fad85b609a372ab88eece6db82baaf [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 Gregor5764f612010-05-08 23:05:03 +0000364 Var->setUsed(D->isUsed());
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000365
Mike Stump390b4cc2009-05-16 07:39:55 +0000366 // FIXME: In theory, we could have a previous declaration for variables that
367 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000368 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000369 // FIXME: having to fake up a LookupResult is dumb.
370 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000371 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000372 if (D->isStaticDataMember())
373 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000374 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Douglas Gregor7caa6822009-07-24 20:34:43 +0000376 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000377 if (!D->isStaticDataMember())
378 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000379 Owner->makeDeclVisibleInContext(Var);
380 } else {
381 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000382
383 if (Owner->isFunctionOrMethod())
384 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000387 // Link instantiations of static data members back to the template from
388 // which they were instantiated.
389 if (Var->isStaticDataMember())
390 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000391 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000392
Douglas Gregor60c93c92010-02-09 07:26:29 +0000393 if (Var->getAnyInitializer()) {
394 // We already have an initializer in the class.
395 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000396 if (Var->isStaticDataMember() && !D->isOutOfLine())
397 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
398 else
399 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
400
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000401 // Instantiate the initializer.
402 SourceLocation LParenLoc, RParenLoc;
403 llvm::SmallVector<SourceLocation, 4> CommaLocs;
404 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
405 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
406 CommaLocs, InitArgs, RParenLoc)) {
407 // Attach the initializer to the declaration.
408 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000409 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000410 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000411 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000412 move_arg(InitArgs),
413 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000414 RParenLoc);
415 } else if (InitArgs.size() == 1) {
416 Expr *Init = (Expr*)(InitArgs.take()[0]);
417 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
418 SemaRef.Owned(Init),
419 false);
420 } else {
421 assert(InitArgs.size() == 0);
422 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000423 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000424 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000425 // FIXME: Not too happy about invalidating the declaration
426 // because of a bogus initializer.
427 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000428 }
429
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000430 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000431 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
432 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000433
Douglas Gregor5764f612010-05-08 23:05:03 +0000434 // Diagnose unused local variables.
435 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
436 SemaRef.DiagnoseUnusedDecl(Var);
437
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000438 return Var;
439}
440
Abramo Bagnara6206d532010-06-05 05:09:32 +0000441Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
442 AccessSpecDecl* AD
443 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
444 D->getAccessSpecifierLoc(), D->getColonLoc());
445 Owner->addHiddenDecl(AD);
446 return AD;
447}
448
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000449Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
450 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000451 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000452 if (DI->getType()->isDependentType() ||
453 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000454 DI = SemaRef.SubstType(DI, TemplateArgs,
455 D->getLocation(), D->getDeclName());
456 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000457 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000458 Invalid = true;
459 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000460 // C++ [temp.arg.type]p3:
461 // If a declaration acquires a function type through a type
462 // dependent on a template-parameter and this causes a
463 // declaration that does not use the syntactic form of a
464 // function declarator to have function type, the program is
465 // ill-formed.
466 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000467 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000468 Invalid = true;
469 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000470 } else {
471 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000472 }
473
474 Expr *BitWidth = D->getBitWidth();
475 if (Invalid)
476 BitWidth = 0;
477 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000478 // The bit-width expression is not potentially evaluated.
479 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000481 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000482 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483 if (InstantiatedBitWidth.isInvalid()) {
484 Invalid = true;
485 BitWidth = 0;
486 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000487 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000488 }
489
John McCall07fb6be2009-10-22 23:33:21 +0000490 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
491 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000492 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000493 D->getLocation(),
494 D->isMutable(),
495 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000496 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000497 D->getAccess(),
498 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000499 if (!Field) {
500 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000501 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000504 InstantiateAttrs(D, Field);
505
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000506 if (Invalid)
507 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000509 if (!Field->getDeclName()) {
510 // Keep track of where this decl came from.
511 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000512 }
513 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
514 if (Parent->isAnonymousStructOrUnion() &&
515 Parent->getLookupContext()->isFunctionOrMethod())
516 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000519 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000520 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000521 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000522
523 return Field;
524}
525
John McCall02cace72009-08-28 07:59:38 +0000526Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000527 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000528 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000529 if (TypeSourceInfo *Ty = D->getFriendType()) {
530 TypeSourceInfo *InstTy =
531 SemaRef.SubstType(Ty, TemplateArgs,
532 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000533 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000534 return 0;
John McCall02cace72009-08-28 07:59:38 +0000535
Douglas Gregor06245bf2010-04-07 17:57:12 +0000536 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
537 if (!FD)
538 return 0;
539
540 FD->setAccess(AS_public);
541 Owner->addDecl(FD);
542 return FD;
543 }
544
545 NamedDecl *ND = D->getFriendDecl();
546 assert(ND && "friend decl must be a decl or a type!");
547
John McCallaf2094e2010-04-08 09:05:18 +0000548 // All of the Visit implementations for the various potential friend
549 // declarations have to be carefully written to work for friend
550 // objects, with the most important detail being that the target
551 // decl should almost certainly not be placed in Owner.
552 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000553 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
John McCall02cace72009-08-28 07:59:38 +0000555 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000556 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
557 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000558 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000559 Owner->addDecl(FD);
560 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000561}
562
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000563Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
564 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregorac7610d2009-06-22 20:57:11 +0000566 // The expression in a static assertion is not potentially evaluated.
567 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000569 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000570 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000571 if (InstantiatedAssertExpr.isInvalid())
572 return 0;
573
Douglas Gregor43d9d922009-08-08 01:41:12 +0000574 OwningExprResult Message(SemaRef, D->getMessage());
575 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000576 Decl *StaticAssert
577 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000578 move(InstantiatedAssertExpr),
579 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000580 return StaticAssert;
581}
582
583Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000584 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000585 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000586 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000588 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000589 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000590 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000591 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000592 Enum->startDefinition();
593
Douglas Gregor96084f12010-03-01 19:00:07 +0000594 if (D->getDeclContext()->isFunctionOrMethod())
595 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
596
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000597 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000598
599 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000600 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
601 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602 EC != ECEnd; ++EC) {
603 // The specified value for the enumerator.
604 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000605 if (Expr *UninstValue = EC->getInitExpr()) {
606 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000607 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000608 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000609
John McCallce3ff2b2009-08-25 22:02:44 +0000610 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000611 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000612
613 // Drop the initial value and continue.
614 bool isInvalid = false;
615 if (Value.isInvalid()) {
616 Value = SemaRef.Owned((Expr *)0);
617 isInvalid = true;
618 }
619
Mike Stump1eb44332009-09-09 15:08:12 +0000620 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000621 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
622 EC->getLocation(), EC->getIdentifier(),
623 move(Value));
624
625 if (isInvalid) {
626 if (EnumConst)
627 EnumConst->setInvalidDecl();
628 Enum->setInvalidDecl();
629 }
630
631 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000632 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000633 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000634 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000635 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000636
637 if (D->getDeclContext()->isFunctionOrMethod()) {
638 // If the enumeration is within a function or method, record the enum
639 // constant as a local.
640 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
641 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000642 }
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000645 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000646 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000647 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
648 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000649 &Enumerators[0], Enumerators.size(),
650 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000651
652 return Enum;
653}
654
Douglas Gregor6477b692009-03-25 15:04:13 +0000655Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
656 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
657 return 0;
658}
659
John McCalle29ba202009-08-20 01:44:21 +0000660Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000661 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
662
Douglas Gregor550d9b22009-10-31 17:21:17 +0000663 // Create a local instantiation scope for this class template, which
664 // will contain the instantiations of the template parameters.
665 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000666 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000667 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000668 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000669 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000670
671 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000672
673 // Instantiate the qualifier. We have to do this first in case
674 // we're a friend declaration, because if we are then we need to put
675 // the new declaration in the appropriate context.
676 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
677 if (Qualifier) {
678 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
679 Pattern->getQualifierRange(),
680 TemplateArgs);
681 if (!Qualifier) return 0;
682 }
683
684 CXXRecordDecl *PrevDecl = 0;
685 ClassTemplateDecl *PrevClassTemplate = 0;
686
687 // If this isn't a friend, then it's a member template, in which
688 // case we just want to build the instantiation in the
689 // specialization. If it is a friend, we want to build it in
690 // the appropriate context.
691 DeclContext *DC = Owner;
692 if (isFriend) {
693 if (Qualifier) {
694 CXXScopeSpec SS;
695 SS.setScopeRep(Qualifier);
696 SS.setRange(Pattern->getQualifierRange());
697 DC = SemaRef.computeDeclContext(SS);
698 if (!DC) return 0;
699 } else {
700 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
701 Pattern->getDeclContext(),
702 TemplateArgs);
703 }
704
705 // Look for a previous declaration of the template in the owning
706 // context.
707 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
708 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
709 SemaRef.LookupQualifiedName(R, DC);
710
711 if (R.isSingleResult()) {
712 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
713 if (PrevClassTemplate)
714 PrevDecl = PrevClassTemplate->getTemplatedDecl();
715 }
716
717 if (!PrevClassTemplate && Qualifier) {
718 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000719 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
720 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000721 return 0;
722 }
723
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000724 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000725 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000726 bool Complain = true;
727
728 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
729 // template for struct std::tr1::__detail::_Map_base, where the
730 // template parameters of the friend declaration don't match the
731 // template parameters of the original declaration. In this one
732 // case, we don't complain about the ill-formed friend
733 // declaration.
734 if (isFriend && Pattern->getIdentifier() &&
735 Pattern->getIdentifier()->isStr("_Map_base") &&
736 DC->isNamespace() &&
737 cast<NamespaceDecl>(DC)->getIdentifier() &&
738 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
739 DeclContext *DCParent = DC->getParent();
740 if (DCParent->isNamespace() &&
741 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
742 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
743 DeclContext *DCParent2 = DCParent->getParent();
744 if (DCParent2->isNamespace() &&
745 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
746 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
747 DCParent2->getParent()->isTranslationUnit())
748 Complain = false;
749 }
750 }
751
John McCall93ba8572010-03-25 06:39:04 +0000752 TemplateParameterList *PrevParams
753 = PrevClassTemplate->getTemplateParameters();
754
755 // Make sure the parameter lists match.
756 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000757 Complain,
758 Sema::TPL_TemplateMatch)) {
759 if (Complain)
760 return 0;
761
762 AdoptedPreviousTemplateParams = true;
763 InstParams = PrevParams;
764 }
John McCall93ba8572010-03-25 06:39:04 +0000765
766 // Do some additional validation, then merge default arguments
767 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000768 if (!AdoptedPreviousTemplateParams &&
769 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000770 Sema::TPC_ClassTemplate))
771 return 0;
772 }
773 }
774
John McCalle29ba202009-08-20 01:44:21 +0000775 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000776 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000777 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000778 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000779 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000780
John McCall93ba8572010-03-25 06:39:04 +0000781 if (Qualifier)
782 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000783
John McCalle29ba202009-08-20 01:44:21 +0000784 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000785 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
786 D->getIdentifier(), InstParams, RecordInst,
787 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000788 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000789
John McCall93ba8572010-03-25 06:39:04 +0000790 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000791 if (PrevClassTemplate)
792 Inst->setAccess(PrevClassTemplate->getAccess());
793 else
794 Inst->setAccess(D->getAccess());
795
John McCall93ba8572010-03-25 06:39:04 +0000796 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
797 // TODO: do we want to track the instantiation progeny of this
798 // friend target decl?
799 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000800 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000801 Inst->setInstantiatedFromMemberTemplate(D);
802 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000803
804 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000805 SemaRef.Context.getInjectedClassNameType(RecordInst,
806 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
John McCallea7390c2010-04-08 20:25:50 +0000807
Douglas Gregor259571e2009-10-30 22:42:42 +0000808 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000809 if (isFriend) {
810 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000811 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000812 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000813
John McCalle29ba202009-08-20 01:44:21 +0000814 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000815
Douglas Gregored9c0f92009-10-29 00:04:11 +0000816 // Instantiate all of the partial specializations of this member class
817 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000818 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
819 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000820 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
821 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
822
John McCalle29ba202009-08-20 01:44:21 +0000823 return Inst;
824}
825
Douglas Gregord60e1052009-08-27 16:57:43 +0000826Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000827TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
828 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000829 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
830
831 // Lookup the already-instantiated declaration in the instantiation
832 // of the class template and return that.
833 DeclContext::lookup_result Found
834 = Owner->lookup(ClassTemplate->getDeclName());
835 if (Found.first == Found.second)
836 return 0;
837
838 ClassTemplateDecl *InstClassTemplate
839 = dyn_cast<ClassTemplateDecl>(*Found.first);
840 if (!InstClassTemplate)
841 return 0;
842
843 Decl *DCanon = D->getCanonicalDecl();
844 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
845 P = InstClassTemplate->getPartialSpecializations().begin(),
846 PEnd = InstClassTemplate->getPartialSpecializations().end();
847 P != PEnd; ++P) {
848 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
849 return &*P;
850 }
851
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000852 return 0;
853}
854
855Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000856TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000857 // Create a local instantiation scope for this function template, which
858 // will contain the instantiations of the template parameters and then get
859 // merged with the local instantiation scope for the function template
860 // itself.
861 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000862
Douglas Gregord60e1052009-08-27 16:57:43 +0000863 TemplateParameterList *TempParams = D->getTemplateParameters();
864 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000865 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000866 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000867
Douglas Gregora735b202009-10-13 14:39:41 +0000868 FunctionDecl *Instantiated = 0;
869 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
870 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
871 InstParams));
872 else
873 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
874 D->getTemplatedDecl(),
875 InstParams));
876
877 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000878 return 0;
879
John McCall46460a62010-01-20 21:53:11 +0000880 Instantiated->setAccess(D->getAccess());
881
Mike Stump1eb44332009-09-09 15:08:12 +0000882 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000883 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000884 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000885 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000886 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000887 assert(InstTemplate &&
888 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000889
John McCallb1a56e72010-03-26 23:10:15 +0000890 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
891
John McCalle976ffe2009-12-14 23:19:40 +0000892 // Link the instantiation back to the pattern *unless* this is a
893 // non-definition friend declaration.
894 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000895 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000896 InstTemplate->setInstantiatedFromMemberTemplate(D);
897
John McCallb1a56e72010-03-26 23:10:15 +0000898 // Make declarations visible in the appropriate context.
899 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000900 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000901
Douglas Gregord60e1052009-08-27 16:57:43 +0000902 return InstTemplate;
903}
904
Douglas Gregord475b8d2009-03-25 21:17:03 +0000905Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
906 CXXRecordDecl *PrevDecl = 0;
907 if (D->isInjectedClassName())
908 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000909 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000910 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
911 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000912 TemplateArgs);
913 if (!Prev) return 0;
914 PrevDecl = cast<CXXRecordDecl>(Prev);
915 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916
917 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000918 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000919 D->getLocation(), D->getIdentifier(),
920 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000921
922 // Substitute the nested name specifier, if any.
923 if (SubstQualifier(D, Record))
924 return 0;
925
Douglas Gregord475b8d2009-03-25 21:17:03 +0000926 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000927 // FIXME: Check against AS_none is an ugly hack to work around the issue that
928 // the tag decls introduced by friend class declarations don't have an access
929 // specifier. Remove once this area of the code gets sorted out.
930 if (D->getAccess() != AS_none)
931 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000932 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000933 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000934
John McCall02cace72009-08-28 07:59:38 +0000935 // If the original function was part of a friend declaration,
936 // inherit its namespace state.
937 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
938 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
939
Douglas Gregor9901c572010-05-21 00:31:19 +0000940 // Make sure that anonymous structs and unions are recorded.
941 if (D->isAnonymousStructOrUnion()) {
942 Record->setAnonymousStructOrUnion(true);
943 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
944 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
945 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000946
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000947 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000948 return Record;
949}
950
John McCall02cace72009-08-28 07:59:38 +0000951/// Normal class members are of more specific types and therefore
952/// don't make it here. This function serves two purposes:
953/// 1) instantiating function templates
954/// 2) substituting friend declarations
955/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000956Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000957 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000958 // Check whether there is already a function template specialization for
959 // this declaration.
960 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
961 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000962 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000963 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000964 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000965 TemplateArgs.getInnermost().getFlatArgumentList(),
966 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000967 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
969 FunctionTemplateSpecializationInfo *Info
970 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000971 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregor127102b2009-06-29 20:59:39 +0000973 // If we already have a function template specialization, return it.
974 if (Info)
975 return Info->Function;
976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
John McCallb0cb0222010-03-27 05:57:59 +0000978 bool isFriend;
979 if (FunctionTemplate)
980 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
981 else
982 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
983
Douglas Gregor79c22782010-01-16 20:21:20 +0000984 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000985 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000986 !(isa<Decl>(Owner) &&
987 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
988 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregore53060f2009-06-25 22:08:12 +0000990 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000991 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
992 TInfo = SubstFunctionType(D, Params);
993 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000994 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000995 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000996
John McCalld325daa2010-03-26 04:53:08 +0000997 NestedNameSpecifier *Qualifier = D->getQualifier();
998 if (Qualifier) {
999 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1000 D->getQualifierRange(),
1001 TemplateArgs);
1002 if (!Qualifier) return 0;
1003 }
1004
John McCall68b6b872010-02-06 01:50:47 +00001005 // If we're instantiating a local function declaration, put the result
1006 // in the owner; otherwise we need to find the instantiated context.
1007 DeclContext *DC;
1008 if (D->getDeclContext()->isFunctionOrMethod())
1009 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001010 else if (isFriend && Qualifier) {
1011 CXXScopeSpec SS;
1012 SS.setScopeRep(Qualifier);
1013 SS.setRange(D->getQualifierRange());
1014 DC = SemaRef.computeDeclContext(SS);
1015 if (!DC) return 0;
1016 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001017 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1018 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001019 }
John McCall68b6b872010-02-06 01:50:47 +00001020
John McCall02cace72009-08-28 07:59:38 +00001021 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001022 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001023 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001024 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001025 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001026
John McCalld325daa2010-03-26 04:53:08 +00001027 if (Qualifier)
1028 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001029
John McCallb1a56e72010-03-26 23:10:15 +00001030 DeclContext *LexicalDC = Owner;
1031 if (!isFriend && D->isOutOfLine()) {
1032 assert(D->getDeclContext()->isFileContext());
1033 LexicalDC = D->getDeclContext();
1034 }
1035
1036 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Douglas Gregore53060f2009-06-25 22:08:12 +00001038 // Attach the parameters
1039 for (unsigned P = 0; P < Params.size(); ++P)
1040 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001041 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001042
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001043 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001044 if (TemplateParams) {
1045 // Our resulting instantiation is actually a function template, since we
1046 // are substituting only the outer template parameters. For example, given
1047 //
1048 // template<typename T>
1049 // struct X {
1050 // template<typename U> friend void f(T, U);
1051 // };
1052 //
1053 // X<int> x;
1054 //
1055 // We are instantiating the friend function template "f" within X<int>,
1056 // which means substituting int for T, but leaving "f" as a friend function
1057 // template.
1058 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001059 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001060 Function->getLocation(),
1061 Function->getDeclName(),
1062 TemplateParams, Function);
1063 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001064
1065 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001066
1067 if (isFriend && D->isThisDeclarationADefinition()) {
1068 // TODO: should we remember this connection regardless of whether
1069 // the friend declaration provided a body?
1070 FunctionTemplate->setInstantiatedFromMemberTemplate(
1071 D->getDescribedFunctionTemplate());
1072 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001073 } else if (FunctionTemplate) {
1074 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001075 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001076 &TemplateArgs.getInnermost(),
1077 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001078 } else if (isFriend && D->isThisDeclarationADefinition()) {
1079 // TODO: should we remember this connection regardless of whether
1080 // the friend declaration provided a body?
1081 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001082 }
Douglas Gregora735b202009-10-13 14:39:41 +00001083
Douglas Gregore53060f2009-06-25 22:08:12 +00001084 if (InitFunctionInstantiation(Function, D))
1085 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Douglas Gregore53060f2009-06-25 22:08:12 +00001087 bool Redeclaration = false;
1088 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001089 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001090
John McCall68263142009-11-18 22:49:29 +00001091 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1092 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1093
John McCallaf2094e2010-04-08 09:05:18 +00001094 if (DependentFunctionTemplateSpecializationInfo *Info
1095 = D->getDependentSpecializationInfo()) {
1096 assert(isFriend && "non-friend has dependent specialization info?");
1097
1098 // This needs to be set now for future sanity.
1099 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1100
1101 // Instantiate the explicit template arguments.
1102 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1103 Info->getRAngleLoc());
1104 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1105 TemplateArgumentLoc Loc;
1106 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1107 return 0;
1108
1109 ExplicitArgs.addArgument(Loc);
1110 }
1111
1112 // Map the candidate templates to their instantiations.
1113 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1114 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1115 Info->getTemplate(I),
1116 TemplateArgs);
1117 if (!Temp) return 0;
1118
1119 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1120 }
1121
1122 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1123 &ExplicitArgs,
1124 Previous))
1125 Function->setInvalidDecl();
1126
1127 isExplicitSpecialization = true;
1128
1129 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001130 // Look only into the namespace where the friend would be declared to
1131 // find a previous declaration. This is the innermost enclosing namespace,
1132 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001133 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001134
Douglas Gregora735b202009-10-13 14:39:41 +00001135 // In C++, the previous declaration we find might be a tag type
1136 // (class or enum). In this case, the new declaration will hide the
1137 // tag type. Note that this does does not apply if we're declaring a
1138 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001139 if (Previous.isSingleTagDecl())
1140 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001141 }
1142
John McCall9f54ad42009-12-10 09:41:52 +00001143 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001144 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001145 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001146
John McCall76d32642010-04-24 01:30:58 +00001147 NamedDecl *PrincipalDecl = (TemplateParams
1148 ? cast<NamedDecl>(FunctionTemplate)
1149 : Function);
1150
Douglas Gregora735b202009-10-13 14:39:41 +00001151 // If the original function was part of a friend declaration,
1152 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001153 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001154 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001155 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001156 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001157 else
Douglas Gregora735b202009-10-13 14:39:41 +00001158 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001159
1160 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1161 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001162
1163 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1164 D->isThisDeclarationADefinition()) {
1165 // Check for a function body.
1166 const FunctionDecl *Definition = 0;
1167 if (Function->getBody(Definition) &&
1168 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1169 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1170 << Function->getDeclName();
1171 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1172 Function->setInvalidDecl();
1173 }
1174 // Check for redefinitions due to other instantiations of this or
1175 // a similar friend function.
1176 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1177 REnd = Function->redecls_end();
1178 R != REnd; ++R) {
1179 if (*R != Function &&
1180 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1181 if (const FunctionDecl *RPattern
1182 = (*R)->getTemplateInstantiationPattern())
1183 if (RPattern->getBody(RPattern)) {
1184 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1185 << Function->getDeclName();
1186 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1187 Function->setInvalidDecl();
1188 break;
1189 }
1190 }
1191 }
1192 }
1193
Douglas Gregora735b202009-10-13 14:39:41 +00001194 }
1195
John McCall76d32642010-04-24 01:30:58 +00001196 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1197 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1198 PrincipalDecl->setNonMemberOperator();
1199
Douglas Gregore53060f2009-06-25 22:08:12 +00001200 return Function;
1201}
1202
Douglas Gregord60e1052009-08-27 16:57:43 +00001203Decl *
1204TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1205 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001206 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1207 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001208 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001209 // We are creating a function template specialization from a function
1210 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001211 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +00001212 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001213 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001214 TemplateArgs.getInnermost().getFlatArgumentList(),
1215 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +00001216 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +00001217
1218 FunctionTemplateSpecializationInfo *Info
1219 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +00001220 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor6b906862009-08-21 00:16:32 +00001222 // If we already have a function template specialization, return it.
1223 if (Info)
1224 return Info->Function;
1225 }
1226
John McCallb0cb0222010-03-27 05:57:59 +00001227 bool isFriend;
1228 if (FunctionTemplate)
1229 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1230 else
1231 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1232
Douglas Gregor79c22782010-01-16 20:21:20 +00001233 bool MergeWithParentScope = (TemplateParams != 0) ||
1234 !(isa<Decl>(Owner) &&
1235 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1236 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001237
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001238 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001239 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1240 TInfo = SubstFunctionType(D, Params);
1241 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001242 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001243 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001244
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001245 // \brief If the type of this function is not *directly* a function
1246 // type, then we're instantiating the a function that was declared
1247 // via a typedef, e.g.,
1248 //
1249 // typedef int functype(int, int);
1250 // functype func;
1251 //
1252 // In this case, we'll just go instantiate the ParmVarDecls that we
1253 // synthesized in the method declaration.
1254 if (!isa<FunctionProtoType>(T)) {
1255 assert(!Params.size() && "Instantiating type could not yield parameters");
1256 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1257 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1258 TemplateArgs);
1259 if (!P)
1260 return 0;
1261
1262 Params.push_back(P);
1263 }
1264 }
1265
John McCallb0cb0222010-03-27 05:57:59 +00001266 NestedNameSpecifier *Qualifier = D->getQualifier();
1267 if (Qualifier) {
1268 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1269 D->getQualifierRange(),
1270 TemplateArgs);
1271 if (!Qualifier) return 0;
1272 }
1273
1274 DeclContext *DC = Owner;
1275 if (isFriend) {
1276 if (Qualifier) {
1277 CXXScopeSpec SS;
1278 SS.setScopeRep(Qualifier);
1279 SS.setRange(D->getQualifierRange());
1280 DC = SemaRef.computeDeclContext(SS);
1281 } else {
1282 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1283 D->getDeclContext(),
1284 TemplateArgs);
1285 }
1286 if (!DC) return 0;
1287 }
1288
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001289 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001290 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001291 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Douglas Gregordec06662009-08-21 18:42:58 +00001293 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +00001294 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +00001295 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1296 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
1297 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +00001298 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1299 Constructor->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001300 Name, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001301 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001302 Constructor->isInlineSpecified(),
1303 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001304 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1305 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
1306 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
1307 SemaRef.Context.getCanonicalType(ClassTy));
1308 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1309 Destructor->getLocation(), Name,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001310 T, Destructor->isInlineSpecified(),
1311 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001312 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001313 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001314 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +00001315 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001316 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
1317 ConvTy);
1318 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1319 Conversion->getLocation(), Name,
John McCall21ef0fa2010-03-11 09:03:00 +00001320 T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001321 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001322 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001323 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001324 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001325 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001326 D->isStatic(),
1327 D->getStorageClassAsWritten(),
1328 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001329 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001330
John McCallb0cb0222010-03-27 05:57:59 +00001331 if (Qualifier)
1332 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001333
Douglas Gregord60e1052009-08-27 16:57:43 +00001334 if (TemplateParams) {
1335 // Our resulting instantiation is actually a function template, since we
1336 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001337 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001338 // template<typename T>
1339 // struct X {
1340 // template<typename U> void f(T, U);
1341 // };
1342 //
1343 // X<int> x;
1344 //
1345 // We are instantiating the member template "f" within X<int>, which means
1346 // substituting int for T, but leaving "f" as a member function template.
1347 // Build the function template itself.
1348 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1349 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001350 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001351 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001352 if (isFriend) {
1353 FunctionTemplate->setLexicalDeclContext(Owner);
1354 FunctionTemplate->setObjectOfFriendDecl(true);
1355 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001356 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001357 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001358 } else if (FunctionTemplate) {
1359 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001360 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001361 &TemplateArgs.getInnermost(),
1362 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001363 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001364 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001365 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001366 }
1367
Mike Stump1eb44332009-09-09 15:08:12 +00001368 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001369 // out-of-line, the instantiation will have the same lexical
1370 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001371 if (isFriend) {
1372 Method->setLexicalDeclContext(Owner);
1373 Method->setObjectOfFriendDecl(true);
1374 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001375 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Douglas Gregor5545e162009-03-24 00:38:23 +00001377 // Attach the parameters
1378 for (unsigned P = 0; P < Params.size(); ++P)
1379 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001380 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001381
1382 if (InitMethodInstantiation(Method, D))
1383 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001384
John McCall68263142009-11-18 22:49:29 +00001385 LookupResult Previous(SemaRef, Name, SourceLocation(),
1386 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
John McCallb0cb0222010-03-27 05:57:59 +00001388 if (!FunctionTemplate || TemplateParams || isFriend) {
1389 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Douglas Gregordec06662009-08-21 18:42:58 +00001391 // In C++, the previous declaration we find might be a tag type
1392 // (class or enum). In this case, the new declaration will hide the
1393 // tag type. Note that this does does not apply if we're declaring a
1394 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001395 if (Previous.isSingleTagDecl())
1396 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001397 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001398
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001399 bool Redeclaration = false;
1400 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001401 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001402 /*FIXME:*/OverloadableAttrRequired);
1403
Douglas Gregor4ba31362009-12-01 17:24:26 +00001404 if (D->isPure())
1405 SemaRef.CheckPureMethod(Method, SourceRange());
1406
John McCall46460a62010-01-20 21:53:11 +00001407 Method->setAccess(D->getAccess());
1408
John McCallb0cb0222010-03-27 05:57:59 +00001409 if (FunctionTemplate) {
1410 // If there's a function template, let our caller handle it.
1411 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1412 // Don't hide a (potentially) valid declaration with an invalid one.
1413 } else {
1414 NamedDecl *DeclToAdd = (TemplateParams
1415 ? cast<NamedDecl>(FunctionTemplate)
1416 : Method);
1417 if (isFriend)
1418 Record->makeDeclVisibleInContext(DeclToAdd);
1419 else
1420 Owner->addDecl(DeclToAdd);
1421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001423 return Method;
1424}
1425
Douglas Gregor615c5d42009-03-24 16:43:20 +00001426Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001427 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001428}
1429
Douglas Gregor03b2b072009-03-24 00:15:49 +00001430Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001431 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001432}
1433
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001434Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001435 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001436}
1437
Douglas Gregor6477b692009-03-25 15:04:13 +00001438ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001439 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001440}
1441
John McCalle29ba202009-08-20 01:44:21 +00001442Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1443 TemplateTypeParmDecl *D) {
1444 // TODO: don't always clone when decls are refcounted.
1445 const Type* T = D->getTypeForDecl();
1446 assert(T->isTemplateTypeParmType());
1447 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001448
John McCalle29ba202009-08-20 01:44:21 +00001449 TemplateTypeParmDecl *Inst =
1450 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001451 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001452 TTPT->getName(),
1453 D->wasDeclaredWithTypename(),
1454 D->isParameterPack());
1455
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001456 if (D->hasDefaultArgument())
1457 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001458
Douglas Gregor550d9b22009-10-31 17:21:17 +00001459 // Introduce this template parameter's instantiation into the instantiation
1460 // scope.
1461 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1462
John McCalle29ba202009-08-20 01:44:21 +00001463 return Inst;
1464}
1465
Douglas Gregor33642df2009-10-23 23:25:44 +00001466Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1467 NonTypeTemplateParmDecl *D) {
1468 // Substitute into the type of the non-type template parameter.
1469 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001470 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001471 if (DI) {
1472 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1473 D->getDeclName());
1474 if (DI) T = DI->getType();
1475 } else {
1476 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1477 D->getDeclName());
1478 DI = 0;
1479 }
1480 if (T.isNull())
1481 return 0;
1482
1483 // Check that this type is acceptable for a non-type template parameter.
1484 bool Invalid = false;
1485 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1486 if (T.isNull()) {
1487 T = SemaRef.Context.IntTy;
1488 Invalid = true;
1489 }
1490
1491 NonTypeTemplateParmDecl *Param
1492 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1493 D->getDepth() - 1, D->getPosition(),
1494 D->getIdentifier(), T, DI);
1495 if (Invalid)
1496 Param->setInvalidDecl();
1497
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001498 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001499
1500 // Introduce this template parameter's instantiation into the instantiation
1501 // scope.
1502 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001503 return Param;
1504}
1505
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001506Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001507TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1508 TemplateTemplateParmDecl *D) {
1509 // Instantiate the template parameter list of the template template parameter.
1510 TemplateParameterList *TempParams = D->getTemplateParameters();
1511 TemplateParameterList *InstParams;
1512 {
1513 // Perform the actual substitution of template parameters within a new,
1514 // local instantiation scope.
1515 Sema::LocalInstantiationScope Scope(SemaRef);
1516 InstParams = SubstTemplateParams(TempParams);
1517 if (!InstParams)
1518 return NULL;
1519 }
1520
1521 // Build the template template parameter.
1522 TemplateTemplateParmDecl *Param
1523 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1524 D->getDepth() - 1, D->getPosition(),
1525 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001526 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001527
Douglas Gregor9106ef72009-11-11 16:58:32 +00001528 // Introduce this template parameter's instantiation into the instantiation
1529 // scope.
1530 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1531
1532 return Param;
1533}
1534
Douglas Gregor48c32a72009-11-17 06:07:40 +00001535Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1536 // Using directives are never dependent, so they require no explicit
1537
1538 UsingDirectiveDecl *Inst
1539 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1540 D->getNamespaceKeyLocation(),
1541 D->getQualifierRange(), D->getQualifier(),
1542 D->getIdentLocation(),
1543 D->getNominatedNamespace(),
1544 D->getCommonAncestor());
1545 Owner->addDecl(Inst);
1546 return Inst;
1547}
1548
John McCalled976492009-12-04 22:46:56 +00001549Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1550 // The nested name specifier is non-dependent, so no transformation
1551 // is required.
1552
John McCall9f54ad42009-12-10 09:41:52 +00001553 // We only need to do redeclaration lookups if we're in a class
1554 // scope (in fact, it's not really even possible in non-class
1555 // scopes).
1556 bool CheckRedeclaration = Owner->isRecord();
1557
1558 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1559 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1560
John McCalled976492009-12-04 22:46:56 +00001561 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1562 D->getLocation(),
1563 D->getNestedNameRange(),
1564 D->getUsingLocation(),
1565 D->getTargetNestedNameDecl(),
1566 D->getDeclName(),
1567 D->isTypeName());
1568
1569 CXXScopeSpec SS;
1570 SS.setScopeRep(D->getTargetNestedNameDecl());
1571 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001572
1573 if (CheckRedeclaration) {
1574 Prev.setHideTags(false);
1575 SemaRef.LookupQualifiedName(Prev, Owner);
1576
1577 // Check for invalid redeclarations.
1578 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1579 D->isTypeName(), SS,
1580 D->getLocation(), Prev))
1581 NewUD->setInvalidDecl();
1582
1583 }
1584
1585 if (!NewUD->isInvalidDecl() &&
1586 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001587 D->getLocation()))
1588 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001589
John McCalled976492009-12-04 22:46:56 +00001590 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1591 NewUD->setAccess(D->getAccess());
1592 Owner->addDecl(NewUD);
1593
John McCall9f54ad42009-12-10 09:41:52 +00001594 // Don't process the shadow decls for an invalid decl.
1595 if (NewUD->isInvalidDecl())
1596 return NewUD;
1597
John McCall323c3102009-12-22 22:26:37 +00001598 bool isFunctionScope = Owner->isFunctionOrMethod();
1599
John McCall9f54ad42009-12-10 09:41:52 +00001600 // Process the shadow decls.
1601 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1602 I != E; ++I) {
1603 UsingShadowDecl *Shadow = *I;
1604 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001605 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1606 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001607 TemplateArgs));
1608
1609 if (CheckRedeclaration &&
1610 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1611 continue;
1612
1613 UsingShadowDecl *InstShadow
1614 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1615 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001616
1617 if (isFunctionScope)
1618 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001619 }
John McCalled976492009-12-04 22:46:56 +00001620
1621 return NewUD;
1622}
1623
1624Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001625 // Ignore these; we handle them in bulk when processing the UsingDecl.
1626 return 0;
John McCalled976492009-12-04 22:46:56 +00001627}
1628
John McCall7ba107a2009-11-18 02:36:19 +00001629Decl * TemplateDeclInstantiator
1630 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001631 NestedNameSpecifier *NNS =
1632 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1633 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001634 TemplateArgs);
1635 if (!NNS)
1636 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001638 CXXScopeSpec SS;
1639 SS.setRange(D->getTargetNestedNameRange());
1640 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001641
1642 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001643 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001644 D->getUsingLoc(), SS, D->getLocation(),
1645 D->getDeclName(), 0,
1646 /*instantiation*/ true,
1647 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001648 if (UD)
John McCalled976492009-12-04 22:46:56 +00001649 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1650
John McCall7ba107a2009-11-18 02:36:19 +00001651 return UD;
1652}
1653
1654Decl * TemplateDeclInstantiator
1655 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1656 NestedNameSpecifier *NNS =
1657 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1658 D->getTargetNestedNameRange(),
1659 TemplateArgs);
1660 if (!NNS)
1661 return 0;
1662
1663 CXXScopeSpec SS;
1664 SS.setRange(D->getTargetNestedNameRange());
1665 SS.setScopeRep(NNS);
1666
1667 NamedDecl *UD =
1668 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1669 D->getUsingLoc(), SS, D->getLocation(),
1670 D->getDeclName(), 0,
1671 /*instantiation*/ true,
1672 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001673 if (UD)
John McCalled976492009-12-04 22:46:56 +00001674 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1675
Anders Carlsson0d8df782009-08-29 19:37:28 +00001676 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001677}
1678
John McCallce3ff2b2009-08-25 22:02:44 +00001679Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001680 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001681 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001682 if (D->isInvalidDecl())
1683 return 0;
1684
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001685 return Instantiator.Visit(D);
1686}
1687
John McCalle29ba202009-08-20 01:44:21 +00001688/// \brief Instantiates a nested template parameter list in the current
1689/// instantiation context.
1690///
1691/// \param L The parameter list to instantiate
1692///
1693/// \returns NULL if there was an error
1694TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001695TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001696 // Get errors for all the parameters before bailing out.
1697 bool Invalid = false;
1698
1699 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001700 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001701 ParamVector Params;
1702 Params.reserve(N);
1703 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1704 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001705 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001706 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001707 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001708 }
1709
1710 // Clean up if we had an error.
1711 if (Invalid) {
1712 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1713 PI != PE; ++PI)
1714 if (*PI)
1715 (*PI)->Destroy(SemaRef.Context);
1716 return NULL;
1717 }
1718
1719 TemplateParameterList *InstL
1720 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1721 L->getLAngleLoc(), &Params.front(), N,
1722 L->getRAngleLoc());
1723 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001724}
John McCalle29ba202009-08-20 01:44:21 +00001725
Douglas Gregored9c0f92009-10-29 00:04:11 +00001726/// \brief Instantiate the declaration of a class template partial
1727/// specialization.
1728///
1729/// \param ClassTemplate the (instantiated) class template that is partially
1730// specialized by the instantiation of \p PartialSpec.
1731///
1732/// \param PartialSpec the (uninstantiated) class template partial
1733/// specialization that we are instantiating.
1734///
1735/// \returns true if there was an error, false otherwise.
1736bool
1737TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1738 ClassTemplateDecl *ClassTemplate,
1739 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001740 // Create a local instantiation scope for this class template partial
1741 // specialization, which will contain the instantiations of the template
1742 // parameters.
1743 Sema::LocalInstantiationScope Scope(SemaRef);
1744
Douglas Gregored9c0f92009-10-29 00:04:11 +00001745 // Substitute into the template parameters of the class template partial
1746 // specialization.
1747 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1748 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1749 if (!InstParams)
1750 return true;
1751
1752 // Substitute into the template arguments of the class template partial
1753 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001754 const TemplateArgumentLoc *PartialSpecTemplateArgs
1755 = PartialSpec->getTemplateArgsAsWritten();
1756 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1757
John McCalld5532b62009-11-23 01:53:49 +00001758 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001759 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001760 TemplateArgumentLoc Loc;
1761 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001762 return true;
John McCalld5532b62009-11-23 01:53:49 +00001763 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001764 }
1765
1766
1767 // Check that the template argument list is well-formed for this
1768 // class template.
1769 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1770 InstTemplateArgs.size());
1771 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1772 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001773 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001774 false,
1775 Converted))
1776 return true;
1777
1778 // Figure out where to insert this class template partial specialization
1779 // in the member template's set of class template partial specializations.
1780 llvm::FoldingSetNodeID ID;
1781 ClassTemplatePartialSpecializationDecl::Profile(ID,
1782 Converted.getFlatArguments(),
1783 Converted.flatSize(),
1784 SemaRef.Context);
1785 void *InsertPos = 0;
1786 ClassTemplateSpecializationDecl *PrevDecl
1787 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1788 InsertPos);
1789
1790 // Build the canonical type that describes the converted template
1791 // arguments of the class template partial specialization.
1792 QualType CanonType
1793 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1794 Converted.getFlatArguments(),
1795 Converted.flatSize());
1796
1797 // Build the fully-sugared type for this class template
1798 // specialization as the user wrote in the specialization
1799 // itself. This means that we'll pretty-print the type retrieved
1800 // from the specialization's declaration the way that the user
1801 // actually wrote the specialization, rather than formatting the
1802 // name based on the "canonical" representation used to store the
1803 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001804 TypeSourceInfo *WrittenTy
1805 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1806 TemplateName(ClassTemplate),
1807 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001808 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001809 CanonType);
1810
1811 if (PrevDecl) {
1812 // We've already seen a partial specialization with the same template
1813 // parameters and template arguments. This can happen, for example, when
1814 // substituting the outer template arguments ends up causing two
1815 // class template partial specializations of a member class template
1816 // to have identical forms, e.g.,
1817 //
1818 // template<typename T, typename U>
1819 // struct Outer {
1820 // template<typename X, typename Y> struct Inner;
1821 // template<typename Y> struct Inner<T, Y>;
1822 // template<typename Y> struct Inner<U, Y>;
1823 // };
1824 //
1825 // Outer<int, int> outer; // error: the partial specializations of Inner
1826 // // have the same signature.
1827 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1828 << WrittenTy;
1829 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1830 << SemaRef.Context.getTypeDeclType(PrevDecl);
1831 return true;
1832 }
1833
1834
1835 // Create the class template partial specialization declaration.
1836 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001837 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1838 PartialSpec->getTagKind(),
1839 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001840 PartialSpec->getLocation(),
1841 InstParams,
1842 ClassTemplate,
1843 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001844 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001845 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001846 0,
1847 ClassTemplate->getPartialSpecializations().size());
John McCallb6217662010-03-15 10:12:16 +00001848 // Substitute the nested name specifier, if any.
1849 if (SubstQualifier(PartialSpec, InstPartialSpec))
1850 return 0;
1851
Douglas Gregored9c0f92009-10-29 00:04:11 +00001852 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001853 InstPartialSpec->setTypeAsWritten(WrittenTy);
1854
Douglas Gregored9c0f92009-10-29 00:04:11 +00001855 // Add this partial specialization to the set of class template partial
1856 // specializations.
1857 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1858 InsertPos);
1859 return false;
1860}
1861
John McCall21ef0fa2010-03-11 09:03:00 +00001862TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001863TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001864 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001865 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1866 assert(OldTInfo && "substituting function without type source info");
1867 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001868 TypeSourceInfo *NewTInfo
1869 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1870 D->getTypeSpecStartLoc(),
1871 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001872 if (!NewTInfo)
1873 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001874
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001875 if (NewTInfo != OldTInfo) {
1876 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001877 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001878 if (FunctionProtoTypeLoc *OldProtoLoc
1879 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1880 TypeLoc NewTL = NewTInfo->getTypeLoc();
1881 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1882 assert(NewProtoLoc && "Missing prototype?");
1883 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1884 // FIXME: Variadic templates will break this.
1885 Params.push_back(NewProtoLoc->getArg(i));
1886 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001887 OldProtoLoc->getArg(i),
1888 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001889 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001890 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001891 } else {
1892 // The function type itself was not dependent and therefore no
1893 // substitution occurred. However, we still need to instantiate
1894 // the function parameters themselves.
1895 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001896 if (FunctionProtoTypeLoc *OldProtoLoc
1897 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1898 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1899 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1900 if (!Parm)
1901 return 0;
1902 Params.push_back(Parm);
1903 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001904 }
1905 }
John McCall21ef0fa2010-03-11 09:03:00 +00001906 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001907}
1908
Mike Stump1eb44332009-09-09 15:08:12 +00001909/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001910/// declaration (New) from the corresponding fields of its template (Tmpl).
1911///
1912/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001913bool
1914TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001915 FunctionDecl *Tmpl) {
1916 if (Tmpl->isDeleted())
1917 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Douglas Gregorcca9e962009-07-01 22:01:06 +00001919 // If we are performing substituting explicitly-specified template arguments
1920 // or deduced template arguments into a function template and we reach this
1921 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001922 // to keeping the new function template specialization. We therefore
1923 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001924 // into a template instantiation for this specific function template
1925 // specialization, which is not a SFINAE context, so that we diagnose any
1926 // further errors in the declaration itself.
1927 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1928 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1929 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1930 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001931 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001932 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001933 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001934 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001935 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001936 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1937 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001938 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001939 }
1940 }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001942 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1943 assert(Proto && "Function template without prototype?");
1944
1945 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1946 Proto->getNoReturnAttr()) {
1947 // The function has an exception specification or a "noreturn"
1948 // attribute. Substitute into each of the exception types.
1949 llvm::SmallVector<QualType, 4> Exceptions;
1950 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1951 // FIXME: Poor location information!
1952 QualType T
1953 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1954 New->getLocation(), New->getDeclName());
1955 if (T.isNull() ||
1956 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1957 continue;
1958
1959 Exceptions.push_back(T);
1960 }
1961
1962 // Rebuild the function type
1963
1964 const FunctionProtoType *NewProto
1965 = New->getType()->getAs<FunctionProtoType>();
1966 assert(NewProto && "Template instantiation without function prototype?");
1967 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1968 NewProto->arg_type_begin(),
1969 NewProto->getNumArgs(),
1970 NewProto->isVariadic(),
1971 NewProto->getTypeQuals(),
1972 Proto->hasExceptionSpec(),
1973 Proto->hasAnyExceptionSpec(),
1974 Exceptions.size(),
1975 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001976 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001977 }
1978
Douglas Gregore53060f2009-06-25 22:08:12 +00001979 return false;
1980}
1981
Douglas Gregor5545e162009-03-24 00:38:23 +00001982/// \brief Initializes common fields of an instantiated method
1983/// declaration (New) from the corresponding fields of its template
1984/// (Tmpl).
1985///
1986/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001987bool
1988TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001989 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001990 if (InitFunctionInstantiation(New, Tmpl))
1991 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Douglas Gregor5545e162009-03-24 00:38:23 +00001993 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1994 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001995 if (Tmpl->isVirtualAsWritten())
1996 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001997
1998 // FIXME: attributes
1999 // FIXME: New needs a pointer to Tmpl
2000 return false;
2001}
Douglas Gregora58861f2009-05-13 20:28:22 +00002002
2003/// \brief Instantiate the definition of the given function from its
2004/// template.
2005///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002006/// \param PointOfInstantiation the point at which the instantiation was
2007/// required. Note that this is not precisely a "point of instantiation"
2008/// for the function, but it's close.
2009///
Douglas Gregora58861f2009-05-13 20:28:22 +00002010/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002011/// function template specialization or member function of a class template
2012/// specialization.
2013///
2014/// \param Recursive if true, recursively instantiates any functions that
2015/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002016///
2017/// \param DefinitionRequired if true, then we are performing an explicit
2018/// instantiation where the body of the function is required. Complain if
2019/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002020void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002021 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002022 bool Recursive,
2023 bool DefinitionRequired) {
Douglas Gregor238058c2010-05-18 05:45:02 +00002024 if (Function->isInvalidDecl() || Function->getBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002025 return;
2026
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002027 // Never instantiate an explicit specialization.
2028 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2029 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002030
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002031 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002032 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002033 Stmt *Pattern = 0;
2034 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002035 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002036
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002037 if (!Pattern) {
2038 if (DefinitionRequired) {
2039 if (Function->getPrimaryTemplate())
2040 Diag(PointOfInstantiation,
2041 diag::err_explicit_instantiation_undefined_func_template)
2042 << Function->getPrimaryTemplate();
2043 else
2044 Diag(PointOfInstantiation,
2045 diag::err_explicit_instantiation_undefined_member)
2046 << 1 << Function->getDeclName() << Function->getDeclContext();
2047
2048 if (PatternDecl)
2049 Diag(PatternDecl->getLocation(),
2050 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002051 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002052 }
2053
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002054 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002055 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002056
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002057 // C++0x [temp.explicit]p9:
2058 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002059 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002060 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002061 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002062 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002063 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002064 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002066 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2067 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002068 return;
2069
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002070 // If we're performing recursive template instantiation, create our own
2071 // queue of pending implicit instantiations that we will instantiate later,
2072 // while we're still within our own instantiation context.
2073 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2074 if (Recursive)
2075 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Douglas Gregor9679caf2010-05-12 17:27:19 +00002077 EnterExpressionEvaluationContext EvalContext(*this,
2078 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002079 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2080
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002081 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002082 // recorded, unless we're actually a member function within a local
2083 // class, in which case we need to merge our results with the parent
2084 // scope (of the enclosing function).
2085 bool MergeWithParentScope = false;
2086 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2087 MergeWithParentScope = Rec->isLocalClass();
2088
2089 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002090
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002091 // Introduce the instantiated function parameters into the local
2092 // instantiation scope.
2093 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
2094 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
2095 Function->getParamDecl(I));
2096
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002097 // Enter the scope of this instantiation. We don't use
2098 // PushDeclContext because we don't have a scope.
2099 DeclContext *PreviousContext = CurContext;
2100 CurContext = Function;
2101
Mike Stump1eb44332009-09-09 15:08:12 +00002102 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002103 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002104
2105 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002106 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002107 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2108 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2109 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002110 }
2111
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002112 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002113 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002114
Douglas Gregor52604ab2009-09-11 21:19:12 +00002115 if (Body.isInvalid())
2116 Function->setInvalidDecl();
2117
Mike Stump1eb44332009-09-09 15:08:12 +00002118 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002119 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002120
John McCall0c01d182010-03-24 05:22:00 +00002121 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2122
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002123 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002124
2125 DeclGroupRef DG(Function);
2126 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Douglas Gregor60406be2010-01-16 22:29:39 +00002128 // This class may have local implicit instantiations that need to be
2129 // instantiation within this scope.
2130 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2131 Scope.Exit();
2132
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002133 if (Recursive) {
2134 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002135 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002136 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002137
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002138 // Restore the set of pending implicit instantiations.
2139 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2140 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002141}
2142
2143/// \brief Instantiate the definition of the given variable from its
2144/// template.
2145///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002146/// \param PointOfInstantiation the point at which the instantiation was
2147/// required. Note that this is not precisely a "point of instantiation"
2148/// for the function, but it's close.
2149///
2150/// \param Var the already-instantiated declaration of a static member
2151/// variable of a class template specialization.
2152///
2153/// \param Recursive if true, recursively instantiates any functions that
2154/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002155///
2156/// \param DefinitionRequired if true, then we are performing an explicit
2157/// instantiation where an out-of-line definition of the member variable
2158/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002159void Sema::InstantiateStaticDataMemberDefinition(
2160 SourceLocation PointOfInstantiation,
2161 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002162 bool Recursive,
2163 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002164 if (Var->isInvalidDecl())
2165 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Douglas Gregor7caa6822009-07-24 20:34:43 +00002167 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002168 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002169 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002170 assert(Def->isStaticDataMember() && "Not a static data member?");
2171 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Douglas Gregor0d035142009-10-27 18:42:08 +00002173 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002174 // We did not find an out-of-line definition of this static data member,
2175 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002176 // instantiate this definition (or provide a specialization for it) in
2177 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002178 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002179 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002180 Diag(PointOfInstantiation,
2181 diag::err_explicit_instantiation_undefined_member)
2182 << 2 << Var->getDeclName() << Var->getDeclContext();
2183 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2184 }
2185
Douglas Gregor7caa6822009-07-24 20:34:43 +00002186 return;
2187 }
2188
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002189 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002190 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002191 return;
2192
2193 // C++0x [temp.explicit]p9:
2194 // Except for inline functions, other explicit instantiation declarations
2195 // have the effect of suppressing the implicit instantiation of the entity
2196 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002197 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002198 == TSK_ExplicitInstantiationDeclaration)
2199 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Douglas Gregor7caa6822009-07-24 20:34:43 +00002201 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2202 if (Inst)
2203 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Douglas Gregor7caa6822009-07-24 20:34:43 +00002205 // If we're performing recursive template instantiation, create our own
2206 // queue of pending implicit instantiations that we will instantiate later,
2207 // while we're still within our own instantiation context.
2208 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2209 if (Recursive)
2210 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor7caa6822009-07-24 20:34:43 +00002212 // Enter the scope of this instantiation. We don't use
2213 // PushDeclContext because we don't have a scope.
2214 DeclContext *PreviousContext = CurContext;
2215 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002217 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002218 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002219 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002220 CurContext = PreviousContext;
2221
2222 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002223 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2224 assert(MSInfo && "Missing member specialization information?");
2225 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2226 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002227 DeclGroupRef DG(Var);
2228 Consumer.HandleTopLevelDecl(DG);
2229 }
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor7caa6822009-07-24 20:34:43 +00002231 if (Recursive) {
2232 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002233 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002234 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregor7caa6822009-07-24 20:34:43 +00002236 // Restore the set of pending implicit instantiations.
2237 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002238 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002239}
Douglas Gregor815215d2009-05-27 05:35:12 +00002240
Anders Carlsson09025312009-08-29 05:16:22 +00002241void
2242Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2243 const CXXConstructorDecl *Tmpl,
2244 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Anders Carlsson09025312009-08-29 05:16:22 +00002246 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002247 bool AnyErrors = false;
2248
Anders Carlsson09025312009-08-29 05:16:22 +00002249 // Instantiate all the initializers.
2250 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002251 InitsEnd = Tmpl->init_end();
2252 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002253 CXXBaseOrMemberInitializer *Init = *Inits;
2254
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002255 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002256 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002257 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002259 // Instantiate the initializer.
2260 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2261 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2262 AnyErrors = true;
2263 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002264 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002265
Anders Carlsson09025312009-08-29 05:16:22 +00002266 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002267 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002268 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002269 TemplateArgs,
2270 Init->getSourceLocation(),
2271 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002272 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002273 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002274 New->setInvalidDecl();
2275 continue;
2276 }
2277
John McCalla93c9342009-12-07 02:54:59 +00002278 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002279 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002280 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002281 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002282 Init->getRParenLoc(),
2283 New->getParent());
2284 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002285 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002287 // Is this an anonymous union?
2288 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002289 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2290 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002291 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002292 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2293 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002294 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002295
2296 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002297 NewArgs.size(),
2298 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002299 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002300 Init->getRParenLoc());
2301 }
2302
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002303 if (NewInit.isInvalid()) {
2304 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002305 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002306 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002307 // FIXME: It would be nice if ASTOwningVector had a release function.
2308 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Anders Carlsson09025312009-08-29 05:16:22 +00002310 NewInits.push_back((MemInitTy *)NewInit.get());
2311 }
2312 }
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Anders Carlsson09025312009-08-29 05:16:22 +00002314 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002315 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002316 /*FIXME: ColonLoc */
2317 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002318 NewInits.data(), NewInits.size(),
2319 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002320}
2321
John McCall52a575a2009-08-29 08:11:13 +00002322// TODO: this could be templated if the various decl types used the
2323// same method name.
2324static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2325 ClassTemplateDecl *Instance) {
2326 Pattern = Pattern->getCanonicalDecl();
2327
2328 do {
2329 Instance = Instance->getCanonicalDecl();
2330 if (Pattern == Instance) return true;
2331 Instance = Instance->getInstantiatedFromMemberTemplate();
2332 } while (Instance);
2333
2334 return false;
2335}
2336
Douglas Gregor0d696532009-09-28 06:34:35 +00002337static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2338 FunctionTemplateDecl *Instance) {
2339 Pattern = Pattern->getCanonicalDecl();
2340
2341 do {
2342 Instance = Instance->getCanonicalDecl();
2343 if (Pattern == Instance) return true;
2344 Instance = Instance->getInstantiatedFromMemberTemplate();
2345 } while (Instance);
2346
2347 return false;
2348}
2349
Douglas Gregored9c0f92009-10-29 00:04:11 +00002350static bool
2351isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2352 ClassTemplatePartialSpecializationDecl *Instance) {
2353 Pattern
2354 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2355 do {
2356 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2357 Instance->getCanonicalDecl());
2358 if (Pattern == Instance)
2359 return true;
2360 Instance = Instance->getInstantiatedFromMember();
2361 } while (Instance);
2362
2363 return false;
2364}
2365
John McCall52a575a2009-08-29 08:11:13 +00002366static bool isInstantiationOf(CXXRecordDecl *Pattern,
2367 CXXRecordDecl *Instance) {
2368 Pattern = Pattern->getCanonicalDecl();
2369
2370 do {
2371 Instance = Instance->getCanonicalDecl();
2372 if (Pattern == Instance) return true;
2373 Instance = Instance->getInstantiatedFromMemberClass();
2374 } while (Instance);
2375
2376 return false;
2377}
2378
2379static bool isInstantiationOf(FunctionDecl *Pattern,
2380 FunctionDecl *Instance) {
2381 Pattern = Pattern->getCanonicalDecl();
2382
2383 do {
2384 Instance = Instance->getCanonicalDecl();
2385 if (Pattern == Instance) return true;
2386 Instance = Instance->getInstantiatedFromMemberFunction();
2387 } while (Instance);
2388
2389 return false;
2390}
2391
2392static bool isInstantiationOf(EnumDecl *Pattern,
2393 EnumDecl *Instance) {
2394 Pattern = Pattern->getCanonicalDecl();
2395
2396 do {
2397 Instance = Instance->getCanonicalDecl();
2398 if (Pattern == Instance) return true;
2399 Instance = Instance->getInstantiatedFromMemberEnum();
2400 } while (Instance);
2401
2402 return false;
2403}
2404
John McCalled976492009-12-04 22:46:56 +00002405static bool isInstantiationOf(UsingShadowDecl *Pattern,
2406 UsingShadowDecl *Instance,
2407 ASTContext &C) {
2408 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2409}
2410
2411static bool isInstantiationOf(UsingDecl *Pattern,
2412 UsingDecl *Instance,
2413 ASTContext &C) {
2414 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2415}
2416
John McCall7ba107a2009-11-18 02:36:19 +00002417static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2418 UsingDecl *Instance,
2419 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002420 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002421}
2422
2423static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002424 UsingDecl *Instance,
2425 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002426 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002427}
2428
John McCall52a575a2009-08-29 08:11:13 +00002429static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2430 VarDecl *Instance) {
2431 assert(Instance->isStaticDataMember());
2432
2433 Pattern = Pattern->getCanonicalDecl();
2434
2435 do {
2436 Instance = Instance->getCanonicalDecl();
2437 if (Pattern == Instance) return true;
2438 Instance = Instance->getInstantiatedFromStaticDataMember();
2439 } while (Instance);
2440
2441 return false;
2442}
2443
John McCalled976492009-12-04 22:46:56 +00002444// Other is the prospective instantiation
2445// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002446static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002447 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002448 if (UnresolvedUsingTypenameDecl *UUD
2449 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2450 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2451 return isInstantiationOf(UUD, UD, Ctx);
2452 }
2453 }
2454
2455 if (UnresolvedUsingValueDecl *UUD
2456 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002457 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2458 return isInstantiationOf(UUD, UD, Ctx);
2459 }
2460 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002461
Anders Carlsson0d8df782009-08-29 19:37:28 +00002462 return false;
2463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464
John McCall52a575a2009-08-29 08:11:13 +00002465 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2466 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002467
John McCall52a575a2009-08-29 08:11:13 +00002468 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2469 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002470
John McCall52a575a2009-08-29 08:11:13 +00002471 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2472 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002473
Douglas Gregor7caa6822009-07-24 20:34:43 +00002474 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002475 if (Var->isStaticDataMember())
2476 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2477
2478 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2479 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002480
Douglas Gregor0d696532009-09-28 06:34:35 +00002481 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2482 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2483
Douglas Gregored9c0f92009-10-29 00:04:11 +00002484 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2485 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2486 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2487 PartialSpec);
2488
Anders Carlssond8b285f2009-09-01 04:26:58 +00002489 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2490 if (!Field->getDeclName()) {
2491 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002492 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002493 cast<FieldDecl>(D);
2494 }
2495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
John McCalled976492009-12-04 22:46:56 +00002497 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2498 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2499
2500 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2501 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2502
Douglas Gregor815215d2009-05-27 05:35:12 +00002503 return D->getDeclName() && isa<NamedDecl>(Other) &&
2504 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2505}
2506
2507template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002508static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002509 NamedDecl *D,
2510 ForwardIterator first,
2511 ForwardIterator last) {
2512 for (; first != last; ++first)
2513 if (isInstantiationOf(Ctx, D, *first))
2514 return cast<NamedDecl>(*first);
2515
2516 return 0;
2517}
2518
John McCall02cace72009-08-28 07:59:38 +00002519/// \brief Finds the instantiation of the given declaration context
2520/// within the current instantiation.
2521///
2522/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002523DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002524 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002525 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002526 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002527 return cast_or_null<DeclContext>(ID);
2528 } else return DC;
2529}
2530
Douglas Gregored961e72009-05-27 17:54:46 +00002531/// \brief Find the instantiation of the given declaration within the
2532/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002533///
2534/// This routine is intended to be used when \p D is a declaration
2535/// referenced from within a template, that needs to mapped into the
2536/// corresponding declaration within an instantiation. For example,
2537/// given:
2538///
2539/// \code
2540/// template<typename T>
2541/// struct X {
2542/// enum Kind {
2543/// KnownValue = sizeof(T)
2544/// };
2545///
2546/// bool getKind() const { return KnownValue; }
2547/// };
2548///
2549/// template struct X<int>;
2550/// \endcode
2551///
2552/// In the instantiation of X<int>::getKind(), we need to map the
2553/// EnumConstantDecl for KnownValue (which refers to
2554/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002555/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2556/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002557NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002558 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002559 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002560 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002561 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002562 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002563 // D is a local of some kind. Look into the map of local
2564 // declarations to their instantiations.
2565 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2566 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002567
Douglas Gregore95b4092009-09-16 18:34:49 +00002568 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2569 if (!Record->isDependentContext())
2570 return D;
2571
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002572 // If the RecordDecl is actually the injected-class-name or a
2573 // "templated" declaration for a class template, class template
2574 // partial specialization, or a member class of a class template,
2575 // substitute into the injected-class-name of the class template
2576 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002577 QualType T;
2578 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2579
2580 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002581 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002582 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2583 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002584 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002585
2586 // If we call SubstType with an InjectedClassNameType here we
2587 // can end up in an infinite loop.
2588 T = Context.getTypeDeclType(Record);
2589 assert(isa<InjectedClassNameType>(T) &&
2590 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002591 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002592 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002593
2594 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002595 // Substitute into the injected-class-name to get the type
2596 // corresponding to the instantiation we want, which may also be
2597 // the current instantiation (if we're in a template
2598 // definition). This substitution should never fail, since we
2599 // know we can instantiate the injected-class-name or we
2600 // wouldn't have gotten to the injected-class-name!
2601
2602 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2603 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002604 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2605 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2606
2607 if (!T->isDependentType()) {
2608 assert(T->isRecordType() && "Instantiation must produce a record type");
2609 return T->getAs<RecordType>()->getDecl();
2610 }
2611
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002612 // We are performing "partial" template instantiation to create
2613 // the member declarations for the members of a class template
2614 // specialization. Therefore, D is actually referring to something
2615 // in the current instantiation. Look through the current
2616 // context, which contains actual instantiations, to find the
2617 // instantiation of the "current instantiation" that D refers
2618 // to.
2619 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002620 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002621 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002622 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002623 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002624 if (isInstantiationOf(ClassTemplate,
2625 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002626 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002627
2628 if (!DC->isDependentContext())
2629 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002630 }
2631
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002632 // We're performing "instantiation" of a member of the current
2633 // instantiation while we are type-checking the
2634 // definition. Compute the declaration context and return that.
2635 assert(!SawNonDependentContext &&
2636 "No dependent context while instantiating record");
2637 DeclContext *DC = computeDeclContext(T);
2638 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002639 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002640 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002641 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002642
Douglas Gregore95b4092009-09-16 18:34:49 +00002643 // Fall through to deal with other dependent record types (e.g.,
2644 // anonymous unions in class templates).
2645 }
John McCall52a575a2009-08-29 08:11:13 +00002646
Douglas Gregore95b4092009-09-16 18:34:49 +00002647 if (!ParentDC->isDependentContext())
2648 return D;
2649
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002650 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002651 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002652 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Douglas Gregor815215d2009-05-27 05:35:12 +00002654 if (ParentDC != D->getDeclContext()) {
2655 // We performed some kind of instantiation in the parent context,
2656 // so now we need to look into the instantiated parent context to
2657 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002658
John McCall3cb0ebd2010-03-10 03:28:59 +00002659 // If our context used to be dependent, we may need to instantiate
2660 // it before performing lookup into that context.
2661 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002662 if (!Spec->isDependentContext()) {
2663 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002664 const RecordType *Tag = T->getAs<RecordType>();
2665 assert(Tag && "type of non-dependent record is not a RecordType");
2666 if (!Tag->isBeingDefined() &&
2667 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2668 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002669 }
2670 }
2671
Douglas Gregor815215d2009-05-27 05:35:12 +00002672 NamedDecl *Result = 0;
2673 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002674 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002675 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2676 } else {
2677 // Since we don't have a name for the entity we're looking for,
2678 // our only option is to walk through all of the declarations to
2679 // find that name. This will occur in a few cases:
2680 //
2681 // - anonymous struct/union within a template
2682 // - unnamed class/struct/union/enum within a template
2683 //
2684 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002685 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002686 ParentDC->decls_begin(),
2687 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689
John McCall9f54ad42009-12-10 09:41:52 +00002690 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002691 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2692 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002693 && "Unable to find instantiation of declaration!");
2694
Douglas Gregor815215d2009-05-27 05:35:12 +00002695 D = Result;
2696 }
2697
Douglas Gregor815215d2009-05-27 05:35:12 +00002698 return D;
2699}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002700
Mike Stump1eb44332009-09-09 15:08:12 +00002701/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002702/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002703void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2704 while (!PendingLocalImplicitInstantiations.empty() ||
2705 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2706 PendingImplicitInstantiation Inst;
2707
2708 if (PendingLocalImplicitInstantiations.empty()) {
2709 Inst = PendingImplicitInstantiations.front();
2710 PendingImplicitInstantiations.pop_front();
2711 } else {
2712 Inst = PendingLocalImplicitInstantiations.front();
2713 PendingLocalImplicitInstantiations.pop_front();
2714 }
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregor7caa6822009-07-24 20:34:43 +00002716 // Instantiate function definitions
2717 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002718 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002719 Function->getLocation(), *this,
2720 Context.getSourceManager(),
2721 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002723 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002724 continue;
2725 }
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Douglas Gregor7caa6822009-07-24 20:34:43 +00002727 // Instantiate static data member definitions.
2728 VarDecl *Var = cast<VarDecl>(Inst.first);
2729 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002730
Chandler Carruth291b4412010-02-13 10:17:50 +00002731 // Don't try to instantiate declarations if the most recent redeclaration
2732 // is invalid.
2733 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2734 continue;
2735
2736 // Check if the most recent declaration has changed the specialization kind
2737 // and removed the need for implicit instantiation.
2738 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2739 case TSK_Undeclared:
2740 assert(false && "Cannot instantitiate an undeclared specialization.");
2741 case TSK_ExplicitInstantiationDeclaration:
2742 case TSK_ExplicitInstantiationDefinition:
2743 case TSK_ExplicitSpecialization:
2744 continue; // No longer need implicit instantiation.
2745 case TSK_ImplicitInstantiation:
2746 break;
2747 }
2748
Mike Stump1eb44332009-09-09 15:08:12 +00002749 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002750 Var->getLocation(), *this,
2751 Context.getSourceManager(),
2752 "instantiating static data member "
2753 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Douglas Gregor7caa6822009-07-24 20:34:43 +00002755 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002756 }
2757}
John McCall0c01d182010-03-24 05:22:00 +00002758
2759void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2760 const MultiLevelTemplateArgumentList &TemplateArgs) {
2761 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2762 E = Pattern->ddiag_end(); I != E; ++I) {
2763 DependentDiagnostic *DD = *I;
2764
2765 switch (DD->getKind()) {
2766 case DependentDiagnostic::Access:
2767 HandleDependentAccessCheck(*DD, TemplateArgs);
2768 break;
2769 }
2770 }
2771}