blob: cbdd9d51c20319985fc760a2d8df0bdbf87930ee [file] [log] [blame]
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001//===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation for declarations.
10//
11//===----------------------------------------------------------------------===/
12#include "Sema.h"
John McCall7d384dd2009-11-18 07:57:50 +000013#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/DeclVisitor.h"
John McCall0c01d182010-03-24 05:22:00 +000018#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000019#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000020#include "clang/AST/ExprCXX.h"
John McCall21ef0fa2010-03-11 09:03:00 +000021#include "clang/AST/TypeLoc.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000022#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000023#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000024
25using namespace clang;
26
27namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000028 class TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000029 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000030 Sema &SemaRef;
31 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000032 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Douglas 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
John McCallfd810b12009-08-14 02:03:10 +000088 const LangOptions &getLangOptions() {
89 return SemaRef.getLangOptions();
90 }
91
Douglas Gregor5545e162009-03-24 00:38:23 +000092 // Helper functions for instantiating methods.
John McCall21ef0fa2010-03-11 09:03:00 +000093 TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000094 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000095 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000096 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000097
98 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000099 SubstTemplateParams(TemplateParameterList *List);
John McCallb6217662010-03-15 10:12:16 +0000100
101 bool SubstQualifier(const DeclaratorDecl *OldDecl,
102 DeclaratorDecl *NewDecl);
103 bool SubstQualifier(const TagDecl *OldDecl,
104 TagDecl *NewDecl);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000105
106 bool InstantiateClassTemplatePartialSpecialization(
107 ClassTemplateDecl *ClassTemplate,
108 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000109 };
110}
111
John McCallb6217662010-03-15 10:12:16 +0000112bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
113 DeclaratorDecl *NewDecl) {
114 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
115 if (!OldQual) return false;
116
117 SourceRange QualRange = OldDecl->getQualifierRange();
118
119 NestedNameSpecifier *NewQual
120 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
121 if (!NewQual)
122 return true;
123
124 NewDecl->setQualifierInfo(NewQual, QualRange);
125 return false;
126}
127
128bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
129 TagDecl *NewDecl) {
130 NestedNameSpecifier *OldQual = OldDecl->getQualifier();
131 if (!OldQual) return false;
132
133 SourceRange QualRange = OldDecl->getQualifierRange();
134
135 NestedNameSpecifier *NewQual
136 = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs);
137 if (!NewQual)
138 return true;
139
140 NewDecl->setQualifierInfo(NewQual, QualRange);
141 return false;
142}
143
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000144// FIXME: Is this still too simple?
John McCall1d8d1cc2010-08-01 02:01:53 +0000145void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
146 Decl *Tmpl, Decl *New) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000147 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000148 TmplAttr = TmplAttr->getNext()) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000149 // FIXME: This should be generalized to more than just the AlignedAttr.
150 if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) {
151 if (Aligned->isDependent()) {
152 // The alignment expression is not potentially evaluated.
John McCall1d8d1cc2010-08-01 02:01:53 +0000153 EnterExpressionEvaluationContext Unevaluated(*this,
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000154 Action::Unevaluated);
155
John McCall1d8d1cc2010-08-01 02:01:53 +0000156 OwningExprResult Result = SubstExpr(Aligned->getAlignmentExpr(),
157 TemplateArgs);
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000158 if (!Result.isInvalid())
159 // FIXME: Is this the correct source location?
John McCall1d8d1cc2010-08-01 02:01:53 +0000160 AddAlignedAttr(Aligned->getAlignmentExpr()->getExprLoc(),
161 New, Result.takeAs<Expr>());
Chandler Carruth4ced79f2010-06-25 03:22:07 +0000162 continue;
163 }
164 }
165
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000166 // FIXME: Is cloning correct for all attributes?
John McCall1d8d1cc2010-08-01 02:01:53 +0000167 Attr *NewAttr = TmplAttr->clone(Context);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000168 New->addAttr(NewAttr);
169 }
170}
171
Douglas Gregor4f722be2009-03-25 15:45:12 +0000172Decl *
173TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
174 assert(false && "Translation units cannot be instantiated");
175 return D;
176}
177
178Decl *
179TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
180 assert(false && "Namespaces cannot be instantiated");
181 return D;
182}
183
John McCall3dbd3d52010-02-16 06:53:13 +0000184Decl *
185TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
186 NamespaceAliasDecl *Inst
187 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
188 D->getNamespaceLoc(),
189 D->getAliasLoc(),
190 D->getNamespace()->getIdentifier(),
191 D->getQualifierRange(),
192 D->getQualifier(),
193 D->getTargetNameLoc(),
194 D->getNamespace());
195 Owner->addDecl(Inst);
196 return Inst;
197}
198
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000199Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
200 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000201 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000202 if (DI->getType()->isDependentType() ||
203 DI->getType()->isVariablyModifiedType()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000204 DI = SemaRef.SubstType(DI, TemplateArgs,
205 D->getLocation(), D->getDeclName());
206 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000207 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000208 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000209 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000210 } else {
211 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000214 // Create the new typedef
215 TypedefDecl *Typedef
216 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000217 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000218 if (Invalid)
219 Typedef->setInvalidDecl();
220
Douglas Gregord57a38e2010-04-23 16:25:07 +0000221 if (const TagType *TT = DI->getType()->getAs<TagType>()) {
222 TagDecl *TD = TT->getDecl();
223
224 // If the TagDecl that the TypedefDecl points to is an anonymous decl
225 // keep track of the TypedefDecl.
226 if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl())
227 TD->setTypedefForAnonDecl(Typedef);
228 }
229
John McCall5126fd02009-12-30 00:31:22 +0000230 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000231 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
232 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000233 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
234 }
235
John McCall1d8d1cc2010-08-01 02:01:53 +0000236 SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
Douglas Gregord57a38e2010-04-23 16:25:07 +0000237
John McCall46460a62010-01-20 21:53:11 +0000238 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000239 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000241 return Typedef;
242}
243
Douglas Gregor6eef5192009-12-14 19:27:10 +0000244/// \brief Instantiate the arguments provided as part of initialization.
245///
246/// \returns true if an error occurred, false otherwise.
247static bool InstantiateInitializationArguments(Sema &SemaRef,
248 Expr **Args, unsigned NumArgs,
249 const MultiLevelTemplateArgumentList &TemplateArgs,
250 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
251 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
252 for (unsigned I = 0; I != NumArgs; ++I) {
253 // When we hit the first defaulted argument, break out of the loop:
254 // we don't pass those default arguments on.
255 if (Args[I]->isDefaultArgument())
256 break;
257
258 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
259 if (Arg.isInvalid())
260 return true;
261
262 Expr *ArgExpr = (Expr *)Arg.get();
263 InitArgs.push_back(Arg.release());
264
265 // FIXME: We're faking all of the comma locations. Do we need them?
266 FakeCommaLocs.push_back(
267 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
268 }
269
270 return false;
271}
272
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000273/// \brief Instantiate an initializer, breaking it into separate
274/// initialization arguments.
275///
276/// \param S The semantic analysis object.
277///
278/// \param Init The initializer to instantiate.
279///
280/// \param TemplateArgs Template arguments to be substituted into the
281/// initializer.
282///
283/// \param NewArgs Will be filled in with the instantiation arguments.
284///
285/// \returns true if an error occurred, false otherwise
286static bool InstantiateInitializer(Sema &S, Expr *Init,
287 const MultiLevelTemplateArgumentList &TemplateArgs,
288 SourceLocation &LParenLoc,
289 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
290 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
291 SourceLocation &RParenLoc) {
292 NewArgs.clear();
293 LParenLoc = SourceLocation();
294 RParenLoc = SourceLocation();
295
296 if (!Init)
297 return false;
298
299 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
300 Init = ExprTemp->getSubExpr();
301
302 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
303 Init = Binder->getSubExpr();
304
305 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
306 Init = ICE->getSubExprAsWritten();
307
308 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
309 LParenLoc = ParenList->getLParenLoc();
310 RParenLoc = ParenList->getRParenLoc();
311 return InstantiateInitializationArguments(S, ParenList->getExprs(),
312 ParenList->getNumExprs(),
313 TemplateArgs, CommaLocs,
314 NewArgs);
315 }
316
317 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
Douglas Gregor28329e52010-03-24 21:22:47 +0000318 if (!isa<CXXTemporaryObjectExpr>(Construct)) {
319 if (InstantiateInitializationArguments(S,
320 Construct->getArgs(),
321 Construct->getNumArgs(),
322 TemplateArgs,
323 CommaLocs, NewArgs))
324 return true;
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000325
Douglas Gregor28329e52010-03-24 21:22:47 +0000326 // FIXME: Fake locations!
327 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
328 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
329 return false;
330 }
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000331 }
332
333 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
334 if (Result.isInvalid())
335 return true;
336
337 NewArgs.push_back(Result.takeAs<Expr>());
338 return false;
339}
340
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000341Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
Douglas Gregor9901c572010-05-21 00:31:19 +0000342 // If this is the variable for an anonymous struct or union,
343 // instantiate the anonymous struct/union type first.
344 if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
345 if (RecordTy->getDecl()->isAnonymousStructOrUnion())
346 if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
347 return 0;
348
John McCallce3ff2b2009-08-25 22:02:44 +0000349 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000350 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000351 TemplateArgs,
352 D->getTypeSpecStartLoc(),
353 D->getDeclName());
354 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000355 return 0;
356
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000357 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000358 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
359 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000360 DI->getType(), DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000361 D->getStorageClass(),
362 D->getStorageClassAsWritten());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000363 Var->setThreadSpecified(D->isThreadSpecified());
364 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
Mike Stump1eb44332009-09-09 15:08:12 +0000365
John McCallb6217662010-03-15 10:12:16 +0000366 // Substitute the nested name specifier, if any.
367 if (SubstQualifier(D, Var))
368 return 0;
369
Mike Stump1eb44332009-09-09 15:08:12 +0000370 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000371 // out-of-line, the instantiation will have the same lexical
372 // context (which will be a namespace scope) as the template.
373 if (D->isOutOfLine())
374 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCall46460a62010-01-20 21:53:11 +0000376 Var->setAccess(D->getAccess());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000377
378 if (!D->isStaticDataMember())
379 Var->setUsed(D->isUsed(false));
Douglas Gregor4469e8a2010-05-19 17:02:24 +0000380
Mike Stump390b4cc2009-05-16 07:39:55 +0000381 // FIXME: In theory, we could have a previous declaration for variables that
382 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000383 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000384 // FIXME: having to fake up a LookupResult is dumb.
385 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000386 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000387 if (D->isStaticDataMember())
388 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000389 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Douglas Gregor7caa6822009-07-24 20:34:43 +0000391 if (D->isOutOfLine()) {
Abramo Bagnaraea7b4882010-06-04 09:35:39 +0000392 if (!D->isStaticDataMember())
393 D->getLexicalDeclContext()->addDecl(Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000394 Owner->makeDeclVisibleInContext(Var);
395 } else {
396 Owner->addDecl(Var);
Douglas Gregorf7d72f52010-05-03 20:22:41 +0000397 if (Owner->isFunctionOrMethod())
398 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000399 }
John McCall1d8d1cc2010-08-01 02:01:53 +0000400 SemaRef.InstantiateAttrs(TemplateArgs, D, Var);
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +0000401
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000402 // Link instantiations of static data members back to the template from
403 // which they were instantiated.
404 if (Var->isStaticDataMember())
405 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000406 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000407
Douglas Gregor60c93c92010-02-09 07:26:29 +0000408 if (Var->getAnyInitializer()) {
409 // We already have an initializer in the class.
410 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000411 if (Var->isStaticDataMember() && !D->isOutOfLine())
412 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
413 else
414 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
415
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000416 // Instantiate the initializer.
417 SourceLocation LParenLoc, RParenLoc;
418 llvm::SmallVector<SourceLocation, 4> CommaLocs;
419 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
420 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
421 CommaLocs, InitArgs, RParenLoc)) {
422 // Attach the initializer to the declaration.
423 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000424 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000425 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000426 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000427 move_arg(InitArgs),
428 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000429 RParenLoc);
430 } else if (InitArgs.size() == 1) {
431 Expr *Init = (Expr*)(InitArgs.take()[0]);
432 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
433 SemaRef.Owned(Init),
434 false);
435 } else {
436 assert(InitArgs.size() == 0);
437 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000438 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000439 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000440 // FIXME: Not too happy about invalidating the declaration
441 // because of a bogus initializer.
442 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000443 }
444
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000445 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000446 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
447 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000448
Douglas Gregor5764f612010-05-08 23:05:03 +0000449 // Diagnose unused local variables.
450 if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed())
451 SemaRef.DiagnoseUnusedDecl(Var);
452
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000453 return Var;
454}
455
Abramo Bagnara6206d532010-06-05 05:09:32 +0000456Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
457 AccessSpecDecl* AD
458 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
459 D->getAccessSpecifierLoc(), D->getColonLoc());
460 Owner->addHiddenDecl(AD);
461 return AD;
462}
463
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000464Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
465 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000466 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor836adf62010-05-24 17:22:01 +0000467 if (DI->getType()->isDependentType() ||
468 DI->getType()->isVariablyModifiedType()) {
John McCall07fb6be2009-10-22 23:33:21 +0000469 DI = SemaRef.SubstType(DI, TemplateArgs,
470 D->getLocation(), D->getDeclName());
471 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000472 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000473 Invalid = true;
474 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000475 // C++ [temp.arg.type]p3:
476 // If a declaration acquires a function type through a type
477 // dependent on a template-parameter and this causes a
478 // declaration that does not use the syntactic form of a
479 // function declarator to have function type, the program is
480 // ill-formed.
481 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000482 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000483 Invalid = true;
484 }
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000485 } else {
486 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000487 }
488
489 Expr *BitWidth = D->getBitWidth();
490 if (Invalid)
491 BitWidth = 0;
492 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000493 // The bit-width expression is not potentially evaluated.
494 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000496 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000497 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000498 if (InstantiatedBitWidth.isInvalid()) {
499 Invalid = true;
500 BitWidth = 0;
501 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000502 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000503 }
504
John McCall07fb6be2009-10-22 23:33:21 +0000505 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
506 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000507 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000508 D->getLocation(),
509 D->isMutable(),
510 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000511 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000512 D->getAccess(),
513 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000514 if (!Field) {
515 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000516 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
John McCall1d8d1cc2010-08-01 02:01:53 +0000519 SemaRef.InstantiateAttrs(TemplateArgs, D, Field);
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000520
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000521 if (Invalid)
522 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000524 if (!Field->getDeclName()) {
525 // Keep track of where this decl came from.
526 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor9901c572010-05-21 00:31:19 +0000527 }
528 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
529 if (Parent->isAnonymousStructOrUnion() &&
530 Parent->getLookupContext()->isFunctionOrMethod())
531 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000534 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000535 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000536 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000537
538 return Field;
539}
540
John McCall02cace72009-08-28 07:59:38 +0000541Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
John McCall02cace72009-08-28 07:59:38 +0000542 // Handle friend type expressions by simply substituting template
Douglas Gregor06245bf2010-04-07 17:57:12 +0000543 // parameters into the pattern type and checking the result.
John McCall32f2fb52010-03-25 18:04:51 +0000544 if (TypeSourceInfo *Ty = D->getFriendType()) {
545 TypeSourceInfo *InstTy =
546 SemaRef.SubstType(Ty, TemplateArgs,
547 D->getLocation(), DeclarationName());
Douglas Gregor06245bf2010-04-07 17:57:12 +0000548 if (!InstTy)
Douglas Gregor7557a132009-12-24 20:56:24 +0000549 return 0;
John McCall02cace72009-08-28 07:59:38 +0000550
Douglas Gregor06245bf2010-04-07 17:57:12 +0000551 FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy);
552 if (!FD)
553 return 0;
554
555 FD->setAccess(AS_public);
556 Owner->addDecl(FD);
557 return FD;
558 }
559
560 NamedDecl *ND = D->getFriendDecl();
561 assert(ND && "friend decl must be a decl or a type!");
562
John McCallaf2094e2010-04-08 09:05:18 +0000563 // All of the Visit implementations for the various potential friend
564 // declarations have to be carefully written to work for friend
565 // objects, with the most important detail being that the target
566 // decl should almost certainly not be placed in Owner.
567 Decl *NewND = Visit(ND);
Douglas Gregor06245bf2010-04-07 17:57:12 +0000568 if (!NewND) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000569
John McCall02cace72009-08-28 07:59:38 +0000570 FriendDecl *FD =
Douglas Gregor06245bf2010-04-07 17:57:12 +0000571 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
572 cast<NamedDecl>(NewND), D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000573 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000574 Owner->addDecl(FD);
575 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000576}
577
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000578Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
579 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregorac7610d2009-06-22 20:57:11 +0000581 // The expression in a static assertion is not potentially evaluated.
582 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000584 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000585 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000586 if (InstantiatedAssertExpr.isInvalid())
587 return 0;
588
Douglas Gregor43d9d922009-08-08 01:41:12 +0000589 OwningExprResult Message(SemaRef, D->getMessage());
590 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000591 Decl *StaticAssert
592 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000593 move(InstantiatedAssertExpr),
594 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000595 return StaticAssert;
596}
597
598Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000600 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000601 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000602 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000603 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000604 Enum->setAccess(D->getAccess());
John McCallb6217662010-03-15 10:12:16 +0000605 if (SubstQualifier(D, Enum)) return 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000606 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000607 Enum->startDefinition();
608
Douglas Gregor96084f12010-03-01 19:00:07 +0000609 if (D->getDeclContext()->isFunctionOrMethod())
610 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
611
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000612 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000613
614 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000615 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
616 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000617 EC != ECEnd; ++EC) {
618 // The specified value for the enumerator.
619 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000620 if (Expr *UninstValue = EC->getInitExpr()) {
621 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000622 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000623 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
John McCallce3ff2b2009-08-25 22:02:44 +0000625 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000626 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000627
628 // Drop the initial value and continue.
629 bool isInvalid = false;
630 if (Value.isInvalid()) {
631 Value = SemaRef.Owned((Expr *)0);
632 isInvalid = true;
633 }
634
Mike Stump1eb44332009-09-09 15:08:12 +0000635 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000636 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
637 EC->getLocation(), EC->getIdentifier(),
638 move(Value));
639
640 if (isInvalid) {
641 if (EnumConst)
642 EnumConst->setInvalidDecl();
643 Enum->setInvalidDecl();
644 }
645
646 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000647 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000648 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000649 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000650 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000651
652 if (D->getDeclContext()->isFunctionOrMethod()) {
653 // If the enumeration is within a function or method, record the enum
654 // constant as a local.
655 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
656 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000657 }
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000660 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000661 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000662 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
663 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000664 &Enumerators[0], Enumerators.size(),
665 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000666
667 return Enum;
668}
669
Douglas Gregor6477b692009-03-25 15:04:13 +0000670Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
671 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
672 return 0;
673}
674
John McCalle29ba202009-08-20 01:44:21 +0000675Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
John McCall93ba8572010-03-25 06:39:04 +0000676 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
677
Douglas Gregor550d9b22009-10-31 17:21:17 +0000678 // Create a local instantiation scope for this class template, which
679 // will contain the instantiations of the template parameters.
680 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000681 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000682 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000683 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000684 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000685
686 CXXRecordDecl *Pattern = D->getTemplatedDecl();
John McCall93ba8572010-03-25 06:39:04 +0000687
688 // Instantiate the qualifier. We have to do this first in case
689 // we're a friend declaration, because if we are then we need to put
690 // the new declaration in the appropriate context.
691 NestedNameSpecifier *Qualifier = Pattern->getQualifier();
692 if (Qualifier) {
693 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
694 Pattern->getQualifierRange(),
695 TemplateArgs);
696 if (!Qualifier) return 0;
697 }
698
699 CXXRecordDecl *PrevDecl = 0;
700 ClassTemplateDecl *PrevClassTemplate = 0;
701
702 // If this isn't a friend, then it's a member template, in which
703 // case we just want to build the instantiation in the
704 // specialization. If it is a friend, we want to build it in
705 // the appropriate context.
706 DeclContext *DC = Owner;
707 if (isFriend) {
708 if (Qualifier) {
709 CXXScopeSpec SS;
710 SS.setScopeRep(Qualifier);
711 SS.setRange(Pattern->getQualifierRange());
712 DC = SemaRef.computeDeclContext(SS);
713 if (!DC) return 0;
714 } else {
715 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
716 Pattern->getDeclContext(),
717 TemplateArgs);
718 }
719
720 // Look for a previous declaration of the template in the owning
721 // context.
722 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
723 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
724 SemaRef.LookupQualifiedName(R, DC);
725
726 if (R.isSingleResult()) {
727 PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
728 if (PrevClassTemplate)
729 PrevDecl = PrevClassTemplate->getTemplatedDecl();
730 }
731
732 if (!PrevClassTemplate && Qualifier) {
733 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
Douglas Gregor1eabb7d2010-03-31 23:17:41 +0000734 << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
735 << Pattern->getQualifierRange();
John McCall93ba8572010-03-25 06:39:04 +0000736 return 0;
737 }
738
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000739 bool AdoptedPreviousTemplateParams = false;
John McCall93ba8572010-03-25 06:39:04 +0000740 if (PrevClassTemplate) {
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000741 bool Complain = true;
742
743 // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
744 // template for struct std::tr1::__detail::_Map_base, where the
745 // template parameters of the friend declaration don't match the
746 // template parameters of the original declaration. In this one
747 // case, we don't complain about the ill-formed friend
748 // declaration.
749 if (isFriend && Pattern->getIdentifier() &&
750 Pattern->getIdentifier()->isStr("_Map_base") &&
751 DC->isNamespace() &&
752 cast<NamespaceDecl>(DC)->getIdentifier() &&
753 cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
754 DeclContext *DCParent = DC->getParent();
755 if (DCParent->isNamespace() &&
756 cast<NamespaceDecl>(DCParent)->getIdentifier() &&
757 cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
758 DeclContext *DCParent2 = DCParent->getParent();
759 if (DCParent2->isNamespace() &&
760 cast<NamespaceDecl>(DCParent2)->getIdentifier() &&
761 cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") &&
762 DCParent2->getParent()->isTranslationUnit())
763 Complain = false;
764 }
765 }
766
John McCall93ba8572010-03-25 06:39:04 +0000767 TemplateParameterList *PrevParams
768 = PrevClassTemplate->getTemplateParameters();
769
770 // Make sure the parameter lists match.
771 if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000772 Complain,
773 Sema::TPL_TemplateMatch)) {
774 if (Complain)
775 return 0;
776
777 AdoptedPreviousTemplateParams = true;
778 InstParams = PrevParams;
779 }
John McCall93ba8572010-03-25 06:39:04 +0000780
781 // Do some additional validation, then merge default arguments
782 // from the existing declarations.
Douglas Gregorc53d0d72010-04-08 18:16:15 +0000783 if (!AdoptedPreviousTemplateParams &&
784 SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
John McCall93ba8572010-03-25 06:39:04 +0000785 Sema::TPC_ClassTemplate))
786 return 0;
787 }
788 }
789
John McCalle29ba202009-08-20 01:44:21 +0000790 CXXRecordDecl *RecordInst
John McCall93ba8572010-03-25 06:39:04 +0000791 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
John McCalle29ba202009-08-20 01:44:21 +0000792 Pattern->getLocation(), Pattern->getIdentifier(),
John McCall93ba8572010-03-25 06:39:04 +0000793 Pattern->getTagKeywordLoc(), PrevDecl,
Douglas Gregorf0510d42009-10-12 23:11:44 +0000794 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000795
John McCall93ba8572010-03-25 06:39:04 +0000796 if (Qualifier)
797 RecordInst->setQualifierInfo(Qualifier, Pattern->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +0000798
John McCalle29ba202009-08-20 01:44:21 +0000799 ClassTemplateDecl *Inst
John McCall93ba8572010-03-25 06:39:04 +0000800 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
801 D->getIdentifier(), InstParams, RecordInst,
802 PrevClassTemplate);
John McCalle29ba202009-08-20 01:44:21 +0000803 RecordInst->setDescribedClassTemplate(Inst);
John McCallea7390c2010-04-08 20:25:50 +0000804
John McCall93ba8572010-03-25 06:39:04 +0000805 if (isFriend) {
John McCallea7390c2010-04-08 20:25:50 +0000806 if (PrevClassTemplate)
807 Inst->setAccess(PrevClassTemplate->getAccess());
808 else
809 Inst->setAccess(D->getAccess());
810
John McCall93ba8572010-03-25 06:39:04 +0000811 Inst->setObjectOfFriendDecl(PrevClassTemplate != 0);
812 // TODO: do we want to track the instantiation progeny of this
813 // friend target decl?
814 } else {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000815 Inst->setAccess(D->getAccess());
John McCall93ba8572010-03-25 06:39:04 +0000816 Inst->setInstantiatedFromMemberTemplate(D);
817 }
Douglas Gregorf0510d42009-10-12 23:11:44 +0000818
819 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000820 SemaRef.Context.getInjectedClassNameType(RecordInst,
Douglas Gregor24bae922010-07-08 18:37:38 +0000821 Inst->getInjectedClassNameSpecialization());
John McCallea7390c2010-04-08 20:25:50 +0000822
Douglas Gregor259571e2009-10-30 22:42:42 +0000823 // Finish handling of friends.
John McCall93ba8572010-03-25 06:39:04 +0000824 if (isFriend) {
825 DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000826 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000827 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000828
John McCalle29ba202009-08-20 01:44:21 +0000829 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000830
Douglas Gregored9c0f92009-10-29 00:04:11 +0000831 // Instantiate all of the partial specializations of this member class
832 // template.
Douglas Gregordc60c1e2010-04-30 05:56:50 +0000833 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
834 D->getPartialSpecializations(PartialSpecs);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000835 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
836 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
837
John McCalle29ba202009-08-20 01:44:21 +0000838 return Inst;
839}
840
Douglas Gregord60e1052009-08-27 16:57:43 +0000841Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000842TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
843 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000844 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
845
846 // Lookup the already-instantiated declaration in the instantiation
847 // of the class template and return that.
848 DeclContext::lookup_result Found
849 = Owner->lookup(ClassTemplate->getDeclName());
850 if (Found.first == Found.second)
851 return 0;
852
853 ClassTemplateDecl *InstClassTemplate
854 = dyn_cast<ClassTemplateDecl>(*Found.first);
855 if (!InstClassTemplate)
856 return 0;
857
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +0000858 return InstClassTemplate->findPartialSpecInstantiatedFromMember(D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000859}
860
861Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000862TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000863 // Create a local instantiation scope for this function template, which
864 // will contain the instantiations of the template parameters and then get
865 // merged with the local instantiation scope for the function template
866 // itself.
867 Sema::LocalInstantiationScope Scope(SemaRef);
Douglas Gregor895162d2010-04-30 18:55:50 +0000868
Douglas Gregord60e1052009-08-27 16:57:43 +0000869 TemplateParameterList *TempParams = D->getTemplateParameters();
870 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000871 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000872 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000873
Douglas Gregora735b202009-10-13 14:39:41 +0000874 FunctionDecl *Instantiated = 0;
875 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
876 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
877 InstParams));
878 else
879 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
880 D->getTemplatedDecl(),
881 InstParams));
882
883 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000884 return 0;
885
John McCall46460a62010-01-20 21:53:11 +0000886 Instantiated->setAccess(D->getAccess());
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000889 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000890 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000891 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000892 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000893 assert(InstTemplate &&
894 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000895
John McCallb1a56e72010-03-26 23:10:15 +0000896 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
897
John McCalle976ffe2009-12-14 23:19:40 +0000898 // Link the instantiation back to the pattern *unless* this is a
899 // non-definition friend declaration.
900 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
John McCallb1a56e72010-03-26 23:10:15 +0000901 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000902 InstTemplate->setInstantiatedFromMemberTemplate(D);
903
John McCallb1a56e72010-03-26 23:10:15 +0000904 // Make declarations visible in the appropriate context.
905 if (!isFriend)
Douglas Gregora735b202009-10-13 14:39:41 +0000906 Owner->addDecl(InstTemplate);
John McCallb1a56e72010-03-26 23:10:15 +0000907
Douglas Gregord60e1052009-08-27 16:57:43 +0000908 return InstTemplate;
909}
910
Douglas Gregord475b8d2009-03-25 21:17:03 +0000911Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
912 CXXRecordDecl *PrevDecl = 0;
913 if (D->isInjectedClassName())
914 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000915 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000916 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
917 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000918 TemplateArgs);
919 if (!Prev) return 0;
920 PrevDecl = cast<CXXRecordDecl>(Prev);
921 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922
923 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000924 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000925 D->getLocation(), D->getIdentifier(),
926 D->getTagKeywordLoc(), PrevDecl);
John McCallb6217662010-03-15 10:12:16 +0000927
928 // Substitute the nested name specifier, if any.
929 if (SubstQualifier(D, Record))
930 return 0;
931
Douglas Gregord475b8d2009-03-25 21:17:03 +0000932 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000933 // FIXME: Check against AS_none is an ugly hack to work around the issue that
934 // the tag decls introduced by friend class declarations don't have an access
935 // specifier. Remove once this area of the code gets sorted out.
936 if (D->getAccess() != AS_none)
937 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000938 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000939 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000940
John McCall02cace72009-08-28 07:59:38 +0000941 // If the original function was part of a friend declaration,
942 // inherit its namespace state.
943 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
944 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
945
Douglas Gregor9901c572010-05-21 00:31:19 +0000946 // Make sure that anonymous structs and unions are recorded.
947 if (D->isAnonymousStructOrUnion()) {
948 Record->setAnonymousStructOrUnion(true);
949 if (Record->getDeclContext()->getLookupContext()->isFunctionOrMethod())
950 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
951 }
Anders Carlssond8b285f2009-09-01 04:26:58 +0000952
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000954 return Record;
955}
956
John McCall02cace72009-08-28 07:59:38 +0000957/// Normal class members are of more specific types and therefore
958/// don't make it here. This function serves two purposes:
959/// 1) instantiating function templates
960/// 2) substituting friend declarations
961/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000962Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000963 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000964 // Check whether there is already a function template specialization for
965 // this declaration.
966 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
967 void *InsertPos = 0;
John McCallb0cb0222010-03-27 05:57:59 +0000968 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor24bae922010-07-08 18:37:38 +0000969 std::pair<const TemplateArgument *, unsigned> Innermost
970 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000972 FunctionDecl *SpecFunc
973 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
974 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor127102b2009-06-29 20:59:39 +0000976 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000977 if (SpecFunc)
978 return SpecFunc;
Douglas Gregor127102b2009-06-29 20:59:39 +0000979 }
Mike Stump1eb44332009-09-09 15:08:12 +0000980
John McCallb0cb0222010-03-27 05:57:59 +0000981 bool isFriend;
982 if (FunctionTemplate)
983 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
984 else
985 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
986
Douglas Gregor79c22782010-01-16 20:21:20 +0000987 bool MergeWithParentScope = (TemplateParams != 0) ||
Douglas Gregorb212d9a2010-05-21 21:25:08 +0000988 Owner->isFunctionOrMethod() ||
Douglas Gregor79c22782010-01-16 20:21:20 +0000989 !(isa<Decl>(Owner) &&
990 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
991 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregore53060f2009-06-25 22:08:12 +0000993 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +0000994 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
995 TInfo = SubstFunctionType(D, Params);
996 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000997 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +0000998 QualType T = TInfo->getType();
John McCallfd810b12009-08-14 02:03:10 +0000999
John McCalld325daa2010-03-26 04:53:08 +00001000 NestedNameSpecifier *Qualifier = D->getQualifier();
1001 if (Qualifier) {
1002 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1003 D->getQualifierRange(),
1004 TemplateArgs);
1005 if (!Qualifier) return 0;
1006 }
1007
John McCall68b6b872010-02-06 01:50:47 +00001008 // If we're instantiating a local function declaration, put the result
1009 // in the owner; otherwise we need to find the instantiated context.
1010 DeclContext *DC;
1011 if (D->getDeclContext()->isFunctionOrMethod())
1012 DC = Owner;
John McCalld325daa2010-03-26 04:53:08 +00001013 else if (isFriend && Qualifier) {
1014 CXXScopeSpec SS;
1015 SS.setScopeRep(Qualifier);
1016 SS.setRange(D->getQualifierRange());
1017 DC = SemaRef.computeDeclContext(SS);
1018 if (!DC) return 0;
1019 } else {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001020 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1021 TemplateArgs);
John McCalld325daa2010-03-26 04:53:08 +00001022 }
John McCall68b6b872010-02-06 01:50:47 +00001023
John McCall02cace72009-08-28 07:59:38 +00001024 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +00001025 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCall21ef0fa2010-03-11 09:03:00 +00001026 D->getDeclName(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001027 D->getStorageClass(), D->getStorageClassAsWritten(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001028 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCallb6217662010-03-15 10:12:16 +00001029
John McCalld325daa2010-03-26 04:53:08 +00001030 if (Qualifier)
1031 Function->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001032
John McCallb1a56e72010-03-26 23:10:15 +00001033 DeclContext *LexicalDC = Owner;
1034 if (!isFriend && D->isOutOfLine()) {
1035 assert(D->getDeclContext()->isFileContext());
1036 LexicalDC = D->getDeclContext();
1037 }
1038
1039 Function->setLexicalDeclContext(LexicalDC);
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregore53060f2009-06-25 22:08:12 +00001041 // Attach the parameters
1042 for (unsigned P = 0; P < Params.size(); ++P)
1043 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +00001044 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +00001045
Douglas Gregorac7c2c82010-05-17 16:38:00 +00001046 SourceLocation InstantiateAtPOI;
Douglas Gregora735b202009-10-13 14:39:41 +00001047 if (TemplateParams) {
1048 // Our resulting instantiation is actually a function template, since we
1049 // are substituting only the outer template parameters. For example, given
1050 //
1051 // template<typename T>
1052 // struct X {
1053 // template<typename U> friend void f(T, U);
1054 // };
1055 //
1056 // X<int> x;
1057 //
1058 // We are instantiating the friend function template "f" within X<int>,
1059 // which means substituting int for T, but leaving "f" as a friend function
1060 // template.
1061 // Build the function template itself.
John McCalld325daa2010-03-26 04:53:08 +00001062 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
Douglas Gregora735b202009-10-13 14:39:41 +00001063 Function->getLocation(),
1064 Function->getDeclName(),
1065 TemplateParams, Function);
1066 Function->setDescribedFunctionTemplate(FunctionTemplate);
John McCallb1a56e72010-03-26 23:10:15 +00001067
1068 FunctionTemplate->setLexicalDeclContext(LexicalDC);
John McCalld325daa2010-03-26 04:53:08 +00001069
1070 if (isFriend && D->isThisDeclarationADefinition()) {
1071 // TODO: should we remember this connection regardless of whether
1072 // the friend declaration provided a body?
1073 FunctionTemplate->setInstantiatedFromMemberTemplate(
1074 D->getDescribedFunctionTemplate());
1075 }
Douglas Gregor66724ea2009-11-14 01:20:54 +00001076 } else if (FunctionTemplate) {
1077 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001078 std::pair<const TemplateArgument *, unsigned> Innermost
1079 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001080 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001081 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1082 Innermost.first,
1083 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001084 InsertPos);
John McCalld325daa2010-03-26 04:53:08 +00001085 } else if (isFriend && D->isThisDeclarationADefinition()) {
1086 // TODO: should we remember this connection regardless of whether
1087 // the friend declaration provided a body?
1088 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
John McCall02cace72009-08-28 07:59:38 +00001089 }
Douglas Gregora735b202009-10-13 14:39:41 +00001090
Douglas Gregore53060f2009-06-25 22:08:12 +00001091 if (InitFunctionInstantiation(Function, D))
1092 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregore53060f2009-06-25 22:08:12 +00001094 bool Redeclaration = false;
1095 bool OverloadableAttrRequired = false;
John McCallaf2094e2010-04-08 09:05:18 +00001096 bool isExplicitSpecialization = false;
Douglas Gregora735b202009-10-13 14:39:41 +00001097
John McCall68263142009-11-18 22:49:29 +00001098 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
1099 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
1100
John McCallaf2094e2010-04-08 09:05:18 +00001101 if (DependentFunctionTemplateSpecializationInfo *Info
1102 = D->getDependentSpecializationInfo()) {
1103 assert(isFriend && "non-friend has dependent specialization info?");
1104
1105 // This needs to be set now for future sanity.
1106 Function->setObjectOfFriendDecl(/*HasPrevious*/ true);
1107
1108 // Instantiate the explicit template arguments.
1109 TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1110 Info->getRAngleLoc());
1111 for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) {
1112 TemplateArgumentLoc Loc;
1113 if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs))
1114 return 0;
1115
1116 ExplicitArgs.addArgument(Loc);
1117 }
1118
1119 // Map the candidate templates to their instantiations.
1120 for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1121 Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1122 Info->getTemplate(I),
1123 TemplateArgs);
1124 if (!Temp) return 0;
1125
1126 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1127 }
1128
1129 if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1130 &ExplicitArgs,
1131 Previous))
1132 Function->setInvalidDecl();
1133
1134 isExplicitSpecialization = true;
1135
1136 } else if (TemplateParams || !FunctionTemplate) {
Douglas Gregora735b202009-10-13 14:39:41 +00001137 // Look only into the namespace where the friend would be declared to
1138 // find a previous declaration. This is the innermost enclosing namespace,
1139 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +00001140 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +00001141
Douglas Gregora735b202009-10-13 14:39:41 +00001142 // In C++, the previous declaration we find might be a tag type
1143 // (class or enum). In this case, the new declaration will hide the
1144 // tag type. Note that this does does not apply if we're declaring a
1145 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001146 if (Previous.isSingleTagDecl())
1147 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +00001148 }
1149
John McCall9f54ad42009-12-10 09:41:52 +00001150 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
John McCallaf2094e2010-04-08 09:05:18 +00001151 isExplicitSpecialization, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +00001152 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001153
John McCall76d32642010-04-24 01:30:58 +00001154 NamedDecl *PrincipalDecl = (TemplateParams
1155 ? cast<NamedDecl>(FunctionTemplate)
1156 : Function);
1157
Douglas Gregora735b202009-10-13 14:39:41 +00001158 // If the original function was part of a friend declaration,
1159 // inherit its namespace state and add it to the owner.
John McCalld325daa2010-03-26 04:53:08 +00001160 if (isFriend) {
John McCall68263142009-11-18 22:49:29 +00001161 NamedDecl *PrevDecl;
John McCall76d32642010-04-24 01:30:58 +00001162 if (TemplateParams)
Douglas Gregora735b202009-10-13 14:39:41 +00001163 PrevDecl = FunctionTemplate->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001164 else
Douglas Gregora735b202009-10-13 14:39:41 +00001165 PrevDecl = Function->getPreviousDeclaration();
John McCall76d32642010-04-24 01:30:58 +00001166
1167 PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0);
1168 DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false);
Douglas Gregor238058c2010-05-18 05:45:02 +00001169
1170 if (!SemaRef.getLangOptions().CPlusPlus0x &&
1171 D->isThisDeclarationADefinition()) {
1172 // Check for a function body.
1173 const FunctionDecl *Definition = 0;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001174 if (Function->hasBody(Definition) &&
Douglas Gregor238058c2010-05-18 05:45:02 +00001175 Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1176 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1177 << Function->getDeclName();
1178 SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1179 Function->setInvalidDecl();
1180 }
1181 // Check for redefinitions due to other instantiations of this or
1182 // a similar friend function.
1183 else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(),
1184 REnd = Function->redecls_end();
1185 R != REnd; ++R) {
1186 if (*R != Function &&
1187 ((*R)->getFriendObjectKind() != Decl::FOK_None)) {
1188 if (const FunctionDecl *RPattern
1189 = (*R)->getTemplateInstantiationPattern())
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001190 if (RPattern->hasBody(RPattern)) {
Douglas Gregor238058c2010-05-18 05:45:02 +00001191 SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1192 << Function->getDeclName();
1193 SemaRef.Diag((*R)->getLocation(), diag::note_previous_definition);
1194 Function->setInvalidDecl();
1195 break;
1196 }
1197 }
1198 }
1199 }
1200
Douglas Gregora735b202009-10-13 14:39:41 +00001201 }
1202
John McCall76d32642010-04-24 01:30:58 +00001203 if (Function->isOverloadedOperator() && !DC->isRecord() &&
1204 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1205 PrincipalDecl->setNonMemberOperator();
1206
Douglas Gregore53060f2009-06-25 22:08:12 +00001207 return Function;
1208}
1209
Douglas Gregord60e1052009-08-27 16:57:43 +00001210Decl *
1211TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1212 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +00001213 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1214 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +00001215 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +00001216 // We are creating a function template specialization from a function
1217 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +00001218 // specialization for this particular set of template arguments.
Douglas Gregor24bae922010-07-08 18:37:38 +00001219 std::pair<const TemplateArgument *, unsigned> Innermost
1220 = TemplateArgs.getInnermost();
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001222 FunctionDecl *SpecFunc
1223 = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
1224 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregor6b906862009-08-21 00:16:32 +00001226 // If we already have a function template specialization, return it.
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001227 if (SpecFunc)
1228 return SpecFunc;
Douglas Gregor6b906862009-08-21 00:16:32 +00001229 }
1230
John McCallb0cb0222010-03-27 05:57:59 +00001231 bool isFriend;
1232 if (FunctionTemplate)
1233 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1234 else
1235 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1236
Douglas Gregor79c22782010-01-16 20:21:20 +00001237 bool MergeWithParentScope = (TemplateParams != 0) ||
1238 !(isa<Decl>(Owner) &&
1239 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1240 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001241
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001242 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCall21ef0fa2010-03-11 09:03:00 +00001243 TypeSourceInfo *TInfo = D->getTypeSourceInfo();
1244 TInfo = SubstFunctionType(D, Params);
1245 if (!TInfo)
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001246 return 0;
John McCall21ef0fa2010-03-11 09:03:00 +00001247 QualType T = TInfo->getType();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001248
Douglas Gregor5f970ee2010-05-04 18:18:31 +00001249 // \brief If the type of this function is not *directly* a function
1250 // type, then we're instantiating the a function that was declared
1251 // via a typedef, e.g.,
1252 //
1253 // typedef int functype(int, int);
1254 // functype func;
1255 //
1256 // In this case, we'll just go instantiate the ParmVarDecls that we
1257 // synthesized in the method declaration.
1258 if (!isa<FunctionProtoType>(T)) {
1259 assert(!Params.size() && "Instantiating type could not yield parameters");
1260 for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) {
1261 ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I),
1262 TemplateArgs);
1263 if (!P)
1264 return 0;
1265
1266 Params.push_back(P);
1267 }
1268 }
1269
John McCallb0cb0222010-03-27 05:57:59 +00001270 NestedNameSpecifier *Qualifier = D->getQualifier();
1271 if (Qualifier) {
1272 Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier,
1273 D->getQualifierRange(),
1274 TemplateArgs);
1275 if (!Qualifier) return 0;
1276 }
1277
1278 DeclContext *DC = Owner;
1279 if (isFriend) {
1280 if (Qualifier) {
1281 CXXScopeSpec SS;
1282 SS.setScopeRep(Qualifier);
1283 SS.setRange(D->getQualifierRange());
1284 DC = SemaRef.computeDeclContext(SS);
1285 } else {
1286 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1287 D->getDeclContext(),
1288 TemplateArgs);
1289 }
1290 if (!DC) return 0;
1291 }
1292
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001293 // Build the instantiated method declaration.
John McCallb0cb0222010-03-27 05:57:59 +00001294 CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
Douglas Gregordec06662009-08-21 18:42:58 +00001295 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Abramo Bagnara25777432010-08-11 22:01:17 +00001297 DeclarationNameInfo NameInfo
1298 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001299 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001300 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001301 NameInfo, T, TInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001302 Constructor->isExplicit(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001303 Constructor->isInlineSpecified(),
1304 false);
Douglas Gregor17e32f32009-08-21 22:43:28 +00001305 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001306 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001307 NameInfo, T,
1308 Destructor->isInlineSpecified(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001309 false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001310 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001311 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
Abramo Bagnara25777432010-08-11 22:01:17 +00001312 NameInfo, T, TInfo,
Douglas Gregor0130f3c2009-10-27 21:01:01 +00001313 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001314 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +00001315 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00001316 Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1317 NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001318 D->isStatic(),
1319 D->getStorageClassAsWritten(),
1320 D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +00001321 }
Douglas Gregor6b906862009-08-21 00:16:32 +00001322
John McCallb0cb0222010-03-27 05:57:59 +00001323 if (Qualifier)
1324 Method->setQualifierInfo(Qualifier, D->getQualifierRange());
John McCallb6217662010-03-15 10:12:16 +00001325
Douglas Gregord60e1052009-08-27 16:57:43 +00001326 if (TemplateParams) {
1327 // Our resulting instantiation is actually a function template, since we
1328 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +00001329 //
Douglas Gregord60e1052009-08-27 16:57:43 +00001330 // template<typename T>
1331 // struct X {
1332 // template<typename U> void f(T, U);
1333 // };
1334 //
1335 // X<int> x;
1336 //
1337 // We are instantiating the member template "f" within X<int>, which means
1338 // substituting int for T, but leaving "f" as a member function template.
1339 // Build the function template itself.
1340 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1341 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001342 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +00001343 TemplateParams, Method);
John McCallb0cb0222010-03-27 05:57:59 +00001344 if (isFriend) {
1345 FunctionTemplate->setLexicalDeclContext(Owner);
1346 FunctionTemplate->setObjectOfFriendDecl(true);
1347 } else if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +00001348 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +00001349 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001350 } else if (FunctionTemplate) {
1351 // Record this function template specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +00001352 std::pair<const TemplateArgument *, unsigned> Innermost
1353 = TemplateArgs.getInnermost();
Douglas Gregor838db382010-02-11 01:19:42 +00001354 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor24bae922010-07-08 18:37:38 +00001355 new (SemaRef.Context) TemplateArgumentList(SemaRef.Context,
1356 Innermost.first,
1357 Innermost.second),
Douglas Gregor66724ea2009-11-14 01:20:54 +00001358 InsertPos);
John McCallb0cb0222010-03-27 05:57:59 +00001359 } else if (!isFriend) {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001360 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001361 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001362 }
1363
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001365 // out-of-line, the instantiation will have the same lexical
1366 // context (which will be a namespace scope) as the template.
John McCallb0cb0222010-03-27 05:57:59 +00001367 if (isFriend) {
1368 Method->setLexicalDeclContext(Owner);
1369 Method->setObjectOfFriendDecl(true);
1370 } else if (D->isOutOfLine())
Douglas Gregor7caa6822009-07-24 20:34:43 +00001371 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor5545e162009-03-24 00:38:23 +00001373 // Attach the parameters
1374 for (unsigned P = 0; P < Params.size(); ++P)
1375 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001376 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001377
1378 if (InitMethodInstantiation(Method, D))
1379 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001380
Abramo Bagnara25777432010-08-11 22:01:17 +00001381 LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1382 Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001383
John McCallb0cb0222010-03-27 05:57:59 +00001384 if (!FunctionTemplate || TemplateParams || isFriend) {
1385 SemaRef.LookupQualifiedName(Previous, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregordec06662009-08-21 18:42:58 +00001387 // In C++, the previous declaration we find might be a tag type
1388 // (class or enum). In this case, the new declaration will hide the
1389 // tag type. Note that this does does not apply if we're declaring a
1390 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001391 if (Previous.isSingleTagDecl())
1392 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001393 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001394
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001395 bool Redeclaration = false;
1396 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001397 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001398 /*FIXME:*/OverloadableAttrRequired);
1399
Douglas Gregor4ba31362009-12-01 17:24:26 +00001400 if (D->isPure())
1401 SemaRef.CheckPureMethod(Method, SourceRange());
1402
John McCall46460a62010-01-20 21:53:11 +00001403 Method->setAccess(D->getAccess());
1404
John McCallb0cb0222010-03-27 05:57:59 +00001405 if (FunctionTemplate) {
1406 // If there's a function template, let our caller handle it.
1407 } else if (Method->isInvalidDecl() && !Previous.empty()) {
1408 // Don't hide a (potentially) valid declaration with an invalid one.
1409 } else {
1410 NamedDecl *DeclToAdd = (TemplateParams
1411 ? cast<NamedDecl>(FunctionTemplate)
1412 : Method);
1413 if (isFriend)
1414 Record->makeDeclVisibleInContext(DeclToAdd);
1415 else
1416 Owner->addDecl(DeclToAdd);
1417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001419 return Method;
1420}
1421
Douglas Gregor615c5d42009-03-24 16:43:20 +00001422Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001423 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001424}
1425
Douglas Gregor03b2b072009-03-24 00:15:49 +00001426Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001427 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001428}
1429
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001430Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001431 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001432}
1433
Douglas Gregor6477b692009-03-25 15:04:13 +00001434ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001435 return SemaRef.SubstParmVarDecl(D, TemplateArgs);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001436}
1437
John McCalle29ba202009-08-20 01:44:21 +00001438Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1439 TemplateTypeParmDecl *D) {
1440 // TODO: don't always clone when decls are refcounted.
Douglas Gregorefed5c82010-06-16 15:23:05 +00001441 const Type* T = D->getTypeForDecl();
1442 assert(T->isTemplateTypeParmType());
1443 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001444
John McCalle29ba202009-08-20 01:44:21 +00001445 TemplateTypeParmDecl *Inst =
1446 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001447 TTPT->getDepth() - 1, TTPT->getIndex(),
1448 TTPT->getName(),
John McCalle29ba202009-08-20 01:44:21 +00001449 D->wasDeclaredWithTypename(),
1450 D->isParameterPack());
1451
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001452 if (D->hasDefaultArgument())
1453 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001454
Douglas Gregor550d9b22009-10-31 17:21:17 +00001455 // Introduce this template parameter's instantiation into the instantiation
1456 // scope.
1457 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1458
John McCalle29ba202009-08-20 01:44:21 +00001459 return Inst;
1460}
1461
Douglas Gregor33642df2009-10-23 23:25:44 +00001462Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1463 NonTypeTemplateParmDecl *D) {
1464 // Substitute into the type of the non-type template parameter.
1465 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001466 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001467 if (DI) {
1468 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1469 D->getDeclName());
1470 if (DI) T = DI->getType();
1471 } else {
1472 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1473 D->getDeclName());
1474 DI = 0;
1475 }
1476 if (T.isNull())
1477 return 0;
1478
1479 // Check that this type is acceptable for a non-type template parameter.
1480 bool Invalid = false;
1481 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1482 if (T.isNull()) {
1483 T = SemaRef.Context.IntTy;
1484 Invalid = true;
1485 }
1486
1487 NonTypeTemplateParmDecl *Param
1488 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1489 D->getDepth() - 1, D->getPosition(),
1490 D->getIdentifier(), T, DI);
1491 if (Invalid)
1492 Param->setInvalidDecl();
1493
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001494 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001495
1496 // Introduce this template parameter's instantiation into the instantiation
1497 // scope.
1498 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001499 return Param;
1500}
1501
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001502Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001503TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1504 TemplateTemplateParmDecl *D) {
1505 // Instantiate the template parameter list of the template template parameter.
1506 TemplateParameterList *TempParams = D->getTemplateParameters();
1507 TemplateParameterList *InstParams;
1508 {
1509 // Perform the actual substitution of template parameters within a new,
1510 // local instantiation scope.
1511 Sema::LocalInstantiationScope Scope(SemaRef);
1512 InstParams = SubstTemplateParams(TempParams);
1513 if (!InstParams)
1514 return NULL;
1515 }
1516
1517 // Build the template template parameter.
1518 TemplateTemplateParmDecl *Param
1519 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1520 D->getDepth() - 1, D->getPosition(),
1521 D->getIdentifier(), InstParams);
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001522 Param->setDefaultArgument(D->getDefaultArgument(), false);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001523
Douglas Gregor9106ef72009-11-11 16:58:32 +00001524 // Introduce this template parameter's instantiation into the instantiation
1525 // scope.
1526 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1527
1528 return Param;
1529}
1530
Douglas Gregor48c32a72009-11-17 06:07:40 +00001531Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1532 // Using directives are never dependent, so they require no explicit
1533
1534 UsingDirectiveDecl *Inst
1535 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1536 D->getNamespaceKeyLocation(),
1537 D->getQualifierRange(), D->getQualifier(),
1538 D->getIdentLocation(),
1539 D->getNominatedNamespace(),
1540 D->getCommonAncestor());
1541 Owner->addDecl(Inst);
1542 return Inst;
1543}
1544
John McCalled976492009-12-04 22:46:56 +00001545Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1546 // The nested name specifier is non-dependent, so no transformation
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001547 // is required. The same holds for the name info.
1548 DeclarationNameInfo NameInfo = D->getNameInfo();
John McCalled976492009-12-04 22:46:56 +00001549
John McCall9f54ad42009-12-10 09:41:52 +00001550 // We only need to do redeclaration lookups if we're in a class
1551 // scope (in fact, it's not really even possible in non-class
1552 // scopes).
1553 bool CheckRedeclaration = Owner->isRecord();
1554
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001555 LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
1556 Sema::ForRedeclaration);
John McCall9f54ad42009-12-10 09:41:52 +00001557
John McCalled976492009-12-04 22:46:56 +00001558 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
John McCalled976492009-12-04 22:46:56 +00001559 D->getNestedNameRange(),
1560 D->getUsingLocation(),
1561 D->getTargetNestedNameDecl(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001562 NameInfo,
John McCalled976492009-12-04 22:46:56 +00001563 D->isTypeName());
1564
1565 CXXScopeSpec SS;
1566 SS.setScopeRep(D->getTargetNestedNameDecl());
1567 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001568
1569 if (CheckRedeclaration) {
1570 Prev.setHideTags(false);
1571 SemaRef.LookupQualifiedName(Prev, Owner);
1572
1573 // Check for invalid redeclarations.
1574 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1575 D->isTypeName(), SS,
1576 D->getLocation(), Prev))
1577 NewUD->setInvalidDecl();
1578
1579 }
1580
1581 if (!NewUD->isInvalidDecl() &&
1582 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001583 D->getLocation()))
1584 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001585
John McCalled976492009-12-04 22:46:56 +00001586 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1587 NewUD->setAccess(D->getAccess());
1588 Owner->addDecl(NewUD);
1589
John McCall9f54ad42009-12-10 09:41:52 +00001590 // Don't process the shadow decls for an invalid decl.
1591 if (NewUD->isInvalidDecl())
1592 return NewUD;
1593
John McCall323c3102009-12-22 22:26:37 +00001594 bool isFunctionScope = Owner->isFunctionOrMethod();
1595
John McCall9f54ad42009-12-10 09:41:52 +00001596 // Process the shadow decls.
1597 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1598 I != E; ++I) {
1599 UsingShadowDecl *Shadow = *I;
1600 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001601 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1602 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001603 TemplateArgs));
1604
1605 if (CheckRedeclaration &&
1606 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1607 continue;
1608
1609 UsingShadowDecl *InstShadow
1610 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1611 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001612
1613 if (isFunctionScope)
1614 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001615 }
John McCalled976492009-12-04 22:46:56 +00001616
1617 return NewUD;
1618}
1619
1620Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001621 // Ignore these; we handle them in bulk when processing the UsingDecl.
1622 return 0;
John McCalled976492009-12-04 22:46:56 +00001623}
1624
John McCall7ba107a2009-11-18 02:36:19 +00001625Decl * TemplateDeclInstantiator
1626 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627 NestedNameSpecifier *NNS =
1628 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1629 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001630 TemplateArgs);
1631 if (!NNS)
1632 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001634 CXXScopeSpec SS;
1635 SS.setRange(D->getTargetNestedNameRange());
1636 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001638 // Since NameInfo refers to a typename, it cannot be a C++ special name.
1639 // Hence, no tranformation is required for it.
1640 DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001641 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001642 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001643 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001644 /*instantiation*/ true,
1645 /*typename*/ true, D->getTypenameLoc());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001646 if (UD)
John McCalled976492009-12-04 22:46:56 +00001647 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1648
John McCall7ba107a2009-11-18 02:36:19 +00001649 return UD;
1650}
1651
1652Decl * TemplateDeclInstantiator
1653 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1654 NestedNameSpecifier *NNS =
1655 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1656 D->getTargetNestedNameRange(),
1657 TemplateArgs);
1658 if (!NNS)
1659 return 0;
1660
1661 CXXScopeSpec SS;
1662 SS.setRange(D->getTargetNestedNameRange());
1663 SS.setScopeRep(NNS);
1664
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001665 DeclarationNameInfo NameInfo
1666 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1667
John McCall7ba107a2009-11-18 02:36:19 +00001668 NamedDecl *UD =
1669 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00001670 D->getUsingLoc(), SS, NameInfo, 0,
John McCall7ba107a2009-11-18 02:36:19 +00001671 /*instantiation*/ true,
1672 /*typename*/ false, SourceLocation());
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001673 if (UD)
John McCalled976492009-12-04 22:46:56 +00001674 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1675
Anders Carlsson0d8df782009-08-29 19:37:28 +00001676 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001677}
1678
John McCallce3ff2b2009-08-25 22:02:44 +00001679Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001680 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001681 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001682 if (D->isInvalidDecl())
1683 return 0;
1684
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001685 return Instantiator.Visit(D);
1686}
1687
John McCalle29ba202009-08-20 01:44:21 +00001688/// \brief Instantiates a nested template parameter list in the current
1689/// instantiation context.
1690///
1691/// \param L The parameter list to instantiate
1692///
1693/// \returns NULL if there was an error
1694TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001695TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001696 // Get errors for all the parameters before bailing out.
1697 bool Invalid = false;
1698
1699 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001700 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001701 ParamVector Params;
1702 Params.reserve(N);
1703 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1704 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001705 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001706 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001707 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001708 }
1709
1710 // Clean up if we had an error.
Douglas Gregorff331c12010-07-25 18:17:45 +00001711 if (Invalid)
John McCalle29ba202009-08-20 01:44:21 +00001712 return NULL;
John McCalle29ba202009-08-20 01:44:21 +00001713
1714 TemplateParameterList *InstL
1715 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1716 L->getLAngleLoc(), &Params.front(), N,
1717 L->getRAngleLoc());
1718 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001719}
John McCalle29ba202009-08-20 01:44:21 +00001720
Douglas Gregored9c0f92009-10-29 00:04:11 +00001721/// \brief Instantiate the declaration of a class template partial
1722/// specialization.
1723///
1724/// \param ClassTemplate the (instantiated) class template that is partially
1725// specialized by the instantiation of \p PartialSpec.
1726///
1727/// \param PartialSpec the (uninstantiated) class template partial
1728/// specialization that we are instantiating.
1729///
1730/// \returns true if there was an error, false otherwise.
1731bool
1732TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1733 ClassTemplateDecl *ClassTemplate,
1734 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001735 // Create a local instantiation scope for this class template partial
1736 // specialization, which will contain the instantiations of the template
1737 // parameters.
1738 Sema::LocalInstantiationScope Scope(SemaRef);
1739
Douglas Gregored9c0f92009-10-29 00:04:11 +00001740 // Substitute into the template parameters of the class template partial
1741 // specialization.
1742 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1743 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1744 if (!InstParams)
1745 return true;
1746
1747 // Substitute into the template arguments of the class template partial
1748 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001749 const TemplateArgumentLoc *PartialSpecTemplateArgs
1750 = PartialSpec->getTemplateArgsAsWritten();
1751 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1752
John McCalld5532b62009-11-23 01:53:49 +00001753 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001754 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001755 TemplateArgumentLoc Loc;
1756 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001757 return true;
John McCalld5532b62009-11-23 01:53:49 +00001758 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001759 }
1760
1761
1762 // Check that the template argument list is well-formed for this
1763 // class template.
1764 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1765 InstTemplateArgs.size());
1766 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1767 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001768 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001769 false,
1770 Converted))
1771 return true;
1772
1773 // Figure out where to insert this class template partial specialization
1774 // in the member template's set of class template partial specializations.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001775 void *InsertPos = 0;
1776 ClassTemplateSpecializationDecl *PrevDecl
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001777 = ClassTemplate->findPartialSpecialization(Converted.getFlatArguments(),
1778 Converted.flatSize(), InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001779
1780 // Build the canonical type that describes the converted template
1781 // arguments of the class template partial specialization.
1782 QualType CanonType
1783 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1784 Converted.getFlatArguments(),
1785 Converted.flatSize());
1786
1787 // Build the fully-sugared type for this class template
1788 // specialization as the user wrote in the specialization
1789 // itself. This means that we'll pretty-print the type retrieved
1790 // from the specialization's declaration the way that the user
1791 // actually wrote the specialization, rather than formatting the
1792 // name based on the "canonical" representation used to store the
1793 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001794 TypeSourceInfo *WrittenTy
1795 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1796 TemplateName(ClassTemplate),
1797 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001798 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001799 CanonType);
1800
1801 if (PrevDecl) {
1802 // We've already seen a partial specialization with the same template
1803 // parameters and template arguments. This can happen, for example, when
1804 // substituting the outer template arguments ends up causing two
1805 // class template partial specializations of a member class template
1806 // to have identical forms, e.g.,
1807 //
1808 // template<typename T, typename U>
1809 // struct Outer {
1810 // template<typename X, typename Y> struct Inner;
1811 // template<typename Y> struct Inner<T, Y>;
1812 // template<typename Y> struct Inner<U, Y>;
1813 // };
1814 //
1815 // Outer<int, int> outer; // error: the partial specializations of Inner
1816 // // have the same signature.
1817 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1818 << WrittenTy;
1819 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1820 << SemaRef.Context.getTypeDeclType(PrevDecl);
1821 return true;
1822 }
1823
1824
1825 // Create the class template partial specialization declaration.
1826 ClassTemplatePartialSpecializationDecl *InstPartialSpec
Douglas Gregor13c85772010-05-06 00:28:52 +00001827 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
1828 PartialSpec->getTagKind(),
1829 Owner,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001830 PartialSpec->getLocation(),
1831 InstParams,
1832 ClassTemplate,
1833 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001834 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001835 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001836 0,
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001837 ClassTemplate->getNextPartialSpecSequenceNumber());
John McCallb6217662010-03-15 10:12:16 +00001838 // Substitute the nested name specifier, if any.
1839 if (SubstQualifier(PartialSpec, InstPartialSpec))
1840 return 0;
1841
Douglas Gregored9c0f92009-10-29 00:04:11 +00001842 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001843 InstPartialSpec->setTypeAsWritten(WrittenTy);
1844
Douglas Gregored9c0f92009-10-29 00:04:11 +00001845 // Add this partial specialization to the set of class template partial
1846 // specializations.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001847 ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001848 return false;
1849}
1850
John McCall21ef0fa2010-03-11 09:03:00 +00001851TypeSourceInfo*
John McCallce3ff2b2009-08-25 22:02:44 +00001852TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001853 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
John McCall21ef0fa2010-03-11 09:03:00 +00001854 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
1855 assert(OldTInfo && "substituting function without type source info");
1856 assert(Params.empty() && "parameter vector is non-empty at start");
John McCall6cd3b9f2010-04-09 17:38:44 +00001857 TypeSourceInfo *NewTInfo
1858 = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
1859 D->getTypeSpecStartLoc(),
1860 D->getDeclName());
John McCall21ef0fa2010-03-11 09:03:00 +00001861 if (!NewTInfo)
1862 return 0;
Douglas Gregor5545e162009-03-24 00:38:23 +00001863
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001864 if (NewTInfo != OldTInfo) {
1865 // Get parameters from the new type info.
Douglas Gregor895162d2010-04-30 18:55:50 +00001866 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001867 if (FunctionProtoTypeLoc *OldProtoLoc
1868 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1869 TypeLoc NewTL = NewTInfo->getTypeLoc();
1870 FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL);
1871 assert(NewProtoLoc && "Missing prototype?");
1872 for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) {
1873 // FIXME: Variadic templates will break this.
1874 Params.push_back(NewProtoLoc->getArg(i));
1875 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
Douglas Gregor895162d2010-04-30 18:55:50 +00001876 OldProtoLoc->getArg(i),
1877 NewProtoLoc->getArg(i));
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001878 }
Douglas Gregor895162d2010-04-30 18:55:50 +00001879 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001880 } else {
1881 // The function type itself was not dependent and therefore no
1882 // substitution occurred. However, we still need to instantiate
1883 // the function parameters themselves.
1884 TypeLoc OldTL = OldTInfo->getTypeLoc();
Douglas Gregor6920cdc2010-05-03 15:32:18 +00001885 if (FunctionProtoTypeLoc *OldProtoLoc
1886 = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) {
1887 for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) {
1888 ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i));
1889 if (!Parm)
1890 return 0;
1891 Params.push_back(Parm);
1892 }
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001893 }
1894 }
John McCall21ef0fa2010-03-11 09:03:00 +00001895 return NewTInfo;
Douglas Gregor5545e162009-03-24 00:38:23 +00001896}
1897
Mike Stump1eb44332009-09-09 15:08:12 +00001898/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001899/// declaration (New) from the corresponding fields of its template (Tmpl).
1900///
1901/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001902bool
1903TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001904 FunctionDecl *Tmpl) {
1905 if (Tmpl->isDeleted())
1906 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Douglas Gregorcca9e962009-07-01 22:01:06 +00001908 // If we are performing substituting explicitly-specified template arguments
1909 // or deduced template arguments into a function template and we reach this
1910 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001911 // to keeping the new function template specialization. We therefore
1912 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001913 // into a template instantiation for this specific function template
1914 // specialization, which is not a SFINAE context, so that we diagnose any
1915 // further errors in the declaration itself.
1916 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1917 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1918 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1919 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001920 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001921 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001922 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001923 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001924 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001925 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1926 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001927 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001928 }
1929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001931 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1932 assert(Proto && "Function template without prototype?");
1933
1934 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1935 Proto->getNoReturnAttr()) {
1936 // The function has an exception specification or a "noreturn"
1937 // attribute. Substitute into each of the exception types.
1938 llvm::SmallVector<QualType, 4> Exceptions;
1939 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1940 // FIXME: Poor location information!
1941 QualType T
1942 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1943 New->getLocation(), New->getDeclName());
1944 if (T.isNull() ||
1945 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1946 continue;
1947
1948 Exceptions.push_back(T);
1949 }
1950
1951 // Rebuild the function type
1952
1953 const FunctionProtoType *NewProto
1954 = New->getType()->getAs<FunctionProtoType>();
1955 assert(NewProto && "Template instantiation without function prototype?");
1956 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1957 NewProto->arg_type_begin(),
1958 NewProto->getNumArgs(),
1959 NewProto->isVariadic(),
1960 NewProto->getTypeQuals(),
1961 Proto->hasExceptionSpec(),
1962 Proto->hasAnyExceptionSpec(),
1963 Exceptions.size(),
1964 Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001965 Proto->getExtInfo()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001966 }
1967
John McCall1d8d1cc2010-08-01 02:01:53 +00001968 SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New);
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001969
Douglas Gregore53060f2009-06-25 22:08:12 +00001970 return false;
1971}
1972
Douglas Gregor5545e162009-03-24 00:38:23 +00001973/// \brief Initializes common fields of an instantiated method
1974/// declaration (New) from the corresponding fields of its template
1975/// (Tmpl).
1976///
1977/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001978bool
1979TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001980 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001981 if (InitFunctionInstantiation(New, Tmpl))
1982 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Douglas Gregor5545e162009-03-24 00:38:23 +00001984 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1985 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001986 if (Tmpl->isVirtualAsWritten())
1987 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001988
1989 // FIXME: attributes
1990 // FIXME: New needs a pointer to Tmpl
1991 return false;
1992}
Douglas Gregora58861f2009-05-13 20:28:22 +00001993
1994/// \brief Instantiate the definition of the given function from its
1995/// template.
1996///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001997/// \param PointOfInstantiation the point at which the instantiation was
1998/// required. Note that this is not precisely a "point of instantiation"
1999/// for the function, but it's close.
2000///
Douglas Gregora58861f2009-05-13 20:28:22 +00002001/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002002/// function template specialization or member function of a class template
2003/// specialization.
2004///
2005/// \param Recursive if true, recursively instantiates any functions that
2006/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002007///
2008/// \param DefinitionRequired if true, then we are performing an explicit
2009/// instantiation where the body of the function is required. Complain if
2010/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002011void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002012 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002013 bool Recursive,
2014 bool DefinitionRequired) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002015 if (Function->isInvalidDecl() || Function->hasBody())
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002016 return;
2017
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002018 // Never instantiate an explicit specialization.
2019 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2020 return;
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002021
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002022 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00002023 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002024 Stmt *Pattern = 0;
2025 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002026 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002027
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002028 if (!Pattern) {
2029 if (DefinitionRequired) {
2030 if (Function->getPrimaryTemplate())
2031 Diag(PointOfInstantiation,
2032 diag::err_explicit_instantiation_undefined_func_template)
2033 << Function->getPrimaryTemplate();
2034 else
2035 Diag(PointOfInstantiation,
2036 diag::err_explicit_instantiation_undefined_member)
2037 << 1 << Function->getDeclName() << Function->getDeclContext();
2038
2039 if (PatternDecl)
2040 Diag(PatternDecl->getLocation(),
2041 diag::note_explicit_instantiation_here);
Douglas Gregorcfe833b2010-05-17 17:57:54 +00002042 Function->setInvalidDecl();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002043 }
2044
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002045 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002046 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00002047
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002048 // C++0x [temp.explicit]p9:
2049 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00002050 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002051 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00002052 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002053 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00002054 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002055 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00002057 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
2058 if (Inst)
Douglas Gregore7089b02010-05-03 23:29:10 +00002059 return;
2060
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002061 // If we're performing recursive template instantiation, create our own
2062 // queue of pending implicit instantiations that we will instantiate later,
2063 // while we're still within our own instantiation context.
2064 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2065 if (Recursive)
2066 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregor9679caf2010-05-12 17:27:19 +00002068 EnterExpressionEvaluationContext EvalContext(*this,
2069 Action::PotentiallyEvaluated);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002070 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
2071
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002072 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00002073 // recorded, unless we're actually a member function within a local
2074 // class, in which case we need to merge our results with the parent
2075 // scope (of the enclosing function).
2076 bool MergeWithParentScope = false;
2077 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
2078 MergeWithParentScope = Rec->isLocalClass();
2079
2080 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002082 // Introduce the instantiated function parameters into the local
Peter Collingbourne8a6c0f12010-07-18 16:45:46 +00002083 // instantiation scope, and set the parameter names to those used
2084 // in the template.
2085 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
2086 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
2087 ParmVarDecl *FunctionParam = Function->getParamDecl(I);
2088 FunctionParam->setDeclName(PatternParam->getDeclName());
2089 Scope.InstantiatedLocal(PatternParam, FunctionParam);
2090 }
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002091
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002092 // Enter the scope of this instantiation. We don't use
2093 // PushDeclContext because we don't have a scope.
2094 DeclContext *PreviousContext = CurContext;
2095 CurContext = Function;
2096
Mike Stump1eb44332009-09-09 15:08:12 +00002097 MultiLevelTemplateArgumentList TemplateArgs =
Douglas Gregore7089b02010-05-03 23:29:10 +00002098 getTemplateInstantiationArgs(Function, 0, false, PatternDecl);
Anders Carlsson09025312009-08-29 05:16:22 +00002099
2100 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00002101 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00002102 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
2103 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
2104 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002105 }
2106
Douglas Gregor54dabfc2009-05-14 23:26:13 +00002107 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00002108 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002109
Douglas Gregor52604ab2009-09-11 21:19:12 +00002110 if (Body.isInvalid())
2111 Function->setInvalidDecl();
2112
Mike Stump1eb44332009-09-09 15:08:12 +00002113 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00002114 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002115
John McCall0c01d182010-03-24 05:22:00 +00002116 PerformDependentDiagnostics(PatternDecl, TemplateArgs);
2117
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00002118 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002119
2120 DeclGroupRef DG(Function);
2121 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Douglas Gregor60406be2010-01-16 22:29:39 +00002123 // This class may have local implicit instantiations that need to be
2124 // instantiation within this scope.
2125 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
2126 Scope.Exit();
2127
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002128 if (Recursive) {
2129 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002130 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002131 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002133 // Restore the set of pending implicit instantiations.
2134 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
2135 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002136}
2137
2138/// \brief Instantiate the definition of the given variable from its
2139/// template.
2140///
Douglas Gregor7caa6822009-07-24 20:34:43 +00002141/// \param PointOfInstantiation the point at which the instantiation was
2142/// required. Note that this is not precisely a "point of instantiation"
2143/// for the function, but it's close.
2144///
2145/// \param Var the already-instantiated declaration of a static member
2146/// variable of a class template specialization.
2147///
2148/// \param Recursive if true, recursively instantiates any functions that
2149/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002150///
2151/// \param DefinitionRequired if true, then we are performing an explicit
2152/// instantiation where an out-of-line definition of the member variable
2153/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002154void Sema::InstantiateStaticDataMemberDefinition(
2155 SourceLocation PointOfInstantiation,
2156 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002157 bool Recursive,
2158 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002159 if (Var->isInvalidDecl())
2160 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Douglas Gregor7caa6822009-07-24 20:34:43 +00002162 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002163 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00002164 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00002165 assert(Def->isStaticDataMember() && "Not a static data member?");
2166 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Douglas Gregor0d035142009-10-27 18:42:08 +00002168 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00002169 // We did not find an out-of-line definition of this static data member,
2170 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00002171 // instantiate this definition (or provide a specialization for it) in
2172 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002173 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002174 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00002175 Diag(PointOfInstantiation,
2176 diag::err_explicit_instantiation_undefined_member)
2177 << 2 << Var->getDeclName() << Var->getDeclContext();
2178 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
2179 }
2180
Douglas Gregor7caa6822009-07-24 20:34:43 +00002181 return;
2182 }
2183
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002184 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002185 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002186 return;
2187
2188 // C++0x [temp.explicit]p9:
2189 // Except for inline functions, other explicit instantiation declarations
2190 // have the effect of suppressing the implicit instantiation of the entity
2191 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002192 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002193 == TSK_ExplicitInstantiationDeclaration)
2194 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Douglas Gregor7caa6822009-07-24 20:34:43 +00002196 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
2197 if (Inst)
2198 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Douglas Gregor7caa6822009-07-24 20:34:43 +00002200 // If we're performing recursive template instantiation, create our own
2201 // queue of pending implicit instantiations that we will instantiate later,
2202 // while we're still within our own instantiation context.
2203 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
2204 if (Recursive)
2205 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Douglas Gregor7caa6822009-07-24 20:34:43 +00002207 // Enter the scope of this instantiation. We don't use
2208 // PushDeclContext because we don't have a scope.
2209 DeclContext *PreviousContext = CurContext;
2210 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor1028c9f2009-10-14 21:29:40 +00002212 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00002213 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00002214 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00002215 CurContext = PreviousContext;
2216
2217 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00002218 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
2219 assert(MSInfo && "Missing member specialization information?");
2220 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
2221 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00002222 DeclGroupRef DG(Var);
2223 Consumer.HandleTopLevelDecl(DG);
2224 }
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Douglas Gregor7caa6822009-07-24 20:34:43 +00002226 if (Recursive) {
2227 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00002228 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00002229 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor7caa6822009-07-24 20:34:43 +00002231 // Restore the set of pending implicit instantiations.
2232 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00002233 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002234}
Douglas Gregor815215d2009-05-27 05:35:12 +00002235
Anders Carlsson09025312009-08-29 05:16:22 +00002236void
2237Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
2238 const CXXConstructorDecl *Tmpl,
2239 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Anders Carlsson09025312009-08-29 05:16:22 +00002241 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002242 bool AnyErrors = false;
2243
Anders Carlsson09025312009-08-29 05:16:22 +00002244 // Instantiate all the initializers.
2245 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00002246 InitsEnd = Tmpl->init_end();
2247 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00002248 CXXBaseOrMemberInitializer *Init = *Inits;
2249
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002250 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00002251 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002252 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00002254 // Instantiate the initializer.
2255 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
2256 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
2257 AnyErrors = true;
2258 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00002259 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002260
Anders Carlsson09025312009-08-29 05:16:22 +00002261 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00002262 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00002263 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002264 TemplateArgs,
2265 Init->getSourceLocation(),
2266 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00002267 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002268 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00002269 New->setInvalidDecl();
2270 continue;
2271 }
2272
John McCalla93c9342009-12-07 02:54:59 +00002273 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00002274 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002275 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002276 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002277 Init->getRParenLoc(),
2278 New->getParent());
2279 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002280 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002282 // Is this an anonymous union?
2283 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002284 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2285 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00002286 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002287 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
2288 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00002289 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00002290
2291 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00002292 NewArgs.size(),
2293 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00002294 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00002295 Init->getRParenLoc());
2296 }
2297
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002298 if (NewInit.isInvalid()) {
2299 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00002300 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002301 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00002302 // FIXME: It would be nice if ASTOwningVector had a release function.
2303 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Anders Carlsson09025312009-08-29 05:16:22 +00002305 NewInits.push_back((MemInitTy *)NewInit.get());
2306 }
2307 }
Mike Stump1eb44332009-09-09 15:08:12 +00002308
Anders Carlsson09025312009-08-29 05:16:22 +00002309 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00002310 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00002311 /*FIXME: ColonLoc */
2312 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002313 NewInits.data(), NewInits.size(),
2314 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00002315}
2316
John McCall52a575a2009-08-29 08:11:13 +00002317// TODO: this could be templated if the various decl types used the
2318// same method name.
2319static bool isInstantiationOf(ClassTemplateDecl *Pattern,
2320 ClassTemplateDecl *Instance) {
2321 Pattern = Pattern->getCanonicalDecl();
2322
2323 do {
2324 Instance = Instance->getCanonicalDecl();
2325 if (Pattern == Instance) return true;
2326 Instance = Instance->getInstantiatedFromMemberTemplate();
2327 } while (Instance);
2328
2329 return false;
2330}
2331
Douglas Gregor0d696532009-09-28 06:34:35 +00002332static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2333 FunctionTemplateDecl *Instance) {
2334 Pattern = Pattern->getCanonicalDecl();
2335
2336 do {
2337 Instance = Instance->getCanonicalDecl();
2338 if (Pattern == Instance) return true;
2339 Instance = Instance->getInstantiatedFromMemberTemplate();
2340 } while (Instance);
2341
2342 return false;
2343}
2344
Douglas Gregored9c0f92009-10-29 00:04:11 +00002345static bool
2346isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2347 ClassTemplatePartialSpecializationDecl *Instance) {
2348 Pattern
2349 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2350 do {
2351 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2352 Instance->getCanonicalDecl());
2353 if (Pattern == Instance)
2354 return true;
2355 Instance = Instance->getInstantiatedFromMember();
2356 } while (Instance);
2357
2358 return false;
2359}
2360
John McCall52a575a2009-08-29 08:11:13 +00002361static bool isInstantiationOf(CXXRecordDecl *Pattern,
2362 CXXRecordDecl *Instance) {
2363 Pattern = Pattern->getCanonicalDecl();
2364
2365 do {
2366 Instance = Instance->getCanonicalDecl();
2367 if (Pattern == Instance) return true;
2368 Instance = Instance->getInstantiatedFromMemberClass();
2369 } while (Instance);
2370
2371 return false;
2372}
2373
2374static bool isInstantiationOf(FunctionDecl *Pattern,
2375 FunctionDecl *Instance) {
2376 Pattern = Pattern->getCanonicalDecl();
2377
2378 do {
2379 Instance = Instance->getCanonicalDecl();
2380 if (Pattern == Instance) return true;
2381 Instance = Instance->getInstantiatedFromMemberFunction();
2382 } while (Instance);
2383
2384 return false;
2385}
2386
2387static bool isInstantiationOf(EnumDecl *Pattern,
2388 EnumDecl *Instance) {
2389 Pattern = Pattern->getCanonicalDecl();
2390
2391 do {
2392 Instance = Instance->getCanonicalDecl();
2393 if (Pattern == Instance) return true;
2394 Instance = Instance->getInstantiatedFromMemberEnum();
2395 } while (Instance);
2396
2397 return false;
2398}
2399
John McCalled976492009-12-04 22:46:56 +00002400static bool isInstantiationOf(UsingShadowDecl *Pattern,
2401 UsingShadowDecl *Instance,
2402 ASTContext &C) {
2403 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2404}
2405
2406static bool isInstantiationOf(UsingDecl *Pattern,
2407 UsingDecl *Instance,
2408 ASTContext &C) {
2409 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2410}
2411
John McCall7ba107a2009-11-18 02:36:19 +00002412static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2413 UsingDecl *Instance,
2414 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002415 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002416}
2417
2418static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002419 UsingDecl *Instance,
2420 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002421 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002422}
2423
John McCall52a575a2009-08-29 08:11:13 +00002424static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2425 VarDecl *Instance) {
2426 assert(Instance->isStaticDataMember());
2427
2428 Pattern = Pattern->getCanonicalDecl();
2429
2430 do {
2431 Instance = Instance->getCanonicalDecl();
2432 if (Pattern == Instance) return true;
2433 Instance = Instance->getInstantiatedFromStaticDataMember();
2434 } while (Instance);
2435
2436 return false;
2437}
2438
John McCalled976492009-12-04 22:46:56 +00002439// Other is the prospective instantiation
2440// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002441static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002442 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002443 if (UnresolvedUsingTypenameDecl *UUD
2444 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2445 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2446 return isInstantiationOf(UUD, UD, Ctx);
2447 }
2448 }
2449
2450 if (UnresolvedUsingValueDecl *UUD
2451 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002452 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2453 return isInstantiationOf(UUD, UD, Ctx);
2454 }
2455 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002456
Anders Carlsson0d8df782009-08-29 19:37:28 +00002457 return false;
2458 }
Mike Stump1eb44332009-09-09 15:08:12 +00002459
John McCall52a575a2009-08-29 08:11:13 +00002460 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2461 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002462
John McCall52a575a2009-08-29 08:11:13 +00002463 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2464 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002465
John McCall52a575a2009-08-29 08:11:13 +00002466 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2467 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002468
Douglas Gregor7caa6822009-07-24 20:34:43 +00002469 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002470 if (Var->isStaticDataMember())
2471 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2472
2473 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2474 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002475
Douglas Gregor0d696532009-09-28 06:34:35 +00002476 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2477 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2478
Douglas Gregored9c0f92009-10-29 00:04:11 +00002479 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2480 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2481 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2482 PartialSpec);
2483
Anders Carlssond8b285f2009-09-01 04:26:58 +00002484 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2485 if (!Field->getDeclName()) {
2486 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002487 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002488 cast<FieldDecl>(D);
2489 }
2490 }
Mike Stump1eb44332009-09-09 15:08:12 +00002491
John McCalled976492009-12-04 22:46:56 +00002492 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2493 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2494
2495 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2496 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2497
Douglas Gregor815215d2009-05-27 05:35:12 +00002498 return D->getDeclName() && isa<NamedDecl>(Other) &&
2499 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2500}
2501
2502template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002503static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002504 NamedDecl *D,
2505 ForwardIterator first,
2506 ForwardIterator last) {
2507 for (; first != last; ++first)
2508 if (isInstantiationOf(Ctx, D, *first))
2509 return cast<NamedDecl>(*first);
2510
2511 return 0;
2512}
2513
John McCall02cace72009-08-28 07:59:38 +00002514/// \brief Finds the instantiation of the given declaration context
2515/// within the current instantiation.
2516///
2517/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002518DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002519 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002520 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002521 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002522 return cast_or_null<DeclContext>(ID);
2523 } else return DC;
2524}
2525
Douglas Gregored961e72009-05-27 17:54:46 +00002526/// \brief Find the instantiation of the given declaration within the
2527/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002528///
2529/// This routine is intended to be used when \p D is a declaration
2530/// referenced from within a template, that needs to mapped into the
2531/// corresponding declaration within an instantiation. For example,
2532/// given:
2533///
2534/// \code
2535/// template<typename T>
2536/// struct X {
2537/// enum Kind {
2538/// KnownValue = sizeof(T)
2539/// };
2540///
2541/// bool getKind() const { return KnownValue; }
2542/// };
2543///
2544/// template struct X<int>;
2545/// \endcode
2546///
2547/// In the instantiation of X<int>::getKind(), we need to map the
2548/// EnumConstantDecl for KnownValue (which refers to
2549/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002550/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2551/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002552NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002553 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002554 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002555 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002556 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002557 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002558 // D is a local of some kind. Look into the map of local
2559 // declarations to their instantiations.
Fariborz Jahanian8dd0c562010-07-13 00:16:40 +00002560 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002561 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002562
Douglas Gregore95b4092009-09-16 18:34:49 +00002563 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2564 if (!Record->isDependentContext())
2565 return D;
2566
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002567 // If the RecordDecl is actually the injected-class-name or a
2568 // "templated" declaration for a class template, class template
2569 // partial specialization, or a member class of a class template,
2570 // substitute into the injected-class-name of the class template
2571 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002572 QualType T;
2573 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2574
2575 if (ClassTemplate) {
Douglas Gregor24bae922010-07-08 18:37:38 +00002576 T = ClassTemplate->getInjectedClassNameSpecialization();
Douglas Gregore95b4092009-09-16 18:34:49 +00002577 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2578 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002579 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002580
2581 // If we call SubstType with an InjectedClassNameType here we
2582 // can end up in an infinite loop.
2583 T = Context.getTypeDeclType(Record);
2584 assert(isa<InjectedClassNameType>(T) &&
2585 "type of partial specialization is not an InjectedClassNameType");
John McCall31f17ec2010-04-27 00:57:59 +00002586 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00002587 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002588
2589 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002590 // Substitute into the injected-class-name to get the type
2591 // corresponding to the instantiation we want, which may also be
2592 // the current instantiation (if we're in a template
2593 // definition). This substitution should never fail, since we
2594 // know we can instantiate the injected-class-name or we
2595 // wouldn't have gotten to the injected-class-name!
2596
2597 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2598 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002599 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2600 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2601
2602 if (!T->isDependentType()) {
2603 assert(T->isRecordType() && "Instantiation must produce a record type");
2604 return T->getAs<RecordType>()->getDecl();
2605 }
2606
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002607 // We are performing "partial" template instantiation to create
2608 // the member declarations for the members of a class template
2609 // specialization. Therefore, D is actually referring to something
2610 // in the current instantiation. Look through the current
2611 // context, which contains actual instantiations, to find the
2612 // instantiation of the "current instantiation" that D refers
2613 // to.
2614 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002615 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002616 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002617 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002618 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002619 if (isInstantiationOf(ClassTemplate,
2620 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002621 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002622
2623 if (!DC->isDependentContext())
2624 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002625 }
2626
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002627 // We're performing "instantiation" of a member of the current
2628 // instantiation while we are type-checking the
2629 // definition. Compute the declaration context and return that.
2630 assert(!SawNonDependentContext &&
2631 "No dependent context while instantiating record");
2632 DeclContext *DC = computeDeclContext(T);
2633 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002634 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002635 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002636 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002637
Douglas Gregore95b4092009-09-16 18:34:49 +00002638 // Fall through to deal with other dependent record types (e.g.,
2639 // anonymous unions in class templates).
2640 }
John McCall52a575a2009-08-29 08:11:13 +00002641
Douglas Gregore95b4092009-09-16 18:34:49 +00002642 if (!ParentDC->isDependentContext())
2643 return D;
2644
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002645 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002646 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002647 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002648
Douglas Gregor815215d2009-05-27 05:35:12 +00002649 if (ParentDC != D->getDeclContext()) {
2650 // We performed some kind of instantiation in the parent context,
2651 // so now we need to look into the instantiated parent context to
2652 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002653
John McCall3cb0ebd2010-03-10 03:28:59 +00002654 // If our context used to be dependent, we may need to instantiate
2655 // it before performing lookup into that context.
2656 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002657 if (!Spec->isDependentContext()) {
2658 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002659 const RecordType *Tag = T->getAs<RecordType>();
2660 assert(Tag && "type of non-dependent record is not a RecordType");
2661 if (!Tag->isBeingDefined() &&
2662 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2663 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002664 }
2665 }
2666
Douglas Gregor815215d2009-05-27 05:35:12 +00002667 NamedDecl *Result = 0;
2668 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002669 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002670 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2671 } else {
2672 // Since we don't have a name for the entity we're looking for,
2673 // our only option is to walk through all of the declarations to
2674 // find that name. This will occur in a few cases:
2675 //
2676 // - anonymous struct/union within a template
2677 // - unnamed class/struct/union/enum within a template
2678 //
2679 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002680 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002681 ParentDC->decls_begin(),
2682 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002683 }
Mike Stump1eb44332009-09-09 15:08:12 +00002684
John McCall9f54ad42009-12-10 09:41:52 +00002685 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002686 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2687 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002688 && "Unable to find instantiation of declaration!");
2689
Douglas Gregor815215d2009-05-27 05:35:12 +00002690 D = Result;
2691 }
2692
Douglas Gregor815215d2009-05-27 05:35:12 +00002693 return D;
2694}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002695
Mike Stump1eb44332009-09-09 15:08:12 +00002696/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002697/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002698void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2699 while (!PendingLocalImplicitInstantiations.empty() ||
2700 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2701 PendingImplicitInstantiation Inst;
2702
2703 if (PendingLocalImplicitInstantiations.empty()) {
2704 Inst = PendingImplicitInstantiations.front();
2705 PendingImplicitInstantiations.pop_front();
2706 } else {
2707 Inst = PendingLocalImplicitInstantiations.front();
2708 PendingLocalImplicitInstantiations.pop_front();
2709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Douglas Gregor7caa6822009-07-24 20:34:43 +00002711 // Instantiate function definitions
2712 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002713 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002714 Function->getLocation(), *this,
2715 Context.getSourceManager(),
2716 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00002718 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002719 continue;
2720 }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Douglas Gregor7caa6822009-07-24 20:34:43 +00002722 // Instantiate static data member definitions.
2723 VarDecl *Var = cast<VarDecl>(Inst.first);
2724 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002725
Chandler Carruth291b4412010-02-13 10:17:50 +00002726 // Don't try to instantiate declarations if the most recent redeclaration
2727 // is invalid.
2728 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2729 continue;
2730
2731 // Check if the most recent declaration has changed the specialization kind
2732 // and removed the need for implicit instantiation.
2733 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2734 case TSK_Undeclared:
2735 assert(false && "Cannot instantitiate an undeclared specialization.");
2736 case TSK_ExplicitInstantiationDeclaration:
2737 case TSK_ExplicitInstantiationDefinition:
2738 case TSK_ExplicitSpecialization:
2739 continue; // No longer need implicit instantiation.
2740 case TSK_ImplicitInstantiation:
2741 break;
2742 }
2743
Mike Stump1eb44332009-09-09 15:08:12 +00002744 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002745 Var->getLocation(), *this,
2746 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}