blob: deb7ff0ccc685bbf7006e7765ee1288f2d83ba44 [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"
Anders Carlssonc17fb7b2009-09-01 05:12:24 +000019#include "clang/Basic/PrettyStackTrace.h"
Douglas Gregor83ddad32009-08-26 21:14:46 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor8dbc2692009-03-17 21:15:40 +000021#include "llvm/Support/Compiler.h"
22
23using namespace clang;
24
25namespace {
Mike Stump1eb44332009-09-09 15:08:12 +000026 class VISIBILITY_HIDDEN 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 McCall7ba107a2009-11-18 02:36:19 +000070 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
71 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor8dbc2692009-03-17 21:15:40 +000073 // Base case. FIXME: Remove once we can instantiate everything.
Douglas Gregor48c32a72009-11-17 06:07:40 +000074 Decl *VisitDecl(Decl *D) {
75 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
76 Diagnostic::Error,
77 "cannot instantiate %0 yet");
78 SemaRef.Diag(D->getLocation(), DiagID)
79 << D->getDeclKindName();
80
Douglas Gregor8dbc2692009-03-17 21:15:40 +000081 return 0;
82 }
Douglas Gregor5545e162009-03-24 00:38:23 +000083
John McCallfd810b12009-08-14 02:03:10 +000084 const LangOptions &getLangOptions() {
85 return SemaRef.getLangOptions();
86 }
87
Douglas Gregor5545e162009-03-24 00:38:23 +000088 // Helper functions for instantiating methods.
John McCallce3ff2b2009-08-25 22:02:44 +000089 QualType SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +000090 llvm::SmallVectorImpl<ParmVarDecl *> &Params);
Douglas Gregore53060f2009-06-25 22:08:12 +000091 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
Douglas Gregor5545e162009-03-24 00:38:23 +000092 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
John McCalle29ba202009-08-20 01:44:21 +000093
94 TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +000095 SubstTemplateParams(TemplateParameterList *List);
Douglas Gregored9c0f92009-10-29 00:04:11 +000096
97 bool InstantiateClassTemplatePartialSpecialization(
98 ClassTemplateDecl *ClassTemplate,
99 ClassTemplatePartialSpecializationDecl *PartialSpec);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000100 };
101}
102
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000103// FIXME: Is this too simple?
104void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) {
105 for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr;
106 TmplAttr = TmplAttr->getNext()) {
107
108 // FIXME: Is cloning correct for all attributes?
109 Attr *NewAttr = TmplAttr->clone(SemaRef.Context);
110
111 New->addAttr(NewAttr);
112 }
113}
114
Douglas Gregor4f722be2009-03-25 15:45:12 +0000115Decl *
116TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
117 assert(false && "Translation units cannot be instantiated");
118 return D;
119}
120
121Decl *
122TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
123 assert(false && "Namespaces cannot be instantiated");
124 return D;
125}
126
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000127Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
128 bool Invalid = false;
John McCallba6a9bd2009-10-24 08:00:42 +0000129 DeclaratorInfo *DI = D->getTypeDeclaratorInfo();
130 if (DI->getType()->isDependentType()) {
131 DI = SemaRef.SubstType(DI, TemplateArgs,
132 D->getLocation(), D->getDeclName());
133 if (!DI) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000134 Invalid = true;
John McCallba6a9bd2009-10-24 08:00:42 +0000135 DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000136 }
137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000139 // Create the new typedef
140 TypedefDecl *Typedef
141 = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(),
John McCallba6a9bd2009-10-24 08:00:42 +0000142 D->getIdentifier(), DI);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000143 if (Invalid)
144 Typedef->setInvalidDecl();
145
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000146 Owner->addDecl(Typedef);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000148 return Typedef;
149}
150
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000151Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000152 // Do substitution on the type of the declaration
John McCall0a5fa062009-10-21 02:39:02 +0000153 DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(),
154 TemplateArgs,
155 D->getTypeSpecStartLoc(),
156 D->getDeclName());
157 if (!DI)
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000158 return 0;
159
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +0000160 // Build the instantiated declaration
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000161 VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner,
162 D->getLocation(), D->getIdentifier(),
John McCall0a5fa062009-10-21 02:39:02 +0000163 DI->getType(), DI,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000164 D->getStorageClass());
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000165 Var->setThreadSpecified(D->isThreadSpecified());
166 Var->setCXXDirectInitializer(D->hasCXXDirectInitializer());
167 Var->setDeclaredInCondition(D->isDeclaredInCondition());
Mike Stump1eb44332009-09-09 15:08:12 +0000168
169 // If we are instantiating a static data member defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000170 // out-of-line, the instantiation will have the same lexical
171 // context (which will be a namespace scope) as the template.
172 if (D->isOutOfLine())
173 Var->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Mike Stump390b4cc2009-05-16 07:39:55 +0000175 // FIXME: In theory, we could have a previous declaration for variables that
176 // are not static data members.
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000177 bool Redeclaration = false;
Chris Lattnereaaebc72009-04-25 08:06:05 +0000178 SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Douglas Gregor7caa6822009-07-24 20:34:43 +0000180 if (D->isOutOfLine()) {
181 D->getLexicalDeclContext()->addDecl(Var);
182 Owner->makeDeclVisibleInContext(Var);
183 } else {
184 Owner->addDecl(Var);
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000187 // Link instantiations of static data members back to the template from
188 // which they were instantiated.
189 if (Var->isStaticDataMember())
190 SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D,
Douglas Gregorcf3293e2009-11-01 20:32:48 +0000191 TSK_ImplicitInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000192
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000193 if (D->getInit()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000194 OwningExprResult Init
John McCallce3ff2b2009-08-25 22:02:44 +0000195 = SemaRef.SubstExpr(D->getInit(), TemplateArgs);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000196 if (Init.isInvalid())
197 Var->setInvalidDecl();
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000198 else if (!D->getType()->isDependentType() &&
199 !D->getInit()->isTypeDependent() &&
Douglas Gregore48319a2009-11-09 17:16:50 +0000200 !D->getInit()->isValueDependent()) {
Sebastian Redl42dddbe2009-11-08 10:16:43 +0000201 // If neither the declaration's type nor its initializer are dependent,
202 // we don't want to redo all the checking, especially since the
203 // initializer might have been wrapped by a CXXConstructExpr since we did
204 // it the first time.
205 Var->setInit(SemaRef.Context, Init.takeAs<Expr>());
206 }
Douglas Gregor83ddad32009-08-26 21:14:46 +0000207 else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000208 // FIXME: We're faking all of the comma locations, which is suboptimal.
Douglas Gregor83ddad32009-08-26 21:14:46 +0000209 // Do we even need these comma locations?
210 llvm::SmallVector<SourceLocation, 4> FakeCommaLocs;
211 if (PLE->getNumExprs() > 0) {
212 FakeCommaLocs.reserve(PLE->getNumExprs() - 1);
213 for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) {
214 Expr *E = PLE->getExpr(I)->Retain();
215 FakeCommaLocs.push_back(
216 SemaRef.PP.getLocForEndOfToken(E->getLocEnd()));
217 }
Douglas Gregore9f8eb62009-08-26 23:26:04 +0000218 PLE->getExpr(PLE->getNumExprs() - 1)->Retain();
Douglas Gregor83ddad32009-08-26 21:14:46 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor83ddad32009-08-26 21:14:46 +0000221 // Add the direct initializer to the declaration.
222 SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var),
Mike Stump1eb44332009-09-09 15:08:12 +0000223 PLE->getLParenLoc(),
Douglas Gregor83ddad32009-08-26 21:14:46 +0000224 Sema::MultiExprArg(SemaRef,
225 (void**)PLE->getExprs(),
226 PLE->getNumExprs()),
227 FakeCommaLocs.data(),
228 PLE->getRParenLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Douglas Gregor83ddad32009-08-26 21:14:46 +0000230 // When Init is destroyed, it will destroy the instantiated ParenListExpr;
231 // we've explicitly retained all of its subexpressions already.
232 } else
Chris Lattnerb28317a2009-03-28 19:18:32 +0000233 SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init),
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000234 D->hasCXXDirectInitializer());
Douglas Gregor65b90052009-07-27 17:43:39 +0000235 } else if (!Var->isStaticDataMember() || Var->isOutOfLine())
236 SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false);
Douglas Gregor3d7a12a2009-03-25 23:32:15 +0000237
238 return Var;
239}
240
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000241Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
242 bool Invalid = false;
John McCall07fb6be2009-10-22 23:33:21 +0000243 DeclaratorInfo *DI = D->getDeclaratorInfo();
244 if (DI->getType()->isDependentType()) {
245 DI = SemaRef.SubstType(DI, TemplateArgs,
246 D->getLocation(), D->getDeclName());
247 if (!DI) {
248 DI = D->getDeclaratorInfo();
249 Invalid = true;
250 } else if (DI->getType()->isFunctionType()) {
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000251 // C++ [temp.arg.type]p3:
252 // If a declaration acquires a function type through a type
253 // dependent on a template-parameter and this causes a
254 // declaration that does not use the syntactic form of a
255 // function declarator to have function type, the program is
256 // ill-formed.
257 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
John McCall07fb6be2009-10-22 23:33:21 +0000258 << DI->getType();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000259 Invalid = true;
260 }
261 }
262
263 Expr *BitWidth = D->getBitWidth();
264 if (Invalid)
265 BitWidth = 0;
266 else if (BitWidth) {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000267 // The bit-width expression is not potentially evaluated.
268 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000270 OwningExprResult InstantiatedBitWidth
John McCallce3ff2b2009-08-25 22:02:44 +0000271 = SemaRef.SubstExpr(BitWidth, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000272 if (InstantiatedBitWidth.isInvalid()) {
273 Invalid = true;
274 BitWidth = 0;
275 } else
Anders Carlssone9146f22009-05-01 19:49:17 +0000276 BitWidth = InstantiatedBitWidth.takeAs<Expr>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000277 }
278
John McCall07fb6be2009-10-22 23:33:21 +0000279 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
280 DI->getType(), DI,
Mike Stump1eb44332009-09-09 15:08:12 +0000281 cast<RecordDecl>(Owner),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000282 D->getLocation(),
283 D->isMutable(),
284 BitWidth,
Steve Naroffea218b82009-07-14 14:58:18 +0000285 D->getTypeSpecStartLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000286 D->getAccess(),
287 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +0000288 if (!Field) {
289 cast<Decl>(Owner)->setInvalidDecl();
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000290 return 0;
Douglas Gregor663b5a02009-10-14 20:14:33 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Anders Carlssond8fe2d52009-11-07 06:07:58 +0000293 InstantiateAttrs(D, Field);
294
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000295 if (Invalid)
296 Field->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000298 if (!Field->getDeclName()) {
299 // Keep track of where this decl came from.
300 SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Anders Carlssonf4b5f5c2009-09-02 19:17:55 +0000303 Field->setImplicit(D->isImplicit());
304 Owner->addDecl(Field);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000305
306 return Field;
307}
308
John McCall02cace72009-08-28 07:59:38 +0000309Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
310 FriendDecl::FriendUnion FU;
311
312 // Handle friend type expressions by simply substituting template
313 // parameters into the pattern type.
314 if (Type *Ty = D->getFriendType()) {
315 QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs,
316 D->getLocation(), DeclarationName());
317 if (T.isNull()) return 0;
318
319 assert(getLangOptions().CPlusPlus0x || T->isRecordType());
320 FU = T.getTypePtr();
321
322 // Handle everything else by appropriate substitution.
323 } else {
324 NamedDecl *ND = D->getFriendDecl();
325 assert(ND && "friend decl must be a decl or a type!");
326
Douglas Gregora735b202009-10-13 14:39:41 +0000327 // FIXME: We have a problem here, because the nested call to Visit(ND)
328 // will inject the thing that the friend references into the current
329 // owner, which is wrong.
John McCall02cace72009-08-28 07:59:38 +0000330 Decl *NewND = Visit(ND);
331 if (!NewND) return 0;
332
333 FU = cast<NamedDecl>(NewND);
John McCallfd810b12009-08-14 02:03:10 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
John McCall02cace72009-08-28 07:59:38 +0000336 FriendDecl *FD =
337 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU,
338 D->getFriendLoc());
John McCall5fee1102009-08-29 03:50:18 +0000339 FD->setAccess(AS_public);
John McCall02cace72009-08-28 07:59:38 +0000340 Owner->addDecl(FD);
341 return FD;
John McCallfd810b12009-08-14 02:03:10 +0000342}
343
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000344Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
345 Expr *AssertExpr = D->getAssertExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Douglas Gregorac7610d2009-06-22 20:57:11 +0000347 // The expression in a static assertion is not potentially evaluated.
348 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000350 OwningExprResult InstantiatedAssertExpr
John McCallce3ff2b2009-08-25 22:02:44 +0000351 = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000352 if (InstantiatedAssertExpr.isInvalid())
353 return 0;
354
Douglas Gregor43d9d922009-08-08 01:41:12 +0000355 OwningExprResult Message(SemaRef, D->getMessage());
356 D->getMessage()->Retain();
Mike Stump1eb44332009-09-09 15:08:12 +0000357 Decl *StaticAssert
358 = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(),
Chris Lattnerb28317a2009-03-28 19:18:32 +0000359 move(InstantiatedAssertExpr),
360 move(Message)).getAs<Decl>();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000361 return StaticAssert;
362}
363
364Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365 EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner,
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000366 D->getLocation(), D->getIdentifier(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000367 D->getTagKeywordLoc(),
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000368 /*PrevDecl=*/0);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000369 Enum->setInstantiationOfMemberEnum(D);
Douglas Gregor06c0fec2009-03-25 22:00:53 +0000370 Enum->setAccess(D->getAccess());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000371 Owner->addDecl(Enum);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000372 Enum->startDefinition();
373
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000374 llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000375
376 EnumConstantDecl *LastEnumConst = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000377 for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(),
378 ECEnd = D->enumerator_end();
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000379 EC != ECEnd; ++EC) {
380 // The specified value for the enumerator.
381 OwningExprResult Value = SemaRef.Owned((Expr *)0);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000382 if (Expr *UninstValue = EC->getInitExpr()) {
383 // The enumerator's value expression is not potentially evaluated.
Mike Stump1eb44332009-09-09 15:08:12 +0000384 EnterExpressionEvaluationContext Unevaluated(SemaRef,
Douglas Gregorac7610d2009-06-22 20:57:11 +0000385 Action::Unevaluated);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
John McCallce3ff2b2009-08-25 22:02:44 +0000387 Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
Douglas Gregorac7610d2009-06-22 20:57:11 +0000388 }
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000389
390 // Drop the initial value and continue.
391 bool isInvalid = false;
392 if (Value.isInvalid()) {
393 Value = SemaRef.Owned((Expr *)0);
394 isInvalid = true;
395 }
396
Mike Stump1eb44332009-09-09 15:08:12 +0000397 EnumConstantDecl *EnumConst
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000398 = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
399 EC->getLocation(), EC->getIdentifier(),
400 move(Value));
401
402 if (isInvalid) {
403 if (EnumConst)
404 EnumConst->setInvalidDecl();
405 Enum->setInvalidDecl();
406 }
407
408 if (EnumConst) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000409 Enum->addDecl(EnumConst);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000410 Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000411 LastEnumConst = EnumConst;
412 }
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000415 // FIXME: Fixup LBraceLoc and RBraceLoc
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000416 // FIXME: Empty Scope and AttributeList (required to handle attribute packed).
Mike Stumpc6e35aa2009-05-16 07:06:02 +0000417 SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(),
418 Sema::DeclPtrTy::make(Enum),
Edward O'Callaghanfee13812009-08-08 14:36:57 +0000419 &Enumerators[0], Enumerators.size(),
420 0, 0);
Douglas Gregor8dbc2692009-03-17 21:15:40 +0000421
422 return Enum;
423}
424
Douglas Gregor6477b692009-03-25 15:04:13 +0000425Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
426 assert(false && "EnumConstantDecls can only occur within EnumDecls.");
427 return 0;
428}
429
Douglas Gregored9c0f92009-10-29 00:04:11 +0000430namespace {
431 class SortDeclByLocation {
432 SourceManager &SourceMgr;
433
434 public:
435 explicit SortDeclByLocation(SourceManager &SourceMgr)
436 : SourceMgr(SourceMgr) { }
437
438 bool operator()(const Decl *X, const Decl *Y) const {
439 return SourceMgr.isBeforeInTranslationUnit(X->getLocation(),
440 Y->getLocation());
441 }
442 };
443}
444
John McCalle29ba202009-08-20 01:44:21 +0000445Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000446 // Create a local instantiation scope for this class template, which
447 // will contain the instantiations of the template parameters.
448 Sema::LocalInstantiationScope Scope(SemaRef);
John McCalle29ba202009-08-20 01:44:21 +0000449 TemplateParameterList *TempParams = D->getTemplateParameters();
John McCallce3ff2b2009-08-25 22:02:44 +0000450 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000451 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000452 return NULL;
John McCalle29ba202009-08-20 01:44:21 +0000453
454 CXXRecordDecl *Pattern = D->getTemplatedDecl();
455 CXXRecordDecl *RecordInst
456 = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner,
457 Pattern->getLocation(), Pattern->getIdentifier(),
Douglas Gregorf0510d42009-10-12 23:11:44 +0000458 Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL,
459 /*DelayTypeCreation=*/true);
John McCalle29ba202009-08-20 01:44:21 +0000460
461 ClassTemplateDecl *Inst
462 = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
463 D->getIdentifier(), InstParams, RecordInst, 0);
464 RecordInst->setDescribedClassTemplate(Inst);
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000465 if (D->getFriendObjectKind())
466 Inst->setObjectOfFriendDecl(true);
467 else
468 Inst->setAccess(D->getAccess());
John McCalle29ba202009-08-20 01:44:21 +0000469 Inst->setInstantiatedFromMemberTemplate(D);
Douglas Gregorf0510d42009-10-12 23:11:44 +0000470
471 // Trigger creation of the type for the instantiation.
472 SemaRef.Context.getTypeDeclType(RecordInst);
473
Douglas Gregor259571e2009-10-30 22:42:42 +0000474 // Finish handling of friends.
475 if (Inst->getFriendObjectKind()) {
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000476 return Inst;
Douglas Gregor259571e2009-10-30 22:42:42 +0000477 }
Douglas Gregore8c01bd2009-10-30 21:07:27 +0000478
John McCalle29ba202009-08-20 01:44:21 +0000479 Owner->addDecl(Inst);
Douglas Gregored9c0f92009-10-29 00:04:11 +0000480
481 // First, we sort the partial specializations by location, so
482 // that we instantiate them in the order they were declared.
483 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
484 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
485 P = D->getPartialSpecializations().begin(),
486 PEnd = D->getPartialSpecializations().end();
487 P != PEnd; ++P)
488 PartialSpecs.push_back(&*P);
489 std::sort(PartialSpecs.begin(), PartialSpecs.end(),
490 SortDeclByLocation(SemaRef.SourceMgr));
491
492 // Instantiate all of the partial specializations of this member class
493 // template.
494 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
495 InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]);
496
John McCalle29ba202009-08-20 01:44:21 +0000497 return Inst;
498}
499
Douglas Gregord60e1052009-08-27 16:57:43 +0000500Decl *
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000501TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
502 ClassTemplatePartialSpecializationDecl *D) {
Douglas Gregored9c0f92009-10-29 00:04:11 +0000503 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
504
505 // Lookup the already-instantiated declaration in the instantiation
506 // of the class template and return that.
507 DeclContext::lookup_result Found
508 = Owner->lookup(ClassTemplate->getDeclName());
509 if (Found.first == Found.second)
510 return 0;
511
512 ClassTemplateDecl *InstClassTemplate
513 = dyn_cast<ClassTemplateDecl>(*Found.first);
514 if (!InstClassTemplate)
515 return 0;
516
517 Decl *DCanon = D->getCanonicalDecl();
518 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
519 P = InstClassTemplate->getPartialSpecializations().begin(),
520 PEnd = InstClassTemplate->getPartialSpecializations().end();
521 P != PEnd; ++P) {
522 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
523 return &*P;
524 }
525
Douglas Gregor7974c3b2009-10-07 17:21:34 +0000526 return 0;
527}
528
529Decl *
Douglas Gregord60e1052009-08-27 16:57:43 +0000530TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000531 // Create a local instantiation scope for this function template, which
532 // will contain the instantiations of the template parameters and then get
533 // merged with the local instantiation scope for the function template
534 // itself.
535 Sema::LocalInstantiationScope Scope(SemaRef);
536
Douglas Gregord60e1052009-08-27 16:57:43 +0000537 TemplateParameterList *TempParams = D->getTemplateParameters();
538 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
Mike Stump1eb44332009-09-09 15:08:12 +0000539 if (!InstParams)
Douglas Gregord60e1052009-08-27 16:57:43 +0000540 return NULL;
Douglas Gregored9c0f92009-10-29 00:04:11 +0000541
Douglas Gregora735b202009-10-13 14:39:41 +0000542 FunctionDecl *Instantiated = 0;
543 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
544 Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
545 InstParams));
546 else
547 Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
548 D->getTemplatedDecl(),
549 InstParams));
550
551 if (!Instantiated)
Douglas Gregord60e1052009-08-27 16:57:43 +0000552 return 0;
553
Mike Stump1eb44332009-09-09 15:08:12 +0000554 // Link the instantiated function template declaration to the function
Douglas Gregord60e1052009-08-27 16:57:43 +0000555 // template from which it was instantiated.
Douglas Gregor37d681852009-10-12 22:27:17 +0000556 FunctionTemplateDecl *InstTemplate
Douglas Gregora735b202009-10-13 14:39:41 +0000557 = Instantiated->getDescribedFunctionTemplate();
Douglas Gregor37d681852009-10-12 22:27:17 +0000558 InstTemplate->setAccess(D->getAccess());
Douglas Gregora735b202009-10-13 14:39:41 +0000559 assert(InstTemplate &&
560 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
561 if (!InstTemplate->getInstantiatedFromMemberTemplate())
562 InstTemplate->setInstantiatedFromMemberTemplate(D);
563
564 // Add non-friends into the owner.
565 if (!InstTemplate->getFriendObjectKind())
566 Owner->addDecl(InstTemplate);
Douglas Gregord60e1052009-08-27 16:57:43 +0000567 return InstTemplate;
568}
569
Douglas Gregord475b8d2009-03-25 21:17:03 +0000570Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
571 CXXRecordDecl *PrevDecl = 0;
572 if (D->isInjectedClassName())
573 PrevDecl = cast<CXXRecordDecl>(Owner);
574
575 CXXRecordDecl *Record
Mike Stump1eb44332009-09-09 15:08:12 +0000576 = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000577 D->getLocation(), D->getIdentifier(),
578 D->getTagKeywordLoc(), PrevDecl);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000579 Record->setImplicit(D->isImplicit());
Eli Friedmaneaba1af2009-08-27 19:11:42 +0000580 // FIXME: Check against AS_none is an ugly hack to work around the issue that
581 // the tag decls introduced by friend class declarations don't have an access
582 // specifier. Remove once this area of the code gets sorted out.
583 if (D->getAccess() != AS_none)
584 Record->setAccess(D->getAccess());
Douglas Gregord475b8d2009-03-25 21:17:03 +0000585 if (!D->isInjectedClassName())
Douglas Gregorf6b11852009-10-08 15:14:33 +0000586 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000587
John McCall02cace72009-08-28 07:59:38 +0000588 // If the original function was part of a friend declaration,
589 // inherit its namespace state.
590 if (Decl::FriendObjectKind FOK = D->getFriendObjectKind())
591 Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared);
592
Anders Carlssond8b285f2009-09-01 04:26:58 +0000593 Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion());
594
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000595 Owner->addDecl(Record);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000596 return Record;
597}
598
John McCall02cace72009-08-28 07:59:38 +0000599/// Normal class members are of more specific types and therefore
600/// don't make it here. This function serves two purposes:
601/// 1) instantiating function templates
602/// 2) substituting friend declarations
603/// FIXME: preserve function definitions in case #2
Douglas Gregora735b202009-10-13 14:39:41 +0000604 Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
605 TemplateParameterList *TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000606 // Check whether there is already a function template specialization for
607 // this declaration.
608 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
609 void *InsertPos = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000610 if (FunctionTemplate && !TemplateParams) {
Douglas Gregor127102b2009-06-29 20:59:39 +0000611 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000612 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000613 TemplateArgs.getInnermost().getFlatArgumentList(),
614 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor828e2262009-07-29 16:09:57 +0000615 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
617 FunctionTemplateSpecializationInfo *Info
618 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor127102b2009-06-29 20:59:39 +0000619 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor127102b2009-06-29 20:59:39 +0000621 // If we already have a function template specialization, return it.
622 if (Info)
623 return Info->Function;
624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregor550d9b22009-10-31 17:21:17 +0000626 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregore53060f2009-06-25 22:08:12 +0000628 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000629 QualType T = SubstFunctionType(D, Params);
Douglas Gregore53060f2009-06-25 22:08:12 +0000630 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000631 return 0;
John McCallfd810b12009-08-14 02:03:10 +0000632
Douglas Gregore53060f2009-06-25 22:08:12 +0000633 // Build the instantiated method declaration.
Douglas Gregore95b4092009-09-16 18:34:49 +0000634 DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(),
635 TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +0000636 FunctionDecl *Function =
Mike Stump1eb44332009-09-09 15:08:12 +0000637 FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000638 D->getDeclName(), T, D->getDeclaratorInfo(),
639 D->getStorageClass(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000640 D->isInlineSpecified(), D->hasWrittenPrototype());
John McCall02cace72009-08-28 07:59:38 +0000641 Function->setLexicalDeclContext(Owner);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregore53060f2009-06-25 22:08:12 +0000643 // Attach the parameters
644 for (unsigned P = 0; P < Params.size(); ++P)
645 Params[P]->setOwningFunction(Function);
646 Function->setParams(SemaRef.Context, Params.data(), Params.size());
John McCall02cace72009-08-28 07:59:38 +0000647
Douglas Gregora735b202009-10-13 14:39:41 +0000648 if (TemplateParams) {
649 // Our resulting instantiation is actually a function template, since we
650 // are substituting only the outer template parameters. For example, given
651 //
652 // template<typename T>
653 // struct X {
654 // template<typename U> friend void f(T, U);
655 // };
656 //
657 // X<int> x;
658 //
659 // We are instantiating the friend function template "f" within X<int>,
660 // which means substituting int for T, but leaving "f" as a friend function
661 // template.
662 // Build the function template itself.
663 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner,
664 Function->getLocation(),
665 Function->getDeclName(),
666 TemplateParams, Function);
667 Function->setDescribedFunctionTemplate(FunctionTemplate);
668 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregor66724ea2009-11-14 01:20:54 +0000669 } else if (FunctionTemplate) {
670 // Record this function template specialization.
671 Function->setFunctionTemplateSpecialization(SemaRef.Context,
672 FunctionTemplate,
673 &TemplateArgs.getInnermost(),
674 InsertPos);
John McCall02cace72009-08-28 07:59:38 +0000675 }
Douglas Gregora735b202009-10-13 14:39:41 +0000676
Douglas Gregore53060f2009-06-25 22:08:12 +0000677 if (InitFunctionInstantiation(Function, D))
678 Function->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregore53060f2009-06-25 22:08:12 +0000680 bool Redeclaration = false;
681 bool OverloadableAttrRequired = false;
Douglas Gregora735b202009-10-13 14:39:41 +0000682
Douglas Gregore53060f2009-06-25 22:08:12 +0000683 NamedDecl *PrevDecl = 0;
Douglas Gregora735b202009-10-13 14:39:41 +0000684 if (TemplateParams || !FunctionTemplate) {
685 // Look only into the namespace where the friend would be declared to
686 // find a previous declaration. This is the innermost enclosing namespace,
687 // as described in ActOnFriendFunctionDecl.
John McCall7d384dd2009-11-18 07:57:50 +0000688 LookupResult R(SemaRef, Function->getDeclName(), SourceLocation(),
689 Sema::LookupOrdinaryName,
690 Sema::ForRedeclaration);
John McCalla24dc2e2009-11-17 02:14:36 +0000691 SemaRef.LookupQualifiedName(R, DC);
Douglas Gregora735b202009-10-13 14:39:41 +0000692
693 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
694
695 // In C++, the previous declaration we find might be a tag type
696 // (class or enum). In this case, the new declaration will hide the
697 // tag type. Note that this does does not apply if we're declaring a
698 // typedef (C++ [dcl.typedef]p4).
699 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
700 PrevDecl = 0;
701 }
702
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000703 SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration,
Douglas Gregore53060f2009-06-25 22:08:12 +0000704 /*FIXME:*/OverloadableAttrRequired);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000705
Douglas Gregora735b202009-10-13 14:39:41 +0000706 // If the original function was part of a friend declaration,
707 // inherit its namespace state and add it to the owner.
708 NamedDecl *FromFriendD
709 = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D;
710 if (FromFriendD->getFriendObjectKind()) {
711 NamedDecl *ToFriendD = 0;
712 if (TemplateParams) {
713 ToFriendD = cast<NamedDecl>(FunctionTemplate);
714 PrevDecl = FunctionTemplate->getPreviousDeclaration();
715 } else {
716 ToFriendD = Function;
717 PrevDecl = Function->getPreviousDeclaration();
718 }
719 ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL);
720 if (!Owner->isDependentContext() && !PrevDecl)
721 DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false);
722
723 if (!TemplateParams)
724 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
725 }
726
Douglas Gregore53060f2009-06-25 22:08:12 +0000727 return Function;
728}
729
Douglas Gregord60e1052009-08-27 16:57:43 +0000730Decl *
731TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
732 TemplateParameterList *TemplateParams) {
Douglas Gregor6b906862009-08-21 00:16:32 +0000733 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
734 void *InsertPos = 0;
Douglas Gregord60e1052009-08-27 16:57:43 +0000735 if (FunctionTemplate && !TemplateParams) {
Mike Stump1eb44332009-09-09 15:08:12 +0000736 // We are creating a function template specialization from a function
737 // template. Check whether there is already a function template
Douglas Gregord60e1052009-08-27 16:57:43 +0000738 // specialization for this particular set of template arguments.
Douglas Gregor6b906862009-08-21 00:16:32 +0000739 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +0000740 FunctionTemplateSpecializationInfo::Profile(ID,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000741 TemplateArgs.getInnermost().getFlatArgumentList(),
742 TemplateArgs.getInnermost().flat_size(),
Douglas Gregor6b906862009-08-21 00:16:32 +0000743 SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
745 FunctionTemplateSpecializationInfo *Info
746 = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
Douglas Gregor6b906862009-08-21 00:16:32 +0000747 InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregor6b906862009-08-21 00:16:32 +0000749 // If we already have a function template specialization, return it.
750 if (Info)
751 return Info->Function;
752 }
753
Douglas Gregor550d9b22009-10-31 17:21:17 +0000754 Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0);
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000755
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000756 llvm::SmallVector<ParmVarDecl *, 4> Params;
John McCallce3ff2b2009-08-25 22:02:44 +0000757 QualType T = SubstFunctionType(D, Params);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000758 if (T.isNull())
759 return 0;
760
761 // Build the instantiated method declaration.
762 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
Douglas Gregordec06662009-08-21 18:42:58 +0000763 CXXMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Douglas Gregordec06662009-08-21 18:42:58 +0000765 DeclarationName Name = D->getDeclName();
Douglas Gregor17e32f32009-08-21 22:43:28 +0000766 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Douglas Gregordec06662009-08-21 18:42:58 +0000767 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
768 Name = SemaRef.Context.DeclarationNames.getCXXConstructorName(
769 SemaRef.Context.getCanonicalType(ClassTy));
Mike Stump1eb44332009-09-09 15:08:12 +0000770 Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
771 Constructor->getLocation(),
772 Name, T,
Douglas Gregor17e32f32009-08-21 22:43:28 +0000773 Constructor->getDeclaratorInfo(),
Mike Stump1eb44332009-09-09 15:08:12 +0000774 Constructor->isExplicit(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000775 Constructor->isInlineSpecified(), false);
Douglas Gregor17e32f32009-08-21 22:43:28 +0000776 } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
777 QualType ClassTy = SemaRef.Context.getTypeDeclType(Record);
778 Name = SemaRef.Context.DeclarationNames.getCXXDestructorName(
779 SemaRef.Context.getCanonicalType(ClassTy));
780 Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
781 Destructor->getLocation(), Name,
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000782 T, Destructor->isInlineSpecified(), false);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000783 } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000784 CanQualType ConvTy
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000785 = SemaRef.Context.getCanonicalType(
John McCall183700f2009-09-21 23:43:11 +0000786 T->getAs<FunctionType>()->getResultType());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000787 Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(
788 ConvTy);
789 Method = CXXConversionDecl::Create(SemaRef.Context, Record,
790 Conversion->getLocation(), Name,
791 T, Conversion->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000792 Conversion->isInlineSpecified(),
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000793 Conversion->isExplicit());
Douglas Gregordec06662009-08-21 18:42:58 +0000794 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000795 Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(),
Douglas Gregordec06662009-08-21 18:42:58 +0000796 D->getDeclName(), T, D->getDeclaratorInfo(),
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000797 D->isStatic(), D->isInlineSpecified());
Douglas Gregordec06662009-08-21 18:42:58 +0000798 }
Douglas Gregor6b906862009-08-21 00:16:32 +0000799
Douglas Gregord60e1052009-08-27 16:57:43 +0000800 if (TemplateParams) {
801 // Our resulting instantiation is actually a function template, since we
802 // are substituting only the outer template parameters. For example, given
Mike Stump1eb44332009-09-09 15:08:12 +0000803 //
Douglas Gregord60e1052009-08-27 16:57:43 +0000804 // template<typename T>
805 // struct X {
806 // template<typename U> void f(T, U);
807 // };
808 //
809 // X<int> x;
810 //
811 // We are instantiating the member template "f" within X<int>, which means
812 // substituting int for T, but leaving "f" as a member function template.
813 // Build the function template itself.
814 FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
815 Method->getLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +0000816 Method->getDeclName(),
Douglas Gregord60e1052009-08-27 16:57:43 +0000817 TemplateParams, Method);
818 if (D->isOutOfLine())
Mike Stump1eb44332009-09-09 15:08:12 +0000819 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
Douglas Gregord60e1052009-08-27 16:57:43 +0000820 Method->setDescribedFunctionTemplate(FunctionTemplate);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000821 } else if (FunctionTemplate) {
822 // Record this function template specialization.
823 Method->setFunctionTemplateSpecialization(SemaRef.Context,
824 FunctionTemplate,
825 &TemplateArgs.getInnermost(),
826 InsertPos);
827 } else {
828 // Record that this is an instantiation of a member function.
Douglas Gregor2db32322009-10-07 23:56:10 +0000829 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
Douglas Gregor66724ea2009-11-14 01:20:54 +0000830 }
831
Mike Stump1eb44332009-09-09 15:08:12 +0000832 // If we are instantiating a member function defined
Douglas Gregor7caa6822009-07-24 20:34:43 +0000833 // out-of-line, the instantiation will have the same lexical
834 // context (which will be a namespace scope) as the template.
835 if (D->isOutOfLine())
836 Method->setLexicalDeclContext(D->getLexicalDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregor5545e162009-03-24 00:38:23 +0000838 // Attach the parameters
839 for (unsigned P = 0; P < Params.size(); ++P)
840 Params[P]->setOwningFunction(Method);
Jay Foadbeaaccd2009-05-21 09:52:38 +0000841 Method->setParams(SemaRef.Context, Params.data(), Params.size());
Douglas Gregor5545e162009-03-24 00:38:23 +0000842
843 if (InitMethodInstantiation(Method, D))
844 Method->setInvalidDecl();
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000845
Douglas Gregordec06662009-08-21 18:42:58 +0000846 NamedDecl *PrevDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregord60e1052009-08-27 16:57:43 +0000848 if (!FunctionTemplate || TemplateParams) {
John McCall7d384dd2009-11-18 07:57:50 +0000849 LookupResult R(SemaRef, Name, SourceLocation(),
850 Sema::LookupOrdinaryName,
851 Sema::ForRedeclaration);
John McCalla24dc2e2009-11-17 02:14:36 +0000852 SemaRef.LookupQualifiedName(R, Owner);
John McCallf36e02d2009-10-09 21:13:30 +0000853 PrevDecl = R.getAsSingleDecl(SemaRef.Context);
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Douglas Gregordec06662009-08-21 18:42:58 +0000855 // In C++, the previous declaration we find might be a tag type
856 // (class or enum). In this case, the new declaration will hide the
857 // tag type. Note that this does does not apply if we're declaring a
858 // typedef (C++ [dcl.typedef]p4).
859 if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag)
860 PrevDecl = 0;
861 }
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000862
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000863 bool Redeclaration = false;
864 bool OverloadableAttrRequired = false;
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000865 SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000866 /*FIXME:*/OverloadableAttrRequired);
867
Douglas Gregora735b202009-10-13 14:39:41 +0000868 if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) &&
869 !Method->getFriendObjectKind())
Douglas Gregordec06662009-08-21 18:42:58 +0000870 Owner->addDecl(Method);
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Sebastian Redla165da02009-11-18 21:51:29 +0000872 SemaRef.AddOverriddenMethods(Record, Method);
873
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000874 return Method;
875}
876
Douglas Gregor615c5d42009-03-24 16:43:20 +0000877Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Douglas Gregordec06662009-08-21 18:42:58 +0000878 return VisitCXXMethodDecl(D);
Douglas Gregor615c5d42009-03-24 16:43:20 +0000879}
880
Douglas Gregor03b2b072009-03-24 00:15:49 +0000881Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Douglas Gregor17e32f32009-08-21 22:43:28 +0000882 return VisitCXXMethodDecl(D);
Douglas Gregor03b2b072009-03-24 00:15:49 +0000883}
884
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000885Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +0000886 return VisitCXXMethodDecl(D);
Douglas Gregorbb969ed2009-03-25 00:34:44 +0000887}
888
Douglas Gregor6477b692009-03-25 15:04:13 +0000889ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
John McCall58e46772009-10-23 21:48:59 +0000890 QualType T;
891 DeclaratorInfo *DI = D->getDeclaratorInfo();
892 if (DI) {
893 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
894 D->getDeclName());
895 if (DI) T = DI->getType();
896 } else {
897 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
898 D->getDeclName());
899 DI = 0;
900 }
901
902 if (T.isNull())
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000903 return 0;
904
John McCall58e46772009-10-23 21:48:59 +0000905 T = SemaRef.adjustParameterType(T);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000906
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000907 // Allocate the parameter
John McCall58e46772009-10-23 21:48:59 +0000908 ParmVarDecl *Param
909 = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(),
910 D->getIdentifier(), T, DI, D->getStorageClass(), 0);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000911
Anders Carlsson9351c172009-08-25 03:18:48 +0000912 // Mark the default argument as being uninstantiated.
Douglas Gregorf43d0b32009-09-25 06:56:31 +0000913 if (D->hasUninstantiatedDefaultArg())
914 Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg());
Douglas Gregor0ed09302009-09-25 07:03:22 +0000915 else if (Expr *Arg = D->getDefaultArg())
916 Param->setUninstantiatedDefaultArg(Arg);
917
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000918 // Note: we don't try to instantiate function parameters until after
919 // we've instantiated the function's type. Therefore, we don't have
920 // to check for 'void' parameter types here.
Douglas Gregor48dd19b2009-05-14 21:44:34 +0000921 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000922 return Param;
923}
924
John McCalle29ba202009-08-20 01:44:21 +0000925Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
926 TemplateTypeParmDecl *D) {
927 // TODO: don't always clone when decls are refcounted.
928 const Type* T = D->getTypeForDecl();
929 assert(T->isTemplateTypeParmType());
930 const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>();
Mike Stump1eb44332009-09-09 15:08:12 +0000931
John McCalle29ba202009-08-20 01:44:21 +0000932 TemplateTypeParmDecl *Inst =
933 TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000934 TTPT->getDepth() - 1, TTPT->getIndex(),
John McCalle29ba202009-08-20 01:44:21 +0000935 TTPT->getName(),
936 D->wasDeclaredWithTypename(),
937 D->isParameterPack());
938
Douglas Gregor0f8716b2009-11-09 19:17:50 +0000939 if (D->hasDefaultArgument())
940 Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false);
John McCalle29ba202009-08-20 01:44:21 +0000941
Douglas Gregor550d9b22009-10-31 17:21:17 +0000942 // Introduce this template parameter's instantiation into the instantiation
943 // scope.
944 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
945
John McCalle29ba202009-08-20 01:44:21 +0000946 return Inst;
947}
948
Douglas Gregor33642df2009-10-23 23:25:44 +0000949Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
950 NonTypeTemplateParmDecl *D) {
951 // Substitute into the type of the non-type template parameter.
952 QualType T;
953 DeclaratorInfo *DI = D->getDeclaratorInfo();
954 if (DI) {
955 DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(),
956 D->getDeclName());
957 if (DI) T = DI->getType();
958 } else {
959 T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(),
960 D->getDeclName());
961 DI = 0;
962 }
963 if (T.isNull())
964 return 0;
965
966 // Check that this type is acceptable for a non-type template parameter.
967 bool Invalid = false;
968 T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation());
969 if (T.isNull()) {
970 T = SemaRef.Context.IntTy;
971 Invalid = true;
972 }
973
974 NonTypeTemplateParmDecl *Param
975 = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
976 D->getDepth() - 1, D->getPosition(),
977 D->getIdentifier(), T, DI);
978 if (Invalid)
979 Param->setInvalidDecl();
980
981 Param->setDefaultArgument(D->getDefaultArgument());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000982
983 // Introduce this template parameter's instantiation into the instantiation
984 // scope.
985 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
Douglas Gregor33642df2009-10-23 23:25:44 +0000986 return Param;
987}
988
Anders Carlsson0dde18e2009-08-28 15:18:15 +0000989Decl *
Douglas Gregor9106ef72009-11-11 16:58:32 +0000990TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
991 TemplateTemplateParmDecl *D) {
992 // Instantiate the template parameter list of the template template parameter.
993 TemplateParameterList *TempParams = D->getTemplateParameters();
994 TemplateParameterList *InstParams;
995 {
996 // Perform the actual substitution of template parameters within a new,
997 // local instantiation scope.
998 Sema::LocalInstantiationScope Scope(SemaRef);
999 InstParams = SubstTemplateParams(TempParams);
1000 if (!InstParams)
1001 return NULL;
1002 }
1003
1004 // Build the template template parameter.
1005 TemplateTemplateParmDecl *Param
1006 = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1007 D->getDepth() - 1, D->getPosition(),
1008 D->getIdentifier(), InstParams);
1009 Param->setDefaultArgument(D->getDefaultArgument());
1010
1011 // Introduce this template parameter's instantiation into the instantiation
1012 // scope.
1013 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
1014
1015 return Param;
1016}
1017
Douglas Gregor48c32a72009-11-17 06:07:40 +00001018Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1019 // Using directives are never dependent, so they require no explicit
1020
1021 UsingDirectiveDecl *Inst
1022 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1023 D->getNamespaceKeyLocation(),
1024 D->getQualifierRange(), D->getQualifier(),
1025 D->getIdentLocation(),
1026 D->getNominatedNamespace(),
1027 D->getCommonAncestor());
1028 Owner->addDecl(Inst);
1029 return Inst;
1030}
1031
John McCall7ba107a2009-11-18 02:36:19 +00001032Decl * TemplateDeclInstantiator
1033 ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001034 NestedNameSpecifier *NNS =
1035 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1036 D->getTargetNestedNameRange(),
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001037 TemplateArgs);
1038 if (!NNS)
1039 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001041 CXXScopeSpec SS;
1042 SS.setRange(D->getTargetNestedNameRange());
1043 SS.setScopeRep(NNS);
Mike Stump1eb44332009-09-09 15:08:12 +00001044
1045 NamedDecl *UD =
John McCall9488ea12009-11-17 05:59:44 +00001046 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
John McCall7ba107a2009-11-18 02:36:19 +00001047 D->getUsingLoc(), SS, D->getLocation(),
1048 D->getDeclName(), 0,
1049 /*instantiation*/ true,
1050 /*typename*/ true, D->getTypenameLoc());
1051 if (UD)
1052 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
1053 D);
1054 return UD;
1055}
1056
1057Decl * TemplateDeclInstantiator
1058 ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1059 NestedNameSpecifier *NNS =
1060 SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(),
1061 D->getTargetNestedNameRange(),
1062 TemplateArgs);
1063 if (!NNS)
1064 return 0;
1065
1066 CXXScopeSpec SS;
1067 SS.setRange(D->getTargetNestedNameRange());
1068 SS.setScopeRep(NNS);
1069
1070 NamedDecl *UD =
1071 SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(),
1072 D->getUsingLoc(), SS, D->getLocation(),
1073 D->getDeclName(), 0,
1074 /*instantiation*/ true,
1075 /*typename*/ false, SourceLocation());
Anders Carlsson0d8df782009-08-29 19:37:28 +00001076 if (UD)
Mike Stump1eb44332009-09-09 15:08:12 +00001077 SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD),
Anders Carlsson0d8df782009-08-29 19:37:28 +00001078 D);
1079 return UD;
Anders Carlsson0dde18e2009-08-28 15:18:15 +00001080}
1081
John McCallce3ff2b2009-08-25 22:02:44 +00001082Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001083 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001084 TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
Douglas Gregor8dbc2692009-03-17 21:15:40 +00001085 return Instantiator.Visit(D);
1086}
1087
John McCalle29ba202009-08-20 01:44:21 +00001088/// \brief Instantiates a nested template parameter list in the current
1089/// instantiation context.
1090///
1091/// \param L The parameter list to instantiate
1092///
1093/// \returns NULL if there was an error
1094TemplateParameterList *
John McCallce3ff2b2009-08-25 22:02:44 +00001095TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
John McCalle29ba202009-08-20 01:44:21 +00001096 // Get errors for all the parameters before bailing out.
1097 bool Invalid = false;
1098
1099 unsigned N = L->size();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001100 typedef llvm::SmallVector<NamedDecl *, 8> ParamVector;
John McCalle29ba202009-08-20 01:44:21 +00001101 ParamVector Params;
1102 Params.reserve(N);
1103 for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
1104 PI != PE; ++PI) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001105 NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
John McCalle29ba202009-08-20 01:44:21 +00001106 Params.push_back(D);
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001107 Invalid = Invalid || !D || D->isInvalidDecl();
John McCalle29ba202009-08-20 01:44:21 +00001108 }
1109
1110 // Clean up if we had an error.
1111 if (Invalid) {
1112 for (ParamVector::iterator PI = Params.begin(), PE = Params.end();
1113 PI != PE; ++PI)
1114 if (*PI)
1115 (*PI)->Destroy(SemaRef.Context);
1116 return NULL;
1117 }
1118
1119 TemplateParameterList *InstL
1120 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
1121 L->getLAngleLoc(), &Params.front(), N,
1122 L->getRAngleLoc());
1123 return InstL;
Mike Stump1eb44332009-09-09 15:08:12 +00001124}
John McCalle29ba202009-08-20 01:44:21 +00001125
Douglas Gregored9c0f92009-10-29 00:04:11 +00001126/// \brief Instantiate the declaration of a class template partial
1127/// specialization.
1128///
1129/// \param ClassTemplate the (instantiated) class template that is partially
1130// specialized by the instantiation of \p PartialSpec.
1131///
1132/// \param PartialSpec the (uninstantiated) class template partial
1133/// specialization that we are instantiating.
1134///
1135/// \returns true if there was an error, false otherwise.
1136bool
1137TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
1138 ClassTemplateDecl *ClassTemplate,
1139 ClassTemplatePartialSpecializationDecl *PartialSpec) {
Douglas Gregor550d9b22009-10-31 17:21:17 +00001140 // Create a local instantiation scope for this class template partial
1141 // specialization, which will contain the instantiations of the template
1142 // parameters.
1143 Sema::LocalInstantiationScope Scope(SemaRef);
1144
Douglas Gregored9c0f92009-10-29 00:04:11 +00001145 // Substitute into the template parameters of the class template partial
1146 // specialization.
1147 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
1148 TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1149 if (!InstParams)
1150 return true;
1151
1152 // Substitute into the template arguments of the class template partial
1153 // specialization.
John McCall833ca992009-10-29 08:12:44 +00001154 const TemplateArgumentLoc *PartialSpecTemplateArgs
1155 = PartialSpec->getTemplateArgsAsWritten();
1156 unsigned N = PartialSpec->getNumTemplateArgsAsWritten();
1157
1158 llvm::SmallVector<TemplateArgumentLoc, 4> InstTemplateArgs(N);
1159 for (unsigned I = 0; I != N; ++I) {
1160 if (SemaRef.Subst(PartialSpecTemplateArgs[I], InstTemplateArgs[I],
1161 TemplateArgs))
Douglas Gregored9c0f92009-10-29 00:04:11 +00001162 return true;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001163 }
1164
1165
1166 // Check that the template argument list is well-formed for this
1167 // class template.
1168 TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(),
1169 InstTemplateArgs.size());
1170 if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
1171 PartialSpec->getLocation(),
1172 /*FIXME:*/PartialSpec->getLocation(),
1173 InstTemplateArgs.data(),
1174 InstTemplateArgs.size(),
1175 /*FIXME:*/PartialSpec->getLocation(),
1176 false,
1177 Converted))
1178 return true;
1179
1180 // Figure out where to insert this class template partial specialization
1181 // in the member template's set of class template partial specializations.
1182 llvm::FoldingSetNodeID ID;
1183 ClassTemplatePartialSpecializationDecl::Profile(ID,
1184 Converted.getFlatArguments(),
1185 Converted.flatSize(),
1186 SemaRef.Context);
1187 void *InsertPos = 0;
1188 ClassTemplateSpecializationDecl *PrevDecl
1189 = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID,
1190 InsertPos);
1191
1192 // Build the canonical type that describes the converted template
1193 // arguments of the class template partial specialization.
1194 QualType CanonType
1195 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1196 Converted.getFlatArguments(),
1197 Converted.flatSize());
1198
1199 // Build the fully-sugared type for this class template
1200 // specialization as the user wrote in the specialization
1201 // itself. This means that we'll pretty-print the type retrieved
1202 // from the specialization's declaration the way that the user
1203 // actually wrote the specialization, rather than formatting the
1204 // name based on the "canonical" representation used to store the
1205 // template arguments in the specialization.
1206 QualType WrittenTy
1207 = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
1208 InstTemplateArgs.data(),
1209 InstTemplateArgs.size(),
1210 CanonType);
1211
1212 if (PrevDecl) {
1213 // We've already seen a partial specialization with the same template
1214 // parameters and template arguments. This can happen, for example, when
1215 // substituting the outer template arguments ends up causing two
1216 // class template partial specializations of a member class template
1217 // to have identical forms, e.g.,
1218 //
1219 // template<typename T, typename U>
1220 // struct Outer {
1221 // template<typename X, typename Y> struct Inner;
1222 // template<typename Y> struct Inner<T, Y>;
1223 // template<typename Y> struct Inner<U, Y>;
1224 // };
1225 //
1226 // Outer<int, int> outer; // error: the partial specializations of Inner
1227 // // have the same signature.
1228 SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
1229 << WrittenTy;
1230 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
1231 << SemaRef.Context.getTypeDeclType(PrevDecl);
1232 return true;
1233 }
1234
1235
1236 // Create the class template partial specialization declaration.
1237 ClassTemplatePartialSpecializationDecl *InstPartialSpec
1238 = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner,
1239 PartialSpec->getLocation(),
1240 InstParams,
1241 ClassTemplate,
1242 Converted,
John McCall833ca992009-10-29 08:12:44 +00001243 InstTemplateArgs.data(),
1244 InstTemplateArgs.size(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00001245 0);
1246 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
1247 InstPartialSpec->setTypeAsWritten(WrittenTy);
1248
1249 // Add this partial specialization to the set of class template partial
1250 // specializations.
1251 ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec,
1252 InsertPos);
1253 return false;
1254}
1255
John McCallce3ff2b2009-08-25 22:02:44 +00001256/// \brief Does substitution on the type of the given function, including
1257/// all of the function parameters.
Douglas Gregor5545e162009-03-24 00:38:23 +00001258///
John McCallce3ff2b2009-08-25 22:02:44 +00001259/// \param D The function whose type will be the basis of the substitution
Douglas Gregor5545e162009-03-24 00:38:23 +00001260///
1261/// \param Params the instantiated parameter declarations
1262
John McCallce3ff2b2009-08-25 22:02:44 +00001263/// \returns the instantiated function's type if successful, a NULL
Douglas Gregor5545e162009-03-24 00:38:23 +00001264/// type if there was an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001265QualType
John McCallce3ff2b2009-08-25 22:02:44 +00001266TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
Douglas Gregor5545e162009-03-24 00:38:23 +00001267 llvm::SmallVectorImpl<ParmVarDecl *> &Params) {
1268 bool InvalidDecl = false;
1269
John McCallce3ff2b2009-08-25 22:02:44 +00001270 // Substitute all of the function's formal parameter types.
Douglas Gregor7e063902009-05-11 23:53:27 +00001271 TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs);
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001272 llvm::SmallVector<QualType, 4> ParamTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001273 for (FunctionDecl::param_iterator P = D->param_begin(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001274 PEnd = D->param_end();
1275 P != PEnd; ++P) {
Douglas Gregor6477b692009-03-25 15:04:13 +00001276 if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) {
Douglas Gregor5545e162009-03-24 00:38:23 +00001277 if (PInst->getType()->isVoidType()) {
1278 SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type);
1279 PInst->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001280 } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(),
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001281 PInst->getType(),
1282 diag::err_abstract_type_in_decl,
1283 Sema::AbstractParamType))
Douglas Gregor5545e162009-03-24 00:38:23 +00001284 PInst->setInvalidDecl();
1285
1286 Params.push_back(PInst);
1287 ParamTys.push_back(PInst->getType());
1288
1289 if (PInst->isInvalidDecl())
1290 InvalidDecl = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001291 } else
Douglas Gregor5545e162009-03-24 00:38:23 +00001292 InvalidDecl = true;
1293 }
1294
1295 // FIXME: Deallocate dead declarations.
1296 if (InvalidDecl)
1297 return QualType();
1298
John McCall183700f2009-09-21 23:43:11 +00001299 const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>();
Douglas Gregor5545e162009-03-24 00:38:23 +00001300 assert(Proto && "Missing prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001301 QualType ResultType
John McCallce3ff2b2009-08-25 22:02:44 +00001302 = SemaRef.SubstType(Proto->getResultType(), TemplateArgs,
1303 D->getLocation(), D->getDeclName());
Douglas Gregor5545e162009-03-24 00:38:23 +00001304 if (ResultType.isNull())
1305 return QualType();
1306
Jay Foadbeaaccd2009-05-21 09:52:38 +00001307 return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(),
Douglas Gregor5545e162009-03-24 00:38:23 +00001308 Proto->isVariadic(), Proto->getTypeQuals(),
1309 D->getLocation(), D->getDeclName());
1310}
1311
Mike Stump1eb44332009-09-09 15:08:12 +00001312/// \brief Initializes the common fields of an instantiation function
Douglas Gregore53060f2009-06-25 22:08:12 +00001313/// declaration (New) from the corresponding fields of its template (Tmpl).
1314///
1315/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001316bool
1317TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
Douglas Gregore53060f2009-06-25 22:08:12 +00001318 FunctionDecl *Tmpl) {
1319 if (Tmpl->isDeleted())
1320 New->setDeleted();
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregorcca9e962009-07-01 22:01:06 +00001322 // If we are performing substituting explicitly-specified template arguments
1323 // or deduced template arguments into a function template and we reach this
1324 // point, we are now past the point where SFINAE applies and have committed
Mike Stump1eb44332009-09-09 15:08:12 +00001325 // to keeping the new function template specialization. We therefore
1326 // convert the active template instantiation for the function template
Douglas Gregorcca9e962009-07-01 22:01:06 +00001327 // into a template instantiation for this specific function template
1328 // specialization, which is not a SFINAE context, so that we diagnose any
1329 // further errors in the declaration itself.
1330 typedef Sema::ActiveTemplateInstantiation ActiveInstType;
1331 ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
1332 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
1333 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
Mike Stump1eb44332009-09-09 15:08:12 +00001334 if (FunctionTemplateDecl *FunTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +00001335 = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001336 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
Douglas Gregorcca9e962009-07-01 22:01:06 +00001337 "Deduction from the wrong function template?");
Daniel Dunbarbcbb8bd2009-07-16 22:10:11 +00001338 (void) FunTmpl;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001339 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
1340 ActiveInst.Entity = reinterpret_cast<uintptr_t>(New);
Douglas Gregorf35f8282009-11-11 21:54:23 +00001341 --SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +00001342 }
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Douglas Gregore53060f2009-06-25 22:08:12 +00001345 return false;
1346}
1347
Douglas Gregor5545e162009-03-24 00:38:23 +00001348/// \brief Initializes common fields of an instantiated method
1349/// declaration (New) from the corresponding fields of its template
1350/// (Tmpl).
1351///
1352/// \returns true if there was an error
Mike Stump1eb44332009-09-09 15:08:12 +00001353bool
1354TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Douglas Gregor5545e162009-03-24 00:38:23 +00001355 CXXMethodDecl *Tmpl) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001356 if (InitFunctionInstantiation(New, Tmpl))
1357 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor5545e162009-03-24 00:38:23 +00001359 CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
1360 New->setAccess(Tmpl->getAccess());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +00001361 if (Tmpl->isVirtualAsWritten()) {
1362 New->setVirtualAsWritten(true);
Douglas Gregor5545e162009-03-24 00:38:23 +00001363 Record->setAggregate(false);
1364 Record->setPOD(false);
Eli Friedman1d954f62009-08-15 21:55:26 +00001365 Record->setEmpty(false);
Douglas Gregor5545e162009-03-24 00:38:23 +00001366 Record->setPolymorphic(true);
1367 }
Douglas Gregor5545e162009-03-24 00:38:23 +00001368 if (Tmpl->isPure()) {
1369 New->setPure();
1370 Record->setAbstract(true);
1371 }
1372
1373 // FIXME: attributes
1374 // FIXME: New needs a pointer to Tmpl
1375 return false;
1376}
Douglas Gregora58861f2009-05-13 20:28:22 +00001377
1378/// \brief Instantiate the definition of the given function from its
1379/// template.
1380///
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001381/// \param PointOfInstantiation the point at which the instantiation was
1382/// required. Note that this is not precisely a "point of instantiation"
1383/// for the function, but it's close.
1384///
Douglas Gregora58861f2009-05-13 20:28:22 +00001385/// \param Function the already-instantiated declaration of a
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001386/// function template specialization or member function of a class template
1387/// specialization.
1388///
1389/// \param Recursive if true, recursively instantiates any functions that
1390/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001391///
1392/// \param DefinitionRequired if true, then we are performing an explicit
1393/// instantiation where the body of the function is required. Complain if
1394/// there is no such body.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001395void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001396 FunctionDecl *Function,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001397 bool Recursive,
1398 bool DefinitionRequired) {
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001399 if (Function->isInvalidDecl())
1400 return;
1401
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001402 assert(!Function->getBody() && "Already instantiated!");
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001404 // Never instantiate an explicit specialization.
1405 if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
1406 return;
1407
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001408 // Find the function body that we'll be substituting.
Douglas Gregor3b846b62009-10-27 20:53:28 +00001409 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001410 Stmt *Pattern = 0;
1411 if (PatternDecl)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001412 Pattern = PatternDecl->getBody(PatternDecl);
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001413
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001414 if (!Pattern) {
1415 if (DefinitionRequired) {
1416 if (Function->getPrimaryTemplate())
1417 Diag(PointOfInstantiation,
1418 diag::err_explicit_instantiation_undefined_func_template)
1419 << Function->getPrimaryTemplate();
1420 else
1421 Diag(PointOfInstantiation,
1422 diag::err_explicit_instantiation_undefined_member)
1423 << 1 << Function->getDeclName() << Function->getDeclContext();
1424
1425 if (PatternDecl)
1426 Diag(PatternDecl->getLocation(),
1427 diag::note_explicit_instantiation_here);
1428 }
1429
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001430 return;
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001431 }
Douglas Gregor1eee0e72009-05-14 21:06:31 +00001432
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001433 // C++0x [temp.explicit]p9:
1434 // Except for inline functions, other explicit instantiation declarations
Mike Stump1eb44332009-09-09 15:08:12 +00001435 // have the effect of suppressing the implicit instantiation of the entity
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001436 // to which they refer.
Mike Stump1eb44332009-09-09 15:08:12 +00001437 if (Function->getTemplateSpecializationKind()
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001438 == TSK_ExplicitInstantiationDeclaration &&
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001439 !PatternDecl->isInlined())
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001440 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001442 InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
1443 if (Inst)
1444 return;
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001445
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001446 // If we're performing recursive template instantiation, create our own
1447 // queue of pending implicit instantiations that we will instantiate later,
1448 // while we're still within our own instantiation context.
1449 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1450 if (Recursive)
1451 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001453 ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function));
1454
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001455 // Introduce a new scope where local variable instantiations will be
1456 // recorded.
1457 LocalInstantiationScope Scope(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001459 // Introduce the instantiated function parameters into the local
1460 // instantiation scope.
1461 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I)
1462 Scope.InstantiatedLocal(PatternDecl->getParamDecl(I),
1463 Function->getParamDecl(I));
1464
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001465 // Enter the scope of this instantiation. We don't use
1466 // PushDeclContext because we don't have a scope.
1467 DeclContext *PreviousContext = CurContext;
1468 CurContext = Function;
1469
Mike Stump1eb44332009-09-09 15:08:12 +00001470 MultiLevelTemplateArgumentList TemplateArgs =
Anders Carlsson09025312009-08-29 05:16:22 +00001471 getTemplateInstantiationArgs(Function);
1472
1473 // If this is a constructor, instantiate the member initializers.
Mike Stump1eb44332009-09-09 15:08:12 +00001474 if (const CXXConstructorDecl *Ctor =
Anders Carlsson09025312009-08-29 05:16:22 +00001475 dyn_cast<CXXConstructorDecl>(PatternDecl)) {
1476 InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
1477 TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001478 }
1479
Douglas Gregor54dabfc2009-05-14 23:26:13 +00001480 // Instantiate the function body.
Anders Carlsson09025312009-08-29 05:16:22 +00001481 OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs);
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001482
Douglas Gregor52604ab2009-09-11 21:19:12 +00001483 if (Body.isInvalid())
1484 Function->setInvalidDecl();
1485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body),
Douglas Gregore2c31ff2009-05-15 17:59:04 +00001487 /*IsInstantiation=*/true);
Douglas Gregorb9f1b8d2009-05-15 00:01:03 +00001488
1489 CurContext = PreviousContext;
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001490
1491 DeclGroupRef DG(Function);
1492 Consumer.HandleTopLevelDecl(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001494 if (Recursive) {
1495 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001496 // instantiation of this template.
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001497 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00001499 // Restore the set of pending implicit instantiations.
1500 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
1501 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001502}
1503
1504/// \brief Instantiate the definition of the given variable from its
1505/// template.
1506///
Douglas Gregor7caa6822009-07-24 20:34:43 +00001507/// \param PointOfInstantiation the point at which the instantiation was
1508/// required. Note that this is not precisely a "point of instantiation"
1509/// for the function, but it's close.
1510///
1511/// \param Var the already-instantiated declaration of a static member
1512/// variable of a class template specialization.
1513///
1514/// \param Recursive if true, recursively instantiates any functions that
1515/// are required by this instantiation.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001516///
1517/// \param DefinitionRequired if true, then we are performing an explicit
1518/// instantiation where an out-of-line definition of the member variable
1519/// is required. Complain if there is no such definition.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001520void Sema::InstantiateStaticDataMemberDefinition(
1521 SourceLocation PointOfInstantiation,
1522 VarDecl *Var,
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001523 bool Recursive,
1524 bool DefinitionRequired) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001525 if (Var->isInvalidDecl())
1526 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregor7caa6822009-07-24 20:34:43 +00001528 // Find the out-of-line definition of this static data member.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001529 VarDecl *Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregor7caa6822009-07-24 20:34:43 +00001530 assert(Def && "This data member was not instantiated from a template?");
Douglas Gregor0d035142009-10-27 18:42:08 +00001531 assert(Def->isStaticDataMember() && "Not a static data member?");
1532 Def = Def->getOutOfLineDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Douglas Gregor0d035142009-10-27 18:42:08 +00001534 if (!Def) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001535 // We did not find an out-of-line definition of this static data member,
1536 // so we won't perform any instantiation. Rather, we rely on the user to
Mike Stump1eb44332009-09-09 15:08:12 +00001537 // instantiate this definition (or provide a specialization for it) in
1538 // another translation unit.
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001539 if (DefinitionRequired) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001540 Def = Var->getInstantiatedFromStaticDataMember();
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00001541 Diag(PointOfInstantiation,
1542 diag::err_explicit_instantiation_undefined_member)
1543 << 2 << Var->getDeclName() << Var->getDeclContext();
1544 Diag(Def->getLocation(), diag::note_explicit_instantiation_here);
1545 }
1546
Douglas Gregor7caa6822009-07-24 20:34:43 +00001547 return;
1548 }
1549
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001550 // Never instantiate an explicit specialization.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001551 if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001552 return;
1553
1554 // C++0x [temp.explicit]p9:
1555 // Except for inline functions, other explicit instantiation declarations
1556 // have the effect of suppressing the implicit instantiation of the entity
1557 // to which they refer.
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001558 if (Var->getTemplateSpecializationKind()
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001559 == TSK_ExplicitInstantiationDeclaration)
1560 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregor7caa6822009-07-24 20:34:43 +00001562 InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
1563 if (Inst)
1564 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor7caa6822009-07-24 20:34:43 +00001566 // If we're performing recursive template instantiation, create our own
1567 // queue of pending implicit instantiations that we will instantiate later,
1568 // while we're still within our own instantiation context.
1569 std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations;
1570 if (Recursive)
1571 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Douglas Gregor7caa6822009-07-24 20:34:43 +00001573 // Enter the scope of this instantiation. We don't use
1574 // PushDeclContext because we don't have a scope.
1575 DeclContext *PreviousContext = CurContext;
1576 CurContext = Var->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001578 VarDecl *OldVar = Var;
John McCallce3ff2b2009-08-25 22:02:44 +00001579 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
Douglas Gregor7caa6822009-07-24 20:34:43 +00001580 getTemplateInstantiationArgs(Var)));
Douglas Gregor7caa6822009-07-24 20:34:43 +00001581 CurContext = PreviousContext;
1582
1583 if (Var) {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001584 Var->setPreviousDeclaration(OldVar);
Douglas Gregor583f33b2009-10-15 18:07:02 +00001585 MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo();
1586 assert(MSInfo && "Missing member specialization information?");
1587 Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(),
1588 MSInfo->getPointOfInstantiation());
Douglas Gregor7caa6822009-07-24 20:34:43 +00001589 DeclGroupRef DG(Var);
1590 Consumer.HandleTopLevelDecl(DG);
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Douglas Gregor7caa6822009-07-24 20:34:43 +00001593 if (Recursive) {
1594 // Instantiate any pending implicit instantiations found during the
Mike Stump1eb44332009-09-09 15:08:12 +00001595 // instantiation of this template.
Douglas Gregor7caa6822009-07-24 20:34:43 +00001596 PerformPendingImplicitInstantiations();
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Douglas Gregor7caa6822009-07-24 20:34:43 +00001598 // Restore the set of pending implicit instantiations.
1599 PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations);
Mike Stump1eb44332009-09-09 15:08:12 +00001600 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001601}
Douglas Gregor815215d2009-05-27 05:35:12 +00001602
Anders Carlsson09025312009-08-29 05:16:22 +00001603void
1604Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
1605 const CXXConstructorDecl *Tmpl,
1606 const MultiLevelTemplateArgumentList &TemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Anders Carlsson09025312009-08-29 05:16:22 +00001608 llvm::SmallVector<MemInitTy*, 4> NewInits;
1609
1610 // Instantiate all the initializers.
1611 for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(),
Douglas Gregor72f6d672009-09-01 21:04:42 +00001612 InitsEnd = Tmpl->init_end();
1613 Inits != InitsEnd; ++Inits) {
Anders Carlsson09025312009-08-29 05:16:22 +00001614 CXXBaseOrMemberInitializer *Init = *Inits;
1615
1616 ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Anders Carlsson09025312009-08-29 05:16:22 +00001618 // Instantiate all the arguments.
1619 for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end();
1620 Args != ArgsEnd; ++Args) {
1621 OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs);
1622
1623 if (NewArg.isInvalid())
1624 New->setInvalidDecl();
1625 else
1626 NewArgs.push_back(NewArg.takeAs<Expr>());
1627 }
1628
1629 MemInitResult NewInit;
1630
1631 if (Init->isBaseInitializer()) {
Eli Friedmanc5573a82009-08-29 22:22:07 +00001632 QualType BaseType(Init->getBaseClass(), 0);
1633 BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(),
1634 New->getDeclName());
Anders Carlsson09025312009-08-29 05:16:22 +00001635
1636 NewInit = BuildBaseInitializer(BaseType,
Mike Stump1eb44332009-09-09 15:08:12 +00001637 (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001638 NewArgs.size(),
1639 Init->getSourceLocation(),
1640 Init->getRParenLoc(),
1641 New->getParent());
1642 } else if (Init->isMemberInitializer()) {
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001643 FieldDecl *Member;
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001645 // Is this an anonymous union?
1646 if (FieldDecl *UnionInit = Init->getAnonUnionMember())
Douglas Gregore95b4092009-09-16 18:34:49 +00001647 Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs));
Anders Carlsson9988d5d2009-09-01 04:31:02 +00001648 else
Douglas Gregore95b4092009-09-16 18:34:49 +00001649 Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(),
1650 TemplateArgs));
Mike Stump1eb44332009-09-09 15:08:12 +00001651
1652 NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(),
Anders Carlsson09025312009-08-29 05:16:22 +00001653 NewArgs.size(),
1654 Init->getSourceLocation(),
1655 Init->getRParenLoc());
1656 }
1657
1658 if (NewInit.isInvalid())
1659 New->setInvalidDecl();
1660 else {
1661 // FIXME: It would be nice if ASTOwningVector had a release function.
1662 NewArgs.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Anders Carlsson09025312009-08-29 05:16:22 +00001664 NewInits.push_back((MemInitTy *)NewInit.get());
1665 }
1666 }
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Anders Carlsson09025312009-08-29 05:16:22 +00001668 // Assign all the initializers to the new constructor.
Mike Stump1eb44332009-09-09 15:08:12 +00001669 ActOnMemInitializers(DeclPtrTy::make(New),
Anders Carlsson09025312009-08-29 05:16:22 +00001670 /*FIXME: ColonLoc */
1671 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001672 NewInits.data(), NewInits.size());
Anders Carlsson09025312009-08-29 05:16:22 +00001673}
1674
John McCall52a575a2009-08-29 08:11:13 +00001675// TODO: this could be templated if the various decl types used the
1676// same method name.
1677static bool isInstantiationOf(ClassTemplateDecl *Pattern,
1678 ClassTemplateDecl *Instance) {
1679 Pattern = Pattern->getCanonicalDecl();
1680
1681 do {
1682 Instance = Instance->getCanonicalDecl();
1683 if (Pattern == Instance) return true;
1684 Instance = Instance->getInstantiatedFromMemberTemplate();
1685 } while (Instance);
1686
1687 return false;
1688}
1689
Douglas Gregor0d696532009-09-28 06:34:35 +00001690static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
1691 FunctionTemplateDecl *Instance) {
1692 Pattern = Pattern->getCanonicalDecl();
1693
1694 do {
1695 Instance = Instance->getCanonicalDecl();
1696 if (Pattern == Instance) return true;
1697 Instance = Instance->getInstantiatedFromMemberTemplate();
1698 } while (Instance);
1699
1700 return false;
1701}
1702
Douglas Gregored9c0f92009-10-29 00:04:11 +00001703static bool
1704isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
1705 ClassTemplatePartialSpecializationDecl *Instance) {
1706 Pattern
1707 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
1708 do {
1709 Instance = cast<ClassTemplatePartialSpecializationDecl>(
1710 Instance->getCanonicalDecl());
1711 if (Pattern == Instance)
1712 return true;
1713 Instance = Instance->getInstantiatedFromMember();
1714 } while (Instance);
1715
1716 return false;
1717}
1718
John McCall52a575a2009-08-29 08:11:13 +00001719static bool isInstantiationOf(CXXRecordDecl *Pattern,
1720 CXXRecordDecl *Instance) {
1721 Pattern = Pattern->getCanonicalDecl();
1722
1723 do {
1724 Instance = Instance->getCanonicalDecl();
1725 if (Pattern == Instance) return true;
1726 Instance = Instance->getInstantiatedFromMemberClass();
1727 } while (Instance);
1728
1729 return false;
1730}
1731
1732static bool isInstantiationOf(FunctionDecl *Pattern,
1733 FunctionDecl *Instance) {
1734 Pattern = Pattern->getCanonicalDecl();
1735
1736 do {
1737 Instance = Instance->getCanonicalDecl();
1738 if (Pattern == Instance) return true;
1739 Instance = Instance->getInstantiatedFromMemberFunction();
1740 } while (Instance);
1741
1742 return false;
1743}
1744
1745static bool isInstantiationOf(EnumDecl *Pattern,
1746 EnumDecl *Instance) {
1747 Pattern = Pattern->getCanonicalDecl();
1748
1749 do {
1750 Instance = Instance->getCanonicalDecl();
1751 if (Pattern == Instance) return true;
1752 Instance = Instance->getInstantiatedFromMemberEnum();
1753 } while (Instance);
1754
1755 return false;
1756}
1757
John McCall7ba107a2009-11-18 02:36:19 +00001758static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
1759 UsingDecl *Instance,
1760 ASTContext &C) {
1761 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1762}
1763
1764static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
Anders Carlsson0d8df782009-08-29 19:37:28 +00001765 UsingDecl *Instance,
1766 ASTContext &C) {
1767 return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern;
1768}
1769
John McCall52a575a2009-08-29 08:11:13 +00001770static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
1771 VarDecl *Instance) {
1772 assert(Instance->isStaticDataMember());
1773
1774 Pattern = Pattern->getCanonicalDecl();
1775
1776 do {
1777 Instance = Instance->getCanonicalDecl();
1778 if (Pattern == Instance) return true;
1779 Instance = Instance->getInstantiatedFromStaticDataMember();
1780 } while (Instance);
1781
1782 return false;
1783}
1784
Douglas Gregor815215d2009-05-27 05:35:12 +00001785static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001786 if (D->getKind() != Other->getKind()) {
John McCall7ba107a2009-11-18 02:36:19 +00001787 if (UnresolvedUsingTypenameDecl *UUD
1788 = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
1789 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1790 return isInstantiationOf(UUD, UD, Ctx);
1791 }
1792 }
1793
1794 if (UnresolvedUsingValueDecl *UUD
1795 = dyn_cast<UnresolvedUsingValueDecl>(D)) {
Anders Carlsson0d8df782009-08-29 19:37:28 +00001796 if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
1797 return isInstantiationOf(UUD, UD, Ctx);
1798 }
1799 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001800
Anders Carlsson0d8df782009-08-29 19:37:28 +00001801 return false;
1802 }
Mike Stump1eb44332009-09-09 15:08:12 +00001803
John McCall52a575a2009-08-29 08:11:13 +00001804 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
1805 return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001806
John McCall52a575a2009-08-29 08:11:13 +00001807 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
1808 return isInstantiationOf(cast<FunctionDecl>(D), Function);
Douglas Gregor815215d2009-05-27 05:35:12 +00001809
John McCall52a575a2009-08-29 08:11:13 +00001810 if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
1811 return isInstantiationOf(cast<EnumDecl>(D), Enum);
Douglas Gregor815215d2009-05-27 05:35:12 +00001812
Douglas Gregor7caa6822009-07-24 20:34:43 +00001813 if (VarDecl *Var = dyn_cast<VarDecl>(Other))
John McCall52a575a2009-08-29 08:11:13 +00001814 if (Var->isStaticDataMember())
1815 return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
1816
1817 if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
1818 return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
Douglas Gregora5bf7f12009-08-28 22:03:51 +00001819
Douglas Gregor0d696532009-09-28 06:34:35 +00001820 if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
1821 return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
1822
Douglas Gregored9c0f92009-10-29 00:04:11 +00001823 if (ClassTemplatePartialSpecializationDecl *PartialSpec
1824 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
1825 return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
1826 PartialSpec);
1827
Anders Carlssond8b285f2009-09-01 04:26:58 +00001828 if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
1829 if (!Field->getDeclName()) {
1830 // This is an unnamed field.
Mike Stump1eb44332009-09-09 15:08:12 +00001831 return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) ==
Anders Carlssond8b285f2009-09-01 04:26:58 +00001832 cast<FieldDecl>(D);
1833 }
1834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor815215d2009-05-27 05:35:12 +00001836 return D->getDeclName() && isa<NamedDecl>(Other) &&
1837 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
1838}
1839
1840template<typename ForwardIterator>
Mike Stump1eb44332009-09-09 15:08:12 +00001841static NamedDecl *findInstantiationOf(ASTContext &Ctx,
Douglas Gregor815215d2009-05-27 05:35:12 +00001842 NamedDecl *D,
1843 ForwardIterator first,
1844 ForwardIterator last) {
1845 for (; first != last; ++first)
1846 if (isInstantiationOf(Ctx, D, *first))
1847 return cast<NamedDecl>(*first);
1848
1849 return 0;
1850}
1851
John McCall02cace72009-08-28 07:59:38 +00001852/// \brief Finds the instantiation of the given declaration context
1853/// within the current instantiation.
1854///
1855/// \returns NULL if there was an error
Douglas Gregore95b4092009-09-16 18:34:49 +00001856DeclContext *Sema::FindInstantiatedContext(DeclContext* DC,
1857 const MultiLevelTemplateArgumentList &TemplateArgs) {
John McCall02cace72009-08-28 07:59:38 +00001858 if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
Douglas Gregore95b4092009-09-16 18:34:49 +00001859 Decl* ID = FindInstantiatedDecl(D, TemplateArgs);
John McCall02cace72009-08-28 07:59:38 +00001860 return cast_or_null<DeclContext>(ID);
1861 } else return DC;
1862}
1863
Douglas Gregored961e72009-05-27 17:54:46 +00001864/// \brief Find the instantiation of the given declaration within the
1865/// current instantiation.
Douglas Gregor815215d2009-05-27 05:35:12 +00001866///
1867/// This routine is intended to be used when \p D is a declaration
1868/// referenced from within a template, that needs to mapped into the
1869/// corresponding declaration within an instantiation. For example,
1870/// given:
1871///
1872/// \code
1873/// template<typename T>
1874/// struct X {
1875/// enum Kind {
1876/// KnownValue = sizeof(T)
1877/// };
1878///
1879/// bool getKind() const { return KnownValue; }
1880/// };
1881///
1882/// template struct X<int>;
1883/// \endcode
1884///
1885/// In the instantiation of X<int>::getKind(), we need to map the
1886/// EnumConstantDecl for KnownValue (which refers to
1887/// X<T>::<Kind>::KnownValue) to its instantiation
Douglas Gregored961e72009-05-27 17:54:46 +00001888/// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs
1889/// this mapping from within the instantiation of X<int>.
Douglas Gregore95b4092009-09-16 18:34:49 +00001890NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D,
1891 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor44c73842009-09-01 17:53:10 +00001892 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
1893 // Transform all of the elements of the overloaded function set.
Mike Stump1eb44332009-09-09 15:08:12 +00001894 OverloadedFunctionDecl *Result
Douglas Gregor44c73842009-09-01 17:53:10 +00001895 = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Douglas Gregor44c73842009-09-01 17:53:10 +00001897 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
1898 FEnd = Ovl->function_end();
1899 F != FEnd; ++F) {
1900 Result->addOverload(
Douglas Gregore95b4092009-09-16 18:34:49 +00001901 AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F,
1902 TemplateArgs)));
Douglas Gregor44c73842009-09-01 17:53:10 +00001903 }
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Douglas Gregor44c73842009-09-01 17:53:10 +00001905 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001906 }
1907
Douglas Gregor815215d2009-05-27 05:35:12 +00001908 DeclContext *ParentDC = D->getDeclContext();
Douglas Gregor550d9b22009-10-31 17:21:17 +00001909 if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
1910 isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
1911 ParentDC->isFunctionOrMethod()) {
Douglas Gregor2bba76b2009-05-27 17:07:49 +00001912 // D is a local of some kind. Look into the map of local
1913 // declarations to their instantiations.
1914 return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D));
1915 }
Douglas Gregor815215d2009-05-27 05:35:12 +00001916
Douglas Gregore95b4092009-09-16 18:34:49 +00001917 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
1918 if (!Record->isDependentContext())
1919 return D;
1920
1921 // If the RecordDecl is actually the injected-class-name or a "templated"
1922 // declaration for a class template or class template partial
1923 // specialization, substitute into the injected-class-name of the
1924 // class template or partial specialization to find the new DeclContext.
1925 QualType T;
1926 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
1927
1928 if (ClassTemplate) {
1929 T = ClassTemplate->getInjectedClassNameType(Context);
1930 } else if (ClassTemplatePartialSpecializationDecl *PartialSpec
1931 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1932 T = Context.getTypeDeclType(Record);
1933 ClassTemplate = PartialSpec->getSpecializedTemplate();
1934 }
1935
1936 if (!T.isNull()) {
1937 // Substitute into the injected-class-name to get the type corresponding
1938 // to the instantiation we want. This substitution should never fail,
1939 // since we know we can instantiate the injected-class-name or we wouldn't
1940 // have gotten to the injected-class-name!
1941 // FIXME: Can we use the CurrentInstantiationScope to avoid this extra
1942 // instantiation in the common case?
1943 T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName());
1944 assert(!T.isNull() && "Instantiation of injected-class-name cannot fail.");
1945
1946 if (!T->isDependentType()) {
1947 assert(T->isRecordType() && "Instantiation must produce a record type");
1948 return T->getAs<RecordType>()->getDecl();
1949 }
1950
1951 // We are performing "partial" template instantiation to create the
1952 // member declarations for the members of a class template
1953 // specialization. Therefore, D is actually referring to something in
1954 // the current instantiation. Look through the current context,
1955 // which contains actual instantiations, to find the instantiation of
1956 // the "current instantiation" that D refers to.
Mike Stump1eb44332009-09-09 15:08:12 +00001957 for (DeclContext *DC = CurContext; !DC->isFileContext();
John McCall52a575a2009-08-29 08:11:13 +00001958 DC = DC->getParent()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001959 if (ClassTemplateSpecializationDecl *Spec
John McCall52a575a2009-08-29 08:11:13 +00001960 = dyn_cast<ClassTemplateSpecializationDecl>(DC))
Douglas Gregore95b4092009-09-16 18:34:49 +00001961 if (isInstantiationOf(ClassTemplate,
1962 Spec->getSpecializedTemplate()))
John McCall52a575a2009-08-29 08:11:13 +00001963 return Spec;
1964 }
1965
Mike Stump1eb44332009-09-09 15:08:12 +00001966 assert(false &&
John McCall52a575a2009-08-29 08:11:13 +00001967 "Unable to find declaration for the current instantiation");
Douglas Gregore95b4092009-09-16 18:34:49 +00001968 return Record;
John McCall52a575a2009-08-29 08:11:13 +00001969 }
Douglas Gregore95b4092009-09-16 18:34:49 +00001970
1971 // Fall through to deal with other dependent record types (e.g.,
1972 // anonymous unions in class templates).
1973 }
John McCall52a575a2009-08-29 08:11:13 +00001974
Douglas Gregore95b4092009-09-16 18:34:49 +00001975 if (!ParentDC->isDependentContext())
1976 return D;
1977
1978 ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001979 if (!ParentDC)
Douglas Gregor44c73842009-09-01 17:53:10 +00001980 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor815215d2009-05-27 05:35:12 +00001982 if (ParentDC != D->getDeclContext()) {
1983 // We performed some kind of instantiation in the parent context,
1984 // so now we need to look into the instantiated parent context to
1985 // find the instantiation of the declaration D.
1986 NamedDecl *Result = 0;
1987 if (D->getDeclName()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001988 DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
Douglas Gregor815215d2009-05-27 05:35:12 +00001989 Result = findInstantiationOf(Context, D, Found.first, Found.second);
1990 } else {
1991 // Since we don't have a name for the entity we're looking for,
1992 // our only option is to walk through all of the declarations to
1993 // find that name. This will occur in a few cases:
1994 //
1995 // - anonymous struct/union within a template
1996 // - unnamed class/struct/union/enum within a template
1997 //
1998 // FIXME: Find a better way to find these instantiations!
Mike Stump1eb44332009-09-09 15:08:12 +00001999 Result = findInstantiationOf(Context, D,
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002000 ParentDC->decls_begin(),
2001 ParentDC->decls_end());
Douglas Gregor815215d2009-05-27 05:35:12 +00002002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Douglas Gregor815215d2009-05-27 05:35:12 +00002004 assert(Result && "Unable to find instantiation of declaration!");
2005 D = Result;
2006 }
2007
Douglas Gregor815215d2009-05-27 05:35:12 +00002008 return D;
2009}
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002010
Mike Stump1eb44332009-09-09 15:08:12 +00002011/// \brief Performs template instantiation for all implicit template
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002012/// instantiations we have seen until this point.
2013void Sema::PerformPendingImplicitInstantiations() {
2014 while (!PendingImplicitInstantiations.empty()) {
2015 PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front();
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002016 PendingImplicitInstantiations.pop_front();
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Douglas Gregor7caa6822009-07-24 20:34:43 +00002018 // Instantiate function definitions
2019 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002020 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002021 Function->getLocation(), *this,
2022 Context.getSourceManager(),
2023 "instantiating function definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00002025 if (!Function->getBody())
Douglas Gregorb33fe2f2009-06-30 17:20:14 +00002026 InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true);
Douglas Gregor7caa6822009-07-24 20:34:43 +00002027 continue;
2028 }
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Douglas Gregor7caa6822009-07-24 20:34:43 +00002030 // Instantiate static data member definitions.
2031 VarDecl *Var = cast<VarDecl>(Inst.first);
2032 assert(Var->isStaticDataMember() && "Not a static data member?");
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002033
Mike Stump1eb44332009-09-09 15:08:12 +00002034 PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var),
Anders Carlssonc17fb7b2009-09-01 05:12:24 +00002035 Var->getLocation(), *this,
2036 Context.getSourceManager(),
2037 "instantiating static data member "
2038 "definition");
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Douglas Gregor7caa6822009-07-24 20:34:43 +00002040 InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true);
Douglas Gregord7f37bf2009-06-22 23:06:13 +00002041 }
2042}