blob: cf8d38c09583978d907dce7d867f7de74d901efb [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"
18#include "clang/AST/Expr.h"
Douglas Gregora88cfbf2009-12-12 18:16:41 +000019#include "clang/AST/ExprCXX.h"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000020#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000022
23using namespace clang;
24
25namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000026 class TemplateDeclInstantiator
Chris Lattnerb28317a2009-03-28 19:18:32 +000027 : public DeclVisitor<TemplateDeclInstantiator, Decl *> {
Douglas Gregor8dbc2692009-03-17 21:15:40 +000028 Sema &SemaRef;
29 DeclContext *Owner;
Douglas Gregord6350ae2009-08-28 20:31:08 +000030 const MultiLevelTemplateArgumentList &TemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +000031
Anders Carlssond8fe2d52009-11-07 06:07:58 +000032 void InstantiateAttrs(Decl *Tmpl, Decl *New);
33
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
43 // clang/AST/DeclNodes.def.
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);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000049 Decl *VisitFieldDecl(FieldDecl *D);
50 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
51 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000052 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000053 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000054 Decl *VisitFunctionDecl(FunctionDecl *D,
55 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000056 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000057 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
58 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000059 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000060 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000061 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000062 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000063 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000064 Decl *VisitClassTemplatePartialSpecializationDecl(
65 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000066 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000067 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000068 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000069 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000070 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000071 Decl *VisitUsingDecl(UsingDecl *D);
72 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000073 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
74 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor8dbc2692009-03-17 21:15:40 +000076 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000077 Decl *VisitDecl(Decl *D) {
78 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
79 Diagnostic::Error,
80 "cannot instantiate %0 yet");
81 SemaRef.Diag(D->getLocation(), DiagID)
82 << D->getDeclKindName();
83
Douglas Gregor8dbc2692009-03-17 21:15:40 +000084 return 0;
85 }
Douglas Gregor5545e162009-03-24 00:38:23 +000086
John McCallfd810b12009-08-14 02:03:10 +000087 const LangOptions &getLangOptions() {
88 return SemaRef.getLangOptions();
89 }
90
Douglas Gregor5545e162009-03-24 00:38:23 +000091 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000092 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000093 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000094 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000095 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000096
97 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000098 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000099
100 bool InstantiateClassTemplatePartialSpecialization(
101 ClassTemplateDecl *ClassTemplate,
102 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000103 };
104}
105
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000106// FIXME: Is this too simple?
107void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
108 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
109 TmplAttr = TmplAttr->getNext()) {
110
111 // FIXME: Is cloning correct for all attributes?
112 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
113
114 New->addAttr(NewAttr);
115 }
116}
117
Douglas Gregor4f722be2009-03-25 15:45:12 +0000118Decl *
119TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
120 assert(false && "Translation units cannot be instantiated");
121 return D;
122}
123
124Decl *
125TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
126 assert(false && "Namespaces cannot be instantiated");
127 return D;
128}
129
John McCall3dbd3d52010-02-16 06:53:13 +0000130Decl *
131TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
132 NamespaceAliasDecl *Inst
133 = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
134 D->getNamespaceLoc(),
135 D->getAliasLoc(),
136 D->getNamespace()->getIdentifier(),
137 D->getQualifierRange(),
138 D->getQualifier(),
139 D->getTargetNameLoc(),
140 D->getNamespace());
141 Owner->addDecl(Inst);
142 return Inst;
143}
144
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
146 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000147 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000148 if (DI->getType()->isDependentType()) {
149 DI = SemaRef.SubstType(DI, TemplateArgs,
150 D->getLocation(), D->getDeclName());
151 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000152 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000153 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000154 }
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000157 // Create the new typedef
158 TypedefDecl *Typedef
159 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000160 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000161 if (Invalid)
162 Typedef->setInvalidDecl();
163
John McCall5126fd02009-12-30 00:31:22 +0000164 if (TypedefDecl *Prev = D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000165 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
166 TemplateArgs);
John McCall5126fd02009-12-30 00:31:22 +0000167 Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev));
168 }
169
John McCall46460a62010-01-20 21:53:11 +0000170 Typedef->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000171 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000173 return Typedef;
174}
175
Douglas Gregor6eef5192009-12-14 19:27:10 +0000176/// \brief Instantiate the arguments provided as part of initialization.
177///
178/// \returns true if an error occurred, false otherwise.
179static bool InstantiateInitializationArguments(Sema &SemaRef,
180 Expr **Args, unsigned NumArgs,
181 const MultiLevelTemplateArgumentList &TemplateArgs,
182 llvm::SmallVectorImpl<SourceLocation> &FakeCommaLocs,
183 ASTOwningVector<&ActionBase::DeleteExpr> &InitArgs) {
184 for (unsigned I = 0; I != NumArgs; ++I) {
185 // When we hit the first defaulted argument, break out of the loop:
186 // we don't pass those default arguments on.
187 if (Args[I]->isDefaultArgument())
188 break;
189
190 Sema::OwningExprResult Arg = SemaRef.SubstExpr(Args[I], TemplateArgs);
191 if (Arg.isInvalid())
192 return true;
193
194 Expr *ArgExpr = (Expr *)Arg.get();
195 InitArgs.push_back(Arg.release());
196
197 // FIXME: We're faking all of the comma locations. Do we need them?
198 FakeCommaLocs.push_back(
199 SemaRef.PP.getLocForEndOfToken(ArgExpr->getLocEnd()));
200 }
201
202 return false;
203}
204
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000205/// \brief Instantiate an initializer, breaking it into separate
206/// initialization arguments.
207///
208/// \param S The semantic analysis object.
209///
210/// \param Init The initializer to instantiate.
211///
212/// \param TemplateArgs Template arguments to be substituted into the
213/// initializer.
214///
215/// \param NewArgs Will be filled in with the instantiation arguments.
216///
217/// \returns true if an error occurred, false otherwise
218static bool InstantiateInitializer(Sema &S, Expr *Init,
219 const MultiLevelTemplateArgumentList &TemplateArgs,
220 SourceLocation &LParenLoc,
221 llvm::SmallVector<SourceLocation, 4> &CommaLocs,
222 ASTOwningVector<&ActionBase::DeleteExpr> &NewArgs,
223 SourceLocation &RParenLoc) {
224 NewArgs.clear();
225 LParenLoc = SourceLocation();
226 RParenLoc = SourceLocation();
227
228 if (!Init)
229 return false;
230
231 if (CXXExprWithTemporaries *ExprTemp = dyn_cast<CXXExprWithTemporaries>(Init))
232 Init = ExprTemp->getSubExpr();
233
234 while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init))
235 Init = Binder->getSubExpr();
236
237 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init))
238 Init = ICE->getSubExprAsWritten();
239
240 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
241 LParenLoc = ParenList->getLParenLoc();
242 RParenLoc = ParenList->getRParenLoc();
243 return InstantiateInitializationArguments(S, ParenList->getExprs(),
244 ParenList->getNumExprs(),
245 TemplateArgs, CommaLocs,
246 NewArgs);
247 }
248
249 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) {
250 if (InstantiateInitializationArguments(S,
251 Construct->getArgs(),
252 Construct->getNumArgs(),
253 TemplateArgs,
254 CommaLocs, NewArgs))
255 return true;
256
257 // FIXME: Fake locations!
258 LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart());
259 RParenLoc = CommaLocs.empty()? LParenLoc : CommaLocs.back();
260 return false;
261 }
262
263 Sema::OwningExprResult Result = S.SubstExpr(Init, TemplateArgs);
264 if (Result.isInvalid())
265 return true;
266
267 NewArgs.push_back(Result.takeAs<Expr>());
268 return false;
269}
270
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000271Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000272 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000273 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000274 TemplateArgs,
275 D->getTypeSpecStartLoc(),
276 D->getDeclName());
277 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000278 return 0;
279
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000280 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000281 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
282 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000283 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000284 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000285 Var->setThreadSpecified(D->isThreadSpecified());
286 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
287 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000288
289 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000290 // out-of-line, the instantiation will have the same lexical
291 // context (which will be a namespace scope) as the template.
292 if (D->isOutOfLine())
293 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000294
John McCall46460a62010-01-20 21:53:11 +0000295 Var->setAccess(D->getAccess());
296
Mike Stump390b4cc2009-05-16 07:39:55 +0000297 // FIXME: In theory, we could have a previous declaration for variables that
298 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000299 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000300 // FIXME: having to fake up a LookupResult is dumb.
301 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
Douglas Gregor449d0a82010-03-01 19:11:54 +0000302 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Douglas Gregor60c93c92010-02-09 07:26:29 +0000303 if (D->isStaticDataMember())
304 SemaRef.LookupQualifiedName(Previous, Owner, false);
John McCall68263142009-11-18 22:49:29 +0000305 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Douglas Gregor7caa6822009-07-24 20:34:43 +0000307 if (D->isOutOfLine()) {
308 D->getLexicalDeclContext()->addDecl(Var);
309 Owner->makeDeclVisibleInContext(Var);
310 } else {
311 Owner->addDecl(Var);
312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000314 // Link instantiations of static data members back to the template from
315 // which they were instantiated.
316 if (Var->isStaticDataMember())
317 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000318 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000319
Douglas Gregor60c93c92010-02-09 07:26:29 +0000320 if (Var->getAnyInitializer()) {
321 // We already have an initializer in the class.
322 } else if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000323 if (Var->isStaticDataMember() && !D->isOutOfLine())
324 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
325 else
326 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
327
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000328 // Instantiate the initializer.
329 SourceLocation LParenLoc, RParenLoc;
330 llvm::SmallVector<SourceLocation, 4> CommaLocs;
331 ASTOwningVector<&ActionBase::DeleteExpr> InitArgs(SemaRef);
332 if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc,
333 CommaLocs, InitArgs, RParenLoc)) {
334 // Attach the initializer to the declaration.
335 if (D->hasCXXDirectInitializer()) {
Douglas Gregor6eef5192009-12-14 19:27:10 +0000336 // Add the direct initializer to the declaration.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000337 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000338 LParenLoc,
Douglas Gregor6eef5192009-12-14 19:27:10 +0000339 move_arg(InitArgs),
340 CommaLocs.data(),
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000341 RParenLoc);
342 } else if (InitArgs.size() == 1) {
343 Expr *Init = (Expr*)(InitArgs.take()[0]);
344 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
345 SemaRef.Owned(Init),
346 false);
347 } else {
348 assert(InitArgs.size() == 0);
349 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor6eef5192009-12-14 19:27:10 +0000350 }
Douglas Gregor6eef5192009-12-14 19:27:10 +0000351 } else {
Douglas Gregor6b98b2e2010-03-02 07:38:39 +0000352 // FIXME: Not too happy about invalidating the declaration
353 // because of a bogus initializer.
354 Var->setInvalidDecl();
Douglas Gregor6eef5192009-12-14 19:27:10 +0000355 }
356
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000357 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000358 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
359 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000360
361 return Var;
362}
363
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000364Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
365 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000366 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000367 if (DI->getType()->isDependentType()) {
368 DI = SemaRef.SubstType(DI, TemplateArgs,
369 D->getLocation(), D->getDeclName());
370 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000371 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000372 Invalid = true;
373 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000374 // C++ [temp.arg.type]p3:
375 // If a declaration acquires a function type through a type
376 // dependent on a template-parameter and this causes a
377 // declaration that does not use the syntactic form of a
378 // function declarator to have function type, the program is
379 // ill-formed.
380 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000381 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000382 Invalid = true;
383 }
384 }
385
386 Expr *BitWidth = D->getBitWidth();
387 if (Invalid)
388 BitWidth = 0;
389 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000390 // The bit-width expression is not potentially evaluated.
391 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000393 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000394 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000395 if (InstantiatedBitWidth.isInvalid()) {
396 Invalid = true;
397 BitWidth = 0;
398 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000399 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000400 }
401
John McCall07fb6be2009-10-22 23:33:21 +0000402 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
403 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000404 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000405 D->getLocation(),
406 D->isMutable(),
407 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000408 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000409 D->getAccess(),
410 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000411 if (!Field) {
412 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000413 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000416 InstantiateAttrs(D, Field);
417
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000418 if (Invalid)
419 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000421 if (!Field->getDeclName()) {
422 // Keep track of where this decl came from.
423 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000426 Field->setImplicit(D->isImplicit());
John McCall46460a62010-01-20 21:53:11 +0000427 Field->setAccess(D->getAccess());
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000428 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000429
430 return Field;
431}
432
John McCall02cace72009-08-28 07:59:38 +0000433Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
434 FriendDecl::FriendUnion FU;
435
436 // Handle friend type expressions by simply substituting template
437 // parameters into the pattern type.
438 if (Type *Ty = D->getFriendType()) {
439 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
440 D->getLocation(), DeclarationName());
441 if (T.isNull()) return 0;
442
443 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
444 FU = T.getTypePtr();
445
446 // Handle everything else by appropriate substitution.
447 } else {
448 NamedDecl *ND = D->getFriendDecl();
449 assert(ND && "friend decl must be a decl or a type!");
450
Douglas Gregora735b202009-10-13 14:39:41 +0000451 // FIXME: We have a problem here, because the nested call to Visit(ND)
452 // will inject the thing that the friend references into the current
453 // owner, which is wrong.
John McCalle129d442009-12-17 23:21:11 +0000454 Decl *NewND;
455
456 // Hack to make this work almost well pending a rewrite.
Douglas Gregor63644fa2010-02-07 10:31:35 +0000457 if (ND->getDeclContext()->isRecord()) {
458 if (!ND->getDeclContext()->isDependentContext()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000459 NewND = SemaRef.FindInstantiatedDecl(D->getLocation(), ND,
460 TemplateArgs);
Douglas Gregor63644fa2010-02-07 10:31:35 +0000461 } else {
462 // FIXME: Hack to avoid crashing when incorrectly trying to instantiate
463 // templated friend declarations. This doesn't produce a correct AST;
464 // however this is sufficient for some AST analysis. The real solution
465 // must be put in place during the pending rewrite. See PR5848.
466 return 0;
467 }
468 } else if (D->wasSpecialization()) {
Douglas Gregor7557a132009-12-24 20:56:24 +0000469 // Totally egregious hack to work around PR5866
470 return 0;
471 } else
John McCalle129d442009-12-17 23:21:11 +0000472 NewND = Visit(ND);
John McCall02cace72009-08-28 07:59:38 +0000473 if (!NewND) return 0;
474
475 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
John McCall02cace72009-08-28 07:59:38 +0000478 FriendDecl *FD =
479 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
480 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000481 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000482 Owner->addDecl(FD);
483 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000484}
485
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000486Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
487 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregorac7610d2009-06-22 20:57:11 +0000489 // The expression in a static assertion is not potentially evaluated.
490 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000492 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000493 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000494 if (InstantiatedAssertExpr.isInvalid())
495 return 0;
496
Douglas Gregor43d9d922009-08-08 01:41:12 +0000497 OwningExprResult Message(SemaRef, D->getMessage());
498 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000499 Decl *StaticAssert
500 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000501 move(InstantiatedAssertExpr),
502 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000503 return StaticAssert;
504}
505
506Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000507 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000508 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000509 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000510 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000511 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000512 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000513 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000514 Enum->startDefinition();
515
Douglas Gregor96084f12010-03-01 19:00:07 +0000516 if (D->getDeclContext()->isFunctionOrMethod())
517 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
518
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000519 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000520
521 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000522 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
523 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000524 EC != ECEnd; ++EC) {
525 // The specified value for the enumerator.
526 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000527 if (Expr *UninstValue = EC->getInitExpr()) {
528 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000529 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000530 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
John McCallce3ff2b2009-08-25 22:02:44 +0000532 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000533 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000534
535 // Drop the initial value and continue.
536 bool isInvalid = false;
537 if (Value.isInvalid()) {
538 Value = SemaRef.Owned((Expr *)0);
539 isInvalid = true;
540 }
541
Mike Stump1eb44332009-09-09 15:08:12 +0000542 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000543 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
544 EC->getLocation(), EC->getIdentifier(),
545 move(Value));
546
547 if (isInvalid) {
548 if (EnumConst)
549 EnumConst->setInvalidDecl();
550 Enum->setInvalidDecl();
551 }
552
553 if (EnumConst) {
John McCall3b85ecf2010-01-23 22:37:59 +0000554 EnumConst->setAccess(Enum->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000555 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000556 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000557 LastEnumConst = EnumConst;
Douglas Gregor96084f12010-03-01 19:00:07 +0000558
559 if (D->getDeclContext()->isFunctionOrMethod()) {
560 // If the enumeration is within a function or method, record the enum
561 // constant as a local.
562 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
563 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000564 }
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000567 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000568 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000569 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
570 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000571 &Enumerators[0], Enumerators.size(),
572 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000573
574 return Enum;
575}
576
Douglas Gregor6477b692009-03-25 15:04:13 +0000577Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
578 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
579 return 0;
580}
581
Douglas Gregored9c0f92009-10-29 00:04:11 +0000582namespace {
583 class SortDeclByLocation {
584 SourceManager &SourceMgr;
585
586 public:
587 explicit SortDeclByLocation(SourceManager &SourceMgr)
588 : SourceMgr(SourceMgr) { }
589
590 bool operator()(const Decl *X, const Decl *Y) const {
591 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
592 Y->getLocation());
593 }
594 };
595}
596
John McCalle29ba202009-08-20 01:44:21 +0000597Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000598 // Create a local instantiation scope for this class template, which
599 // will contain the instantiations of the template parameters.
600 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000601 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000602 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000603 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000604 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000605
606 CXXRecordDecl *Pattern = D->getTemplatedDecl();
607 CXXRecordDecl *RecordInst
608 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
609 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000610 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
611 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000612
613 ClassTemplateDecl *Inst
614 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
615 D->getIdentifier(), InstParams, RecordInst, 0);
616 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000617 if (D->getFriendObjectKind())
618 Inst->setObjectOfFriendDecl(true);
619 else
620 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000621 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000622
623 // Trigger creation of the type for the instantiation.
John McCall3cb0ebd2010-03-10 03:28:59 +0000624 SemaRef.Context.getInjectedClassNameType(RecordInst,
625 Inst->getInjectedClassNameSpecialization(SemaRef.Context));
Douglas Gregorf0510d42009-10-12 23:11:44 +0000626
Douglas Gregor259571e2009-10-30 22:42:42 +0000627 // Finish handling of friends.
628 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000629 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000630 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000631
John McCall46460a62010-01-20 21:53:11 +0000632 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000633 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000634
635 // First, we sort the partial specializations by location, so
636 // that we instantiate them in the order they were declared.
637 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
638 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
639 P = D->getPartialSpecializations().begin(),
640 PEnd = D->getPartialSpecializations().end();
641 P != PEnd; ++P)
642 PartialSpecs.push_back(&*P);
643 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
644 SortDeclByLocation(SemaRef.SourceMgr));
645
646 // Instantiate all of the partial specializations of this member class
647 // template.
648 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
649 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
650
John McCalle29ba202009-08-20 01:44:21 +0000651 return Inst;
652}
653
Douglas Gregord60e1052009-08-27 16:57:43 +0000654Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000655TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
656 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000657 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
658
659 // Lookup the already-instantiated declaration in the instantiation
660 // of the class template and return that.
661 DeclContext::lookup_result Found
662 = Owner->lookup(ClassTemplate->getDeclName());
663 if (Found.first == Found.second)
664 return 0;
665
666 ClassTemplateDecl *InstClassTemplate
667 = dyn_cast<ClassTemplateDecl>(*Found.first);
668 if (!InstClassTemplate)
669 return 0;
670
671 Decl *DCanon = D->getCanonicalDecl();
672 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
673 P = InstClassTemplate->getPartialSpecializations().begin(),
674 PEnd = InstClassTemplate->getPartialSpecializations().end();
675 P != PEnd; ++P) {
676 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
677 return &*P;
678 }
679
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000680 return 0;
681}
682
683Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000684TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000685 // Create a local instantiation scope for this function template, which
686 // will contain the instantiations of the template parameters and then get
687 // merged with the local instantiation scope for the function template
688 // itself.
689 Sema::LocalInstantiationScope Scope(SemaRef);
690
Douglas Gregord60e1052009-08-27 16:57:43 +0000691 TemplateParameterList *TempParams = D->getTemplateParameters();
692 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000693 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000694 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000695
Douglas Gregora735b202009-10-13 14:39:41 +0000696 FunctionDecl *Instantiated = 0;
697 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
698 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
699 InstParams));
700 else
701 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
702 D->getTemplatedDecl(),
703 InstParams));
704
705 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000706 return 0;
707
John McCall46460a62010-01-20 21:53:11 +0000708 Instantiated->setAccess(D->getAccess());
709
Mike Stump1eb44332009-09-09 15:08:12 +0000710 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000711 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000712 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000713 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000714 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000715 assert(InstTemplate &&
716 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
John McCalle976ffe2009-12-14 23:19:40 +0000717
718 // Link the instantiation back to the pattern *unless* this is a
719 // non-definition friend declaration.
720 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
721 !(InstTemplate->getFriendObjectKind() &&
722 !D->getTemplatedDecl()->isThisDeclarationADefinition()))
Douglas Gregora735b202009-10-13 14:39:41 +0000723 InstTemplate->setInstantiatedFromMemberTemplate(D);
724
725 // Add non-friends into the owner.
726 if (!InstTemplate->getFriendObjectKind())
727 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000728 return InstTemplate;
729}
730
Douglas Gregord475b8d2009-03-25 21:17:03 +0000731Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
732 CXXRecordDecl *PrevDecl = 0;
733 if (D->isInjectedClassName())
734 PrevDecl = cast<CXXRecordDecl>(Owner);
John McCall6c1c1b82009-12-15 22:29:06 +0000735 else if (D->getPreviousDeclaration()) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000736 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
737 D->getPreviousDeclaration(),
John McCall6c1c1b82009-12-15 22:29:06 +0000738 TemplateArgs);
739 if (!Prev) return 0;
740 PrevDecl = cast<CXXRecordDecl>(Prev);
741 }
Douglas Gregord475b8d2009-03-25 21:17:03 +0000742
743 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000744 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000745 D->getLocation(), D->getIdentifier(),
746 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000747 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000748 // FIXME: Check against AS_none is an ugly hack to work around the issue that
749 // the tag decls introduced by friend class declarations don't have an access
750 // specifier. Remove once this area of the code gets sorted out.
751 if (D->getAccess() != AS_none)
752 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000753 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000754 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000755
John McCall02cace72009-08-28 07:59:38 +0000756 // If the original function was part of a friend declaration,
757 // inherit its namespace state.
758 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
759 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
760
Anders Carlssond8b285f2009-09-01 04:26:58 +0000761 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
762
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000763 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000764 return Record;
765}
766
John McCall02cace72009-08-28 07:59:38 +0000767/// Normal class members are of more specific types and therefore
768/// don't make it here. This function serves two purposes:
769/// 1) instantiating function templates
770/// 2) substituting friend declarations
771/// FIXME: preserve function definitions in case #2
Douglas Gregor7557a132009-12-24 20:56:24 +0000772Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
Douglas Gregora735b202009-10-13 14:39:41 +0000773 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000774 // Check whether there is already a function template specialization for
775 // this declaration.
776 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
777 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000778 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000779 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000780 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000781 TemplateArgs.getInnermost().getFlatArgumentList(),
782 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000783 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000784
785 FunctionTemplateSpecializationInfo *Info
786 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000787 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Douglas Gregor127102b2009-06-29 20:59:39 +0000789 // If we already have a function template specialization, return it.
790 if (Info)
791 return Info->Function;
792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Douglas Gregor79c22782010-01-16 20:21:20 +0000794 bool MergeWithParentScope = (TemplateParams != 0) ||
795 !(isa<Decl>(Owner) &&
796 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
797 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Douglas Gregore53060f2009-06-25 22:08:12 +0000799 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000800 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000801 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000802 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000803
John McCall68b6b872010-02-06 01:50:47 +0000804 // If we're instantiating a local function declaration, put the result
805 // in the owner; otherwise we need to find the instantiated context.
806 DeclContext *DC;
807 if (D->getDeclContext()->isFunctionOrMethod())
808 DC = Owner;
809 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000810 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
811 TemplateArgs);
John McCall68b6b872010-02-06 01:50:47 +0000812
John McCall02cace72009-08-28 07:59:38 +0000813 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000814 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000815 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000816 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000817 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000818 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregore53060f2009-06-25 22:08:12 +0000820 // Attach the parameters
821 for (unsigned P = 0; P < Params.size(); ++P)
822 Params[P]->setOwningFunction(Function);
Douglas Gregor838db382010-02-11 01:19:42 +0000823 Function->setParams(Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000824
Douglas Gregora735b202009-10-13 14:39:41 +0000825 if (TemplateParams) {
826 // Our resulting instantiation is actually a function template, since we
827 // are substituting only the outer template parameters. For example, given
828 //
829 // template<typename T>
830 // struct X {
831 // template<typename U> friend void f(T, U);
832 // };
833 //
834 // X<int> x;
835 //
836 // We are instantiating the friend function template "f" within X<int>,
837 // which means substituting int for T, but leaving "f" as a friend function
838 // template.
839 // Build the function template itself.
840 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
841 Function->getLocation(),
842 Function->getDeclName(),
843 TemplateParams, Function);
844 Function->setDescribedFunctionTemplate(FunctionTemplate);
845 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000846 } else if (FunctionTemplate) {
847 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +0000848 Function->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +0000849 &TemplateArgs.getInnermost(),
850 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000851 }
Douglas Gregora735b202009-10-13 14:39:41 +0000852
Douglas Gregore53060f2009-06-25 22:08:12 +0000853 if (InitFunctionInstantiation(Function, D))
854 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregore53060f2009-06-25 22:08:12 +0000856 bool Redeclaration = false;
857 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000858
John McCall68263142009-11-18 22:49:29 +0000859 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
860 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
861
Douglas Gregora735b202009-10-13 14:39:41 +0000862 if (TemplateParams || !FunctionTemplate) {
863 // Look only into the namespace where the friend would be declared to
864 // find a previous declaration. This is the innermost enclosing namespace,
865 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000866 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000867
Douglas Gregora735b202009-10-13 14:39:41 +0000868 // In C++, the previous declaration we find might be a tag type
869 // (class or enum). In this case, the new declaration will hide the
870 // tag type. Note that this does does not apply if we're declaring a
871 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000872 if (Previous.isSingleTagDecl())
873 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000874 }
875
John McCall9f54ad42009-12-10 09:41:52 +0000876 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
877 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000878 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000879
Douglas Gregora735b202009-10-13 14:39:41 +0000880 // If the original function was part of a friend declaration,
881 // inherit its namespace state and add it to the owner.
882 NamedDecl *FromFriendD
883 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
884 if (FromFriendD->getFriendObjectKind()) {
885 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000886 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000887 if (TemplateParams) {
888 ToFriendD = cast<NamedDecl>(FunctionTemplate);
889 PrevDecl = FunctionTemplate->getPreviousDeclaration();
890 } else {
891 ToFriendD = Function;
892 PrevDecl = Function->getPreviousDeclaration();
893 }
894 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
895 if (!Owner->isDependentContext() && !PrevDecl)
896 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
897
898 if (!TemplateParams)
899 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
900 }
901
Douglas Gregore53060f2009-06-25 22:08:12 +0000902 return Function;
903}
904
Douglas Gregord60e1052009-08-27 16:57:43 +0000905Decl *
906TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
907 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000908 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
909 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000910 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 // We are creating a function template specialization from a function
912 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000913 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000914 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000915 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000916 TemplateArgs.getInnermost().getFlatArgumentList(),
917 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000918 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
920 FunctionTemplateSpecializationInfo *Info
921 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000922 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregor6b906862009-08-21 00:16:32 +0000924 // If we already have a function template specialization, return it.
925 if (Info)
926 return Info->Function;
927 }
928
Douglas Gregor79c22782010-01-16 20:21:20 +0000929 bool MergeWithParentScope = (TemplateParams != 0) ||
930 !(isa<Decl>(Owner) &&
931 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
932 Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000933
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000934 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000935 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000936 if (T.isNull())
937 return 0;
938
939 // Build the instantiated method declaration.
940 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000941 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregordec06662009-08-21 18:42:58 +0000943 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000944 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000945 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
946 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
947 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000948 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
949 Constructor->getLocation(),
950 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000951 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000952 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000953 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000954 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
955 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
956 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
957 SemaRef.Context.getCanonicalType(ClassTy));
958 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
959 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000960 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000961 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000962 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000963 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000964 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000965 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
966 ConvTy);
967 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
968 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000969 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000970 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000971 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000972 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000973 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000974 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000975 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000976 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000977
Douglas Gregord60e1052009-08-27 16:57:43 +0000978 if (TemplateParams) {
979 // Our resulting instantiation is actually a function template, since we
980 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000981 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000982 // template<typename T>
983 // struct X {
984 // template<typename U> void f(T, U);
985 // };
986 //
987 // X<int> x;
988 //
989 // We are instantiating the member template "f" within X<int>, which means
990 // substituting int for T, but leaving "f" as a member function template.
991 // Build the function template itself.
992 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
993 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000994 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000995 TemplateParams, Method);
996 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000997 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000998 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000999 } else if (FunctionTemplate) {
1000 // Record this function template specialization.
Douglas Gregor838db382010-02-11 01:19:42 +00001001 Method->setFunctionTemplateSpecialization(FunctionTemplate,
Douglas Gregor66724ea2009-11-14 01:20:54 +00001002 &TemplateArgs.getInnermost(),
1003 InsertPos);
1004 } else {
1005 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +00001006 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +00001007 }
1008
Mike Stump1eb44332009-09-09 15:08:12 +00001009 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +00001010 // out-of-line, the instantiation will have the same lexical
1011 // context (which will be a namespace scope) as the template.
1012 if (D->isOutOfLine())
1013 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor5545e162009-03-24 00:38:23 +00001015 // Attach the parameters
1016 for (unsigned P = 0; P < Params.size(); ++P)
1017 Params[P]->setOwningFunction(Method);
Douglas Gregor838db382010-02-11 01:19:42 +00001018 Method->setParams(Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +00001019
1020 if (InitMethodInstantiation(Method, D))
1021 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001022
John McCall68263142009-11-18 22:49:29 +00001023 LookupResult Previous(SemaRef, Name, SourceLocation(),
1024 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregord60e1052009-08-27 16:57:43 +00001026 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +00001027 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Douglas Gregordec06662009-08-21 18:42:58 +00001029 // In C++, the previous declaration we find might be a tag type
1030 // (class or enum). In this case, the new declaration will hide the
1031 // tag type. Note that this does does not apply if we're declaring a
1032 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +00001033 if (Previous.isSingleTagDecl())
1034 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +00001035 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001036
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001037 bool Redeclaration = false;
1038 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +00001039 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001040 /*FIXME:*/OverloadableAttrRequired);
1041
Douglas Gregor4ba31362009-12-01 17:24:26 +00001042 if (D->isPure())
1043 SemaRef.CheckPureMethod(Method, SourceRange());
1044
John McCall46460a62010-01-20 21:53:11 +00001045 Method->setAccess(D->getAccess());
1046
John McCall68263142009-11-18 22:49:29 +00001047 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +00001048 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +00001049 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001051 return Method;
1052}
1053
Douglas Gregor615c5d42009-03-24 16:43:20 +00001054Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +00001055 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +00001056}
1057
Douglas Gregor03b2b072009-03-24 00:15:49 +00001058Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +00001059 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +00001060}
1061
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001062Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001063 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +00001064}
1065
Douglas Gregor6477b692009-03-25 15:04:13 +00001066ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +00001067 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001068 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +00001069 if (DI) {
1070 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1071 D->getDeclName());
1072 if (DI) T = DI->getType();
1073 } else {
1074 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1075 D->getDeclName());
1076 DI = 0;
1077 }
1078
1079 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001080 return 0;
1081
John McCall58e46772009-10-23 21:48:59 +00001082 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001083
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001084 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +00001085 ParmVarDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +00001086 = ParmVarDecl::Create(SemaRef.Context,
1087 SemaRef.Context.getTranslationUnitDecl(),
1088 D->getLocation(),
John McCall58e46772009-10-23 21:48:59 +00001089 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001090
Anders Carlsson9351c172009-08-25 03:18:48 +00001091 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +00001092 if (D->hasUninstantiatedDefaultArg())
1093 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +00001094 else if (Expr *Arg = D->getDefaultArg())
1095 Param->setUninstantiatedDefaultArg(Arg);
1096
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001097 // Note: we don't try to instantiate function parameters until after
1098 // we've instantiated the function's type. Therefore, we don't have
1099 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +00001100 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001101 return Param;
1102}
1103
John McCalle29ba202009-08-20 01:44:21 +00001104Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1105 TemplateTypeParmDecl *D) {
1106 // TODO: don't always clone when decls are refcounted.
1107 const Type* T = D->getTypeForDecl();
1108 assert(T->isTemplateTypeParmType());
1109 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +00001110
John McCalle29ba202009-08-20 01:44:21 +00001111 TemplateTypeParmDecl *Inst =
1112 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +00001113 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +00001114 TTPT->getName(),
1115 D->wasDeclaredWithTypename(),
1116 D->isParameterPack());
1117
Douglas Gregor0f8716b2009-11-09 19:17:50 +00001118 if (D->hasDefaultArgument())
1119 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +00001120
Douglas Gregor550d9b22009-10-31 17:21:17 +00001121 // Introduce this template parameter's instantiation into the instantiation
1122 // scope.
1123 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1124
John McCalle29ba202009-08-20 01:44:21 +00001125 return Inst;
1126}
1127
Douglas Gregor33642df2009-10-23 23:25:44 +00001128Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1129 NonTypeTemplateParmDecl *D) {
1130 // Substitute into the type of the non-type template parameter.
1131 QualType T;
John McCalla93c9342009-12-07 02:54:59 +00001132 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +00001133 if (DI) {
1134 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
1135 D->getDeclName());
1136 if (DI) T = DI->getType();
1137 } else {
1138 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
1139 D->getDeclName());
1140 DI = 0;
1141 }
1142 if (T.isNull())
1143 return 0;
1144
1145 // Check that this type is acceptable for a non-type template parameter.
1146 bool Invalid = false;
1147 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1148 if (T.isNull()) {
1149 T = SemaRef.Context.IntTy;
1150 Invalid = true;
1151 }
1152
1153 NonTypeTemplateParmDecl *Param
1154 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1155 D->getDepth() - 1, D->getPosition(),
1156 D->getIdentifier(), T, DI);
1157 if (Invalid)
1158 Param->setInvalidDecl();
1159
1160 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001161
1162 // Introduce this template parameter's instantiation into the instantiation
1163 // scope.
1164 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001165 return Param;
1166}
1167
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001168Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001169TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1170 TemplateTemplateParmDecl *D) {
1171 // Instantiate the template parameter list of the template template parameter.
1172 TemplateParameterList *TempParams = D->getTemplateParameters();
1173 TemplateParameterList *InstParams;
1174 {
1175 // Perform the actual substitution of template parameters within a new,
1176 // local instantiation scope.
1177 Sema::LocalInstantiationScope Scope(SemaRef);
1178 InstParams = SubstTemplateParams(TempParams);
1179 if (!InstParams)
1180 return NULL;
1181 }
1182
1183 // Build the template template parameter.
1184 TemplateTemplateParmDecl *Param
1185 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1186 D->getDepth() - 1, D->getPosition(),
1187 D->getIdentifier(), InstParams);
1188 Param->setDefaultArgument(D->getDefaultArgument());
1189
1190 // Introduce this template parameter's instantiation into the instantiation
1191 // scope.
1192 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1193
1194 return Param;
1195}
1196
Douglas Gregor48c32a72009-11-17 06:07:40 +00001197Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1198 // Using directives are never dependent, so they require no explicit
1199
1200 UsingDirectiveDecl *Inst
1201 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1202 D->getNamespaceKeyLocation(),
1203 D->getQualifierRange(), D->getQualifier(),
1204 D->getIdentLocation(),
1205 D->getNominatedNamespace(),
1206 D->getCommonAncestor());
1207 Owner->addDecl(Inst);
1208 return Inst;
1209}
1210
John McCalled976492009-12-04 22:46:56 +00001211Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1212 // The nested name specifier is non-dependent, so no transformation
1213 // is required.
1214
John McCall9f54ad42009-12-10 09:41:52 +00001215 // We only need to do redeclaration lookups if we're in a class
1216 // scope (in fact, it's not really even possible in non-class
1217 // scopes).
1218 bool CheckRedeclaration = Owner->isRecord();
1219
1220 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1221 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1222
John McCalled976492009-12-04 22:46:56 +00001223 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1224 D->getLocation(),
1225 D->getNestedNameRange(),
1226 D->getUsingLocation(),
1227 D->getTargetNestedNameDecl(),
1228 D->getDeclName(),
1229 D->isTypeName());
1230
1231 CXXScopeSpec SS;
1232 SS.setScopeRep(D->getTargetNestedNameDecl());
1233 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001234
1235 if (CheckRedeclaration) {
1236 Prev.setHideTags(false);
1237 SemaRef.LookupQualifiedName(Prev, Owner);
1238
1239 // Check for invalid redeclarations.
1240 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1241 D->isTypeName(), SS,
1242 D->getLocation(), Prev))
1243 NewUD->setInvalidDecl();
1244
1245 }
1246
1247 if (!NewUD->isInvalidDecl() &&
1248 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001249 D->getLocation()))
1250 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001251
John McCalled976492009-12-04 22:46:56 +00001252 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1253 NewUD->setAccess(D->getAccess());
1254 Owner->addDecl(NewUD);
1255
John McCall9f54ad42009-12-10 09:41:52 +00001256 // Don't process the shadow decls for an invalid decl.
1257 if (NewUD->isInvalidDecl())
1258 return NewUD;
1259
John McCall323c3102009-12-22 22:26:37 +00001260 bool isFunctionScope = Owner->isFunctionOrMethod();
1261
John McCall9f54ad42009-12-10 09:41:52 +00001262 // Process the shadow decls.
1263 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1264 I != E; ++I) {
1265 UsingShadowDecl *Shadow = *I;
1266 NamedDecl *InstTarget =
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001267 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(),
1268 Shadow->getTargetDecl(),
John McCall9f54ad42009-12-10 09:41:52 +00001269 TemplateArgs));
1270
1271 if (CheckRedeclaration &&
1272 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1273 continue;
1274
1275 UsingShadowDecl *InstShadow
1276 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1277 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
John McCall323c3102009-12-22 22:26:37 +00001278
1279 if (isFunctionScope)
1280 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
John McCall9f54ad42009-12-10 09:41:52 +00001281 }
John McCalled976492009-12-04 22:46:56 +00001282
1283 return NewUD;
1284}
1285
1286Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001287 // Ignore these; we handle them in bulk when processing the UsingDecl.
1288 return 0;
John McCalled976492009-12-04 22:46:56 +00001289}
1290
John McCall7ba107a2009-11-18 02:36:19 +00001291Decl * TemplateDeclInstantiator
1292 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001293 NestedNameSpecifier *NNS =
1294 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1295 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001296 TemplateArgs);
1297 if (!NNS)
1298 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001299
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001300 CXXScopeSpec SS;
1301 SS.setRange(D->getTargetNestedNameRange());
1302 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001303
1304 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001305 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001306 D->getUsingLoc(), SS, D->getLocation(),
1307 D->getDeclName(), 0,
1308 /*instantiation*/ true,
1309 /*typename*/ true, D->getTypenameLoc());
1310 if (UD)
John McCalled976492009-12-04 22:46:56 +00001311 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1312
John McCall7ba107a2009-11-18 02:36:19 +00001313 return UD;
1314}
1315
1316Decl * TemplateDeclInstantiator
1317 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1318 NestedNameSpecifier *NNS =
1319 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1320 D->getTargetNestedNameRange(),
1321 TemplateArgs);
1322 if (!NNS)
1323 return 0;
1324
1325 CXXScopeSpec SS;
1326 SS.setRange(D->getTargetNestedNameRange());
1327 SS.setScopeRep(NNS);
1328
1329 NamedDecl *UD =
1330 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1331 D->getUsingLoc(), SS, D->getLocation(),
1332 D->getDeclName(), 0,
1333 /*instantiation*/ true,
1334 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001335 if (UD)
John McCalled976492009-12-04 22:46:56 +00001336 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1337
Anders Carlsson0d8df782009-08-29 19:37:28 +00001338 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001339}
1340
John McCallce3ff2b2009-08-25 22:02:44 +00001341Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001342 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001343 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor2fa98002010-02-16 19:28:15 +00001344 if (D->isInvalidDecl())
1345 return 0;
1346
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001347 return Instantiator.Visit(D);
1348}
1349
John McCalle29ba202009-08-20 01:44:21 +00001350/// \brief Instantiates a nested template parameter list in the current
1351/// instantiation context.
1352///
1353/// \param L The parameter list to instantiate
1354///
1355/// \returns NULL if there was an error
1356TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001357TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001358 // Get errors for all the parameters before bailing out.
1359 bool Invalid = false;
1360
1361 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001362 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001363 ParamVector Params;
1364 Params.reserve(N);
1365 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1366 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001367 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001368 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001369 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001370 }
1371
1372 // Clean up if we had an error.
1373 if (Invalid) {
1374 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1375 PI != PE; ++PI)
1376 if (*PI)
1377 (*PI)->Destroy(SemaRef.Context);
1378 return NULL;
1379 }
1380
1381 TemplateParameterList *InstL
1382 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1383 L->getLAngleLoc(), &Params.front(), N,
1384 L->getRAngleLoc());
1385 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001386}
John McCalle29ba202009-08-20 01:44:21 +00001387
Douglas Gregored9c0f92009-10-29 00:04:11 +00001388/// \brief Instantiate the declaration of a class template partial
1389/// specialization.
1390///
1391/// \param ClassTemplate the (instantiated) class template that is partially
1392// specialized by the instantiation of \p PartialSpec.
1393///
1394/// \param PartialSpec the (uninstantiated) class template partial
1395/// specialization that we are instantiating.
1396///
1397/// \returns true if there was an error, false otherwise.
1398bool
1399TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1400 ClassTemplateDecl *ClassTemplate,
1401 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001402 // Create a local instantiation scope for this class template partial
1403 // specialization, which will contain the instantiations of the template
1404 // parameters.
1405 Sema::LocalInstantiationScope Scope(SemaRef);
1406
Douglas Gregored9c0f92009-10-29 00:04:11 +00001407 // Substitute into the template parameters of the class template partial
1408 // specialization.
1409 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1410 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1411 if (!InstParams)
1412 return true;
1413
1414 // Substitute into the template arguments of the class template partial
1415 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001416 const TemplateArgumentLoc *PartialSpecTemplateArgs
1417 = PartialSpec->getTemplateArgsAsWritten();
1418 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1419
John McCalld5532b62009-11-23 01:53:49 +00001420 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001421 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001422 TemplateArgumentLoc Loc;
1423 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001424 return true;
John McCalld5532b62009-11-23 01:53:49 +00001425 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001426 }
1427
1428
1429 // Check that the template argument list is well-formed for this
1430 // class template.
1431 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1432 InstTemplateArgs.size());
1433 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1434 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001435 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001436 false,
1437 Converted))
1438 return true;
1439
1440 // Figure out where to insert this class template partial specialization
1441 // in the member template's set of class template partial specializations.
1442 llvm::FoldingSetNodeID ID;
1443 ClassTemplatePartialSpecializationDecl::Profile(ID,
1444 Converted.getFlatArguments(),
1445 Converted.flatSize(),
1446 SemaRef.Context);
1447 void *InsertPos = 0;
1448 ClassTemplateSpecializationDecl *PrevDecl
1449 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1450 InsertPos);
1451
1452 // Build the canonical type that describes the converted template
1453 // arguments of the class template partial specialization.
1454 QualType CanonType
1455 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1456 Converted.getFlatArguments(),
1457 Converted.flatSize());
1458
1459 // Build the fully-sugared type for this class template
1460 // specialization as the user wrote in the specialization
1461 // itself. This means that we'll pretty-print the type retrieved
1462 // from the specialization's declaration the way that the user
1463 // actually wrote the specialization, rather than formatting the
1464 // name based on the "canonical" representation used to store the
1465 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00001466 TypeSourceInfo *WrittenTy
1467 = SemaRef.Context.getTemplateSpecializationTypeInfo(
1468 TemplateName(ClassTemplate),
1469 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001470 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001471 CanonType);
1472
1473 if (PrevDecl) {
1474 // We've already seen a partial specialization with the same template
1475 // parameters and template arguments. This can happen, for example, when
1476 // substituting the outer template arguments ends up causing two
1477 // class template partial specializations of a member class template
1478 // to have identical forms, e.g.,
1479 //
1480 // template<typename T, typename U>
1481 // struct Outer {
1482 // template<typename X, typename Y> struct Inner;
1483 // template<typename Y> struct Inner<T, Y>;
1484 // template<typename Y> struct Inner<U, Y>;
1485 // };
1486 //
1487 // Outer<int, int> outer; // error: the partial specializations of Inner
1488 // // have the same signature.
1489 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1490 << WrittenTy;
1491 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1492 << SemaRef.Context.getTypeDeclType(PrevDecl);
1493 return true;
1494 }
1495
1496
1497 // Create the class template partial specialization declaration.
1498 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1499 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1500 PartialSpec->getLocation(),
1501 InstParams,
1502 ClassTemplate,
1503 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001504 InstTemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00001505 CanonType,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001506 0);
1507 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1508 InstPartialSpec->setTypeAsWritten(WrittenTy);
1509
1510 // Add this partial specialization to the set of class template partial
1511 // specializations.
1512 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1513 InsertPos);
1514 return false;
1515}
1516
John McCallce3ff2b2009-08-25 22:02:44 +00001517/// \brief Does substitution on the type of the given function, including
1518/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001519///
John McCallce3ff2b2009-08-25 22:02:44 +00001520/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001521///
1522/// \param Params the instantiated parameter declarations
1523
John McCallce3ff2b2009-08-25 22:02:44 +00001524/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001525/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001526QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001527TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001528 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1529 bool InvalidDecl = false;
1530
John McCallce3ff2b2009-08-25 22:02:44 +00001531 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001532 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001533 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001534 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001535 PEnd = D->param_end();
1536 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001537 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001538 if (PInst->getType()->isVoidType()) {
1539 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1540 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001541 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001542 PInst->getType(),
1543 diag::err_abstract_type_in_decl,
1544 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001545 PInst->setInvalidDecl();
1546
1547 Params.push_back(PInst);
1548 ParamTys.push_back(PInst->getType());
1549
1550 if (PInst->isInvalidDecl())
1551 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001552 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001553 InvalidDecl = true;
1554 }
1555
1556 // FIXME: Deallocate dead declarations.
1557 if (InvalidDecl)
1558 return QualType();
1559
John McCall183700f2009-09-21 23:43:11 +00001560 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001561 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001562 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001563 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1564 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001565 if (ResultType.isNull())
1566 return QualType();
1567
Jay Foadbeaaccd2009-05-21 09:52:38 +00001568 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001569 Proto->isVariadic(), Proto->getTypeQuals(),
1570 D->getLocation(), D->getDeclName());
1571}
1572
Mike Stump1eb44332009-09-09 15:08:12 +00001573/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001574/// declaration (New) from the corresponding fields of its template (Tmpl).
1575///
1576/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001577bool
1578TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001579 FunctionDecl *Tmpl) {
1580 if (Tmpl->isDeleted())
1581 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Douglas Gregorcca9e962009-07-01 22:01:06 +00001583 // If we are performing substituting explicitly-specified template arguments
1584 // or deduced template arguments into a function template and we reach this
1585 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001586 // to keeping the new function template specialization. We therefore
1587 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001588 // into a template instantiation for this specific function template
1589 // specialization, which is not a SFINAE context, so that we diagnose any
1590 // further errors in the declaration itself.
1591 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1592 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1593 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1594 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001595 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001596 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001597 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001598 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001599 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001600 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1601 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001602 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001603 }
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001606 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1607 assert(Proto && "Function template without prototype?");
1608
1609 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1610 Proto->getNoReturnAttr()) {
1611 // The function has an exception specification or a "noreturn"
1612 // attribute. Substitute into each of the exception types.
1613 llvm::SmallVector<QualType, 4> Exceptions;
1614 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1615 // FIXME: Poor location information!
1616 QualType T
1617 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1618 New->getLocation(), New->getDeclName());
1619 if (T.isNull() ||
1620 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1621 continue;
1622
1623 Exceptions.push_back(T);
1624 }
1625
1626 // Rebuild the function type
1627
1628 const FunctionProtoType *NewProto
1629 = New->getType()->getAs<FunctionProtoType>();
1630 assert(NewProto && "Template instantiation without function prototype?");
1631 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1632 NewProto->arg_type_begin(),
1633 NewProto->getNumArgs(),
1634 NewProto->isVariadic(),
1635 NewProto->getTypeQuals(),
1636 Proto->hasExceptionSpec(),
1637 Proto->hasAnyExceptionSpec(),
1638 Exceptions.size(),
1639 Exceptions.data(),
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001640 Proto->getNoReturnAttr(),
1641 Proto->getCallConv()));
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001642 }
1643
Douglas Gregore53060f2009-06-25 22:08:12 +00001644 return false;
1645}
1646
Douglas Gregor5545e162009-03-24 00:38:23 +00001647/// \brief Initializes common fields of an instantiated method
1648/// declaration (New) from the corresponding fields of its template
1649/// (Tmpl).
1650///
1651/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001652bool
1653TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001654 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001655 if (InitFunctionInstantiation(New, Tmpl))
1656 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Douglas Gregor5545e162009-03-24 00:38:23 +00001658 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1659 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001660 if (Tmpl->isVirtualAsWritten())
1661 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001662
1663 // FIXME: attributes
1664 // FIXME: New needs a pointer to Tmpl
1665 return false;
1666}
Douglas Gregora58861f2009-05-13 20:28:22 +00001667
1668/// \brief Instantiate the definition of the given function from its
1669/// template.
1670///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001671/// \param PointOfInstantiation the point at which the instantiation was
1672/// required. Note that this is not precisely a "point of instantiation"
1673/// for the function, but it's close.
1674///
Douglas Gregora58861f2009-05-13 20:28:22 +00001675/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001676/// function template specialization or member function of a class template
1677/// specialization.
1678///
1679/// \param Recursive if true, recursively instantiates any functions that
1680/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001681///
1682/// \param DefinitionRequired if true, then we are performing an explicit
1683/// instantiation where the body of the function is required. Complain if
1684/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001685void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001686 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001687 bool Recursive,
1688 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001689 if (Function->isInvalidDecl())
1690 return;
1691
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001692 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001694 // Never instantiate an explicit specialization.
1695 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1696 return;
1697
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001698 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001699 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001700 Stmt *Pattern = 0;
1701 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001702 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001703
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001704 if (!Pattern) {
1705 if (DefinitionRequired) {
1706 if (Function->getPrimaryTemplate())
1707 Diag(PointOfInstantiation,
1708 diag::err_explicit_instantiation_undefined_func_template)
1709 << Function->getPrimaryTemplate();
1710 else
1711 Diag(PointOfInstantiation,
1712 diag::err_explicit_instantiation_undefined_member)
1713 << 1 << Function->getDeclName() << Function->getDeclContext();
1714
1715 if (PatternDecl)
1716 Diag(PatternDecl->getLocation(),
1717 diag::note_explicit_instantiation_here);
1718 }
1719
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001720 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001721 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001722
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001723 // C++0x [temp.explicit]p9:
1724 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001725 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001726 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001727 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001728 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001729 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001730 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001732 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1733 if (Inst)
1734 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001735
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001736 // If we're performing recursive template instantiation, create our own
1737 // queue of pending implicit instantiations that we will instantiate later,
1738 // while we're still within our own instantiation context.
1739 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1740 if (Recursive)
1741 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001743 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1744
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001745 // Introduce a new scope where local variable instantiations will be
Douglas Gregor60406be2010-01-16 22:29:39 +00001746 // recorded, unless we're actually a member function within a local
1747 // class, in which case we need to merge our results with the parent
1748 // scope (of the enclosing function).
1749 bool MergeWithParentScope = false;
1750 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
1751 MergeWithParentScope = Rec->isLocalClass();
1752
1753 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001755 // Introduce the instantiated function parameters into the local
1756 // instantiation scope.
1757 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1758 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1759 Function->getParamDecl(I));
1760
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001761 // Enter the scope of this instantiation. We don't use
1762 // PushDeclContext because we don't have a scope.
1763 DeclContext *PreviousContext = CurContext;
1764 CurContext = Function;
1765
Mike Stump1eb44332009-09-09 15:08:12 +00001766 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001767 getTemplateInstantiationArgs(Function);
1768
1769 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001770 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001771 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1772 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1773 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001774 }
1775
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001776 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001777 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001778
Douglas Gregor52604ab2009-09-11 21:19:12 +00001779 if (Body.isInvalid())
1780 Function->setInvalidDecl();
1781
Mike Stump1eb44332009-09-09 15:08:12 +00001782 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001783 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001784
1785 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001786
1787 DeclGroupRef DG(Function);
1788 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregor60406be2010-01-16 22:29:39 +00001790 // This class may have local implicit instantiations that need to be
1791 // instantiation within this scope.
1792 PerformPendingImplicitInstantiations(/*LocalOnly=*/true);
1793 Scope.Exit();
1794
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001795 if (Recursive) {
1796 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001797 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001798 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001800 // Restore the set of pending implicit instantiations.
1801 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1802 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001803}
1804
1805/// \brief Instantiate the definition of the given variable from its
1806/// template.
1807///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001808/// \param PointOfInstantiation the point at which the instantiation was
1809/// required. Note that this is not precisely a "point of instantiation"
1810/// for the function, but it's close.
1811///
1812/// \param Var the already-instantiated declaration of a static member
1813/// variable of a class template specialization.
1814///
1815/// \param Recursive if true, recursively instantiates any functions that
1816/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001817///
1818/// \param DefinitionRequired if true, then we are performing an explicit
1819/// instantiation where an out-of-line definition of the member variable
1820/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001821void Sema::InstantiateStaticDataMemberDefinition(
1822 SourceLocation PointOfInstantiation,
1823 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001824 bool Recursive,
1825 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001826 if (Var->isInvalidDecl())
1827 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Douglas Gregor7caa6822009-07-24 20:34:43 +00001829 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001830 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001831 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001832 assert(Def->isStaticDataMember() && "Not a static data member?");
1833 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Douglas Gregor0d035142009-10-27 18:42:08 +00001835 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001836 // We did not find an out-of-line definition of this static data member,
1837 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001838 // instantiate this definition (or provide a specialization for it) in
1839 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001840 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001841 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001842 Diag(PointOfInstantiation,
1843 diag::err_explicit_instantiation_undefined_member)
1844 << 2 << Var->getDeclName() << Var->getDeclContext();
1845 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1846 }
1847
Douglas Gregor7caa6822009-07-24 20:34:43 +00001848 return;
1849 }
1850
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001851 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001852 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001853 return;
1854
1855 // C++0x [temp.explicit]p9:
1856 // Except for inline functions, other explicit instantiation declarations
1857 // have the effect of suppressing the implicit instantiation of the entity
1858 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001859 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001860 == TSK_ExplicitInstantiationDeclaration)
1861 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Douglas Gregor7caa6822009-07-24 20:34:43 +00001863 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1864 if (Inst)
1865 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Douglas Gregor7caa6822009-07-24 20:34:43 +00001867 // If we're performing recursive template instantiation, create our own
1868 // queue of pending implicit instantiations that we will instantiate later,
1869 // while we're still within our own instantiation context.
1870 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1871 if (Recursive)
1872 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Douglas Gregor7caa6822009-07-24 20:34:43 +00001874 // Enter the scope of this instantiation. We don't use
1875 // PushDeclContext because we don't have a scope.
1876 DeclContext *PreviousContext = CurContext;
1877 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001879 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001880 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001881 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001882 CurContext = PreviousContext;
1883
1884 if (Var) {
Douglas Gregor583f33b2009-10-15 18:07:02 +00001885 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1886 assert(MSInfo && "Missing member specialization information?");
1887 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1888 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001889 DeclGroupRef DG(Var);
1890 Consumer.HandleTopLevelDecl(DG);
1891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Douglas Gregor7caa6822009-07-24 20:34:43 +00001893 if (Recursive) {
1894 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001895 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001896 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Douglas Gregor7caa6822009-07-24 20:34:43 +00001898 // Restore the set of pending implicit instantiations.
1899 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001900 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001901}
Douglas Gregor815215d2009-05-27 05:35:12 +00001902
Anders Carlsson09025312009-08-29 05:16:22 +00001903void
1904Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1905 const CXXConstructorDecl *Tmpl,
1906 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Anders Carlsson09025312009-08-29 05:16:22 +00001908 llvm::SmallVector<MemInitTy*, 4> NewInits;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001909 bool AnyErrors = false;
1910
Anders Carlsson09025312009-08-29 05:16:22 +00001911 // Instantiate all the initializers.
1912 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001913 InitsEnd = Tmpl->init_end();
1914 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001915 CXXBaseOrMemberInitializer *Init = *Inits;
1916
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001917 SourceLocation LParenLoc, RParenLoc;
Anders Carlsson09025312009-08-29 05:16:22 +00001918 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001919 llvm::SmallVector<SourceLocation, 4> CommaLocs;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregor6b98b2e2010-03-02 07:38:39 +00001921 // Instantiate the initializer.
1922 if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs,
1923 LParenLoc, CommaLocs, NewArgs, RParenLoc)) {
1924 AnyErrors = true;
1925 continue;
Anders Carlsson09025312009-08-29 05:16:22 +00001926 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001927
Anders Carlsson09025312009-08-29 05:16:22 +00001928 MemInitResult NewInit;
Anders Carlsson09025312009-08-29 05:16:22 +00001929 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001930 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001931 TemplateArgs,
1932 Init->getSourceLocation(),
1933 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001934 if (!BaseTInfo) {
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001935 AnyErrors = true;
Douglas Gregor802ab452009-12-02 22:36:29 +00001936 New->setInvalidDecl();
1937 continue;
1938 }
1939
John McCalla93c9342009-12-07 02:54:59 +00001940 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001941 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001942 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001943 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001944 Init->getRParenLoc(),
1945 New->getParent());
1946 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001947 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001949 // Is this an anonymous union?
1950 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001951 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
1952 UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001953 else
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001954 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(),
1955 Init->getMember(),
Douglas Gregore95b4092009-09-16 18:34:49 +00001956 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001957
1958 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001959 NewArgs.size(),
1960 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001961 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001962 Init->getRParenLoc());
1963 }
1964
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001965 if (NewInit.isInvalid()) {
1966 AnyErrors = true;
Anders Carlsson09025312009-08-29 05:16:22 +00001967 New->setInvalidDecl();
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001968 } else {
Anders Carlsson09025312009-08-29 05:16:22 +00001969 // FIXME: It would be nice if ASTOwningVector had a release function.
1970 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Anders Carlsson09025312009-08-29 05:16:22 +00001972 NewInits.push_back((MemInitTy *)NewInit.get());
1973 }
1974 }
Mike Stump1eb44332009-09-09 15:08:12 +00001975
Anders Carlsson09025312009-08-29 05:16:22 +00001976 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001977 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001978 /*FIXME: ColonLoc */
1979 SourceLocation(),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00001980 NewInits.data(), NewInits.size(),
1981 AnyErrors);
Anders Carlsson09025312009-08-29 05:16:22 +00001982}
1983
John McCall52a575a2009-08-29 08:11:13 +00001984// TODO: this could be templated if the various decl types used the
1985// same method name.
1986static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1987 ClassTemplateDecl *Instance) {
1988 Pattern = Pattern->getCanonicalDecl();
1989
1990 do {
1991 Instance = Instance->getCanonicalDecl();
1992 if (Pattern == Instance) return true;
1993 Instance = Instance->getInstantiatedFromMemberTemplate();
1994 } while (Instance);
1995
1996 return false;
1997}
1998
Douglas Gregor0d696532009-09-28 06:34:35 +00001999static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
2000 FunctionTemplateDecl *Instance) {
2001 Pattern = Pattern->getCanonicalDecl();
2002
2003 do {
2004 Instance = Instance->getCanonicalDecl();
2005 if (Pattern == Instance) return true;
2006 Instance = Instance->getInstantiatedFromMemberTemplate();
2007 } while (Instance);
2008
2009 return false;
2010}
2011
Douglas Gregored9c0f92009-10-29 00:04:11 +00002012static bool
2013isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
2014 ClassTemplatePartialSpecializationDecl *Instance) {
2015 Pattern
2016 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
2017 do {
2018 Instance = cast<ClassTemplatePartialSpecializationDecl>(
2019 Instance->getCanonicalDecl());
2020 if (Pattern == Instance)
2021 return true;
2022 Instance = Instance->getInstantiatedFromMember();
2023 } while (Instance);
2024
2025 return false;
2026}
2027
John McCall52a575a2009-08-29 08:11:13 +00002028static bool isInstantiationOf(CXXRecordDecl *Pattern,
2029 CXXRecordDecl *Instance) {
2030 Pattern = Pattern->getCanonicalDecl();
2031
2032 do {
2033 Instance = Instance->getCanonicalDecl();
2034 if (Pattern == Instance) return true;
2035 Instance = Instance->getInstantiatedFromMemberClass();
2036 } while (Instance);
2037
2038 return false;
2039}
2040
2041static bool isInstantiationOf(FunctionDecl *Pattern,
2042 FunctionDecl *Instance) {
2043 Pattern = Pattern->getCanonicalDecl();
2044
2045 do {
2046 Instance = Instance->getCanonicalDecl();
2047 if (Pattern == Instance) return true;
2048 Instance = Instance->getInstantiatedFromMemberFunction();
2049 } while (Instance);
2050
2051 return false;
2052}
2053
2054static bool isInstantiationOf(EnumDecl *Pattern,
2055 EnumDecl *Instance) {
2056 Pattern = Pattern->getCanonicalDecl();
2057
2058 do {
2059 Instance = Instance->getCanonicalDecl();
2060 if (Pattern == Instance) return true;
2061 Instance = Instance->getInstantiatedFromMemberEnum();
2062 } while (Instance);
2063
2064 return false;
2065}
2066
John McCalled976492009-12-04 22:46:56 +00002067static bool isInstantiationOf(UsingShadowDecl *Pattern,
2068 UsingShadowDecl *Instance,
2069 ASTContext &C) {
2070 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
2071}
2072
2073static bool isInstantiationOf(UsingDecl *Pattern,
2074 UsingDecl *Instance,
2075 ASTContext &C) {
2076 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
2077}
2078
John McCall7ba107a2009-11-18 02:36:19 +00002079static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
2080 UsingDecl *Instance,
2081 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002082 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00002083}
2084
2085static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00002086 UsingDecl *Instance,
2087 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00002088 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00002089}
2090
John McCall52a575a2009-08-29 08:11:13 +00002091static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
2092 VarDecl *Instance) {
2093 assert(Instance->isStaticDataMember());
2094
2095 Pattern = Pattern->getCanonicalDecl();
2096
2097 do {
2098 Instance = Instance->getCanonicalDecl();
2099 if (Pattern == Instance) return true;
2100 Instance = Instance->getInstantiatedFromStaticDataMember();
2101 } while (Instance);
2102
2103 return false;
2104}
2105
John McCalled976492009-12-04 22:46:56 +00002106// Other is the prospective instantiation
2107// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00002108static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002109 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00002110 if (UnresolvedUsingTypenameDecl *UUD
2111 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
2112 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2113 return isInstantiationOf(UUD, UD, Ctx);
2114 }
2115 }
2116
2117 if (UnresolvedUsingValueDecl *UUD
2118 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00002119 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
2120 return isInstantiationOf(UUD, UD, Ctx);
2121 }
2122 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002123
Anders Carlsson0d8df782009-08-29 19:37:28 +00002124 return false;
2125 }
Mike Stump1eb44332009-09-09 15:08:12 +00002126
John McCall52a575a2009-08-29 08:11:13 +00002127 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
2128 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
John McCall52a575a2009-08-29 08:11:13 +00002130 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
2131 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00002132
John McCall52a575a2009-08-29 08:11:13 +00002133 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
2134 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00002135
Douglas Gregor7caa6822009-07-24 20:34:43 +00002136 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00002137 if (Var->isStaticDataMember())
2138 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
2139
2140 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
2141 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00002142
Douglas Gregor0d696532009-09-28 06:34:35 +00002143 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
2144 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
2145
Douglas Gregored9c0f92009-10-29 00:04:11 +00002146 if (ClassTemplatePartialSpecializationDecl *PartialSpec
2147 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
2148 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
2149 PartialSpec);
2150
Anders Carlssond8b285f2009-09-01 04:26:58 +00002151 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
2152 if (!Field->getDeclName()) {
2153 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00002154 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00002155 cast<FieldDecl>(D);
2156 }
2157 }
Mike Stump1eb44332009-09-09 15:08:12 +00002158
John McCalled976492009-12-04 22:46:56 +00002159 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
2160 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
2161
2162 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
2163 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
2164
Douglas Gregor815215d2009-05-27 05:35:12 +00002165 return D->getDeclName() && isa<NamedDecl>(Other) &&
2166 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2167}
2168
2169template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002170static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002171 NamedDecl *D,
2172 ForwardIterator first,
2173 ForwardIterator last) {
2174 for (; first != last; ++first)
2175 if (isInstantiationOf(Ctx, D, *first))
2176 return cast<NamedDecl>(*first);
2177
2178 return 0;
2179}
2180
John McCall02cace72009-08-28 07:59:38 +00002181/// \brief Finds the instantiation of the given declaration context
2182/// within the current instantiation.
2183///
2184/// \returns NULL if there was an error
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002185DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
Douglas Gregore95b4092009-09-16 18:34:49 +00002186 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002187 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002188 Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002189 return cast_or_null<DeclContext>(ID);
2190 } else return DC;
2191}
2192
Douglas Gregored961e72009-05-27 17:54:46 +00002193/// \brief Find the instantiation of the given declaration within the
2194/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002195///
2196/// This routine is intended to be used when \p D is a declaration
2197/// referenced from within a template, that needs to mapped into the
2198/// corresponding declaration within an instantiation. For example,
2199/// given:
2200///
2201/// \code
2202/// template<typename T>
2203/// struct X {
2204/// enum Kind {
2205/// KnownValue = sizeof(T)
2206/// };
2207///
2208/// bool getKind() const { return KnownValue; }
2209/// };
2210///
2211/// template struct X<int>;
2212/// \endcode
2213///
2214/// In the instantiation of X<int>::getKind(), we need to map the
2215/// EnumConstantDecl for KnownValue (which refers to
2216/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002217/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2218/// this mapping from within the instantiation of X<int>.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002219NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
Douglas Gregore95b4092009-09-16 18:34:49 +00002220 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002221 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002222 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
Douglas Gregor6d3e6272010-02-05 19:54:12 +00002223 isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
Douglas Gregor550d9b22009-10-31 17:21:17 +00002224 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002225 // D is a local of some kind. Look into the map of local
2226 // declarations to their instantiations.
2227 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2228 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002229
Douglas Gregore95b4092009-09-16 18:34:49 +00002230 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2231 if (!Record->isDependentContext())
2232 return D;
2233
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002234 // If the RecordDecl is actually the injected-class-name or a
2235 // "templated" declaration for a class template, class template
2236 // partial specialization, or a member class of a class template,
2237 // substitute into the injected-class-name of the class template
2238 // or partial specialization to find the new DeclContext.
Douglas Gregore95b4092009-09-16 18:34:49 +00002239 QualType T;
2240 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2241
2242 if (ClassTemplate) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002243 T = ClassTemplate->getInjectedClassNameSpecialization(Context);
Douglas Gregore95b4092009-09-16 18:34:49 +00002244 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2245 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002246 ClassTemplate = PartialSpec->getSpecializedTemplate();
John McCall3cb0ebd2010-03-10 03:28:59 +00002247
2248 // If we call SubstType with an InjectedClassNameType here we
2249 // can end up in an infinite loop.
2250 T = Context.getTypeDeclType(Record);
2251 assert(isa<InjectedClassNameType>(T) &&
2252 "type of partial specialization is not an InjectedClassNameType");
2253 T = cast<InjectedClassNameType>(T)->getUnderlyingType();
2254 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002255
2256 if (!T.isNull()) {
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002257 // Substitute into the injected-class-name to get the type
2258 // corresponding to the instantiation we want, which may also be
2259 // the current instantiation (if we're in a template
2260 // definition). This substitution should never fail, since we
2261 // know we can instantiate the injected-class-name or we
2262 // wouldn't have gotten to the injected-class-name!
2263
2264 // FIXME: Can we use the CurrentInstantiationScope to avoid this
2265 // extra instantiation in the common case?
Douglas Gregore95b4092009-09-16 18:34:49 +00002266 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2267 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2268
2269 if (!T->isDependentType()) {
2270 assert(T->isRecordType() && "Instantiation must produce a record type");
2271 return T->getAs<RecordType>()->getDecl();
2272 }
2273
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002274 // We are performing "partial" template instantiation to create
2275 // the member declarations for the members of a class template
2276 // specialization. Therefore, D is actually referring to something
2277 // in the current instantiation. Look through the current
2278 // context, which contains actual instantiations, to find the
2279 // instantiation of the "current instantiation" that D refers
2280 // to.
2281 bool SawNonDependentContext = false;
Mike Stump1eb44332009-09-09 15:08:12 +00002282 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002283 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002284 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002285 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002286 if (isInstantiationOf(ClassTemplate,
2287 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002288 return Spec;
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002289
2290 if (!DC->isDependentContext())
2291 SawNonDependentContext = true;
John McCall52a575a2009-08-29 08:11:13 +00002292 }
2293
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002294 // We're performing "instantiation" of a member of the current
2295 // instantiation while we are type-checking the
2296 // definition. Compute the declaration context and return that.
2297 assert(!SawNonDependentContext &&
2298 "No dependent context while instantiating record");
2299 DeclContext *DC = computeDeclContext(T);
2300 assert(DC &&
John McCall52a575a2009-08-29 08:11:13 +00002301 "Unable to find declaration for the current instantiation");
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002302 return cast<CXXRecordDecl>(DC);
John McCall52a575a2009-08-29 08:11:13 +00002303 }
Douglas Gregor8b013bd2010-02-05 22:40:03 +00002304
Douglas Gregore95b4092009-09-16 18:34:49 +00002305 // Fall through to deal with other dependent record types (e.g.,
2306 // anonymous unions in class templates).
2307 }
John McCall52a575a2009-08-29 08:11:13 +00002308
Douglas Gregore95b4092009-09-16 18:34:49 +00002309 if (!ParentDC->isDependentContext())
2310 return D;
2311
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002312 ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002313 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002314 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Douglas Gregor815215d2009-05-27 05:35:12 +00002316 if (ParentDC != D->getDeclContext()) {
2317 // We performed some kind of instantiation in the parent context,
2318 // so now we need to look into the instantiated parent context to
2319 // find the instantiation of the declaration D.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002320
John McCall3cb0ebd2010-03-10 03:28:59 +00002321 // If our context used to be dependent, we may need to instantiate
2322 // it before performing lookup into that context.
2323 if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002324 if (!Spec->isDependentContext()) {
2325 QualType T = Context.getTypeDeclType(Spec);
John McCall3cb0ebd2010-03-10 03:28:59 +00002326 const RecordType *Tag = T->getAs<RecordType>();
2327 assert(Tag && "type of non-dependent record is not a RecordType");
2328 if (!Tag->isBeingDefined() &&
2329 RequireCompleteType(Loc, T, diag::err_incomplete_type))
2330 return 0;
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00002331 }
2332 }
2333
Douglas Gregor815215d2009-05-27 05:35:12 +00002334 NamedDecl *Result = 0;
2335 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002336 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002337 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2338 } else {
2339 // Since we don't have a name for the entity we're looking for,
2340 // our only option is to walk through all of the declarations to
2341 // find that name. This will occur in a few cases:
2342 //
2343 // - anonymous struct/union within a template
2344 // - unnamed class/struct/union/enum within a template
2345 //
2346 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002347 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002348 ParentDC->decls_begin(),
2349 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002350 }
Mike Stump1eb44332009-09-09 15:08:12 +00002351
John McCall9f54ad42009-12-10 09:41:52 +00002352 // UsingShadowDecls can instantiate to nothing because of using hiding.
Douglas Gregor00225542010-03-01 18:27:54 +00002353 assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() ||
2354 cast<Decl>(ParentDC)->isInvalidDecl())
John McCall9f54ad42009-12-10 09:41:52 +00002355 && "Unable to find instantiation of declaration!");
2356
Douglas Gregor815215d2009-05-27 05:35:12 +00002357 D = Result;
2358 }
2359
Douglas Gregor815215d2009-05-27 05:35:12 +00002360 return D;
2361}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002362
Mike Stump1eb44332009-09-09 15:08:12 +00002363/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002364/// instantiations we have seen until this point.
Douglas Gregor60406be2010-01-16 22:29:39 +00002365void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) {
2366 while (!PendingLocalImplicitInstantiations.empty() ||
2367 (!LocalOnly && !PendingImplicitInstantiations.empty())) {
2368 PendingImplicitInstantiation Inst;
2369
2370 if (PendingLocalImplicitInstantiations.empty()) {
2371 Inst = PendingImplicitInstantiations.front();
2372 PendingImplicitInstantiations.pop_front();
2373 } else {
2374 Inst = PendingLocalImplicitInstantiations.front();
2375 PendingLocalImplicitInstantiations.pop_front();
2376 }
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Douglas Gregor7caa6822009-07-24 20:34:43 +00002378 // Instantiate function definitions
2379 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002380 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002381 Function->getLocation(), *this,
2382 Context.getSourceManager(),
2383 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002385 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002386 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002387 continue;
2388 }
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Douglas Gregor7caa6822009-07-24 20:34:43 +00002390 // Instantiate static data member definitions.
2391 VarDecl *Var = cast<VarDecl>(Inst.first);
2392 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002393
Chandler Carruth291b4412010-02-13 10:17:50 +00002394 // Don't try to instantiate declarations if the most recent redeclaration
2395 // is invalid.
2396 if (Var->getMostRecentDeclaration()->isInvalidDecl())
2397 continue;
2398
2399 // Check if the most recent declaration has changed the specialization kind
2400 // and removed the need for implicit instantiation.
2401 switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) {
2402 case TSK_Undeclared:
2403 assert(false && "Cannot instantitiate an undeclared specialization.");
2404 case TSK_ExplicitInstantiationDeclaration:
2405 case TSK_ExplicitInstantiationDefinition:
2406 case TSK_ExplicitSpecialization:
2407 continue; // No longer need implicit instantiation.
2408 case TSK_ImplicitInstantiation:
2409 break;
2410 }
2411
Mike Stump1eb44332009-09-09 15:08:12 +00002412 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002413 Var->getLocation(), *this,
2414 Context.getSourceManager(),
2415 "instantiating static data member "
2416 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Douglas Gregor7caa6822009-07-24 20:34:43 +00002418 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002419 }
2420}