blob: 4512bcde164fb3ccfa614380eb076d43674b7617 [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);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000046 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +000047 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +000048 Decl *VisitFieldDecl(FieldDecl *D);
49 Decl *VisitStaticAssertDecl(StaticAssertDecl *D);
50 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000051 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
John McCall02cace72009-08-28 07:59:38 +000052 Decl *VisitFriendDecl(FriendDecl *D);
Douglas Gregora735b202009-10-13 14:39:41 +000053 Decl *VisitFunctionDecl(FunctionDecl *D,
54 TemplateParameterList *TemplateParams = 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +000055 Decl *VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000056 Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
57 TemplateParameterList *TemplateParams = 0);
Douglas Gregor615c5d42009-03-24 16:43:20 +000058 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
Douglas Gregor03b2b072009-03-24 00:15:49 +000059 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +000060 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor6477b692009-03-25 15:04:13 +000061 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000062 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregor7974c3b2009-10-07 17:21:34 +000063 Decl *VisitClassTemplatePartialSpecializationDecl(
64 ClassTemplatePartialSpecializationDecl *D);
Douglas Gregord60e1052009-08-27 16:57:43 +000065 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
John McCalle29ba202009-08-20 01:44:21 +000066 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Douglas Gregor33642df2009-10-23 23:25:44 +000067 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
Douglas Gregor9106ef72009-11-11 16:58:32 +000068 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Douglas Gregor48c32a72009-11-17 06:07:40 +000069 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
John McCalled976492009-12-04 22:46:56 +000070 Decl *VisitUsingDecl(UsingDecl *D);
71 Decl *VisitUsingShadowDecl(UsingShadowDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000072 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
73 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor8dbc2692009-03-17 21:15:40 +000075 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000076 Decl *VisitDecl(Decl *D) {
77 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
78 Diagnostic::Error,
79 "cannot instantiate %0 yet");
80 SemaRef.Diag(D->getLocation(), DiagID)
81 << D->getDeclKindName();
82
Douglas Gregor8dbc2692009-03-17 21:15:40 +000083 return 0;
84 }
Douglas Gregor5545e162009-03-24 00:38:23 +000085
John McCallfd810b12009-08-14 02:03:10 +000086 const LangOptions &getLangOptions() {
87 return SemaRef.getLangOptions();
88 }
89
Douglas Gregor5545e162009-03-24 00:38:23 +000090 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000091 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000092 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000093 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000094 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000095
96 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000097 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000098
99 bool InstantiateClassTemplatePartialSpecialization(
100 ClassTemplateDecl *ClassTemplate,
101 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000102 };
103}
104
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000105// FIXME: Is this too simple?
106void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
107 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
108 TmplAttr = TmplAttr->getNext()) {
109
110 // FIXME: Is cloning correct for all attributes?
111 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
112
113 New->addAttr(NewAttr);
114 }
115}
116
Douglas Gregor4f722be2009-03-25 15:45:12 +0000117Decl *
118TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
119 assert(false && "Translation units cannot be instantiated");
120 return D;
121}
122
123Decl *
124TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
125 assert(false && "Namespaces cannot be instantiated");
126 return D;
127}
128
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000129Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
130 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000131 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCallba6a9bd2009-10-24 08:00:42 +0000132 if (DI->getType()->isDependentType()) {
133 DI = SemaRef.SubstType(DI, TemplateArgs,
134 D->getLocation(), D->getDeclName());
135 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000136 Invalid = true;
John McCalla93c9342009-12-07 02:54:59 +0000137 DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000138 }
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000141 // Create the new typedef
142 TypedefDecl *Typedef
143 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000144 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000145 if (Invalid)
146 Typedef->setInvalidDecl();
147
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000148 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000150 return Typedef;
151}
152
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000153Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000154 // Do substitution on the type of the declaration
John McCalla93c9342009-12-07 02:54:59 +0000155 TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
John McCall0a5fa062009-10-21 02:39:02 +0000156 TemplateArgs,
157 D->getTypeSpecStartLoc(),
158 D->getDeclName());
159 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000160 return 0;
161
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000162 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000163 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
164 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000165 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000166 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000167 Var->setThreadSpecified(D->isThreadSpecified());
168 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
169 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000170
171 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000172 // out-of-line, the instantiation will have the same lexical
173 // context (which will be a namespace scope) as the template.
174 if (D->isOutOfLine())
175 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Mike Stump390b4cc2009-05-16 07:39:55 +0000177 // FIXME: In theory, we could have a previous declaration for variables that
178 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000179 bool Redeclaration = false;
John McCall68263142009-11-18 22:49:29 +0000180 // FIXME: having to fake up a LookupResult is dumb.
181 LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(),
182 Sema::LookupOrdinaryName);
183 SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregor7caa6822009-07-24 20:34:43 +0000185 if (D->isOutOfLine()) {
186 D->getLexicalDeclContext()->addDecl(Var);
187 Owner->makeDeclVisibleInContext(Var);
188 } else {
189 Owner->addDecl(Var);
190 }
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000192 // Link instantiations of static data members back to the template from
193 // which they were instantiated.
194 if (Var->isStaticDataMember())
195 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000196 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000197
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000198 if (D->getInit()) {
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000199 if (Var->isStaticDataMember() && !D->isOutOfLine())
200 SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated);
201 else
202 SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
203
Mike Stump1eb44332009-09-09 15:08:12 +0000204 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000205 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000206 if (Init.isInvalid())
207 Var->setInvalidDecl();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000208 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000209 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000210 // Do we even need these comma locations?
211 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
212 if (PLE->getNumExprs() > 0) {
213 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
214 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
215 Expr *E = PLE->getExpr(I)->Retain();
216 FakeCommaLocs.push_back(
217 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
218 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000219 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Douglas Gregor83ddad32009-08-26 21:14:46 +0000222 // Add the direct initializer to the declaration.
223 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000224 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000225 Sema::MultiExprArg(SemaRef,
226 (void**)PLE->getExprs(),
227 PLE->getNumExprs()),
228 FakeCommaLocs.data(),
229 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Douglas Gregor83ddad32009-08-26 21:14:46 +0000231 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
232 // we've explicitly retained all of its subexpressions already.
Douglas Gregora88cfbf2009-12-12 18:16:41 +0000233 } else if (CXXConstructExpr *Construct
234 = dyn_cast<CXXConstructExpr>((Expr *)Init.get())) {
235 // We build CXXConstructExpr nodes to capture the implicit
236 // construction of objects. Rip apart the CXXConstructExpr to
237 // pass its pieces down to the appropriate initialization
238 // function.
239 if (D->hasCXXDirectInitializer()) {
240 // FIXME: Poor source location information
241 SourceLocation FakeLParenLoc =
242 SemaRef.PP.getLocForEndOfToken(D->getLocation());
243 SourceLocation FakeRParenLoc = FakeLParenLoc;
244 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
245 if (Construct->getNumArgs() > 0) {
246 FakeRParenLoc
247 = SemaRef.PP.getLocForEndOfToken(
248 Construct->getArg(Construct->getNumArgs() - 1)->getLocEnd());
249
250 FakeCommaLocs.reserve(Construct->getNumArgs() - 1);
251 for (unsigned I = 0, N = Construct->getNumArgs() - 1; I != N; ++I) {
252 Expr *E = Construct->getArg(I)->Retain();
253 FakeCommaLocs.push_back(
254 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
255 }
256 Construct->getArg(Construct->getNumArgs() - 1)->Retain();
257 }
258
259 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
260 FakeLParenLoc,
261 Sema::MultiExprArg(SemaRef,
262 (void **)Construct->getArgs(),
263 Construct->getNumArgs()),
264 FakeCommaLocs.data(),
265 FakeRParenLoc);
266
267 } else if (Construct->getNumArgs() >= 1) {
268 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var),
269 SemaRef.Owned(Construct->getArg(0)->Retain()),
270 false);
271 } else
272 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
273 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000274 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000275 D->hasCXXDirectInitializer());
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000276 SemaRef.PopExpressionEvaluationContext();
Douglas Gregor65b90052009-07-27 17:43:39 +0000277 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
278 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000279
280 return Var;
281}
282
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000283Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
284 bool Invalid = false;
John McCalla93c9342009-12-07 02:54:59 +0000285 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000286 if (DI->getType()->isDependentType()) {
287 DI = SemaRef.SubstType(DI, TemplateArgs,
288 D->getLocation(), D->getDeclName());
289 if (!DI) {
John McCalla93c9342009-12-07 02:54:59 +0000290 DI = D->getTypeSourceInfo();
John McCall07fb6be2009-10-22 23:33:21 +0000291 Invalid = true;
292 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000293 // C++ [temp.arg.type]p3:
294 // If a declaration acquires a function type through a type
295 // dependent on a template-parameter and this causes a
296 // declaration that does not use the syntactic form of a
297 // function declarator to have function type, the program is
298 // ill-formed.
299 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000300 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000301 Invalid = true;
302 }
303 }
304
305 Expr *BitWidth = D->getBitWidth();
306 if (Invalid)
307 BitWidth = 0;
308 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000309 // The bit-width expression is not potentially evaluated.
310 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000312 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000313 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000314 if (InstantiatedBitWidth.isInvalid()) {
315 Invalid = true;
316 BitWidth = 0;
317 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000318 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000319 }
320
John McCall07fb6be2009-10-22 23:33:21 +0000321 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
322 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000323 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000324 D->getLocation(),
325 D->isMutable(),
326 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000327 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000328 D->getAccess(),
329 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000330 if (!Field) {
331 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000332 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000335 InstantiateAttrs(D, Field);
336
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000337 if (Invalid)
338 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000340 if (!Field->getDeclName()) {
341 // Keep track of where this decl came from.
342 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000345 Field->setImplicit(D->isImplicit());
346 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000347
348 return Field;
349}
350
John McCall02cace72009-08-28 07:59:38 +0000351Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
352 FriendDecl::FriendUnion FU;
353
354 // Handle friend type expressions by simply substituting template
355 // parameters into the pattern type.
356 if (Type *Ty = D->getFriendType()) {
357 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
358 D->getLocation(), DeclarationName());
359 if (T.isNull()) return 0;
360
361 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
362 FU = T.getTypePtr();
363
364 // Handle everything else by appropriate substitution.
365 } else {
366 NamedDecl *ND = D->getFriendDecl();
367 assert(ND && "friend decl must be a decl or a type!");
368
Douglas Gregora735b202009-10-13 14:39:41 +0000369 // FIXME: We have a problem here, because the nested call to Visit(ND)
370 // will inject the thing that the friend references into the current
371 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000372 Decl *NewND = Visit(ND);
373 if (!NewND) return 0;
374
375 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCall02cace72009-08-28 07:59:38 +0000378 FriendDecl *FD =
379 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
380 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000381 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000382 Owner->addDecl(FD);
383 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000384}
385
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000386Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
387 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Douglas Gregorac7610d2009-06-22 20:57:11 +0000389 // The expression in a static assertion is not potentially evaluated.
390 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000392 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000393 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000394 if (InstantiatedAssertExpr.isInvalid())
395 return 0;
396
Douglas Gregor43d9d922009-08-08 01:41:12 +0000397 OwningExprResult Message(SemaRef, D->getMessage());
398 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000399 Decl *StaticAssert
400 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000401 move(InstantiatedAssertExpr),
402 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000403 return StaticAssert;
404}
405
406Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000407 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000408 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000409 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000410 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000411 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000412 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000413 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000414 Enum->startDefinition();
415
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000416 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000417
418 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000419 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
420 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000421 EC != ECEnd; ++EC) {
422 // The specified value for the enumerator.
423 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000424 if (Expr *UninstValue = EC->getInitExpr()) {
425 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000426 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000427 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
John McCallce3ff2b2009-08-25 22:02:44 +0000429 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000430 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000431
432 // Drop the initial value and continue.
433 bool isInvalid = false;
434 if (Value.isInvalid()) {
435 Value = SemaRef.Owned((Expr *)0);
436 isInvalid = true;
437 }
438
Mike Stump1eb44332009-09-09 15:08:12 +0000439 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000440 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
441 EC->getLocation(), EC->getIdentifier(),
442 move(Value));
443
444 if (isInvalid) {
445 if (EnumConst)
446 EnumConst->setInvalidDecl();
447 Enum->setInvalidDecl();
448 }
449
450 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000451 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000452 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000453 LastEnumConst = EnumConst;
454 }
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000457 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000458 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000459 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
460 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000461 &Enumerators[0], Enumerators.size(),
462 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000463
464 return Enum;
465}
466
Douglas Gregor6477b692009-03-25 15:04:13 +0000467Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
468 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
469 return 0;
470}
471
Douglas Gregored9c0f92009-10-29 00:04:11 +0000472namespace {
473 class SortDeclByLocation {
474 SourceManager &SourceMgr;
475
476 public:
477 explicit SortDeclByLocation(SourceManager &SourceMgr)
478 : SourceMgr(SourceMgr) { }
479
480 bool operator()(const Decl *X, const Decl *Y) const {
481 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
482 Y->getLocation());
483 }
484 };
485}
486
John McCalle29ba202009-08-20 01:44:21 +0000487Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000488 // Create a local instantiation scope for this class template, which
489 // will contain the instantiations of the template parameters.
490 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000491 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000492 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000493 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000494 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000495
496 CXXRecordDecl *Pattern = D->getTemplatedDecl();
497 CXXRecordDecl *RecordInst
498 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
499 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000500 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
501 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000502
503 ClassTemplateDecl *Inst
504 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
505 D->getIdentifier(), InstParams, RecordInst, 0);
506 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000507 if (D->getFriendObjectKind())
508 Inst->setObjectOfFriendDecl(true);
509 else
510 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000511 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000512
513 // Trigger creation of the type for the instantiation.
514 SemaRef.Context.getTypeDeclType(RecordInst);
515
Douglas Gregor259571e2009-10-30 22:42:42 +0000516 // Finish handling of friends.
517 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000518 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000519 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000520
John McCalle29ba202009-08-20 01:44:21 +0000521 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000522
523 // First, we sort the partial specializations by location, so
524 // that we instantiate them in the order they were declared.
525 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
526 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
527 P = D->getPartialSpecializations().begin(),
528 PEnd = D->getPartialSpecializations().end();
529 P != PEnd; ++P)
530 PartialSpecs.push_back(&*P);
531 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
532 SortDeclByLocation(SemaRef.SourceMgr));
533
534 // Instantiate all of the partial specializations of this member class
535 // template.
536 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
537 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
538
John McCalle29ba202009-08-20 01:44:21 +0000539 return Inst;
540}
541
Douglas Gregord60e1052009-08-27 16:57:43 +0000542Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000543TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
544 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000545 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
546
547 // Lookup the already-instantiated declaration in the instantiation
548 // of the class template and return that.
549 DeclContext::lookup_result Found
550 = Owner->lookup(ClassTemplate->getDeclName());
551 if (Found.first == Found.second)
552 return 0;
553
554 ClassTemplateDecl *InstClassTemplate
555 = dyn_cast<ClassTemplateDecl>(*Found.first);
556 if (!InstClassTemplate)
557 return 0;
558
559 Decl *DCanon = D->getCanonicalDecl();
560 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
561 P = InstClassTemplate->getPartialSpecializations().begin(),
562 PEnd = InstClassTemplate->getPartialSpecializations().end();
563 P != PEnd; ++P) {
564 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
565 return &*P;
566 }
567
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000568 return 0;
569}
570
571Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000572TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000573 // Create a local instantiation scope for this function template, which
574 // will contain the instantiations of the template parameters and then get
575 // merged with the local instantiation scope for the function template
576 // itself.
577 Sema::LocalInstantiationScope Scope(SemaRef);
578
Douglas Gregord60e1052009-08-27 16:57:43 +0000579 TemplateParameterList *TempParams = D->getTemplateParameters();
580 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000581 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000582 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000583
Douglas Gregora735b202009-10-13 14:39:41 +0000584 FunctionDecl *Instantiated = 0;
585 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
586 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
587 InstParams));
588 else
589 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
590 D->getTemplatedDecl(),
591 InstParams));
592
593 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000594 return 0;
595
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000597 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000598 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000599 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000600 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000601 assert(InstTemplate &&
602 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
603 if (!InstTemplate->getInstantiatedFromMemberTemplate())
604 InstTemplate->setInstantiatedFromMemberTemplate(D);
605
606 // Add non-friends into the owner.
607 if (!InstTemplate->getFriendObjectKind())
608 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000609 return InstTemplate;
610}
611
Douglas Gregord475b8d2009-03-25 21:17:03 +0000612Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
613 CXXRecordDecl *PrevDecl = 0;
614 if (D->isInjectedClassName())
615 PrevDecl = cast<CXXRecordDecl>(Owner);
616
617 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000618 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000619 D->getLocation(), D->getIdentifier(),
620 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000621 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000622 // FIXME: Check against AS_none is an ugly hack to work around the issue that
623 // the tag decls introduced by friend class declarations don't have an access
624 // specifier. Remove once this area of the code gets sorted out.
625 if (D->getAccess() != AS_none)
626 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000627 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000628 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000629
John McCall02cace72009-08-28 07:59:38 +0000630 // If the original function was part of a friend declaration,
631 // inherit its namespace state.
632 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
633 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
634
Anders Carlssond8b285f2009-09-01 04:26:58 +0000635 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
636
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000637 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000638 return Record;
639}
640
John McCall02cace72009-08-28 07:59:38 +0000641/// Normal class members are of more specific types and therefore
642/// don't make it here. This function serves two purposes:
643/// 1) instantiating function templates
644/// 2) substituting friend declarations
645/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000646 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
647 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000648 // Check whether there is already a function template specialization for
649 // this declaration.
650 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
651 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000652 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000653 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000654 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000655 TemplateArgs.getInnermost().getFlatArgumentList(),
656 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000657 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 FunctionTemplateSpecializationInfo *Info
660 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000661 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregor127102b2009-06-29 20:59:39 +0000663 // If we already have a function template specialization, return it.
664 if (Info)
665 return Info->Function;
666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor550d9b22009-10-31 17:21:17 +0000668 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregore53060f2009-06-25 22:08:12 +0000670 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000671 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000672 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000673 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000674
Douglas Gregore53060f2009-06-25 22:08:12 +0000675 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000676 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
677 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000678 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000679 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000680 D->getDeclName(), T, D->getTypeSourceInfo(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000681 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000682 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000683 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregore53060f2009-06-25 22:08:12 +0000685 // Attach the parameters
686 for (unsigned P = 0; P < Params.size(); ++P)
687 Params[P]->setOwningFunction(Function);
688 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000689
Douglas Gregora735b202009-10-13 14:39:41 +0000690 if (TemplateParams) {
691 // Our resulting instantiation is actually a function template, since we
692 // are substituting only the outer template parameters. For example, given
693 //
694 // template<typename T>
695 // struct X {
696 // template<typename U> friend void f(T, U);
697 // };
698 //
699 // X<int> x;
700 //
701 // We are instantiating the friend function template "f" within X<int>,
702 // which means substituting int for T, but leaving "f" as a friend function
703 // template.
704 // Build the function template itself.
705 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
706 Function->getLocation(),
707 Function->getDeclName(),
708 TemplateParams, Function);
709 Function->setDescribedFunctionTemplate(FunctionTemplate);
710 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000711 } else if (FunctionTemplate) {
712 // Record this function template specialization.
713 Function->setFunctionTemplateSpecialization(SemaRef.Context,
714 FunctionTemplate,
715 &TemplateArgs.getInnermost(),
716 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000717 }
Douglas Gregora735b202009-10-13 14:39:41 +0000718
Douglas Gregore53060f2009-06-25 22:08:12 +0000719 if (InitFunctionInstantiation(Function, D))
720 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregore53060f2009-06-25 22:08:12 +0000722 bool Redeclaration = false;
723 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000724
John McCall68263142009-11-18 22:49:29 +0000725 LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(),
726 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
727
Douglas Gregora735b202009-10-13 14:39:41 +0000728 if (TemplateParams || !FunctionTemplate) {
729 // Look only into the namespace where the friend would be declared to
730 // find a previous declaration. This is the innermost enclosing namespace,
731 // as described in ActOnFriendFunctionDecl.
John McCall68263142009-11-18 22:49:29 +0000732 SemaRef.LookupQualifiedName(Previous, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000733
Douglas Gregora735b202009-10-13 14:39:41 +0000734 // In C++, the previous declaration we find might be a tag type
735 // (class or enum). In this case, the new declaration will hide the
736 // tag type. Note that this does does not apply if we're declaring a
737 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000738 if (Previous.isSingleTagDecl())
739 Previous.clear();
Douglas Gregora735b202009-10-13 14:39:41 +0000740 }
741
John McCall9f54ad42009-12-10 09:41:52 +0000742 SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous,
743 false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000744 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000745
Douglas Gregora735b202009-10-13 14:39:41 +0000746 // If the original function was part of a friend declaration,
747 // inherit its namespace state and add it to the owner.
748 NamedDecl *FromFriendD
749 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
750 if (FromFriendD->getFriendObjectKind()) {
751 NamedDecl *ToFriendD = 0;
John McCall68263142009-11-18 22:49:29 +0000752 NamedDecl *PrevDecl;
Douglas Gregora735b202009-10-13 14:39:41 +0000753 if (TemplateParams) {
754 ToFriendD = cast<NamedDecl>(FunctionTemplate);
755 PrevDecl = FunctionTemplate->getPreviousDeclaration();
756 } else {
757 ToFriendD = Function;
758 PrevDecl = Function->getPreviousDeclaration();
759 }
760 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
761 if (!Owner->isDependentContext() && !PrevDecl)
762 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
763
764 if (!TemplateParams)
765 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
766 }
767
Douglas Gregore53060f2009-06-25 22:08:12 +0000768 return Function;
769}
770
Douglas Gregord60e1052009-08-27 16:57:43 +0000771Decl *
772TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
773 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000774 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
775 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000776 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000777 // We are creating a function template specialization from a function
778 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000779 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000780 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000781 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000782 TemplateArgs.getInnermost().getFlatArgumentList(),
783 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000784 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
786 FunctionTemplateSpecializationInfo *Info
787 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000788 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Douglas Gregor6b906862009-08-21 00:16:32 +0000790 // If we already have a function template specialization, return it.
791 if (Info)
792 return Info->Function;
793 }
794
Douglas Gregor550d9b22009-10-31 17:21:17 +0000795 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000796
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000797 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000798 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000799 if (T.isNull())
800 return 0;
801
802 // Build the instantiated method declaration.
803 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000804 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Douglas Gregordec06662009-08-21 18:42:58 +0000806 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000807 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000808 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
809 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
810 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000811 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
812 Constructor->getLocation(),
813 Name, T,
John McCalla93c9342009-12-07 02:54:59 +0000814 Constructor->getTypeSourceInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000815 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000816 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000817 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
818 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
819 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
820 SemaRef.Context.getCanonicalType(ClassTy));
821 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
822 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000823 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000824 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000825 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000826 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000827 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000828 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
829 ConvTy);
830 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
831 Conversion->getLocation(), Name,
John McCalla93c9342009-12-07 02:54:59 +0000832 T, Conversion->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000833 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000834 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000835 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000836 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
John McCalla93c9342009-12-07 02:54:59 +0000837 D->getDeclName(), T, D->getTypeSourceInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000838 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000839 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000840
Douglas Gregord60e1052009-08-27 16:57:43 +0000841 if (TemplateParams) {
842 // Our resulting instantiation is actually a function template, since we
843 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000844 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000845 // template<typename T>
846 // struct X {
847 // template<typename U> void f(T, U);
848 // };
849 //
850 // X<int> x;
851 //
852 // We are instantiating the member template "f" within X<int>, which means
853 // substituting int for T, but leaving "f" as a member function template.
854 // Build the function template itself.
855 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
856 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000857 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000858 TemplateParams, Method);
859 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000860 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000861 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000862 } else if (FunctionTemplate) {
863 // Record this function template specialization.
864 Method->setFunctionTemplateSpecialization(SemaRef.Context,
865 FunctionTemplate,
866 &TemplateArgs.getInnermost(),
867 InsertPos);
868 } else {
869 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000870 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000871 }
872
Mike Stump1eb44332009-09-09 15:08:12 +0000873 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000874 // out-of-line, the instantiation will have the same lexical
875 // context (which will be a namespace scope) as the template.
876 if (D->isOutOfLine())
877 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregor5545e162009-03-24 00:38:23 +0000879 // Attach the parameters
880 for (unsigned P = 0; P < Params.size(); ++P)
881 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000882 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000883
884 if (InitMethodInstantiation(Method, D))
885 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000886
John McCall68263142009-11-18 22:49:29 +0000887 LookupResult Previous(SemaRef, Name, SourceLocation(),
888 Sema::LookupOrdinaryName, Sema::ForRedeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregord60e1052009-08-27 16:57:43 +0000890 if (!FunctionTemplate || TemplateParams) {
John McCall68263142009-11-18 22:49:29 +0000891 SemaRef.LookupQualifiedName(Previous, Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Douglas Gregordec06662009-08-21 18:42:58 +0000893 // In C++, the previous declaration we find might be a tag type
894 // (class or enum). In this case, the new declaration will hide the
895 // tag type. Note that this does does not apply if we're declaring a
896 // typedef (C++ [dcl.typedef]p4).
John McCall68263142009-11-18 22:49:29 +0000897 if (Previous.isSingleTagDecl())
898 Previous.clear();
Douglas Gregordec06662009-08-21 18:42:58 +0000899 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000900
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000901 bool Redeclaration = false;
902 bool OverloadableAttrRequired = false;
John McCall9f54ad42009-12-10 09:41:52 +0000903 SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000904 /*FIXME:*/OverloadableAttrRequired);
905
Douglas Gregor4ba31362009-12-01 17:24:26 +0000906 if (D->isPure())
907 SemaRef.CheckPureMethod(Method, SourceRange());
908
John McCall68263142009-11-18 22:49:29 +0000909 if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
Douglas Gregora735b202009-10-13 14:39:41 +0000910 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000911 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000913 return Method;
914}
915
Douglas Gregor615c5d42009-03-24 16:43:20 +0000916Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000917 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000918}
919
Douglas Gregor03b2b072009-03-24 00:15:49 +0000920Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000921 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000922}
923
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000924Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000925 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000926}
927
Douglas Gregor6477b692009-03-25 15:04:13 +0000928ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000929 QualType T;
John McCalla93c9342009-12-07 02:54:59 +0000930 TypeSourceInfo *DI = D->getTypeSourceInfo();
John McCall58e46772009-10-23 21:48:59 +0000931 if (DI) {
932 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
933 D->getDeclName());
934 if (DI) T = DI->getType();
935 } else {
936 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
937 D->getDeclName());
938 DI = 0;
939 }
940
941 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000942 return 0;
943
John McCall58e46772009-10-23 21:48:59 +0000944 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000945
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000946 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000947 ParmVarDecl *Param
948 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
949 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000950
Anders Carlsson9351c172009-08-25 03:18:48 +0000951 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000952 if (D->hasUninstantiatedDefaultArg())
953 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000954 else if (Expr *Arg = D->getDefaultArg())
955 Param->setUninstantiatedDefaultArg(Arg);
956
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000957 // Note: we don't try to instantiate function parameters until after
958 // we've instantiated the function's type. Therefore, we don't have
959 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000960 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000961 return Param;
962}
963
John McCalle29ba202009-08-20 01:44:21 +0000964Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
965 TemplateTypeParmDecl *D) {
966 // TODO: don't always clone when decls are refcounted.
967 const Type* T = D->getTypeForDecl();
968 assert(T->isTemplateTypeParmType());
969 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000970
John McCalle29ba202009-08-20 01:44:21 +0000971 TemplateTypeParmDecl *Inst =
972 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000973 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000974 TTPT->getName(),
975 D->wasDeclaredWithTypename(),
976 D->isParameterPack());
977
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000978 if (D->hasDefaultArgument())
979 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000980
Douglas Gregor550d9b22009-10-31 17:21:17 +0000981 // Introduce this template parameter's instantiation into the instantiation
982 // scope.
983 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
984
John McCalle29ba202009-08-20 01:44:21 +0000985 return Inst;
986}
987
Douglas Gregor33642df2009-10-23 23:25:44 +0000988Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
989 NonTypeTemplateParmDecl *D) {
990 // Substitute into the type of the non-type template parameter.
991 QualType T;
John McCalla93c9342009-12-07 02:54:59 +0000992 TypeSourceInfo *DI = D->getTypeSourceInfo();
Douglas Gregor33642df2009-10-23 23:25:44 +0000993 if (DI) {
994 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
995 D->getDeclName());
996 if (DI) T = DI->getType();
997 } else {
998 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
999 D->getDeclName());
1000 DI = 0;
1001 }
1002 if (T.isNull())
1003 return 0;
1004
1005 // Check that this type is acceptable for a non-type template parameter.
1006 bool Invalid = false;
1007 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
1008 if (T.isNull()) {
1009 T = SemaRef.Context.IntTy;
1010 Invalid = true;
1011 }
1012
1013 NonTypeTemplateParmDecl *Param
1014 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1015 D->getDepth() - 1, D->getPosition(),
1016 D->getIdentifier(), T, DI);
1017 if (Invalid)
1018 Param->setInvalidDecl();
1019
1020 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +00001021
1022 // Introduce this template parameter's instantiation into the instantiation
1023 // scope.
1024 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +00001025 return Param;
1026}
1027
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001028Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +00001029TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
1030 TemplateTemplateParmDecl *D) {
1031 // Instantiate the template parameter list of the template template parameter.
1032 TemplateParameterList *TempParams = D->getTemplateParameters();
1033 TemplateParameterList *InstParams;
1034 {
1035 // Perform the actual substitution of template parameters within a new,
1036 // local instantiation scope.
1037 Sema::LocalInstantiationScope Scope(SemaRef);
1038 InstParams = SubstTemplateParams(TempParams);
1039 if (!InstParams)
1040 return NULL;
1041 }
1042
1043 // Build the template template parameter.
1044 TemplateTemplateParmDecl *Param
1045 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1046 D->getDepth() - 1, D->getPosition(),
1047 D->getIdentifier(), InstParams);
1048 Param->setDefaultArgument(D->getDefaultArgument());
1049
1050 // Introduce this template parameter's instantiation into the instantiation
1051 // scope.
1052 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1053
1054 return Param;
1055}
1056
Douglas Gregor48c32a72009-11-17 06:07:40 +00001057Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1058 // Using directives are never dependent, so they require no explicit
1059
1060 UsingDirectiveDecl *Inst
1061 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1062 D->getNamespaceKeyLocation(),
1063 D->getQualifierRange(), D->getQualifier(),
1064 D->getIdentLocation(),
1065 D->getNominatedNamespace(),
1066 D->getCommonAncestor());
1067 Owner->addDecl(Inst);
1068 return Inst;
1069}
1070
John McCalled976492009-12-04 22:46:56 +00001071Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
1072 // The nested name specifier is non-dependent, so no transformation
1073 // is required.
1074
John McCall9f54ad42009-12-10 09:41:52 +00001075 // We only need to do redeclaration lookups if we're in a class
1076 // scope (in fact, it's not really even possible in non-class
1077 // scopes).
1078 bool CheckRedeclaration = Owner->isRecord();
1079
1080 LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(),
1081 Sema::LookupUsingDeclName, Sema::ForRedeclaration);
1082
John McCalled976492009-12-04 22:46:56 +00001083 UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
1084 D->getLocation(),
1085 D->getNestedNameRange(),
1086 D->getUsingLocation(),
1087 D->getTargetNestedNameDecl(),
1088 D->getDeclName(),
1089 D->isTypeName());
1090
1091 CXXScopeSpec SS;
1092 SS.setScopeRep(D->getTargetNestedNameDecl());
1093 SS.setRange(D->getNestedNameRange());
John McCall9f54ad42009-12-10 09:41:52 +00001094
1095 if (CheckRedeclaration) {
1096 Prev.setHideTags(false);
1097 SemaRef.LookupQualifiedName(Prev, Owner);
1098
1099 // Check for invalid redeclarations.
1100 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(),
1101 D->isTypeName(), SS,
1102 D->getLocation(), Prev))
1103 NewUD->setInvalidDecl();
1104
1105 }
1106
1107 if (!NewUD->isInvalidDecl() &&
1108 SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS,
John McCalled976492009-12-04 22:46:56 +00001109 D->getLocation()))
1110 NewUD->setInvalidDecl();
John McCall9f54ad42009-12-10 09:41:52 +00001111
John McCalled976492009-12-04 22:46:56 +00001112 SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
1113 NewUD->setAccess(D->getAccess());
1114 Owner->addDecl(NewUD);
1115
John McCall9f54ad42009-12-10 09:41:52 +00001116 // Don't process the shadow decls for an invalid decl.
1117 if (NewUD->isInvalidDecl())
1118 return NewUD;
1119
1120 // Process the shadow decls.
1121 for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end();
1122 I != E; ++I) {
1123 UsingShadowDecl *Shadow = *I;
1124 NamedDecl *InstTarget =
1125 cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(),
1126 TemplateArgs));
1127
1128 if (CheckRedeclaration &&
1129 SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev))
1130 continue;
1131
1132 UsingShadowDecl *InstShadow
1133 = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget);
1134 SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
1135 }
John McCalled976492009-12-04 22:46:56 +00001136
1137 return NewUD;
1138}
1139
1140Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
John McCall9f54ad42009-12-10 09:41:52 +00001141 // Ignore these; we handle them in bulk when processing the UsingDecl.
1142 return 0;
John McCalled976492009-12-04 22:46:56 +00001143}
1144
John McCall7ba107a2009-11-18 02:36:19 +00001145Decl * TemplateDeclInstantiator
1146 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001147 NestedNameSpecifier *NNS =
1148 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1149 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001150 TemplateArgs);
1151 if (!NNS)
1152 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001154 CXXScopeSpec SS;
1155 SS.setRange(D->getTargetNestedNameRange());
1156 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001157
1158 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001159 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001160 D->getUsingLoc(), SS, D->getLocation(),
1161 D->getDeclName(), 0,
1162 /*instantiation*/ true,
1163 /*typename*/ true, D->getTypenameLoc());
1164 if (UD)
John McCalled976492009-12-04 22:46:56 +00001165 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1166
John McCall7ba107a2009-11-18 02:36:19 +00001167 return UD;
1168}
1169
1170Decl * TemplateDeclInstantiator
1171 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1172 NestedNameSpecifier *NNS =
1173 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1174 D->getTargetNestedNameRange(),
1175 TemplateArgs);
1176 if (!NNS)
1177 return 0;
1178
1179 CXXScopeSpec SS;
1180 SS.setRange(D->getTargetNestedNameRange());
1181 SS.setScopeRep(NNS);
1182
1183 NamedDecl *UD =
1184 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1185 D->getUsingLoc(), SS, D->getLocation(),
1186 D->getDeclName(), 0,
1187 /*instantiation*/ true,
1188 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001189 if (UD)
John McCalled976492009-12-04 22:46:56 +00001190 SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
1191
Anders Carlsson0d8df782009-08-29 19:37:28 +00001192 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001193}
1194
John McCallce3ff2b2009-08-25 22:02:44 +00001195Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001196 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001197 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001198 return Instantiator.Visit(D);
1199}
1200
John McCalle29ba202009-08-20 01:44:21 +00001201/// \brief Instantiates a nested template parameter list in the current
1202/// instantiation context.
1203///
1204/// \param L The parameter list to instantiate
1205///
1206/// \returns NULL if there was an error
1207TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001208TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001209 // Get errors for all the parameters before bailing out.
1210 bool Invalid = false;
1211
1212 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001213 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001214 ParamVector Params;
1215 Params.reserve(N);
1216 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1217 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001218 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001219 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001220 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001221 }
1222
1223 // Clean up if we had an error.
1224 if (Invalid) {
1225 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1226 PI != PE; ++PI)
1227 if (*PI)
1228 (*PI)->Destroy(SemaRef.Context);
1229 return NULL;
1230 }
1231
1232 TemplateParameterList *InstL
1233 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1234 L->getLAngleLoc(), &Params.front(), N,
1235 L->getRAngleLoc());
1236 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001237}
John McCalle29ba202009-08-20 01:44:21 +00001238
Douglas Gregored9c0f92009-10-29 00:04:11 +00001239/// \brief Instantiate the declaration of a class template partial
1240/// specialization.
1241///
1242/// \param ClassTemplate the (instantiated) class template that is partially
1243// specialized by the instantiation of \p PartialSpec.
1244///
1245/// \param PartialSpec the (uninstantiated) class template partial
1246/// specialization that we are instantiating.
1247///
1248/// \returns true if there was an error, false otherwise.
1249bool
1250TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1251 ClassTemplateDecl *ClassTemplate,
1252 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001253 // Create a local instantiation scope for this class template partial
1254 // specialization, which will contain the instantiations of the template
1255 // parameters.
1256 Sema::LocalInstantiationScope Scope(SemaRef);
1257
Douglas Gregored9c0f92009-10-29 00:04:11 +00001258 // Substitute into the template parameters of the class template partial
1259 // specialization.
1260 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1261 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1262 if (!InstParams)
1263 return true;
1264
1265 // Substitute into the template arguments of the class template partial
1266 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001267 const TemplateArgumentLoc *PartialSpecTemplateArgs
1268 = PartialSpec->getTemplateArgsAsWritten();
1269 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1270
John McCalld5532b62009-11-23 01:53:49 +00001271 TemplateArgumentListInfo InstTemplateArgs; // no angle locations
John McCall833ca992009-10-29 08:12:44 +00001272 for (unsigned I = 0; I != N; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001273 TemplateArgumentLoc Loc;
1274 if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001275 return true;
John McCalld5532b62009-11-23 01:53:49 +00001276 InstTemplateArgs.addArgument(Loc);
Douglas Gregored9c0f92009-10-29 00:04:11 +00001277 }
1278
1279
1280 // Check that the template argument list is well-formed for this
1281 // class template.
1282 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1283 InstTemplateArgs.size());
1284 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1285 PartialSpec->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001286 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001287 false,
1288 Converted))
1289 return true;
1290
1291 // Figure out where to insert this class template partial specialization
1292 // in the member template's set of class template partial specializations.
1293 llvm::FoldingSetNodeID ID;
1294 ClassTemplatePartialSpecializationDecl::Profile(ID,
1295 Converted.getFlatArguments(),
1296 Converted.flatSize(),
1297 SemaRef.Context);
1298 void *InsertPos = 0;
1299 ClassTemplateSpecializationDecl *PrevDecl
1300 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1301 InsertPos);
1302
1303 // Build the canonical type that describes the converted template
1304 // arguments of the class template partial specialization.
1305 QualType CanonType
1306 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1307 Converted.getFlatArguments(),
1308 Converted.flatSize());
1309
1310 // Build the fully-sugared type for this class template
1311 // specialization as the user wrote in the specialization
1312 // itself. This means that we'll pretty-print the type retrieved
1313 // from the specialization's declaration the way that the user
1314 // actually wrote the specialization, rather than formatting the
1315 // name based on the "canonical" representation used to store the
1316 // template arguments in the specialization.
1317 QualType WrittenTy
1318 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
John McCalld5532b62009-11-23 01:53:49 +00001319 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001320 CanonType);
1321
1322 if (PrevDecl) {
1323 // We've already seen a partial specialization with the same template
1324 // parameters and template arguments. This can happen, for example, when
1325 // substituting the outer template arguments ends up causing two
1326 // class template partial specializations of a member class template
1327 // to have identical forms, e.g.,
1328 //
1329 // template<typename T, typename U>
1330 // struct Outer {
1331 // template<typename X, typename Y> struct Inner;
1332 // template<typename Y> struct Inner<T, Y>;
1333 // template<typename Y> struct Inner<U, Y>;
1334 // };
1335 //
1336 // Outer<int, int> outer; // error: the partial specializations of Inner
1337 // // have the same signature.
1338 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1339 << WrittenTy;
1340 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1341 << SemaRef.Context.getTypeDeclType(PrevDecl);
1342 return true;
1343 }
1344
1345
1346 // Create the class template partial specialization declaration.
1347 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1348 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1349 PartialSpec->getLocation(),
1350 InstParams,
1351 ClassTemplate,
1352 Converted,
John McCalld5532b62009-11-23 01:53:49 +00001353 InstTemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001354 0);
1355 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1356 InstPartialSpec->setTypeAsWritten(WrittenTy);
1357
1358 // Add this partial specialization to the set of class template partial
1359 // specializations.
1360 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1361 InsertPos);
1362 return false;
1363}
1364
John McCallce3ff2b2009-08-25 22:02:44 +00001365/// \brief Does substitution on the type of the given function, including
1366/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001367///
John McCallce3ff2b2009-08-25 22:02:44 +00001368/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001369///
1370/// \param Params the instantiated parameter declarations
1371
John McCallce3ff2b2009-08-25 22:02:44 +00001372/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001373/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001374QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001375TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001376 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1377 bool InvalidDecl = false;
1378
John McCallce3ff2b2009-08-25 22:02:44 +00001379 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001380 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001381 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001382 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001383 PEnd = D->param_end();
1384 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001385 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001386 if (PInst->getType()->isVoidType()) {
1387 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1388 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001389 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001390 PInst->getType(),
1391 diag::err_abstract_type_in_decl,
1392 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001393 PInst->setInvalidDecl();
1394
1395 Params.push_back(PInst);
1396 ParamTys.push_back(PInst->getType());
1397
1398 if (PInst->isInvalidDecl())
1399 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001400 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001401 InvalidDecl = true;
1402 }
1403
1404 // FIXME: Deallocate dead declarations.
1405 if (InvalidDecl)
1406 return QualType();
1407
John McCall183700f2009-09-21 23:43:11 +00001408 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001409 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001410 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001411 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1412 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001413 if (ResultType.isNull())
1414 return QualType();
1415
Jay Foadbeaaccd2009-05-21 09:52:38 +00001416 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001417 Proto->isVariadic(), Proto->getTypeQuals(),
1418 D->getLocation(), D->getDeclName());
1419}
1420
Mike Stump1eb44332009-09-09 15:08:12 +00001421/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001422/// declaration (New) from the corresponding fields of its template (Tmpl).
1423///
1424/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001425bool
1426TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001427 FunctionDecl *Tmpl) {
1428 if (Tmpl->isDeleted())
1429 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Douglas Gregorcca9e962009-07-01 22:01:06 +00001431 // If we are performing substituting explicitly-specified template arguments
1432 // or deduced template arguments into a function template and we reach this
1433 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001434 // to keeping the new function template specialization. We therefore
1435 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001436 // into a template instantiation for this specific function template
1437 // specialization, which is not a SFINAE context, so that we diagnose any
1438 // further errors in the declaration itself.
1439 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1440 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1441 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1442 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001443 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001444 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001445 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001446 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001447 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001448 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1449 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001450 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001451 }
1452 }
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +00001454 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
1455 assert(Proto && "Function template without prototype?");
1456
1457 if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() ||
1458 Proto->getNoReturnAttr()) {
1459 // The function has an exception specification or a "noreturn"
1460 // attribute. Substitute into each of the exception types.
1461 llvm::SmallVector<QualType, 4> Exceptions;
1462 for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) {
1463 // FIXME: Poor location information!
1464 QualType T
1465 = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs,
1466 New->getLocation(), New->getDeclName());
1467 if (T.isNull() ||
1468 SemaRef.CheckSpecifiedExceptionType(T, New->getLocation()))
1469 continue;
1470
1471 Exceptions.push_back(T);
1472 }
1473
1474 // Rebuild the function type
1475
1476 const FunctionProtoType *NewProto
1477 = New->getType()->getAs<FunctionProtoType>();
1478 assert(NewProto && "Template instantiation without function prototype?");
1479 New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(),
1480 NewProto->arg_type_begin(),
1481 NewProto->getNumArgs(),
1482 NewProto->isVariadic(),
1483 NewProto->getTypeQuals(),
1484 Proto->hasExceptionSpec(),
1485 Proto->hasAnyExceptionSpec(),
1486 Exceptions.size(),
1487 Exceptions.data(),
1488 Proto->getNoReturnAttr()));
1489 }
1490
Douglas Gregore53060f2009-06-25 22:08:12 +00001491 return false;
1492}
1493
Douglas Gregor5545e162009-03-24 00:38:23 +00001494/// \brief Initializes common fields of an instantiated method
1495/// declaration (New) from the corresponding fields of its template
1496/// (Tmpl).
1497///
1498/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001499bool
1500TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001501 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001502 if (InitFunctionInstantiation(New, Tmpl))
1503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregor5545e162009-03-24 00:38:23 +00001505 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1506 New->setAccess(Tmpl->getAccess());
Fariborz Jahaniane7184df2009-12-03 18:44:40 +00001507 if (Tmpl->isVirtualAsWritten())
1508 Record->setMethodAsVirtual(New);
Douglas Gregor5545e162009-03-24 00:38:23 +00001509
1510 // FIXME: attributes
1511 // FIXME: New needs a pointer to Tmpl
1512 return false;
1513}
Douglas Gregora58861f2009-05-13 20:28:22 +00001514
1515/// \brief Instantiate the definition of the given function from its
1516/// template.
1517///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001518/// \param PointOfInstantiation the point at which the instantiation was
1519/// required. Note that this is not precisely a "point of instantiation"
1520/// for the function, but it's close.
1521///
Douglas Gregora58861f2009-05-13 20:28:22 +00001522/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001523/// function template specialization or member function of a class template
1524/// specialization.
1525///
1526/// \param Recursive if true, recursively instantiates any functions that
1527/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001528///
1529/// \param DefinitionRequired if true, then we are performing an explicit
1530/// instantiation where the body of the function is required. Complain if
1531/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001532void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001533 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001534 bool Recursive,
1535 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001536 if (Function->isInvalidDecl())
1537 return;
1538
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001539 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001541 // Never instantiate an explicit specialization.
1542 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1543 return;
1544
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001545 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001546 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001547 Stmt *Pattern = 0;
1548 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001549 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001550
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001551 if (!Pattern) {
1552 if (DefinitionRequired) {
1553 if (Function->getPrimaryTemplate())
1554 Diag(PointOfInstantiation,
1555 diag::err_explicit_instantiation_undefined_func_template)
1556 << Function->getPrimaryTemplate();
1557 else
1558 Diag(PointOfInstantiation,
1559 diag::err_explicit_instantiation_undefined_member)
1560 << 1 << Function->getDeclName() << Function->getDeclContext();
1561
1562 if (PatternDecl)
1563 Diag(PatternDecl->getLocation(),
1564 diag::note_explicit_instantiation_here);
1565 }
1566
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001567 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001568 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001569
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001570 // C++0x [temp.explicit]p9:
1571 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001572 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001573 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001574 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001575 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001576 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001577 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001579 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1580 if (Inst)
1581 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001582
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001583 // If we're performing recursive template instantiation, create our own
1584 // queue of pending implicit instantiations that we will instantiate later,
1585 // while we're still within our own instantiation context.
1586 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1587 if (Recursive)
1588 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001590 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1591
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001592 // Introduce a new scope where local variable instantiations will be
1593 // recorded.
1594 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001596 // Introduce the instantiated function parameters into the local
1597 // instantiation scope.
1598 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1599 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1600 Function->getParamDecl(I));
1601
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001602 // Enter the scope of this instantiation. We don't use
1603 // PushDeclContext because we don't have a scope.
1604 DeclContext *PreviousContext = CurContext;
1605 CurContext = Function;
1606
Mike Stump1eb44332009-09-09 15:08:12 +00001607 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001608 getTemplateInstantiationArgs(Function);
1609
1610 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001611 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001612 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1613 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1614 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001615 }
1616
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001617 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001618 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001619
Douglas Gregor52604ab2009-09-11 21:19:12 +00001620 if (Body.isInvalid())
1621 Function->setInvalidDecl();
1622
Mike Stump1eb44332009-09-09 15:08:12 +00001623 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001624 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001625
1626 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001627
1628 DeclGroupRef DG(Function);
1629 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001631 if (Recursive) {
1632 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001633 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001634 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001636 // Restore the set of pending implicit instantiations.
1637 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1638 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001639}
1640
1641/// \brief Instantiate the definition of the given variable from its
1642/// template.
1643///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001644/// \param PointOfInstantiation the point at which the instantiation was
1645/// required. Note that this is not precisely a "point of instantiation"
1646/// for the function, but it's close.
1647///
1648/// \param Var the already-instantiated declaration of a static member
1649/// variable of a class template specialization.
1650///
1651/// \param Recursive if true, recursively instantiates any functions that
1652/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001653///
1654/// \param DefinitionRequired if true, then we are performing an explicit
1655/// instantiation where an out-of-line definition of the member variable
1656/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001657void Sema::InstantiateStaticDataMemberDefinition(
1658 SourceLocation PointOfInstantiation,
1659 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001660 bool Recursive,
1661 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001662 if (Var->isInvalidDecl())
1663 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregor7caa6822009-07-24 20:34:43 +00001665 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001666 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001667 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001668 assert(Def->isStaticDataMember() && "Not a static data member?");
1669 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor0d035142009-10-27 18:42:08 +00001671 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001672 // We did not find an out-of-line definition of this static data member,
1673 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001674 // instantiate this definition (or provide a specialization for it) in
1675 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001676 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001677 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001678 Diag(PointOfInstantiation,
1679 diag::err_explicit_instantiation_undefined_member)
1680 << 2 << Var->getDeclName() << Var->getDeclContext();
1681 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1682 }
1683
Douglas Gregor7caa6822009-07-24 20:34:43 +00001684 return;
1685 }
1686
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001687 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001688 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001689 return;
1690
1691 // C++0x [temp.explicit]p9:
1692 // Except for inline functions, other explicit instantiation declarations
1693 // have the effect of suppressing the implicit instantiation of the entity
1694 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001695 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001696 == TSK_ExplicitInstantiationDeclaration)
1697 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Douglas Gregor7caa6822009-07-24 20:34:43 +00001699 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1700 if (Inst)
1701 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregor7caa6822009-07-24 20:34:43 +00001703 // If we're performing recursive template instantiation, create our own
1704 // queue of pending implicit instantiations that we will instantiate later,
1705 // while we're still within our own instantiation context.
1706 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1707 if (Recursive)
1708 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Douglas Gregor7caa6822009-07-24 20:34:43 +00001710 // Enter the scope of this instantiation. We don't use
1711 // PushDeclContext because we don't have a scope.
1712 DeclContext *PreviousContext = CurContext;
1713 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001715 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001716 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001717 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001718 CurContext = PreviousContext;
1719
1720 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001721 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001722 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1723 assert(MSInfo && "Missing member specialization information?");
1724 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1725 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001726 DeclGroupRef DG(Var);
1727 Consumer.HandleTopLevelDecl(DG);
1728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor7caa6822009-07-24 20:34:43 +00001730 if (Recursive) {
1731 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001732 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001733 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Douglas Gregor7caa6822009-07-24 20:34:43 +00001735 // Restore the set of pending implicit instantiations.
1736 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001737 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001738}
Douglas Gregor815215d2009-05-27 05:35:12 +00001739
Anders Carlsson09025312009-08-29 05:16:22 +00001740void
1741Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1742 const CXXConstructorDecl *Tmpl,
1743 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Anders Carlsson09025312009-08-29 05:16:22 +00001745 llvm::SmallVector<MemInitTy*, 4> NewInits;
1746
1747 // Instantiate all the initializers.
1748 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001749 InitsEnd = Tmpl->init_end();
1750 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001751 CXXBaseOrMemberInitializer *Init = *Inits;
1752
1753 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Anders Carlsson09025312009-08-29 05:16:22 +00001755 // Instantiate all the arguments.
1756 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1757 Args != ArgsEnd; ++Args) {
1758 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1759
1760 if (NewArg.isInvalid())
1761 New->setInvalidDecl();
1762 else
1763 NewArgs.push_back(NewArg.takeAs<Expr>());
1764 }
1765
1766 MemInitResult NewInit;
1767
1768 if (Init->isBaseInitializer()) {
John McCalla93c9342009-12-07 02:54:59 +00001769 TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001770 TemplateArgs,
1771 Init->getSourceLocation(),
1772 New->getDeclName());
John McCalla93c9342009-12-07 02:54:59 +00001773 if (!BaseTInfo) {
Douglas Gregor802ab452009-12-02 22:36:29 +00001774 New->setInvalidDecl();
1775 continue;
1776 }
1777
John McCalla93c9342009-12-07 02:54:59 +00001778 NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001779 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001780 NewArgs.size(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001781 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001782 Init->getRParenLoc(),
1783 New->getParent());
1784 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001785 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001787 // Is this an anonymous union?
1788 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001789 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001790 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001791 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1792 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001793
1794 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001795 NewArgs.size(),
1796 Init->getSourceLocation(),
Douglas Gregor802ab452009-12-02 22:36:29 +00001797 Init->getLParenLoc(),
Anders Carlsson09025312009-08-29 05:16:22 +00001798 Init->getRParenLoc());
1799 }
1800
1801 if (NewInit.isInvalid())
1802 New->setInvalidDecl();
1803 else {
1804 // FIXME: It would be nice if ASTOwningVector had a release function.
1805 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Anders Carlsson09025312009-08-29 05:16:22 +00001807 NewInits.push_back((MemInitTy *)NewInit.get());
1808 }
1809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Anders Carlsson09025312009-08-29 05:16:22 +00001811 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001812 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001813 /*FIXME: ColonLoc */
1814 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001815 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001816}
1817
John McCall52a575a2009-08-29 08:11:13 +00001818// TODO: this could be templated if the various decl types used the
1819// same method name.
1820static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1821 ClassTemplateDecl *Instance) {
1822 Pattern = Pattern->getCanonicalDecl();
1823
1824 do {
1825 Instance = Instance->getCanonicalDecl();
1826 if (Pattern == Instance) return true;
1827 Instance = Instance->getInstantiatedFromMemberTemplate();
1828 } while (Instance);
1829
1830 return false;
1831}
1832
Douglas Gregor0d696532009-09-28 06:34:35 +00001833static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1834 FunctionTemplateDecl *Instance) {
1835 Pattern = Pattern->getCanonicalDecl();
1836
1837 do {
1838 Instance = Instance->getCanonicalDecl();
1839 if (Pattern == Instance) return true;
1840 Instance = Instance->getInstantiatedFromMemberTemplate();
1841 } while (Instance);
1842
1843 return false;
1844}
1845
Douglas Gregored9c0f92009-10-29 00:04:11 +00001846static bool
1847isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1848 ClassTemplatePartialSpecializationDecl *Instance) {
1849 Pattern
1850 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1851 do {
1852 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1853 Instance->getCanonicalDecl());
1854 if (Pattern == Instance)
1855 return true;
1856 Instance = Instance->getInstantiatedFromMember();
1857 } while (Instance);
1858
1859 return false;
1860}
1861
John McCall52a575a2009-08-29 08:11:13 +00001862static bool isInstantiationOf(CXXRecordDecl *Pattern,
1863 CXXRecordDecl *Instance) {
1864 Pattern = Pattern->getCanonicalDecl();
1865
1866 do {
1867 Instance = Instance->getCanonicalDecl();
1868 if (Pattern == Instance) return true;
1869 Instance = Instance->getInstantiatedFromMemberClass();
1870 } while (Instance);
1871
1872 return false;
1873}
1874
1875static bool isInstantiationOf(FunctionDecl *Pattern,
1876 FunctionDecl *Instance) {
1877 Pattern = Pattern->getCanonicalDecl();
1878
1879 do {
1880 Instance = Instance->getCanonicalDecl();
1881 if (Pattern == Instance) return true;
1882 Instance = Instance->getInstantiatedFromMemberFunction();
1883 } while (Instance);
1884
1885 return false;
1886}
1887
1888static bool isInstantiationOf(EnumDecl *Pattern,
1889 EnumDecl *Instance) {
1890 Pattern = Pattern->getCanonicalDecl();
1891
1892 do {
1893 Instance = Instance->getCanonicalDecl();
1894 if (Pattern == Instance) return true;
1895 Instance = Instance->getInstantiatedFromMemberEnum();
1896 } while (Instance);
1897
1898 return false;
1899}
1900
John McCalled976492009-12-04 22:46:56 +00001901static bool isInstantiationOf(UsingShadowDecl *Pattern,
1902 UsingShadowDecl *Instance,
1903 ASTContext &C) {
1904 return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern;
1905}
1906
1907static bool isInstantiationOf(UsingDecl *Pattern,
1908 UsingDecl *Instance,
1909 ASTContext &C) {
1910 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
1911}
1912
John McCall7ba107a2009-11-18 02:36:19 +00001913static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
1914 UsingDecl *Instance,
1915 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00001916 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
John McCall7ba107a2009-11-18 02:36:19 +00001917}
1918
1919static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00001920 UsingDecl *Instance,
1921 ASTContext &C) {
John McCalled976492009-12-04 22:46:56 +00001922 return C.getInstantiatedFromUsingDecl(Instance) == Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +00001923}
1924
John McCall52a575a2009-08-29 08:11:13 +00001925static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1926 VarDecl *Instance) {
1927 assert(Instance->isStaticDataMember());
1928
1929 Pattern = Pattern->getCanonicalDecl();
1930
1931 do {
1932 Instance = Instance->getCanonicalDecl();
1933 if (Pattern == Instance) return true;
1934 Instance = Instance->getInstantiatedFromStaticDataMember();
1935 } while (Instance);
1936
1937 return false;
1938}
1939
John McCalled976492009-12-04 22:46:56 +00001940// Other is the prospective instantiation
1941// D is the prospective pattern
Douglas Gregor815215d2009-05-27 05:35:12 +00001942static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001943 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00001944 if (UnresolvedUsingTypenameDecl *UUD
1945 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
1946 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1947 return isInstantiationOf(UUD, UD, Ctx);
1948 }
1949 }
1950
1951 if (UnresolvedUsingValueDecl *UUD
1952 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001953 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1954 return isInstantiationOf(UUD, UD, Ctx);
1955 }
1956 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001957
Anders Carlsson0d8df782009-08-29 19:37:28 +00001958 return false;
1959 }
Mike Stump1eb44332009-09-09 15:08:12 +00001960
John McCall52a575a2009-08-29 08:11:13 +00001961 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1962 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001963
John McCall52a575a2009-08-29 08:11:13 +00001964 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1965 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001966
John McCall52a575a2009-08-29 08:11:13 +00001967 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1968 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001969
Douglas Gregor7caa6822009-07-24 20:34:43 +00001970 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001971 if (Var->isStaticDataMember())
1972 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1973
1974 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1975 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001976
Douglas Gregor0d696532009-09-28 06:34:35 +00001977 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1978 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1979
Douglas Gregored9c0f92009-10-29 00:04:11 +00001980 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1981 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1982 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1983 PartialSpec);
1984
Anders Carlssond8b285f2009-09-01 04:26:58 +00001985 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1986 if (!Field->getDeclName()) {
1987 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001988 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001989 cast<FieldDecl>(D);
1990 }
1991 }
Mike Stump1eb44332009-09-09 15:08:12 +00001992
John McCalled976492009-12-04 22:46:56 +00001993 if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
1994 return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
1995
1996 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
1997 return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
1998
Douglas Gregor815215d2009-05-27 05:35:12 +00001999 return D->getDeclName() && isa<NamedDecl>(Other) &&
2000 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
2001}
2002
2003template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00002004static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00002005 NamedDecl *D,
2006 ForwardIterator first,
2007 ForwardIterator last) {
2008 for (; first != last; ++first)
2009 if (isInstantiationOf(Ctx, D, *first))
2010 return cast<NamedDecl>(*first);
2011
2012 return 0;
2013}
2014
John McCall02cace72009-08-28 07:59:38 +00002015/// \brief Finds the instantiation of the given declaration context
2016/// within the current instantiation.
2017///
2018/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00002019DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
2020 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00002021 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00002022 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00002023 return cast_or_null<DeclContext>(ID);
2024 } else return DC;
2025}
2026
Douglas Gregored961e72009-05-27 17:54:46 +00002027/// \brief Find the instantiation of the given declaration within the
2028/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00002029///
2030/// This routine is intended to be used when \p D is a declaration
2031/// referenced from within a template, that needs to mapped into the
2032/// corresponding declaration within an instantiation. For example,
2033/// given:
2034///
2035/// \code
2036/// template<typename T>
2037/// struct X {
2038/// enum Kind {
2039/// KnownValue = sizeof(T)
2040/// };
2041///
2042/// bool getKind() const { return KnownValue; }
2043/// };
2044///
2045/// template struct X<int>;
2046/// \endcode
2047///
2048/// In the instantiation of X<int>::getKind(), we need to map the
2049/// EnumConstantDecl for KnownValue (which refers to
2050/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00002051/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
2052/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00002053NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
2054 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor815215d2009-05-27 05:35:12 +00002055 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00002056 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
2057 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2058 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00002059 // D is a local of some kind. Look into the map of local
2060 // declarations to their instantiations.
2061 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
2062 }
Douglas Gregor815215d2009-05-27 05:35:12 +00002063
Douglas Gregore95b4092009-09-16 18:34:49 +00002064 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
2065 if (!Record->isDependentContext())
2066 return D;
2067
2068 // If the RecordDecl is actually the injected-class-name or a "templated"
2069 // declaration for a class template or class template partial
2070 // specialization, substitute into the injected-class-name of the
2071 // class template or partial specialization to find the new DeclContext.
2072 QualType T;
2073 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
2074
2075 if (ClassTemplate) {
2076 T = ClassTemplate->getInjectedClassNameType(Context);
2077 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
2078 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
2079 T = Context.getTypeDeclType(Record);
2080 ClassTemplate = PartialSpec->getSpecializedTemplate();
2081 }
2082
2083 if (!T.isNull()) {
2084 // Substitute into the injected-class-name to get the type corresponding
2085 // to the instantiation we want. This substitution should never fail,
2086 // since we know we can instantiate the injected-class-name or we wouldn't
2087 // have gotten to the injected-class-name!
2088 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
2089 // instantiation in the common case?
2090 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
2091 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
2092
2093 if (!T->isDependentType()) {
2094 assert(T->isRecordType() && "Instantiation must produce a record type");
2095 return T->getAs<RecordType>()->getDecl();
2096 }
2097
2098 // We are performing "partial" template instantiation to create the
2099 // member declarations for the members of a class template
2100 // specialization. Therefore, D is actually referring to something in
2101 // the current instantiation. Look through the current context,
2102 // which contains actual instantiations, to find the instantiation of
2103 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00002104 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00002105 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002106 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00002107 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00002108 if (isInstantiationOf(ClassTemplate,
2109 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00002110 return Spec;
2111 }
2112
Mike Stump1eb44332009-09-09 15:08:12 +00002113 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00002114 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00002115 return Record;
John McCall52a575a2009-08-29 08:11:13 +00002116 }
Douglas Gregore95b4092009-09-16 18:34:49 +00002117
2118 // Fall through to deal with other dependent record types (e.g.,
2119 // anonymous unions in class templates).
2120 }
John McCall52a575a2009-08-29 08:11:13 +00002121
Douglas Gregore95b4092009-09-16 18:34:49 +00002122 if (!ParentDC->isDependentContext())
2123 return D;
2124
2125 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002126 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00002127 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Douglas Gregor815215d2009-05-27 05:35:12 +00002129 if (ParentDC != D->getDeclContext()) {
2130 // We performed some kind of instantiation in the parent context,
2131 // so now we need to look into the instantiated parent context to
2132 // find the instantiation of the declaration D.
2133 NamedDecl *Result = 0;
2134 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002135 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00002136 Result = findInstantiationOf(Context, D, Found.first, Found.second);
2137 } else {
2138 // Since we don't have a name for the entity we're looking for,
2139 // our only option is to walk through all of the declarations to
2140 // find that name. This will occur in a few cases:
2141 //
2142 // - anonymous struct/union within a template
2143 // - unnamed class/struct/union/enum within a template
2144 //
2145 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00002146 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002147 ParentDC->decls_begin(),
2148 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002149 }
Mike Stump1eb44332009-09-09 15:08:12 +00002150
John McCall9f54ad42009-12-10 09:41:52 +00002151 // UsingShadowDecls can instantiate to nothing because of using hiding.
2152 assert((Result || isa<UsingShadowDecl>(D))
2153 && "Unable to find instantiation of declaration!");
2154
Douglas Gregor815215d2009-05-27 05:35:12 +00002155 D = Result;
2156 }
2157
Douglas Gregor815215d2009-05-27 05:35:12 +00002158 return D;
2159}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002160
Mike Stump1eb44332009-09-09 15:08:12 +00002161/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002162/// instantiations we have seen until this point.
2163void Sema::PerformPendingImplicitInstantiations() {
2164 while (!PendingImplicitInstantiations.empty()) {
2165 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002166 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00002167
Douglas Gregor7caa6822009-07-24 20:34:43 +00002168 // Instantiate function definitions
2169 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002170 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002171 Function->getLocation(), *this,
2172 Context.getSourceManager(),
2173 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002175 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002176 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002177 continue;
2178 }
Mike Stump1eb44332009-09-09 15:08:12 +00002179
Douglas Gregor7caa6822009-07-24 20:34:43 +00002180 // Instantiate static data member definitions.
2181 VarDecl *Var = cast<VarDecl>(Inst.first);
2182 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002183
Mike Stump1eb44332009-09-09 15:08:12 +00002184 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002185 Var->getLocation(), *this,
2186 Context.getSourceManager(),
2187 "instantiating static data member "
2188 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregor7caa6822009-07-24 20:34:43 +00002190 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002191 }
2192}