blob: efdcec81afffcaaefa6cfba2b02ea64dec665bd2 [file] [log] [blame]
Douglas Gregorb70ccad2009-05-15 18:22:25 +00001//===--- SemaTemplateInstantiateStmt.cpp - C++ Template Stmt 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 statements.
10//
11//===----------------------------------------------------------------------===/
12#include "Sema.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/AST/StmtVisitor.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/Parse/DeclSpec.h"
19#include "llvm/Support/Compiler.h"
20using namespace clang;
21
22namespace {
23 class VISIBILITY_HIDDEN TemplateStmtInstantiator
24 : public StmtVisitor<TemplateStmtInstantiator, Sema::OwningStmtResult> {
25 Sema &SemaRef;
26 const TemplateArgumentList &TemplateArgs;
27
Anders Carlssondf3b2082009-05-17 19:34:11 +000028 template<typename T>
29 Sema::FullExprArg FullExpr(T &expr) {
Anders Carlssona99fad82009-05-17 18:26:53 +000030 return SemaRef.FullExpr(expr);
31 }
32
Douglas Gregorb70ccad2009-05-15 18:22:25 +000033 public:
34 typedef Sema::OwningExprResult OwningExprResult;
35 typedef Sema::OwningStmtResult OwningStmtResult;
36
37 TemplateStmtInstantiator(Sema &SemaRef,
38 const TemplateArgumentList &TemplateArgs)
39 : SemaRef(SemaRef), TemplateArgs(TemplateArgs) { }
40
Douglas Gregor68d10c42009-05-18 19:08:47 +000041 // Declare VisitXXXStmt nodes for all of the statement kinds.
42#define STMT(Type, Base) OwningStmtResult Visit##Type(Type *S);
43#define EXPR(Type, Base)
44#include "clang/AST/StmtNodes.def"
45
46 // Visit an expression (which will use the expression
47 // instantiator).
Douglas Gregorb70ccad2009-05-15 18:22:25 +000048 OwningStmtResult VisitExpr(Expr *E);
Douglas Gregorb70ccad2009-05-15 18:22:25 +000049
50 // Base case. I'm supposed to ignore this.
51 OwningStmtResult VisitStmt(Stmt *S) {
52 S->dump();
53 assert(false && "Cannot instantiate this kind of statement");
54 return SemaRef.StmtError();
55 }
56 };
57}
58
Douglas Gregor68d10c42009-05-18 19:08:47 +000059//===----------------------------------------------------------------------===/
60// Common/C statements
61//===----------------------------------------------------------------------===/
Douglas Gregorb70ccad2009-05-15 18:22:25 +000062Sema::OwningStmtResult TemplateStmtInstantiator::VisitDeclStmt(DeclStmt *S) {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +000063 llvm::SmallVector<Decl *, 4> Decls;
Douglas Gregorb70ccad2009-05-15 18:22:25 +000064 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
65 D != DEnd; ++D) {
66 Decl *Instantiated = SemaRef.InstantiateDecl(*D, SemaRef.CurContext,
67 TemplateArgs);
68 if (!Instantiated)
69 return SemaRef.StmtError();
70
71 Decls.push_back(Instantiated);
Douglas Gregor6569d682009-05-27 23:11:45 +000072 SemaRef.CurrentInstantiationScope->InstantiatedLocal(*D, Instantiated);
Douglas Gregorb70ccad2009-05-15 18:22:25 +000073 }
74
75 return SemaRef.Owned(new (SemaRef.Context) DeclStmt(
76 DeclGroupRef::Create(SemaRef.Context,
77 &Decls[0],
78 Decls.size()),
79 S->getStartLoc(),
80 S->getEndLoc()));
81}
82
83Sema::OwningStmtResult TemplateStmtInstantiator::VisitNullStmt(NullStmt *S) {
84 return SemaRef.Owned(S->Clone(SemaRef.Context));
85}
86
87Sema::OwningStmtResult TemplateStmtInstantiator::VisitLabelStmt(LabelStmt *S) {
88 OwningStmtResult SubStmt = Visit(S->getSubStmt());
89
90 if (SubStmt.isInvalid())
91 return SemaRef.StmtError();
92
93 // FIXME: Pass the real colon loc in.
94 return SemaRef.ActOnLabelStmt(S->getIdentLoc(), S->getID(), SourceLocation(),
95 move(SubStmt));
96}
97
98Sema::OwningStmtResult TemplateStmtInstantiator::VisitGotoStmt(GotoStmt *S) {
99 return SemaRef.ActOnGotoStmt(S->getGotoLoc(), S->getLabelLoc(),
100 S->getLabel()->getID());
101}
102
Douglas Gregor5f1b9e62009-05-16 00:20:29 +0000103Sema::OwningStmtResult
104TemplateStmtInstantiator::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
105 OwningExprResult Target = SemaRef.InstantiateExpr(S->getTarget(),
106 TemplateArgs);
107 if (Target.isInvalid())
108 return SemaRef.StmtError();
109
110 return SemaRef.ActOnIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(),
111 move(Target));
112}
113
Douglas Gregor861ce312009-05-15 22:32:39 +0000114Sema::OwningStmtResult TemplateStmtInstantiator::VisitBreakStmt(BreakStmt *S) {
115 return SemaRef.Owned(S->Clone(SemaRef.Context));
116}
117
118Sema::OwningStmtResult
119TemplateStmtInstantiator::VisitContinueStmt(ContinueStmt *S) {
120 return SemaRef.Owned(S->Clone(SemaRef.Context));
121}
122
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000123Sema::OwningStmtResult
124TemplateStmtInstantiator::VisitReturnStmt(ReturnStmt *S) {
Anders Carlssone28be432009-05-15 20:29:28 +0000125 Sema::OwningExprResult Result = SemaRef.ExprEmpty();
126 if (Expr *E = S->getRetValue()) {
127 Result = SemaRef.InstantiateExpr(E, TemplateArgs);
128
129 if (Result.isInvalid())
130 return SemaRef.StmtError();
131 }
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000132
Anders Carlssona0ab25d2009-05-30 21:42:34 +0000133 return SemaRef.ActOnReturnStmt(S->getReturnLoc(), FullExpr(Result));
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000134}
135
136Sema::OwningStmtResult
137TemplateStmtInstantiator::VisitCompoundStmt(CompoundStmt *S) {
Douglas Gregor66b46be2009-05-20 22:57:03 +0000138 return SemaRef.InstantiateCompoundStmt(S, TemplateArgs, false);
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000139}
140
Douglas Gregor68d10c42009-05-18 19:08:47 +0000141Sema::OwningStmtResult
142TemplateStmtInstantiator::VisitSwitchCase(SwitchCase *S) {
143 assert(false && "SwitchCase statements are never directly instantiated");
144 return SemaRef.StmtError();
145}
146
Douglas Gregordbb26db2009-05-15 23:57:33 +0000147Sema::OwningStmtResult TemplateStmtInstantiator::VisitCaseStmt(CaseStmt *S) {
148 // Instantiate left-hand case value.
149 OwningExprResult LHS = SemaRef.InstantiateExpr(S->getLHS(), TemplateArgs);
150 if (LHS.isInvalid())
151 return SemaRef.StmtError();
152
153 // Instantiate right-hand case value (for the GNU case-range extension).
154 OwningExprResult RHS = SemaRef.InstantiateExpr(S->getRHS(), TemplateArgs);
155 if (RHS.isInvalid())
156 return SemaRef.StmtError();
157
158 // Build the case statement.
159 OwningStmtResult Case = SemaRef.ActOnCaseStmt(S->getCaseLoc(),
160 move(LHS),
161 S->getEllipsisLoc(),
162 move(RHS),
163 S->getColonLoc());
164 if (Case.isInvalid())
165 return SemaRef.StmtError();
166
167 // Instantiate the statement following the case
168 OwningStmtResult SubStmt = SemaRef.InstantiateStmt(S->getSubStmt(),
169 TemplateArgs);
170 if (SubStmt.isInvalid())
171 return SemaRef.StmtError();
172
173 SemaRef.ActOnCaseStmtBody(Case.get(), move(SubStmt));
174 return move(Case);
175}
176
177Sema::OwningStmtResult
178TemplateStmtInstantiator::VisitDefaultStmt(DefaultStmt *S) {
179 // Instantiate the statement following the default case
180 OwningStmtResult SubStmt = SemaRef.InstantiateStmt(S->getSubStmt(),
181 TemplateArgs);
182 if (SubStmt.isInvalid())
183 return SemaRef.StmtError();
184
185 return SemaRef.ActOnDefaultStmt(S->getDefaultLoc(),
186 S->getColonLoc(),
187 move(SubStmt),
188 /*CurScope=*/0);
189}
190
Douglas Gregord06f6ca2009-05-15 18:53:42 +0000191Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) {
192 // Instantiate the condition
193 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
194 if (Cond.isInvalid())
195 return SemaRef.StmtError();
196
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000197 Sema::FullExprArg FullCond(FullExpr(Cond));
198
Douglas Gregord06f6ca2009-05-15 18:53:42 +0000199 // Instantiate the "then" branch.
200 OwningStmtResult Then = SemaRef.InstantiateStmt(S->getThen(), TemplateArgs);
201 if (Then.isInvalid())
202 return SemaRef.StmtError();
203
204 // Instantiate the "else" branch.
205 OwningStmtResult Else = SemaRef.InstantiateStmt(S->getElse(), TemplateArgs);
206 if (Else.isInvalid())
207 return SemaRef.StmtError();
208
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000209 return SemaRef.ActOnIfStmt(S->getIfLoc(), FullCond, move(Then),
Douglas Gregord06f6ca2009-05-15 18:53:42 +0000210 S->getElseLoc(), move(Else));
211}
212
Douglas Gregordbb26db2009-05-15 23:57:33 +0000213Sema::OwningStmtResult
214TemplateStmtInstantiator::VisitSwitchStmt(SwitchStmt *S) {
215 // Instantiate the condition.
216 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
217 if (Cond.isInvalid())
218 return SemaRef.StmtError();
219
220 // Start the switch statement itself.
221 OwningStmtResult Switch = SemaRef.ActOnStartOfSwitchStmt(move(Cond));
222 if (Switch.isInvalid())
223 return SemaRef.StmtError();
224
225 // Instantiate the body of the switch statement.
226 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
227 if (Body.isInvalid())
228 return SemaRef.StmtError();
229
230 // Complete the switch statement.
231 return SemaRef.ActOnFinishSwitchStmt(S->getSwitchLoc(), move(Switch),
232 move(Body));
233}
234
Douglas Gregor4a2e2042009-05-15 21:45:53 +0000235Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) {
236 // Instantiate the condition
237 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
238 if (Cond.isInvalid())
239 return SemaRef.StmtError();
240
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000241 Sema::FullExprArg FullCond(FullExpr(Cond));
242
Douglas Gregor4a2e2042009-05-15 21:45:53 +0000243 // Instantiate the body
244 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
245 if (Body.isInvalid())
246 return SemaRef.StmtError();
247
Anders Carlssonc4ee1702009-06-04 02:18:15 +0000248 return SemaRef.ActOnWhileStmt(S->getWhileLoc(), FullCond, move(Body));
Douglas Gregor4a2e2042009-05-15 21:45:53 +0000249}
250
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000251Sema::OwningStmtResult TemplateStmtInstantiator::VisitDoStmt(DoStmt *S) {
252 // Instantiate the condition
253 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
254 if (Cond.isInvalid())
255 return SemaRef.StmtError();
256
257 // Instantiate the body
258 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
259 if (Body.isInvalid())
260 return SemaRef.StmtError();
261
262 return SemaRef.ActOnDoStmt(S->getDoLoc(), move(Body), S->getWhileLoc(),
Chris Lattner98913592009-06-12 23:04:47 +0000263 SourceLocation(), move(Cond), S->getRParenLoc());
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000264}
265
Douglas Gregor5831c6a2009-05-15 22:12:32 +0000266Sema::OwningStmtResult TemplateStmtInstantiator::VisitForStmt(ForStmt *S) {
267 // Instantiate the initialization statement
268 OwningStmtResult Init = SemaRef.InstantiateStmt(S->getInit(), TemplateArgs);
269 if (Init.isInvalid())
270 return SemaRef.StmtError();
271
272 // Instantiate the condition
273 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
274 if (Cond.isInvalid())
275 return SemaRef.StmtError();
276
277 // Instantiate the increment
278 OwningExprResult Inc = SemaRef.InstantiateExpr(S->getInc(), TemplateArgs);
279 if (Inc.isInvalid())
280 return SemaRef.StmtError();
281
282 // Instantiate the body
283 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
284 if (Body.isInvalid())
285 return SemaRef.StmtError();
286
287 return SemaRef.ActOnForStmt(S->getForLoc(), S->getLParenLoc(),
288 move(Init), move(Cond), move(Inc),
289 S->getRParenLoc(), move(Body));
290}
291
Douglas Gregor68d10c42009-05-18 19:08:47 +0000292Sema::OwningStmtResult
293TemplateStmtInstantiator::VisitAsmStmt(AsmStmt *S) {
294 // FIXME: Implement this
295 assert(false && "Cannot instantiate an 'asm' statement");
296 return SemaRef.StmtError();
297}
298
299//===----------------------------------------------------------------------===/
300// C++ statements
301//===----------------------------------------------------------------------===/
302Sema::OwningStmtResult
303TemplateStmtInstantiator::VisitCXXTryStmt(CXXTryStmt *S) {
Douglas Gregord308e622009-05-18 20:51:54 +0000304 // Instantiate the try block itself.
305 OwningStmtResult TryBlock = VisitCompoundStmt(S->getTryBlock());
306 if (TryBlock.isInvalid())
307 return SemaRef.StmtError();
308
309 // Instantiate the handlers.
Douglas Gregord7e27052009-05-20 22:33:37 +0000310 ASTOwningVector<&ActionBase::DeleteStmt> Handlers(SemaRef);
Douglas Gregord308e622009-05-18 20:51:54 +0000311 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
312 OwningStmtResult Handler = VisitCXXCatchStmt(S->getHandler(I));
Douglas Gregord7e27052009-05-20 22:33:37 +0000313 if (Handler.isInvalid())
Douglas Gregord308e622009-05-18 20:51:54 +0000314 return SemaRef.StmtError();
Douglas Gregord308e622009-05-18 20:51:54 +0000315
316 Handlers.push_back(Handler.takeAs<Stmt>());
317 }
318
319 return SemaRef.ActOnCXXTryBlock(S->getTryLoc(), move(TryBlock),
Douglas Gregor45db71d2009-05-21 16:25:11 +0000320 move_arg(Handlers));
Douglas Gregor68d10c42009-05-18 19:08:47 +0000321}
322
323Sema::OwningStmtResult
324TemplateStmtInstantiator::VisitCXXCatchStmt(CXXCatchStmt *S) {
Douglas Gregord308e622009-05-18 20:51:54 +0000325 // Instantiate the exception declaration, if any.
326 VarDecl *Var = 0;
327 if (S->getExceptionDecl()) {
328 VarDecl *ExceptionDecl = S->getExceptionDecl();
329 QualType T = SemaRef.InstantiateType(ExceptionDecl->getType(),
330 TemplateArgs,
331 ExceptionDecl->getLocation(),
332 ExceptionDecl->getDeclName());
333 if (T.isNull())
334 return SemaRef.StmtError();
335
336 Var = SemaRef.BuildExceptionDeclaration(0, T,
337 ExceptionDecl->getIdentifier(),
338 ExceptionDecl->getLocation(),
339 /*FIXME: Inaccurate*/
340 SourceRange(ExceptionDecl->getLocation()));
341 if (Var->isInvalidDecl()) {
342 Var->Destroy(SemaRef.Context);
343 return SemaRef.StmtError();
344 }
345
346 // Introduce the exception declaration into scope.
347 SemaRef.CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
348 }
349
350 // Instantiate the actual exception handler.
351 OwningStmtResult Handler = Visit(S->getHandlerBlock());
352 if (Handler.isInvalid()) {
353 if (Var)
354 Var->Destroy(SemaRef.Context);
355 return SemaRef.StmtError();
356 }
357
358 return SemaRef.Owned(new (SemaRef.Context) CXXCatchStmt(S->getCatchLoc(),
359 Var,
360 Handler.takeAs<Stmt>()));
Douglas Gregor68d10c42009-05-18 19:08:47 +0000361}
362
363//===----------------------------------------------------------------------===/
364// Objective-C statements
365//===----------------------------------------------------------------------===/
366Sema::OwningStmtResult
367TemplateStmtInstantiator::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
368 // FIXME: Implement this
369 assert(false && "Cannot instantiate an Objective-C @finally statement");
370 return SemaRef.StmtError();
371}
372
373Sema::OwningStmtResult
374TemplateStmtInstantiator::VisitObjCAtSynchronizedStmt(
375 ObjCAtSynchronizedStmt *S) {
376 // FIXME: Implement this
377 assert(false && "Cannot instantiate an Objective-C @synchronized statement");
378 return SemaRef.StmtError();
379}
380
381Sema::OwningStmtResult
382TemplateStmtInstantiator::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
383 // FIXME: Implement this
384 assert(false && "Cannot instantiate an Objective-C @try statement");
385 return SemaRef.StmtError();
386}
387
388Sema::OwningStmtResult
389TemplateStmtInstantiator::VisitObjCForCollectionStmt(
390 ObjCForCollectionStmt *S) {
391 // FIXME: Implement this
392 assert(false && "Cannot instantiate an Objective-C \"for\" statement");
393 return SemaRef.StmtError();
394}
395
396Sema::OwningStmtResult
397TemplateStmtInstantiator::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
398 // FIXME: Implement this
399 assert(false && "Cannot instantiate an Objective-C @throw statement");
400 return SemaRef.StmtError();
401}
402
403Sema::OwningStmtResult
404TemplateStmtInstantiator::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
405 // FIXME: Implement this
406 assert(false && "Cannot instantiate an Objective-C @catch statement");
407 return SemaRef.StmtError();
408}
409
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000410Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
411 Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
412 if (Result.isInvalid())
413 return SemaRef.StmtError();
414
415 return SemaRef.Owned(Result.takeAs<Stmt>());
416}
417
418Sema::OwningStmtResult
419Sema::InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor50557a72009-05-15 20:47:12 +0000420 if (!S)
421 return Owned((Stmt *)0);
422
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000423 TemplateStmtInstantiator Instantiator(*this, TemplateArgs);
424 return Instantiator.Visit(S);
425}
Douglas Gregor66b46be2009-05-20 22:57:03 +0000426
427Sema::OwningStmtResult
428Sema::InstantiateCompoundStmt(CompoundStmt *S,
429 const TemplateArgumentList &TemplateArgs,
430 bool isStmtExpr) {
431 if (!S)
432 return Owned((Stmt *)0);
433
434 TemplateStmtInstantiator Instantiator(*this, TemplateArgs);
435 ASTOwningVector<&ActionBase::DeleteStmt> Statements(*this);
436 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
437 B != BEnd; ++B) {
438 OwningStmtResult Result = Instantiator.Visit(*B);
439 if (Result.isInvalid())
440 return StmtError();
441
442 Statements.push_back(Result.takeAs<Stmt>());
443 }
444
445 return ActOnCompoundStmt(S->getLBracLoc(), S->getRBracLoc(),
Douglas Gregor45db71d2009-05-21 16:25:11 +0000446 move_arg(Statements), isStmtExpr);
Douglas Gregor66b46be2009-05-20 22:57:03 +0000447}