blob: 14ae73f4668c0f9da2956e135e8e304a361d7e2c [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//===----------------------------------------------------------------------===/
Douglas Gregore737f502010-08-12 20:07:10 +000012#include "clang/Sema/Sema.h"
13#include "clang/Sema/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 Gregor8dbc2692009-03-17 21:15:40 +000034 public:
35 typedef Sema::OwningExprResult OwningExprResult;
36
37 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +000038 const MultiLevelTemplateArgumentList &TemplateArgs)
Douglas Gregor7e063902009-05-11 23:53:27 +000039 : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { }
Mike Stump1eb44332009-09-09 15:08:12 +000040
Mike Stump390b4cc2009-05-16 07:39:55 +000041 // FIXME: Once we get closer to completion, replace these manually-written
42 // declarations with automatically-generated ones from
Sean Hunt9a555912010-05-30 07:21:58 +000043 // clang/AST/DeclNodes.inc.
Douglas Gregor4f722be2009-03-25 15:45:12 +000044 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
45 Decl *VisitNamespaceDecl(NamespaceDecl *D);
John McCall3dbd3d52010-02-16 06:53:13 +000046 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000047 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000048 Decl *VisitVarDecl(VarDecl *D);
Abramo Bagnara6206d532010-06-05 05:09:32 +000049 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000050 Decl *VisitFieldDecl(FieldDecl *D);
51 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
52 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000053 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000054 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000055 Decl *VisitFunctionDecl(FunctionDecl *D,
56 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000057 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000058 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
59 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000060 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000061 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000062 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000063 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000064 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000065 Decl *VisitClassTemplatePartialSpecializationDecl(
66 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000067 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000068 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000069 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000070 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000071 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000072 Decl *VisitUsingDecl(UsingDecl *D);
73 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000074 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
75 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Douglas Gregor8dbc2692009-03-17 21:15:40 +000077 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000078 Decl *VisitDecl(Decl *D) {
79 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
80 Diagnostic::Error,
81 "cannot instantiate %0 yet");
82 SemaRef.Diag(D->getLocation(), DiagID)
83 << D->getDeclKindName();
84
Douglas Gregor8dbc2692009-03-17 21:15:40 +000085 return 0;
86 }
Douglas Gregor5545e162009-03-24 00:38:23 +000087
88 // Helper functions for instantiating methods.
John McCall21ef0fa2010-03-11 09:03:00 +000089 TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000090 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000091 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000092 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000093
94 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000095 SubstTemplateParams(TemplateParameterList *List);
John McCallb6217662010-03-15 10:12:16 +000096
97 bool SubstQualifier(const DeclaratorDecl *OldDecl,
98 DeclaratorDecl *NewDecl);
99 bool SubstQualifier(const TagDecl *OldDecl,
100 TagDecl *NewDecl);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000101
102 bool InstantiateClassTemplatePartialSpecialization(
103 ClassTemplateDecl *ClassTemplate,
104 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000105 };
106}
107
John McCallb6217662010-03-15 10:12:16 +0000108bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
109 DeclaratorDecl *NewDecl) {
110 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
111 if (!OldQual) return false;
112
113 SourceRange QualRange = OldDecl->getQualifierRange();
114
115 NestedNameSpecifier *NewQual
116 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
117 if (!NewQual)
118 return true;
119
120 NewDecl->setQualifierInfo(NewQual, QualRange);
121 return false;
122}
123
124bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
125 TagDecl *NewDecl) {
126 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
127 if (!OldQual) return false;
128
129 SourceRange QualRange = OldDecl->getQualifierRange();
130
131 NestedNameSpecifier *NewQual
132 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
133 if (!NewQual)
134 return true;
135
136 NewDecl->setQualifierInfo(NewQual, QualRange);
137 return false;
138}
139
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000140// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +0000141void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
142 Decl *Tmpl, Decl *New) {
Sean Huntcf807c42010-08-18 23:23:40 +0000143 for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end();
144 i != e; ++i) {
145 const Attr *TmplAttr = *i;
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000146 // FIXME: This should be generalized to more than just the AlignedAttr.
147 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000148 if (Aligned->isAlignmentDependent()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000149 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +0000150 EnterExpressionEvaluationContext Unevaluated(*this,
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000151 Action::Unevaluated);
152
Sean Huntcf807c42010-08-18 23:23:40 +0000153 if (Aligned->isAlignmentExpr()) {
154 OwningExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
155 TemplateArgs);
156 if (!Result.isInvalid())
157 AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>());
158 }
159 else {
160 TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(),
161 TemplateArgs,
162 Aligned->getLocation(),
163 DeclarationName());
164 if (Result)
165 AddAlignedAttr(Aligned->getLocation(), New, Result);
166 }
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000167 continue;
168 }
169 }
170
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000171 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +0000172 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000173 New->addAttr(NewAttr);
174 }
175}
176
Douglas Gregor4f722be2009-03-25 15:45:12 +0000177Decl *
178TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
179 assert(false && "Translation units cannot be instantiated");
180 return D;
181}
182
183Decl *
184TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
185 assert(false && "Namespaces cannot be instantiated");
186 return D;
187}
188
John McCall3dbd3d52010-02-16 06:53:13 +0000189Decl *
190TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
191 NamespaceAliasDecl *Inst
192 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
193 D->getNamespaceLoc(),
194 D->getAliasLoc(),
195 D->getNamespace()->getIdentifier(),
196 D->getQualifierRange(),
197 D->getQualifier(),
198 D->getTargetNameLoc(),
199 D->getNamespace());
200 Owner->addDecl(Inst);
201 return Inst;
202}
203
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000204Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
205 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000206 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000207 if (DI->getType()->isDependentType() ||
208 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000209 DI = SemaRef.SubstType(DI, TemplateArgs,
210 D->getLocation(), D->getDeclName());
211 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000212 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000213 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000214 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000215 } else {
216 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000219 // Create the new typedef
220 TypedefDecl *Typedef
221 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000222 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000223 if (Invalid)
224 Typedef->setInvalidDecl();
225
Douglas Gregord57a38e2010-04-23 16:25:07 +0000226 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
227 TagDecl *TD = TT->getDecl();
228
229 // If the TagDecl that the TypedefDecl points to is an anonymous decl
230 // keep track of the TypedefDecl.
231 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
232 TD->setTypedefForAnonDecl(Typedef);
233 }
234
John McCall5126fd02009-12-30 00:31:22 +0000235 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000236 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
237 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000238 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
239 }
240
John McCall1d8d1cc2010-08-01 02:01:53 +0000241 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000242
John McCall46460a62010-01-20 21:53:11 +0000243 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000244 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000246 return Typedef;
247}
248
Douglas Gregor6eef5192009-12-14 19:27:10 +0000249/// \brief Instantiate the arguments provided as part of initialization.
250///
251/// \returns true if an error occurred, false otherwise.
252static bool InstantiateInitializationArguments(Sema &SemaRef,
253 Expr **Args, unsigned NumArgs,
254 const MultiLevelTemplateArgumentList &TemplateArgs,
255 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
John McCallca0408f2010-08-23 06:44:23 +0000256 ASTOwningVector<Expr*> &InitArgs) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000257 for (unsigned I = 0; I != NumArgs; ++I) {
258 // When we hit the first defaulted argument, break out of the loop:
259 // we don't pass those default arguments on.
260 if (Args[I]->isDefaultArgument())
261 break;
262
263 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
264 if (Arg.isInvalid())
265 return true;
266
267 Expr *ArgExpr = (Expr *)Arg.get();
268 InitArgs.push_back(Arg.release());
269
270 // FIXME: We're faking all of the comma locations. Do we need them?
271 FakeCommaLocs.push_back(
272 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
273 }
274
275 return false;
276}
277
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000278/// \brief Instantiate an initializer, breaking it into separate
279/// initialization arguments.
280///
281/// \param S The semantic analysis object.
282///
283/// \param Init The initializer to instantiate.
284///
285/// \param TemplateArgs Template arguments to be substituted into the
286/// initializer.
287///
288/// \param NewArgs Will be filled in with the instantiation arguments.
289///
290/// \returns true if an error occurred, false otherwise
291static bool InstantiateInitializer(Sema &S, Expr *Init,
292 const MultiLevelTemplateArgumentList &TemplateArgs,
293 SourceLocation &LParenLoc,
294 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
John McCallca0408f2010-08-23 06:44:23 +0000295 ASTOwningVector<Expr*> &NewArgs,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000296 SourceLocation &RParenLoc) {
297 NewArgs.clear();
298 LParenLoc = SourceLocation();
299 RParenLoc = SourceLocation();
300
301 if (!Init)
302 return false;
303
304 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
305 Init = ExprTemp->getSubExpr();
306
307 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
308 Init = Binder->getSubExpr();
309
310 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
311 Init = ICE->getSubExprAsWritten();
312
313 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
314 LParenLoc = ParenList->getLParenLoc();
315 RParenLoc = ParenList->getRParenLoc();
316 return InstantiateInitializationArguments(S, ParenList->getExprs(),
317 ParenList->getNumExprs(),
318 TemplateArgs, CommaLocs,
319 NewArgs);
320 }
321
322 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000323 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
324 if (InstantiateInitializationArguments(S,
325 Construct->getArgs(),
326 Construct->getNumArgs(),
327 TemplateArgs,
328 CommaLocs, NewArgs))
329 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000330
Douglas Gregor28329e52010-03-24 21:22:47 +0000331 // FIXME: Fake locations!
332 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
333 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
334 return false;
335 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000336 }
337
338 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
339 if (Result.isInvalid())
340 return true;
341
342 NewArgs.push_back(Result.takeAs<Expr>());
343 return false;
344}
345
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000346Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000347 // If this is the variable for an anonymous struct or union,
348 // instantiate the anonymous struct/union type first.
349 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
350 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
351 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
352 return 0;
353
John McCallce3ff2b2009-08-25 22:02:44 +0000354 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000355 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000356 TemplateArgs,
357 D->getTypeSpecStartLoc(),
358 D->getDeclName());
359 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000360 return 0;
361
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000362 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000363 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
364 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000365 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000366 D->getStorageClass(),
367 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000368 Var->setThreadSpecified(D->isThreadSpecified());
369 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000370
John McCallb6217662010-03-15 10:12:16 +0000371 // Substitute the nested name specifier, if any.
372 if (SubstQualifier(D, Var))
373 return 0;
374
Mike Stump1eb44332009-09-09 15:08:12 +0000375 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000376 // out-of-line, the instantiation will have the same lexical
377 // context (which will be a namespace scope) as the template.
378 if (D->isOutOfLine())
379 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000380
John McCall46460a62010-01-20 21:53:11 +0000381 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000382
383 if (!D->isStaticDataMember())
384 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000385
Mike Stump390b4cc2009-05-16 07:39:55 +0000386 // FIXME: In theory, we could have a previous declaration for variables that
387 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000388 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000389 // FIXME: having to fake up a LookupResult is dumb.
390 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000391 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000392 if (D->isStaticDataMember())
393 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000394 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregor7caa6822009-07-24 20:34:43 +0000396 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000397 if (!D->isStaticDataMember())
398 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000399 Owner->makeDeclVisibleInContext(Var);
400 } else {
401 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000402 if (Owner->isFunctionOrMethod())
403 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000404 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000405 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000406
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000407 // Link instantiations of static data members back to the template from
408 // which they were instantiated.
409 if (Var->isStaticDataMember())
410 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000411 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000412
Douglas Gregor60c93c92010-02-09 07:26:29 +0000413 if (Var->getAnyInitializer()) {
414 // We already have an initializer in the class.
415 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000416 if (Var->isStaticDataMember() && !D->isOutOfLine())
417 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
418 else
419 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
420
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000421 // Instantiate the initializer.
422 SourceLocation LParenLoc, RParenLoc;
423 llvm::SmallVector<SourceLocation, 4> CommaLocs;
John McCallca0408f2010-08-23 06:44:23 +0000424 ASTOwningVector<Expr*> InitArgs(SemaRef);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000425 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
426 CommaLocs, InitArgs, RParenLoc)) {
427 // Attach the initializer to the declaration.
428 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000429 // Add the direct initializer to the declaration.
John McCalld226f652010-08-21 09:40:31 +0000430 SemaRef.AddCXXDirectInitializerToDecl(Var,
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000431 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000432 move_arg(InitArgs),
433 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000434 RParenLoc);
435 } else if (InitArgs.size() == 1) {
John McCall9ae2f072010-08-23 23:25:46 +0000436 Expr *Init = InitArgs.take()[0];
437 SemaRef.AddInitializerToDecl(Var, Init, false);
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000438 } else {
439 assert(InitArgs.size() == 0);
John McCalld226f652010-08-21 09:40:31 +0000440 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000441 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000442 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000443 // FIXME: Not too happy about invalidating the declaration
444 // because of a bogus initializer.
445 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000446 }
447
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000448 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000449 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
John McCalld226f652010-08-21 09:40:31 +0000450 SemaRef.ActOnUninitializedDecl(Var, false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000451
Douglas Gregor5764f612010-05-08 23:05:03 +0000452 // Diagnose unused local variables.
453 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
454 SemaRef.DiagnoseUnusedDecl(Var);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +0000455
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000456 return Var;
457}
458
Abramo Bagnara6206d532010-06-05 05:09:32 +0000459Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
460 AccessSpecDecl* AD
461 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
462 D->getAccessSpecifierLoc(), D->getColonLoc());
463 Owner->addHiddenDecl(AD);
464 return AD;
465}
466
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000467Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
468 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000469 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000470 if (DI->getType()->isDependentType() ||
471 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000472 DI = SemaRef.SubstType(DI, TemplateArgs,
473 D->getLocation(), D->getDeclName());
474 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000475 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000476 Invalid = true;
477 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000478 // C++ [temp.arg.type]p3:
479 // If a declaration acquires a function type through a type
480 // dependent on a template-parameter and this causes a
481 // declaration that does not use the syntactic form of a
482 // function declarator to have function type, the program is
483 // ill-formed.
484 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000485 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486 Invalid = true;
487 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000488 } else {
489 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000490 }
491
492 Expr *BitWidth = D->getBitWidth();
493 if (Invalid)
494 BitWidth = 0;
495 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000496 // The bit-width expression is not potentially evaluated.
497 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000499 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000500 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000501 if (InstantiatedBitWidth.isInvalid()) {
502 Invalid = true;
503 BitWidth = 0;
504 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000505 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000506 }
507
John McCall07fb6be2009-10-22 23:33:21 +0000508 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
509 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000510 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000511 D->getLocation(),
512 D->isMutable(),
513 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000514 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000515 D->getAccess(),
516 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000517 if (!Field) {
518 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000519 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
John McCall1d8d1cc2010-08-01 02:01:53 +0000522 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000523
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000524 if (Invalid)
525 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000527 if (!Field->getDeclName()) {
528 // Keep track of where this decl came from.
529 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000530 }
531 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
532 if (Parent->isAnonymousStructOrUnion() &&
533 Parent->getLookupContext()->isFunctionOrMethod())
534 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000537 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000538 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000539 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000540
541 return Field;
542}
543
John McCall02cace72009-08-28 07:59:38 +0000544Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000545 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000546 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000547 if (TypeSourceInfo *Ty = D->getFriendType()) {
548 TypeSourceInfo *InstTy =
549 SemaRef.SubstType(Ty, TemplateArgs,
550 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000551 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000552 return 0;
John McCall02cace72009-08-28 07:59:38 +0000553
Douglas Gregor06245bf2010-04-07 17:57:12 +0000554 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
555 if (!FD)
556 return 0;
557
558 FD->setAccess(AS_public);
559 Owner->addDecl(FD);
560 return FD;
561 }
562
563 NamedDecl *ND = D->getFriendDecl();
564 assert(ND && "friend decl must be a decl or a type!");
565
John McCallaf2094e2010-04-08 09:05:18 +0000566 // All of the Visit implementations for the various potential friend
567 // declarations have to be carefully written to work for friend
568 // objects, with the most important detail being that the target
569 // decl should almost certainly not be placed in Owner.
570 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000571 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
John McCall02cace72009-08-28 07:59:38 +0000573 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000574 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
575 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000576 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000577 Owner->addDecl(FD);
578 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000579}
580
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000581Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
582 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Douglas Gregorac7610d2009-06-22 20:57:11 +0000584 // The expression in a static assertion is not potentially evaluated.
585 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000587 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000588 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000589 if (InstantiatedAssertExpr.isInvalid())
590 return 0;
591
John McCallca0408f2010-08-23 06:44:23 +0000592 OwningExprResult Message(D->getMessage());
Douglas Gregor43d9d922009-08-08 01:41:12 +0000593 D->getMessage()->Retain();
John McCalld226f652010-08-21 09:40:31 +0000594 return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
John McCall9ae2f072010-08-23 23:25:46 +0000595 InstantiatedAssertExpr.get(),
596 Message.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000597}
598
599Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000600 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000601 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000602 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000603 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000604 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000605 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000606 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000607 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000608 Enum->startDefinition();
609
Douglas Gregor96084f12010-03-01 19:00:07 +0000610 if (D->getDeclContext()->isFunctionOrMethod())
611 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
612
John McCalld226f652010-08-21 09:40:31 +0000613 llvm::SmallVector<Decl*, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000614
615 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000616 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
617 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000618 EC != ECEnd; ++EC) {
619 // The specified value for the enumerator.
620 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000621 if (Expr *UninstValue = EC->getInitExpr()) {
622 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000623 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000624 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
John McCallce3ff2b2009-08-25 22:02:44 +0000626 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000627 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000628
629 // Drop the initial value and continue.
630 bool isInvalid = false;
631 if (Value.isInvalid()) {
632 Value = SemaRef.Owned((Expr *)0);
633 isInvalid = true;
634 }
635
Mike Stump1eb44332009-09-09 15:08:12 +0000636 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000637 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
638 EC->getLocation(), EC->getIdentifier(),
John McCall9ae2f072010-08-23 23:25:46 +0000639 Value.get());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000640
641 if (isInvalid) {
642 if (EnumConst)
643 EnumConst->setInvalidDecl();
644 Enum->setInvalidDecl();
645 }
646
647 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000648 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000649 Enum->addDecl(EnumConst);
John McCalld226f652010-08-21 09:40:31 +0000650 Enumerators.push_back(EnumConst);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000651 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000652
653 if (D->getDeclContext()->isFunctionOrMethod()) {
654 // If the enumeration is within a function or method, record the enum
655 // constant as a local.
656 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
657 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000658 }
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000661 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000662 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000663 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +0000664 Enum,
Eli Friedmande7a0fc2010-08-15 02:27:09 +0000665 Enumerators.data(), Enumerators.size(),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000666 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000667
668 return Enum;
669}
670
Douglas Gregor6477b692009-03-25 15:04:13 +0000671Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
672 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
673 return 0;
674}
675
John McCalle29ba202009-08-20 01:44:21 +0000676Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000677 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
678
Douglas Gregor550d9b22009-10-31 17:21:17 +0000679 // Create a local instantiation scope for this class template, which
680 // will contain the instantiations of the template parameters.
681 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000682 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000683 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000684 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000685 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000686
687 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000688
689 // Instantiate the qualifier. We have to do this first in case
690 // we're a friend declaration, because if we are then we need to put
691 // the new declaration in the appropriate context.
692 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
693 if (Qualifier) {
694 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
695 Pattern->getQualifierRange(),
696 TemplateArgs);
697 if (!Qualifier) return 0;
698 }
699
700 CXXRecordDecl *PrevDecl = 0;
701 ClassTemplateDecl *PrevClassTemplate = 0;
702
703 // If this isn't a friend, then it's a member template, in which
704 // case we just want to build the instantiation in the
705 // specialization. If it is a friend, we want to build it in
706 // the appropriate context.
707 DeclContext *DC = Owner;
708 if (isFriend) {
709 if (Qualifier) {
710 CXXScopeSpec SS;
711 SS.setScopeRep(Qualifier);
712 SS.setRange(Pattern->getQualifierRange());
713 DC = SemaRef.computeDeclContext(SS);
714 if (!DC) return 0;
715 } else {
716 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
717 Pattern->getDeclContext(),
718 TemplateArgs);
719 }
720
721 // Look for a previous declaration of the template in the owning
722 // context.
723 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
724 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
725 SemaRef.LookupQualifiedName(R, DC);
726
727 if (R.isSingleResult()) {
728 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
729 if (PrevClassTemplate)
730 PrevDecl = PrevClassTemplate->getTemplatedDecl();
731 }
732
733 if (!PrevClassTemplate && Qualifier) {
734 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000735 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
736 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000737 return 0;
738 }
739
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000740 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000741 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000742 bool Complain = true;
743
744 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
745 // template for struct std::tr1::__detail::_Map_base, where the
746 // template parameters of the friend declaration don't match the
747 // template parameters of the original declaration. In this one
748 // case, we don't complain about the ill-formed friend
749 // declaration.
750 if (isFriend && Pattern->getIdentifier() &&
751 Pattern->getIdentifier()->isStr("_Map_base") &&
752 DC->isNamespace() &&
753 cast<NamespaceDecl>(DC)->getIdentifier() &&
754 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
755 DeclContext *DCParent = DC->getParent();
756 if (DCParent->isNamespace() &&
757 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
758 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
759 DeclContext *DCParent2 = DCParent->getParent();
760 if (DCParent2->isNamespace() &&
761 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
762 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
763 DCParent2->getParent()->isTranslationUnit())
764 Complain = false;
765 }
766 }
767
John McCall93ba8572010-03-25 06:39:04 +0000768 TemplateParameterList *PrevParams
769 = PrevClassTemplate->getTemplateParameters();
770
771 // Make sure the parameter lists match.
772 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000773 Complain,
774 Sema::TPL_TemplateMatch)) {
775 if (Complain)
776 return 0;
777
778 AdoptedPreviousTemplateParams = true;
779 InstParams = PrevParams;
780 }
John McCall93ba8572010-03-25 06:39:04 +0000781
782 // Do some additional validation, then merge default arguments
783 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000784 if (!AdoptedPreviousTemplateParams &&
785 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000786 Sema::TPC_ClassTemplate))
787 return 0;
788 }
789 }
790
John McCalle29ba202009-08-20 01:44:21 +0000791 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000792 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000793 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000794 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000795 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000796
John McCall93ba8572010-03-25 06:39:04 +0000797 if (Qualifier)
798 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000799
John McCalle29ba202009-08-20 01:44:21 +0000800 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000801 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
802 D->getIdentifier(), InstParams, RecordInst,
803 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000804 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000805
John McCall93ba8572010-03-25 06:39:04 +0000806 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000807 if (PrevClassTemplate)
808 Inst->setAccess(PrevClassTemplate->getAccess());
809 else
810 Inst->setAccess(D->getAccess());
811
John McCall93ba8572010-03-25 06:39:04 +0000812 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
813 // TODO: do we want to track the instantiation progeny of this
814 // friend target decl?
815 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000816 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000817 Inst->setInstantiatedFromMemberTemplate(D);
818 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000819
820 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000821 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000822 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000823
Douglas Gregor259571e2009-10-30 22:42:42 +0000824 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000825 if (isFriend) {
826 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000827 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000828 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000829
John McCalle29ba202009-08-20 01:44:21 +0000830 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000831
Douglas Gregored9c0f92009-10-29 00:04:11 +0000832 // Instantiate all of the partial specializations of this member class
833 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000834 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
835 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000836 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
837 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
838
John McCalle29ba202009-08-20 01:44:21 +0000839 return Inst;
840}
841
Douglas Gregord60e1052009-08-27 16:57:43 +0000842Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000843TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
844 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000845 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
846
847 // Lookup the already-instantiated declaration in the instantiation
848 // of the class template and return that.
849 DeclContext::lookup_result Found
850 = Owner->lookup(ClassTemplate->getDeclName());
851 if (Found.first == Found.second)
852 return 0;
853
854 ClassTemplateDecl *InstClassTemplate
855 = dyn_cast<ClassTemplateDecl>(*Found.first);
856 if (!InstClassTemplate)
857 return 0;
858
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000859 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000860}
861
862Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000863TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000864 // Create a local instantiation scope for this function template, which
865 // will contain the instantiations of the template parameters and then get
866 // merged with the local instantiation scope for the function template
867 // itself.
868 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000869
Douglas Gregord60e1052009-08-27 16:57:43 +0000870 TemplateParameterList *TempParams = D->getTemplateParameters();
871 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000872 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000873 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000874
Douglas Gregora735b202009-10-13 14:39:41 +0000875 FunctionDecl *Instantiated = 0;
876 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
877 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
878 InstParams));
879 else
880 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
881 D->getTemplatedDecl(),
882 InstParams));
883
884 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000885 return 0;
886
John McCall46460a62010-01-20 21:53:11 +0000887 Instantiated->setAccess(D->getAccess());
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000890 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000891 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000892 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000893 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000894 assert(InstTemplate &&
895 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000896
John McCallb1a56e72010-03-26 23:10:15 +0000897 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
898
John McCalle976ffe2009-12-14 23:19:40 +0000899 // Link the instantiation back to the pattern *unless* this is a
900 // non-definition friend declaration.
901 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000902 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000903 InstTemplate->setInstantiatedFromMemberTemplate(D);
904
John McCallb1a56e72010-03-26 23:10:15 +0000905 // Make declarations visible in the appropriate context.
906 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000907 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000908
Douglas Gregord60e1052009-08-27 16:57:43 +0000909 return InstTemplate;
910}
911
Douglas Gregord475b8d2009-03-25 21:17:03 +0000912Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
913 CXXRecordDecl *PrevDecl = 0;
914 if (D->isInjectedClassName())
915 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000916 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000917 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
918 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000919 TemplateArgs);
920 if (!Prev) return 0;
921 PrevDecl = cast<CXXRecordDecl>(Prev);
922 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000923
924 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000925 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000926 D->getLocation(), D->getIdentifier(),
927 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000928
929 // Substitute the nested name specifier, if any.
930 if (SubstQualifier(D, Record))
931 return 0;
932
Douglas Gregord475b8d2009-03-25 21:17:03 +0000933 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000934 // FIXME: Check against AS_none is an ugly hack to work around the issue that
935 // the tag decls introduced by friend class declarations don't have an access
936 // specifier. Remove once this area of the code gets sorted out.
937 if (D->getAccess() != AS_none)
938 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000939 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000940 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000941
John McCall02cace72009-08-28 07:59:38 +0000942 // If the original function was part of a friend declaration,
943 // inherit its namespace state.
944 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
945 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
946
Douglas Gregor9901c572010-05-21 00:31:19 +0000947 // Make sure that anonymous structs and unions are recorded.
948 if (D->isAnonymousStructOrUnion()) {
949 Record->setAnonymousStructOrUnion(true);
950 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
951 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
952 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000953
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000954 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000955 return Record;
956}
957
John McCall02cace72009-08-28 07:59:38 +0000958/// Normal class members are of more specific types and therefore
959/// don't make it here. This function serves two purposes:
960/// 1) instantiating function templates
961/// 2) substituting friend declarations
962/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000963Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000964 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000965 // Check whether there is already a function template specialization for
966 // this declaration.
967 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
968 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000969 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000970 std::pair<const TemplateArgument *, unsigned> Innermost
971 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000973 FunctionDecl *SpecFunc
974 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
975 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor127102b2009-06-29 20:59:39 +0000977 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000978 if (SpecFunc)
979 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
John McCallb0cb0222010-03-27 05:57:59 +0000982 bool isFriend;
983 if (FunctionTemplate)
984 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
985 else
986 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
987
Douglas Gregor79c22782010-01-16 20:21:20 +0000988 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000989 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000990 !(isa<Decl>(Owner) &&
991 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
992 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregore53060f2009-06-25 22:08:12 +0000994 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000995 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
996 TInfo = SubstFunctionType(D, Params);
997 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000998 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000999 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +00001000
John McCalld325daa2010-03-26 04:53:08 +00001001 NestedNameSpecifier *Qualifier = D->getQualifier();
1002 if (Qualifier) {
1003 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1004 D->getQualifierRange(),
1005 TemplateArgs);
1006 if (!Qualifier) return 0;
1007 }
1008
John McCall68b6b872010-02-06 01:50:47 +00001009 // If we're instantiating a local function declaration, put the result
1010 // in the owner; otherwise we need to find the instantiated context.
1011 DeclContext *DC;
1012 if (D->getDeclContext()->isFunctionOrMethod())
1013 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001014 else if (isFriend && Qualifier) {
1015 CXXScopeSpec SS;
1016 SS.setScopeRep(Qualifier);
1017 SS.setRange(D->getQualifierRange());
1018 DC = SemaRef.computeDeclContext(SS);
1019 if (!DC) return 0;
1020 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001021 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1022 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001023 }
John McCall68b6b872010-02-06 01:50:47 +00001024
John McCall02cace72009-08-28 07:59:38 +00001025 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001026 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001027 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001028 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001029 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001030
John McCalld325daa2010-03-26 04:53:08 +00001031 if (Qualifier)
1032 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001033
John McCallb1a56e72010-03-26 23:10:15 +00001034 DeclContext *LexicalDC = Owner;
1035 if (!isFriend && D->isOutOfLine()) {
1036 assert(D->getDeclContext()->isFileContext());
1037 LexicalDC = D->getDeclContext();
1038 }
1039
1040 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregore53060f2009-06-25 22:08:12 +00001042 // Attach the parameters
1043 for (unsigned P = 0; P < Params.size(); ++P)
1044 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001045 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001046
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001047 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001048 if (TemplateParams) {
1049 // Our resulting instantiation is actually a function template, since we
1050 // are substituting only the outer template parameters. For example, given
1051 //
1052 // template<typename T>
1053 // struct X {
1054 // template<typename U> friend void f(T, U);
1055 // };
1056 //
1057 // X<int> x;
1058 //
1059 // We are instantiating the friend function template "f" within X<int>,
1060 // which means substituting int for T, but leaving "f" as a friend function
1061 // template.
1062 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001063 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001064 Function->getLocation(),
1065 Function->getDeclName(),
1066 TemplateParams, Function);
1067 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001068
1069 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001070
1071 if (isFriend && D->isThisDeclarationADefinition()) {
1072 // TODO: should we remember this connection regardless of whether
1073 // the friend declaration provided a body?
1074 FunctionTemplate->setInstantiatedFromMemberTemplate(
1075 D->getDescribedFunctionTemplate());
1076 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001077 } else if (FunctionTemplate) {
1078 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001079 std::pair<const TemplateArgument *, unsigned> Innermost
1080 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001081 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001082 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1083 Innermost.first,
1084 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001085 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001086 } else if (isFriend && D->isThisDeclarationADefinition()) {
1087 // TODO: should we remember this connection regardless of whether
1088 // the friend declaration provided a body?
1089 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001090 }
Douglas Gregora735b202009-10-13 14:39:41 +00001091
Douglas Gregore53060f2009-06-25 22:08:12 +00001092 if (InitFunctionInstantiation(Function, D))
1093 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregore53060f2009-06-25 22:08:12 +00001095 bool Redeclaration = false;
1096 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001097 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001098
John McCall68263142009-11-18 22:49:29 +00001099 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1100 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1101
John McCallaf2094e2010-04-08 09:05:18 +00001102 if (DependentFunctionTemplateSpecializationInfo *Info
1103 = D->getDependentSpecializationInfo()) {
1104 assert(isFriend && "non-friend has dependent specialization info?");
1105
1106 // This needs to be set now for future sanity.
1107 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1108
1109 // Instantiate the explicit template arguments.
1110 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1111 Info->getRAngleLoc());
1112 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1113 TemplateArgumentLoc Loc;
1114 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1115 return 0;
1116
1117 ExplicitArgs.addArgument(Loc);
1118 }
1119
1120 // Map the candidate templates to their instantiations.
1121 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1122 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1123 Info->getTemplate(I),
1124 TemplateArgs);
1125 if (!Temp) return 0;
1126
1127 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1128 }
1129
1130 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1131 &ExplicitArgs,
1132 Previous))
1133 Function->setInvalidDecl();
1134
1135 isExplicitSpecialization = true;
1136
1137 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001138 // Look only into the namespace where the friend would be declared to
1139 // find a previous declaration. This is the innermost enclosing namespace,
1140 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001141 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001142
Douglas Gregora735b202009-10-13 14:39:41 +00001143 // In C++, the previous declaration we find might be a tag type
1144 // (class or enum). In this case, the new declaration will hide the
1145 // tag type. Note that this does does not apply if we're declaring a
1146 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001147 if (Previous.isSingleTagDecl())
1148 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001149 }
1150
John McCall9f54ad42009-12-10 09:41:52 +00001151 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001152 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001153 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001154
John McCall76d32642010-04-24 01:30:58 +00001155 NamedDecl *PrincipalDecl = (TemplateParams
1156 ? cast<NamedDecl>(FunctionTemplate)
1157 : Function);
1158
Douglas Gregora735b202009-10-13 14:39:41 +00001159 // If the original function was part of a friend declaration,
1160 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001161 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001162 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001163 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001164 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001165 else
Douglas Gregora735b202009-10-13 14:39:41 +00001166 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001167
1168 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1169 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001170
1171 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1172 D->isThisDeclarationADefinition()) {
1173 // Check for a function body.
1174 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001175 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001176 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1177 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1178 << Function->getDeclName();
1179 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1180 Function->setInvalidDecl();
1181 }
1182 // Check for redefinitions due to other instantiations of this or
1183 // a similar friend function.
1184 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1185 REnd = Function->redecls_end();
1186 R != REnd; ++R) {
1187 if (*R != Function &&
1188 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1189 if (const FunctionDecl *RPattern
1190 = (*R)->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001191 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001192 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1193 << Function->getDeclName();
1194 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1195 Function->setInvalidDecl();
1196 break;
1197 }
1198 }
1199 }
1200 }
1201
Douglas Gregora735b202009-10-13 14:39:41 +00001202 }
1203
John McCall76d32642010-04-24 01:30:58 +00001204 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1205 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1206 PrincipalDecl->setNonMemberOperator();
1207
Douglas Gregore53060f2009-06-25 22:08:12 +00001208 return Function;
1209}
1210
Douglas Gregord60e1052009-08-27 16:57:43 +00001211Decl *
1212TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1213 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001214 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1215 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001216 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001217 // We are creating a function template specialization from a function
1218 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001219 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001220 std::pair<const TemplateArgument *, unsigned> Innermost
1221 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001223 FunctionDecl *SpecFunc
1224 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1225 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor6b906862009-08-21 00:16:32 +00001227 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001228 if (SpecFunc)
1229 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001230 }
1231
John McCallb0cb0222010-03-27 05:57:59 +00001232 bool isFriend;
1233 if (FunctionTemplate)
1234 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1235 else
1236 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1237
Douglas Gregor79c22782010-01-16 20:21:20 +00001238 bool MergeWithParentScope = (TemplateParams != 0) ||
1239 !(isa<Decl>(Owner) &&
1240 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1241 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001242
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001243 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001244 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1245 TInfo = SubstFunctionType(D, Params);
1246 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001247 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001248 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001249
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001250 // \brief If the type of this function is not *directly* a function
1251 // type, then we're instantiating the a function that was declared
1252 // via a typedef, e.g.,
1253 //
1254 // typedef int functype(int, int);
1255 // functype func;
1256 //
1257 // In this case, we'll just go instantiate the ParmVarDecls that we
1258 // synthesized in the method declaration.
1259 if (!isa<FunctionProtoType>(T)) {
1260 assert(!Params.size() && "Instantiating type could not yield parameters");
1261 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1262 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1263 TemplateArgs);
1264 if (!P)
1265 return 0;
1266
1267 Params.push_back(P);
1268 }
1269 }
1270
John McCallb0cb0222010-03-27 05:57:59 +00001271 NestedNameSpecifier *Qualifier = D->getQualifier();
1272 if (Qualifier) {
1273 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1274 D->getQualifierRange(),
1275 TemplateArgs);
1276 if (!Qualifier) return 0;
1277 }
1278
1279 DeclContext *DC = Owner;
1280 if (isFriend) {
1281 if (Qualifier) {
1282 CXXScopeSpec SS;
1283 SS.setScopeRep(Qualifier);
1284 SS.setRange(D->getQualifierRange());
1285 DC = SemaRef.computeDeclContext(SS);
1286 } else {
1287 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1288 D->getDeclContext(),
1289 TemplateArgs);
1290 }
1291 if (!DC) return 0;
1292 }
1293
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001294 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001295 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001296 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Abramo Bagnara25777432010-08-11 22:01:17 +00001298 DeclarationNameInfo NameInfo
1299 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001300 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001301 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001302 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001303 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001304 Constructor->isInlineSpecified(),
1305 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001306 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001307 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001308 NameInfo, T,
1309 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001310 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001311 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001312 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001313 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001314 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001315 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001316 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001317 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1318 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001319 D->isStatic(),
1320 D->getStorageClassAsWritten(),
1321 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001322 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001323
John McCallb0cb0222010-03-27 05:57:59 +00001324 if (Qualifier)
1325 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001326
Douglas Gregord60e1052009-08-27 16:57:43 +00001327 if (TemplateParams) {
1328 // Our resulting instantiation is actually a function template, since we
1329 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001330 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001331 // template<typename T>
1332 // struct X {
1333 // template<typename U> void f(T, U);
1334 // };
1335 //
1336 // X<int> x;
1337 //
1338 // We are instantiating the member template "f" within X<int>, which means
1339 // substituting int for T, but leaving "f" as a member function template.
1340 // Build the function template itself.
1341 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1342 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001343 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001344 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001345 if (isFriend) {
1346 FunctionTemplate->setLexicalDeclContext(Owner);
1347 FunctionTemplate->setObjectOfFriendDecl(true);
1348 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001349 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001350 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001351 } else if (FunctionTemplate) {
1352 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001353 std::pair<const TemplateArgument *, unsigned> Innermost
1354 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001355 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001356 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1357 Innermost.first,
1358 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001359 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001360 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001361 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001362 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001363 }
1364
Mike Stump1eb44332009-09-09 15:08:12 +00001365 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001366 // out-of-line, the instantiation will have the same lexical
1367 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001368 if (isFriend) {
1369 Method->setLexicalDeclContext(Owner);
1370 Method->setObjectOfFriendDecl(true);
1371 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001372 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor5545e162009-03-24 00:38:23 +00001374 // Attach the parameters
1375 for (unsigned P = 0; P < Params.size(); ++P)
1376 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001377 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001378
1379 if (InitMethodInstantiation(Method, D))
1380 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001381
Abramo Bagnara25777432010-08-11 22:01:17 +00001382 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1383 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
John McCallb0cb0222010-03-27 05:57:59 +00001385 if (!FunctionTemplate || TemplateParams || isFriend) {
1386 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregordec06662009-08-21 18:42:58 +00001388 // In C++, the previous declaration we find might be a tag type
1389 // (class or enum). In this case, the new declaration will hide the
1390 // tag type. Note that this does does not apply if we're declaring a
1391 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001392 if (Previous.isSingleTagDecl())
1393 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001394 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001395
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001396 bool Redeclaration = false;
1397 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001398 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001399 /*FIXME:*/OverloadableAttrRequired);
1400
Douglas Gregor4ba31362009-12-01 17:24:26 +00001401 if (D->isPure())
1402 SemaRef.CheckPureMethod(Method, SourceRange());
1403
John McCall46460a62010-01-20 21:53:11 +00001404 Method->setAccess(D->getAccess());
1405
John McCallb0cb0222010-03-27 05:57:59 +00001406 if (FunctionTemplate) {
1407 // If there's a function template, let our caller handle it.
1408 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1409 // Don't hide a (potentially) valid declaration with an invalid one.
1410 } else {
1411 NamedDecl *DeclToAdd = (TemplateParams
1412 ? cast<NamedDecl>(FunctionTemplate)
1413 : Method);
1414 if (isFriend)
1415 Record->makeDeclVisibleInContext(DeclToAdd);
1416 else
1417 Owner->addDecl(DeclToAdd);
1418 }
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00001419
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001420 return Method;
1421}
1422
Douglas Gregor615c5d42009-03-24 16:43:20 +00001423Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001424 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001425}
1426
Douglas Gregor03b2b072009-03-24 00:15:49 +00001427Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001428 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001429}
1430
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001431Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001432 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001433}
1434
Douglas Gregor6477b692009-03-25 15:04:13 +00001435ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001436 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001437}
1438
John McCalle29ba202009-08-20 01:44:21 +00001439Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1440 TemplateTypeParmDecl *D) {
1441 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001442 const Type* T = D->getTypeForDecl();
1443 assert(T->isTemplateTypeParmType());
1444 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001445
John McCalle29ba202009-08-20 01:44:21 +00001446 TemplateTypeParmDecl *Inst =
1447 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001448 TTPT->getDepth() - 1, TTPT->getIndex(),
1449 TTPT->getName(),
John McCalle29ba202009-08-20 01:44:21 +00001450 D->wasDeclaredWithTypename(),
1451 D->isParameterPack());
1452
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001453 if (D->hasDefaultArgument())
1454 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001455
Douglas Gregor550d9b22009-10-31 17:21:17 +00001456 // Introduce this template parameter's instantiation into the instantiation
1457 // scope.
1458 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1459
John McCalle29ba202009-08-20 01:44:21 +00001460 return Inst;
1461}
1462
Douglas Gregor33642df2009-10-23 23:25:44 +00001463Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1464 NonTypeTemplateParmDecl *D) {
1465 // Substitute into the type of the non-type template parameter.
1466 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001467 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001468 if (DI) {
1469 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1470 D->getDeclName());
1471 if (DI) T = DI->getType();
1472 } else {
1473 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1474 D->getDeclName());
1475 DI = 0;
1476 }
1477 if (T.isNull())
1478 return 0;
1479
1480 // Check that this type is acceptable for a non-type template parameter.
1481 bool Invalid = false;
1482 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1483 if (T.isNull()) {
1484 T = SemaRef.Context.IntTy;
1485 Invalid = true;
1486 }
1487
1488 NonTypeTemplateParmDecl *Param
1489 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1490 D->getDepth() - 1, D->getPosition(),
1491 D->getIdentifier(), T, DI);
1492 if (Invalid)
1493 Param->setInvalidDecl();
1494
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001495 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001496
1497 // Introduce this template parameter's instantiation into the instantiation
1498 // scope.
1499 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001500 return Param;
1501}
1502
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001503Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001504TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1505 TemplateTemplateParmDecl *D) {
1506 // Instantiate the template parameter list of the template template parameter.
1507 TemplateParameterList *TempParams = D->getTemplateParameters();
1508 TemplateParameterList *InstParams;
1509 {
1510 // Perform the actual substitution of template parameters within a new,
1511 // local instantiation scope.
1512 Sema::LocalInstantiationScope Scope(SemaRef);
1513 InstParams = SubstTemplateParams(TempParams);
1514 if (!InstParams)
1515 return NULL;
1516 }
1517
1518 // Build the template template parameter.
1519 TemplateTemplateParmDecl *Param
1520 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1521 D->getDepth() - 1, D->getPosition(),
1522 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001523 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001524
Douglas Gregor9106ef72009-11-11 16:58:32 +00001525 // Introduce this template parameter's instantiation into the instantiation
1526 // scope.
1527 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1528
1529 return Param;
1530}
1531
Douglas Gregor48c32a72009-11-17 06:07:40 +00001532Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1533 // Using directives are never dependent, so they require no explicit
1534
1535 UsingDirectiveDecl *Inst
1536 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1537 D->getNamespaceKeyLocation(),
1538 D->getQualifierRange(), D->getQualifier(),
1539 D->getIdentLocation(),
1540 D->getNominatedNamespace(),
1541 D->getCommonAncestor());
1542 Owner->addDecl(Inst);
1543 return Inst;
1544}
1545
John McCalled976492009-12-04 22:46:56 +00001546Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1547 // The nested name specifier is non-dependent, so no transformation
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001548 // is required. The same holds for the name info.
1549 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001550
John McCall9f54ad42009-12-10 09:41:52 +00001551 // We only need to do redeclaration lookups if we're in a class
1552 // scope (in fact, it's not really even possible in non-class
1553 // scopes).
1554 bool CheckRedeclaration = Owner->isRecord();
1555
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001556 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1557 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001558
John McCalled976492009-12-04 22:46:56 +00001559 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001560 D->getNestedNameRange(),
1561 D->getUsingLocation(),
1562 D->getTargetNestedNameDecl(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001563 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001564 D->isTypeName());
1565
1566 CXXScopeSpec SS;
1567 SS.setScopeRep(D->getTargetNestedNameDecl());
1568 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001569
1570 if (CheckRedeclaration) {
1571 Prev.setHideTags(false);
1572 SemaRef.LookupQualifiedName(Prev, Owner);
1573
1574 // Check for invalid redeclarations.
1575 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1576 D->isTypeName(), SS,
1577 D->getLocation(), Prev))
1578 NewUD->setInvalidDecl();
1579
1580 }
1581
1582 if (!NewUD->isInvalidDecl() &&
1583 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001584 D->getLocation()))
1585 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001586
John McCalled976492009-12-04 22:46:56 +00001587 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1588 NewUD->setAccess(D->getAccess());
1589 Owner->addDecl(NewUD);
1590
John McCall9f54ad42009-12-10 09:41:52 +00001591 // Don't process the shadow decls for an invalid decl.
1592 if (NewUD->isInvalidDecl())
1593 return NewUD;
1594
John McCall323c3102009-12-22 22:26:37 +00001595 bool isFunctionScope = Owner->isFunctionOrMethod();
1596
John McCall9f54ad42009-12-10 09:41:52 +00001597 // Process the shadow decls.
1598 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1599 I != E; ++I) {
1600 UsingShadowDecl *Shadow = *I;
1601 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001602 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1603 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001604 TemplateArgs));
1605
1606 if (CheckRedeclaration &&
1607 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1608 continue;
1609
1610 UsingShadowDecl *InstShadow
1611 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1612 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001613
1614 if (isFunctionScope)
1615 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001616 }
John McCalled976492009-12-04 22:46:56 +00001617
1618 return NewUD;
1619}
1620
1621Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001622 // Ignore these; we handle them in bulk when processing the UsingDecl.
1623 return 0;
John McCalled976492009-12-04 22:46:56 +00001624}
1625
John McCall7ba107a2009-11-18 02:36:19 +00001626Decl * TemplateDeclInstantiator
1627 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001628 NestedNameSpecifier *NNS =
1629 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1630 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001631 TemplateArgs);
1632 if (!NNS)
1633 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001635 CXXScopeSpec SS;
1636 SS.setRange(D->getTargetNestedNameRange());
1637 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001639 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1640 // Hence, no tranformation is required for it.
1641 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001642 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001643 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001644 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001645 /*instantiation*/ true,
1646 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001647 if (UD)
John McCalled976492009-12-04 22:46:56 +00001648 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1649
John McCall7ba107a2009-11-18 02:36:19 +00001650 return UD;
1651}
1652
1653Decl * TemplateDeclInstantiator
1654 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1655 NestedNameSpecifier *NNS =
1656 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1657 D->getTargetNestedNameRange(),
1658 TemplateArgs);
1659 if (!NNS)
1660 return 0;
1661
1662 CXXScopeSpec SS;
1663 SS.setRange(D->getTargetNestedNameRange());
1664 SS.setScopeRep(NNS);
1665
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001666 DeclarationNameInfo NameInfo
1667 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1668
John McCall7ba107a2009-11-18 02:36:19 +00001669 NamedDecl *UD =
1670 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001671 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001672 /*instantiation*/ true,
1673 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001674 if (UD)
John McCalled976492009-12-04 22:46:56 +00001675 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1676
Anders Carlsson0d8df782009-08-29 19:37:28 +00001677 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001678}
1679
John McCallce3ff2b2009-08-25 22:02:44 +00001680Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001681 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001682 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001683 if (D->isInvalidDecl())
1684 return 0;
1685
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001686 return Instantiator.Visit(D);
1687}
1688
John McCalle29ba202009-08-20 01:44:21 +00001689/// \brief Instantiates a nested template parameter list in the current
1690/// instantiation context.
1691///
1692/// \param L The parameter list to instantiate
1693///
1694/// \returns NULL if there was an error
1695TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001696TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001697 // Get errors for all the parameters before bailing out.
1698 bool Invalid = false;
1699
1700 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001701 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001702 ParamVector Params;
1703 Params.reserve(N);
1704 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1705 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001706 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001707 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001708 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001709 }
1710
1711 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001712 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001713 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001714
1715 TemplateParameterList *InstL
1716 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1717 L->getLAngleLoc(), &Params.front(), N,
1718 L->getRAngleLoc());
1719 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001720}
John McCalle29ba202009-08-20 01:44:21 +00001721
Douglas Gregored9c0f92009-10-29 00:04:11 +00001722/// \brief Instantiate the declaration of a class template partial
1723/// specialization.
1724///
1725/// \param ClassTemplate the (instantiated) class template that is partially
1726// specialized by the instantiation of \p PartialSpec.
1727///
1728/// \param PartialSpec the (uninstantiated) class template partial
1729/// specialization that we are instantiating.
1730///
1731/// \returns true if there was an error, false otherwise.
1732bool
1733TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1734 ClassTemplateDecl *ClassTemplate,
1735 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001736 // Create a local instantiation scope for this class template partial
1737 // specialization, which will contain the instantiations of the template
1738 // parameters.
1739 Sema::LocalInstantiationScope Scope(SemaRef);
1740
Douglas Gregored9c0f92009-10-29 00:04:11 +00001741 // Substitute into the template parameters of the class template partial
1742 // specialization.
1743 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1744 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1745 if (!InstParams)
1746 return true;
1747
1748 // Substitute into the template arguments of the class template partial
1749 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001750 const TemplateArgumentLoc *PartialSpecTemplateArgs
1751 = PartialSpec->getTemplateArgsAsWritten();
1752 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1753
John McCalld5532b62009-11-23 01:53:49 +00001754 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001755 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001756 TemplateArgumentLoc Loc;
1757 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001758 return true;
John McCalld5532b62009-11-23 01:53:49 +00001759 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001760 }
1761
1762
1763 // Check that the template argument list is well-formed for this
1764 // class template.
1765 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1766 InstTemplateArgs.size());
1767 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1768 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001769 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001770 false,
1771 Converted))
1772 return true;
1773
1774 // Figure out where to insert this class template partial specialization
1775 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001776 void *InsertPos = 0;
1777 ClassTemplateSpecializationDecl *PrevDecl
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001778 = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1779 Converted.flatSize(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001780
1781 // Build the canonical type that describes the converted template
1782 // arguments of the class template partial specialization.
1783 QualType CanonType
1784 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1785 Converted.getFlatArguments(),
1786 Converted.flatSize());
1787
1788 // Build the fully-sugared type for this class template
1789 // specialization as the user wrote in the specialization
1790 // itself. This means that we'll pretty-print the type retrieved
1791 // from the specialization's declaration the way that the user
1792 // actually wrote the specialization, rather than formatting the
1793 // name based on the "canonical" representation used to store the
1794 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001795 TypeSourceInfo *WrittenTy
1796 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1797 TemplateName(ClassTemplate),
1798 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001799 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001800 CanonType);
1801
1802 if (PrevDecl) {
1803 // We've already seen a partial specialization with the same template
1804 // parameters and template arguments. This can happen, for example, when
1805 // substituting the outer template arguments ends up causing two
1806 // class template partial specializations of a member class template
1807 // to have identical forms, e.g.,
1808 //
1809 // template<typename T, typename U>
1810 // struct Outer {
1811 // template<typename X, typename Y> struct Inner;
1812 // template<typename Y> struct Inner<T, Y>;
1813 // template<typename Y> struct Inner<U, Y>;
1814 // };
1815 //
1816 // Outer<int, int> outer; // error: the partial specializations of Inner
1817 // // have the same signature.
1818 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1819 << WrittenTy;
1820 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1821 << SemaRef.Context.getTypeDeclType(PrevDecl);
1822 return true;
1823 }
1824
1825
1826 // Create the class template partial specialization declaration.
1827 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001828 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1829 PartialSpec->getTagKind(),
1830 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001831 PartialSpec->getLocation(),
1832 InstParams,
1833 ClassTemplate,
1834 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001835 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001836 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001837 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001838 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001839 // Substitute the nested name specifier, if any.
1840 if (SubstQualifier(PartialSpec, InstPartialSpec))
1841 return 0;
1842
Douglas Gregored9c0f92009-10-29 00:04:11 +00001843 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001844 InstPartialSpec->setTypeAsWritten(WrittenTy);
1845
Douglas Gregored9c0f92009-10-29 00:04:11 +00001846 // Add this partial specialization to the set of class template partial
1847 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001848 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001849 return false;
1850}
1851
John McCall21ef0fa2010-03-11 09:03:00 +00001852TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001853TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001854 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001855 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1856 assert(OldTInfo && "substituting function without type source info");
1857 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001858 TypeSourceInfo *NewTInfo
1859 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1860 D->getTypeSpecStartLoc(),
1861 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001862 if (!NewTInfo)
1863 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001864
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001865 if (NewTInfo != OldTInfo) {
1866 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001867 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001868 if (FunctionProtoTypeLoc *OldProtoLoc
1869 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1870 TypeLoc NewTL = NewTInfo->getTypeLoc();
1871 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1872 assert(NewProtoLoc && "Missing prototype?");
1873 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1874 // FIXME: Variadic templates will break this.
1875 Params.push_back(NewProtoLoc->getArg(i));
1876 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001877 OldProtoLoc->getArg(i),
1878 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001879 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001880 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001881 } else {
1882 // The function type itself was not dependent and therefore no
1883 // substitution occurred. However, we still need to instantiate
1884 // the function parameters themselves.
1885 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001886 if (FunctionProtoTypeLoc *OldProtoLoc
1887 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1888 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1889 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1890 if (!Parm)
1891 return 0;
1892 Params.push_back(Parm);
1893 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001894 }
1895 }
John McCall21ef0fa2010-03-11 09:03:00 +00001896 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001897}
1898
Mike Stump1eb44332009-09-09 15:08:12 +00001899/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001900/// declaration (New) from the corresponding fields of its template (Tmpl).
1901///
1902/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001903bool
1904TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001905 FunctionDecl *Tmpl) {
1906 if (Tmpl->isDeleted())
1907 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Douglas Gregorcca9e962009-07-01 22:01:06 +00001909 // If we are performing substituting explicitly-specified template arguments
1910 // or deduced template arguments into a function template and we reach this
1911 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001912 // to keeping the new function template specialization. We therefore
1913 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001914 // into a template instantiation for this specific function template
1915 // specialization, which is not a SFINAE context, so that we diagnose any
1916 // further errors in the declaration itself.
1917 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1918 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1919 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1920 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001922 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001923 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001924 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001925 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001926 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1927 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001928 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001929 }
1930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001932 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1933 assert(Proto && "Function template without prototype?");
1934
1935 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1936 Proto->getNoReturnAttr()) {
1937 // The function has an exception specification or a "noreturn"
1938 // attribute. Substitute into each of the exception types.
1939 llvm::SmallVector<QualType, 4> Exceptions;
1940 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1941 // FIXME: Poor location information!
1942 QualType T
1943 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1944 New->getLocation(), New->getDeclName());
1945 if (T.isNull() ||
1946 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1947 continue;
1948
1949 Exceptions.push_back(T);
1950 }
1951
1952 // Rebuild the function type
1953
1954 const FunctionProtoType *NewProto
1955 = New->getType()->getAs<FunctionProtoType>();
1956 assert(NewProto && "Template instantiation without function prototype?");
1957 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1958 NewProto->arg_type_begin(),
1959 NewProto->getNumArgs(),
1960 NewProto->isVariadic(),
1961 NewProto->getTypeQuals(),
1962 Proto->hasExceptionSpec(),
1963 Proto->hasAnyExceptionSpec(),
1964 Exceptions.size(),
1965 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001966 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001967 }
1968
John McCall1d8d1cc2010-08-01 02:01:53 +00001969 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001970
Douglas Gregore53060f2009-06-25 22:08:12 +00001971 return false;
1972}
1973
Douglas Gregor5545e162009-03-24 00:38:23 +00001974/// \brief Initializes common fields of an instantiated method
1975/// declaration (New) from the corresponding fields of its template
1976/// (Tmpl).
1977///
1978/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001979bool
1980TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001981 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001982 if (InitFunctionInstantiation(New, Tmpl))
1983 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001984
Douglas Gregor5545e162009-03-24 00:38:23 +00001985 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1986 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001987 if (Tmpl->isVirtualAsWritten())
1988 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001989
1990 // FIXME: attributes
1991 // FIXME: New needs a pointer to Tmpl
1992 return false;
1993}
Douglas Gregora58861f2009-05-13 20:28:22 +00001994
1995/// \brief Instantiate the definition of the given function from its
1996/// template.
1997///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001998/// \param PointOfInstantiation the point at which the instantiation was
1999/// required. Note that this is not precisely a "point of instantiation"
2000/// for the function, but it's close.
2001///
Douglas Gregora58861f2009-05-13 20:28:22 +00002002/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002003/// function template specialization or member function of a class template
2004/// specialization.
2005///
2006/// \param Recursive if true, recursively instantiates any functions that
2007/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002008///
2009/// \param DefinitionRequired if true, then we are performing an explicit
2010/// instantiation where the body of the function is required. Complain if
2011/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002012void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002013 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002014 bool Recursive,
2015 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002016 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002017 return;
2018
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002019 // Never instantiate an explicit specialization.
2020 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2021 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002022
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002023 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002024 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002025 Stmt *Pattern = 0;
2026 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002027 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002028
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002029 if (!Pattern) {
2030 if (DefinitionRequired) {
2031 if (Function->getPrimaryTemplate())
2032 Diag(PointOfInstantiation,
2033 diag::err_explicit_instantiation_undefined_func_template)
2034 << Function->getPrimaryTemplate();
2035 else
2036 Diag(PointOfInstantiation,
2037 diag::err_explicit_instantiation_undefined_member)
2038 << 1 << Function->getDeclName() << Function->getDeclContext();
2039
2040 if (PatternDecl)
2041 Diag(PatternDecl->getLocation(),
2042 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002043 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002044 }
2045
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002046 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002047 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002048
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002049 // C++0x [temp.explicit]p9:
2050 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002051 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002052 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002053 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002054 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002055 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002056 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002058 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2059 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002060 return;
2061
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002062 // If we're performing recursive template instantiation, create our own
2063 // queue of pending implicit instantiations that we will instantiate later,
2064 // while we're still within our own instantiation context.
2065 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2066 if (Recursive)
2067 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Douglas Gregor9679caf2010-05-12 17:27:19 +00002069 EnterExpressionEvaluationContext EvalContext(*this,
2070 Action::PotentiallyEvaluated);
John McCalld226f652010-08-21 09:40:31 +00002071 ActOnStartOfFunctionDef(0, Function);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002072
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002073 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002074 // recorded, unless we're actually a member function within a local
2075 // class, in which case we need to merge our results with the parent
2076 // scope (of the enclosing function).
2077 bool MergeWithParentScope = false;
2078 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2079 MergeWithParentScope = Rec->isLocalClass();
2080
2081 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002083 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002084 // instantiation scope, and set the parameter names to those used
2085 // in the template.
2086 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2087 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2088 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2089 FunctionParam->setDeclName(PatternParam->getDeclName());
2090 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2091 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002092
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002093 // Enter the scope of this instantiation. We don't use
2094 // PushDeclContext because we don't have a scope.
2095 DeclContext *PreviousContext = CurContext;
2096 CurContext = Function;
2097
Mike Stump1eb44332009-09-09 15:08:12 +00002098 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002099 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002100
2101 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002102 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002103 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2104 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2105 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002106 }
2107
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002108 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002109 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002110
Douglas Gregor52604ab2009-09-11 21:19:12 +00002111 if (Body.isInvalid())
2112 Function->setInvalidDecl();
2113
John McCall9ae2f072010-08-23 23:25:46 +00002114 ActOnFinishFunctionBody(Function, Body.get(),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002115 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002116
John McCall0c01d182010-03-24 05:22:00 +00002117 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2118
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002119 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002120
2121 DeclGroupRef DG(Function);
2122 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Douglas Gregor60406be2010-01-16 22:29:39 +00002124 // This class may have local implicit instantiations that need to be
2125 // instantiation within this scope.
2126 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2127 Scope.Exit();
2128
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002129 if (Recursive) {
2130 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002131 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002132 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002134 // Restore the set of pending implicit instantiations.
2135 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2136 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002137}
2138
2139/// \brief Instantiate the definition of the given variable from its
2140/// template.
2141///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002142/// \param PointOfInstantiation the point at which the instantiation was
2143/// required. Note that this is not precisely a "point of instantiation"
2144/// for the function, but it's close.
2145///
2146/// \param Var the already-instantiated declaration of a static member
2147/// variable of a class template specialization.
2148///
2149/// \param Recursive if true, recursively instantiates any functions that
2150/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002151///
2152/// \param DefinitionRequired if true, then we are performing an explicit
2153/// instantiation where an out-of-line definition of the member variable
2154/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002155void Sema::InstantiateStaticDataMemberDefinition(
2156 SourceLocation PointOfInstantiation,
2157 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002158 bool Recursive,
2159 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002160 if (Var->isInvalidDecl())
2161 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002162
Douglas Gregor7caa6822009-07-24 20:34:43 +00002163 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002164 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002165 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002166 assert(Def->isStaticDataMember() && "Not a static data member?");
2167 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Douglas Gregor0d035142009-10-27 18:42:08 +00002169 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002170 // We did not find an out-of-line definition of this static data member,
2171 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002172 // instantiate this definition (or provide a specialization for it) in
2173 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002174 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002175 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002176 Diag(PointOfInstantiation,
2177 diag::err_explicit_instantiation_undefined_member)
2178 << 2 << Var->getDeclName() << Var->getDeclContext();
2179 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2180 }
2181
Douglas Gregor7caa6822009-07-24 20:34:43 +00002182 return;
2183 }
2184
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002185 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002186 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002187 return;
2188
2189 // C++0x [temp.explicit]p9:
2190 // Except for inline functions, other explicit instantiation declarations
2191 // have the effect of suppressing the implicit instantiation of the entity
2192 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002193 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002194 == TSK_ExplicitInstantiationDeclaration)
2195 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Douglas Gregor7caa6822009-07-24 20:34:43 +00002197 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2198 if (Inst)
2199 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Douglas Gregor7caa6822009-07-24 20:34:43 +00002201 // If we're performing recursive template instantiation, create our own
2202 // queue of pending implicit instantiations that we will instantiate later,
2203 // while we're still within our own instantiation context.
2204 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2205 if (Recursive)
2206 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Douglas Gregor7caa6822009-07-24 20:34:43 +00002208 // Enter the scope of this instantiation. We don't use
2209 // PushDeclContext because we don't have a scope.
2210 DeclContext *PreviousContext = CurContext;
2211 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002213 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002214 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002215 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002216 CurContext = PreviousContext;
2217
2218 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002219 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2220 assert(MSInfo && "Missing member specialization information?");
2221 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2222 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002223 DeclGroupRef DG(Var);
2224 Consumer.HandleTopLevelDecl(DG);
2225 }
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Douglas Gregor7caa6822009-07-24 20:34:43 +00002227 if (Recursive) {
2228 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002229 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002230 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002231
Douglas Gregor7caa6822009-07-24 20:34:43 +00002232 // Restore the set of pending implicit instantiations.
2233 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002234 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002235}
Douglas Gregor815215d2009-05-27 05:35:12 +00002236
Anders Carlsson09025312009-08-29 05:16:22 +00002237void
2238Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2239 const CXXConstructorDecl *Tmpl,
2240 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Anders Carlsson09025312009-08-29 05:16:22 +00002242 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002243 bool AnyErrors = false;
2244
Anders Carlsson09025312009-08-29 05:16:22 +00002245 // Instantiate all the initializers.
2246 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002247 InitsEnd = Tmpl->init_end();
2248 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002249 CXXBaseOrMemberInitializer *Init = *Inits;
2250
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002251 SourceLocation LParenLoc, RParenLoc;
John McCallca0408f2010-08-23 06:44:23 +00002252 ASTOwningVector<Expr*> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002253 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002254
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002255 // Instantiate the initializer.
2256 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2257 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2258 AnyErrors = true;
2259 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002260 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002261
Anders Carlsson09025312009-08-29 05:16:22 +00002262 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002263 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002264 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002265 TemplateArgs,
2266 Init->getSourceLocation(),
2267 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002268 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002269 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002270 New->setInvalidDecl();
2271 continue;
2272 }
2273
John McCalla93c9342009-12-07 02:54:59 +00002274 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002275 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002276 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002277 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002278 Init->getRParenLoc(),
2279 New->getParent());
2280 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002281 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002282
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002283 // Is this an anonymous union?
2284 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002285 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2286 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002287 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002288 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2289 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002290 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002291
2292 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002293 NewArgs.size(),
2294 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002295 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002296 Init->getRParenLoc());
2297 }
2298
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002299 if (NewInit.isInvalid()) {
2300 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002301 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002302 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002303 // FIXME: It would be nice if ASTOwningVector had a release function.
2304 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Anders Carlsson09025312009-08-29 05:16:22 +00002306 NewInits.push_back((MemInitTy *)NewInit.get());
2307 }
2308 }
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Anders Carlsson09025312009-08-29 05:16:22 +00002310 // Assign all the initializers to the new constructor.
John McCalld226f652010-08-21 09:40:31 +00002311 ActOnMemInitializers(New,
Anders Carlsson09025312009-08-29 05:16:22 +00002312 /*FIXME: ColonLoc */
2313 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002314 NewInits.data(), NewInits.size(),
2315 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002316}
2317
John McCall52a575a2009-08-29 08:11:13 +00002318// TODO: this could be templated if the various decl types used the
2319// same method name.
2320static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2321 ClassTemplateDecl *Instance) {
2322 Pattern = Pattern->getCanonicalDecl();
2323
2324 do {
2325 Instance = Instance->getCanonicalDecl();
2326 if (Pattern == Instance) return true;
2327 Instance = Instance->getInstantiatedFromMemberTemplate();
2328 } while (Instance);
2329
2330 return false;
2331}
2332
Douglas Gregor0d696532009-09-28 06:34:35 +00002333static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2334 FunctionTemplateDecl *Instance) {
2335 Pattern = Pattern->getCanonicalDecl();
2336
2337 do {
2338 Instance = Instance->getCanonicalDecl();
2339 if (Pattern == Instance) return true;
2340 Instance = Instance->getInstantiatedFromMemberTemplate();
2341 } while (Instance);
2342
2343 return false;
2344}
2345
Douglas Gregored9c0f92009-10-29 00:04:11 +00002346static bool
2347isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2348 ClassTemplatePartialSpecializationDecl *Instance) {
2349 Pattern
2350 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2351 do {
2352 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2353 Instance->getCanonicalDecl());
2354 if (Pattern == Instance)
2355 return true;
2356 Instance = Instance->getInstantiatedFromMember();
2357 } while (Instance);
2358
2359 return false;
2360}
2361
John McCall52a575a2009-08-29 08:11:13 +00002362static bool isInstantiationOf(CXXRecordDecl *Pattern,
2363 CXXRecordDecl *Instance) {
2364 Pattern = Pattern->getCanonicalDecl();
2365
2366 do {
2367 Instance = Instance->getCanonicalDecl();
2368 if (Pattern == Instance) return true;
2369 Instance = Instance->getInstantiatedFromMemberClass();
2370 } while (Instance);
2371
2372 return false;
2373}
2374
2375static bool isInstantiationOf(FunctionDecl *Pattern,
2376 FunctionDecl *Instance) {
2377 Pattern = Pattern->getCanonicalDecl();
2378
2379 do {
2380 Instance = Instance->getCanonicalDecl();
2381 if (Pattern == Instance) return true;
2382 Instance = Instance->getInstantiatedFromMemberFunction();
2383 } while (Instance);
2384
2385 return false;
2386}
2387
2388static bool isInstantiationOf(EnumDecl *Pattern,
2389 EnumDecl *Instance) {
2390 Pattern = Pattern->getCanonicalDecl();
2391
2392 do {
2393 Instance = Instance->getCanonicalDecl();
2394 if (Pattern == Instance) return true;
2395 Instance = Instance->getInstantiatedFromMemberEnum();
2396 } while (Instance);
2397
2398 return false;
2399}
2400
John McCalled976492009-12-04 22:46:56 +00002401static bool isInstantiationOf(UsingShadowDecl *Pattern,
2402 UsingShadowDecl *Instance,
2403 ASTContext &C) {
2404 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2405}
2406
2407static bool isInstantiationOf(UsingDecl *Pattern,
2408 UsingDecl *Instance,
2409 ASTContext &C) {
2410 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2411}
2412
John McCall7ba107a2009-11-18 02:36:19 +00002413static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2414 UsingDecl *Instance,
2415 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002416 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002417}
2418
2419static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002420 UsingDecl *Instance,
2421 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002422 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002423}
2424
John McCall52a575a2009-08-29 08:11:13 +00002425static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2426 VarDecl *Instance) {
2427 assert(Instance->isStaticDataMember());
2428
2429 Pattern = Pattern->getCanonicalDecl();
2430
2431 do {
2432 Instance = Instance->getCanonicalDecl();
2433 if (Pattern == Instance) return true;
2434 Instance = Instance->getInstantiatedFromStaticDataMember();
2435 } while (Instance);
2436
2437 return false;
2438}
2439
John McCalled976492009-12-04 22:46:56 +00002440// Other is the prospective instantiation
2441// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002442static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002443 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002444 if (UnresolvedUsingTypenameDecl *UUD
2445 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2446 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2447 return isInstantiationOf(UUD, UD, Ctx);
2448 }
2449 }
2450
2451 if (UnresolvedUsingValueDecl *UUD
2452 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002453 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2454 return isInstantiationOf(UUD, UD, Ctx);
2455 }
2456 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002457
Anders Carlsson0d8df782009-08-29 19:37:28 +00002458 return false;
2459 }
Mike Stump1eb44332009-09-09 15:08:12 +00002460
John McCall52a575a2009-08-29 08:11:13 +00002461 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2462 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002463
John McCall52a575a2009-08-29 08:11:13 +00002464 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2465 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002466
John McCall52a575a2009-08-29 08:11:13 +00002467 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2468 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002469
Douglas Gregor7caa6822009-07-24 20:34:43 +00002470 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002471 if (Var->isStaticDataMember())
2472 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2473
2474 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2475 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002476
Douglas Gregor0d696532009-09-28 06:34:35 +00002477 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2478 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2479
Douglas Gregored9c0f92009-10-29 00:04:11 +00002480 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2481 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2482 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2483 PartialSpec);
2484
Anders Carlssond8b285f2009-09-01 04:26:58 +00002485 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2486 if (!Field->getDeclName()) {
2487 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002488 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002489 cast<FieldDecl>(D);
2490 }
2491 }
Mike Stump1eb44332009-09-09 15:08:12 +00002492
John McCalled976492009-12-04 22:46:56 +00002493 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2494 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2495
2496 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2497 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2498
Douglas Gregor815215d2009-05-27 05:35:12 +00002499 return D->getDeclName() && isa<NamedDecl>(Other) &&
2500 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2501}
2502
2503template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002504static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002505 NamedDecl *D,
2506 ForwardIterator first,
2507 ForwardIterator last) {
2508 for (; first != last; ++first)
2509 if (isInstantiationOf(Ctx, D, *first))
2510 return cast<NamedDecl>(*first);
2511
2512 return 0;
2513}
2514
John McCall02cace72009-08-28 07:59:38 +00002515/// \brief Finds the instantiation of the given declaration context
2516/// within the current instantiation.
2517///
2518/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002519DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002520 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002521 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002522 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002523 return cast_or_null<DeclContext>(ID);
2524 } else return DC;
2525}
2526
Douglas Gregored961e72009-05-27 17:54:46 +00002527/// \brief Find the instantiation of the given declaration within the
2528/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002529///
2530/// This routine is intended to be used when \p D is a declaration
2531/// referenced from within a template, that needs to mapped into the
2532/// corresponding declaration within an instantiation. For example,
2533/// given:
2534///
2535/// \code
2536/// template<typename T>
2537/// struct X {
2538/// enum Kind {
2539/// KnownValue = sizeof(T)
2540/// };
2541///
2542/// bool getKind() const { return KnownValue; }
2543/// };
2544///
2545/// template struct X<int>;
2546/// \endcode
2547///
2548/// In the instantiation of X<int>::getKind(), we need to map the
2549/// EnumConstantDecl for KnownValue (which refers to
2550/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002551/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2552/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002553NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002554 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002555 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002556 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002557 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
John McCall76672452010-08-19 23:06:02 +00002558 (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002559 // D is a local of some kind. Look into the map of local
2560 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002561 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002562 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002563
Douglas Gregore95b4092009-09-16 18:34:49 +00002564 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2565 if (!Record->isDependentContext())
2566 return D;
2567
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002568 // If the RecordDecl is actually the injected-class-name or a
2569 // "templated" declaration for a class template, class template
2570 // partial specialization, or a member class of a class template,
2571 // substitute into the injected-class-name of the class template
2572 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002573 QualType T;
2574 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2575
2576 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002577 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002578 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2579 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002580 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002581
2582 // If we call SubstType with an InjectedClassNameType here we
2583 // can end up in an infinite loop.
2584 T = Context.getTypeDeclType(Record);
2585 assert(isa<InjectedClassNameType>(T) &&
2586 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002587 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002588 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002589
2590 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002591 // Substitute into the injected-class-name to get the type
2592 // corresponding to the instantiation we want, which may also be
2593 // the current instantiation (if we're in a template
2594 // definition). This substitution should never fail, since we
2595 // know we can instantiate the injected-class-name or we
2596 // wouldn't have gotten to the injected-class-name!
2597
2598 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2599 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002600 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2601 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2602
2603 if (!T->isDependentType()) {
2604 assert(T->isRecordType() && "Instantiation must produce a record type");
2605 return T->getAs<RecordType>()->getDecl();
2606 }
2607
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002608 // We are performing "partial" template instantiation to create
2609 // the member declarations for the members of a class template
2610 // specialization. Therefore, D is actually referring to something
2611 // in the current instantiation. Look through the current
2612 // context, which contains actual instantiations, to find the
2613 // instantiation of the "current instantiation" that D refers
2614 // to.
2615 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002616 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002617 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002618 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002619 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002620 if (isInstantiationOf(ClassTemplate,
2621 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002622 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002623
2624 if (!DC->isDependentContext())
2625 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002626 }
2627
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002628 // We're performing "instantiation" of a member of the current
2629 // instantiation while we are type-checking the
2630 // definition. Compute the declaration context and return that.
2631 assert(!SawNonDependentContext &&
2632 "No dependent context while instantiating record");
2633 DeclContext *DC = computeDeclContext(T);
2634 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002635 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002636 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002637 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002638
Douglas Gregore95b4092009-09-16 18:34:49 +00002639 // Fall through to deal with other dependent record types (e.g.,
2640 // anonymous unions in class templates).
2641 }
John McCall52a575a2009-08-29 08:11:13 +00002642
Douglas Gregore95b4092009-09-16 18:34:49 +00002643 if (!ParentDC->isDependentContext())
2644 return D;
2645
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002646 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002647 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002648 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Douglas Gregor815215d2009-05-27 05:35:12 +00002650 if (ParentDC != D->getDeclContext()) {
2651 // We performed some kind of instantiation in the parent context,
2652 // so now we need to look into the instantiated parent context to
2653 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002654
John McCall3cb0ebd2010-03-10 03:28:59 +00002655 // If our context used to be dependent, we may need to instantiate
2656 // it before performing lookup into that context.
2657 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002658 if (!Spec->isDependentContext()) {
2659 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002660 const RecordType *Tag = T->getAs<RecordType>();
2661 assert(Tag && "type of non-dependent record is not a RecordType");
2662 if (!Tag->isBeingDefined() &&
2663 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2664 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002665 }
2666 }
2667
Douglas Gregor815215d2009-05-27 05:35:12 +00002668 NamedDecl *Result = 0;
2669 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002670 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002671 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2672 } else {
2673 // Since we don't have a name for the entity we're looking for,
2674 // our only option is to walk through all of the declarations to
2675 // find that name. This will occur in a few cases:
2676 //
2677 // - anonymous struct/union within a template
2678 // - unnamed class/struct/union/enum within a template
2679 //
2680 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002681 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002682 ParentDC->decls_begin(),
2683 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002684 }
Mike Stump1eb44332009-09-09 15:08:12 +00002685
John McCall9f54ad42009-12-10 09:41:52 +00002686 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002687 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2688 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002689 && "Unable to find instantiation of declaration!");
2690
Douglas Gregor815215d2009-05-27 05:35:12 +00002691 D = Result;
2692 }
2693
Douglas Gregor815215d2009-05-27 05:35:12 +00002694 return D;
2695}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002696
Mike Stump1eb44332009-09-09 15:08:12 +00002697/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002698/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002699void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2700 while (!PendingLocalImplicitInstantiations.empty() ||
2701 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2702 PendingImplicitInstantiation Inst;
2703
2704 if (PendingLocalImplicitInstantiations.empty()) {
2705 Inst = PendingImplicitInstantiations.front();
2706 PendingImplicitInstantiations.pop_front();
2707 } else {
2708 Inst = PendingLocalImplicitInstantiations.front();
2709 PendingLocalImplicitInstantiations.pop_front();
2710 }
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Douglas Gregor7caa6822009-07-24 20:34:43 +00002712 // Instantiate function definitions
2713 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
John McCalld226f652010-08-21 09:40:31 +00002714 PrettyStackTraceActionsDecl CrashInfo(Function,
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002715 Function->getLocation(), *this,
2716 Context.getSourceManager(),
2717 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002719 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002720 continue;
2721 }
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregor7caa6822009-07-24 20:34:43 +00002723 // Instantiate static data member definitions.
2724 VarDecl *Var = cast<VarDecl>(Inst.first);
2725 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002726
Chandler Carruth291b4412010-02-13 10:17:50 +00002727 // Don't try to instantiate declarations if the most recent redeclaration
2728 // is invalid.
2729 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2730 continue;
2731
2732 // Check if the most recent declaration has changed the specialization kind
2733 // and removed the need for implicit instantiation.
2734 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2735 case TSK_Undeclared:
2736 assert(false && "Cannot instantitiate an undeclared specialization.");
2737 case TSK_ExplicitInstantiationDeclaration:
2738 case TSK_ExplicitInstantiationDefinition:
2739 case TSK_ExplicitSpecialization:
2740 continue; // No longer need implicit instantiation.
2741 case TSK_ImplicitInstantiation:
2742 break;
2743 }
2744
John McCalld226f652010-08-21 09:40:31 +00002745 PrettyStackTraceActionsDecl CrashInfo(Var, Var->getLocation(), *this,
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002746 Context.getSourceManager(),
2747 "instantiating static data member "
2748 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002749
Douglas Gregor7caa6822009-07-24 20:34:43 +00002750 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002751 }
2752}
John McCall0c01d182010-03-24 05:22:00 +00002753
2754void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
2755 const MultiLevelTemplateArgumentList &TemplateArgs) {
2756 for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(),
2757 E = Pattern->ddiag_end(); I != E; ++I) {
2758 DependentDiagnostic *DD = *I;
2759
2760 switch (DD->getKind()) {
2761 case DependentDiagnostic::Access:
2762 HandleDependentAccessCheck(*DD, TemplateArgs);
2763 break;
2764 }
2765 }
2766}