blob: 1f69479a0e7c998d3796b331544c3f91cf8a6e1c [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
197 // Instantiate the "then" branch.
198 OwningStmtResult Then = SemaRef.InstantiateStmt(S->getThen(), TemplateArgs);
199 if (Then.isInvalid())
200 return SemaRef.StmtError();
201
202 // Instantiate the "else" branch.
203 OwningStmtResult Else = SemaRef.InstantiateStmt(S->getElse(), TemplateArgs);
204 if (Else.isInvalid())
205 return SemaRef.StmtError();
206
Anders Carlssona99fad82009-05-17 18:26:53 +0000207 return SemaRef.ActOnIfStmt(S->getIfLoc(), FullExpr(Cond), move(Then),
Douglas Gregord06f6ca2009-05-15 18:53:42 +0000208 S->getElseLoc(), move(Else));
209}
210
Douglas Gregordbb26db2009-05-15 23:57:33 +0000211Sema::OwningStmtResult
212TemplateStmtInstantiator::VisitSwitchStmt(SwitchStmt *S) {
213 // Instantiate the condition.
214 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
215 if (Cond.isInvalid())
216 return SemaRef.StmtError();
217
218 // Start the switch statement itself.
219 OwningStmtResult Switch = SemaRef.ActOnStartOfSwitchStmt(move(Cond));
220 if (Switch.isInvalid())
221 return SemaRef.StmtError();
222
223 // Instantiate the body of the switch statement.
224 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
225 if (Body.isInvalid())
226 return SemaRef.StmtError();
227
228 // Complete the switch statement.
229 return SemaRef.ActOnFinishSwitchStmt(S->getSwitchLoc(), move(Switch),
230 move(Body));
231}
232
Douglas Gregor4a2e2042009-05-15 21:45:53 +0000233Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) {
234 // Instantiate the condition
235 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
236 if (Cond.isInvalid())
237 return SemaRef.StmtError();
238
239 // Instantiate the body
240 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
241 if (Body.isInvalid())
242 return SemaRef.StmtError();
243
Anders Carlsson7f537c12009-05-17 21:22:26 +0000244 return SemaRef.ActOnWhileStmt(S->getWhileLoc(), FullExpr(Cond), move(Body));
Douglas Gregor4a2e2042009-05-15 21:45:53 +0000245}
246
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000247Sema::OwningStmtResult TemplateStmtInstantiator::VisitDoStmt(DoStmt *S) {
248 // Instantiate the condition
249 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
250 if (Cond.isInvalid())
251 return SemaRef.StmtError();
252
253 // Instantiate the body
254 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
255 if (Body.isInvalid())
256 return SemaRef.StmtError();
257
258 return SemaRef.ActOnDoStmt(S->getDoLoc(), move(Body), S->getWhileLoc(),
259 move(Cond));
260}
261
Douglas Gregor5831c6a2009-05-15 22:12:32 +0000262Sema::OwningStmtResult TemplateStmtInstantiator::VisitForStmt(ForStmt *S) {
263 // Instantiate the initialization statement
264 OwningStmtResult Init = SemaRef.InstantiateStmt(S->getInit(), TemplateArgs);
265 if (Init.isInvalid())
266 return SemaRef.StmtError();
267
268 // Instantiate the condition
269 OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs);
270 if (Cond.isInvalid())
271 return SemaRef.StmtError();
272
273 // Instantiate the increment
274 OwningExprResult Inc = SemaRef.InstantiateExpr(S->getInc(), TemplateArgs);
275 if (Inc.isInvalid())
276 return SemaRef.StmtError();
277
278 // Instantiate the body
279 OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs);
280 if (Body.isInvalid())
281 return SemaRef.StmtError();
282
283 return SemaRef.ActOnForStmt(S->getForLoc(), S->getLParenLoc(),
284 move(Init), move(Cond), move(Inc),
285 S->getRParenLoc(), move(Body));
286}
287
Douglas Gregor68d10c42009-05-18 19:08:47 +0000288Sema::OwningStmtResult
289TemplateStmtInstantiator::VisitAsmStmt(AsmStmt *S) {
290 // FIXME: Implement this
291 assert(false && "Cannot instantiate an 'asm' statement");
292 return SemaRef.StmtError();
293}
294
295//===----------------------------------------------------------------------===/
296// C++ statements
297//===----------------------------------------------------------------------===/
298Sema::OwningStmtResult
299TemplateStmtInstantiator::VisitCXXTryStmt(CXXTryStmt *S) {
Douglas Gregord308e622009-05-18 20:51:54 +0000300 // Instantiate the try block itself.
301 OwningStmtResult TryBlock = VisitCompoundStmt(S->getTryBlock());
302 if (TryBlock.isInvalid())
303 return SemaRef.StmtError();
304
305 // Instantiate the handlers.
Douglas Gregord7e27052009-05-20 22:33:37 +0000306 ASTOwningVector<&ActionBase::DeleteStmt> Handlers(SemaRef);
Douglas Gregord308e622009-05-18 20:51:54 +0000307 for (unsigned I = 0, N = S->getNumHandlers(); I != N; ++I) {
308 OwningStmtResult Handler = VisitCXXCatchStmt(S->getHandler(I));
Douglas Gregord7e27052009-05-20 22:33:37 +0000309 if (Handler.isInvalid())
Douglas Gregord308e622009-05-18 20:51:54 +0000310 return SemaRef.StmtError();
Douglas Gregord308e622009-05-18 20:51:54 +0000311
312 Handlers.push_back(Handler.takeAs<Stmt>());
313 }
314
315 return SemaRef.ActOnCXXTryBlock(S->getTryLoc(), move(TryBlock),
Douglas Gregor45db71d2009-05-21 16:25:11 +0000316 move_arg(Handlers));
Douglas Gregor68d10c42009-05-18 19:08:47 +0000317}
318
319Sema::OwningStmtResult
320TemplateStmtInstantiator::VisitCXXCatchStmt(CXXCatchStmt *S) {
Douglas Gregord308e622009-05-18 20:51:54 +0000321 // Instantiate the exception declaration, if any.
322 VarDecl *Var = 0;
323 if (S->getExceptionDecl()) {
324 VarDecl *ExceptionDecl = S->getExceptionDecl();
325 QualType T = SemaRef.InstantiateType(ExceptionDecl->getType(),
326 TemplateArgs,
327 ExceptionDecl->getLocation(),
328 ExceptionDecl->getDeclName());
329 if (T.isNull())
330 return SemaRef.StmtError();
331
332 Var = SemaRef.BuildExceptionDeclaration(0, T,
333 ExceptionDecl->getIdentifier(),
334 ExceptionDecl->getLocation(),
335 /*FIXME: Inaccurate*/
336 SourceRange(ExceptionDecl->getLocation()));
337 if (Var->isInvalidDecl()) {
338 Var->Destroy(SemaRef.Context);
339 return SemaRef.StmtError();
340 }
341
342 // Introduce the exception declaration into scope.
343 SemaRef.CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
344 }
345
346 // Instantiate the actual exception handler.
347 OwningStmtResult Handler = Visit(S->getHandlerBlock());
348 if (Handler.isInvalid()) {
349 if (Var)
350 Var->Destroy(SemaRef.Context);
351 return SemaRef.StmtError();
352 }
353
354 return SemaRef.Owned(new (SemaRef.Context) CXXCatchStmt(S->getCatchLoc(),
355 Var,
356 Handler.takeAs<Stmt>()));
Douglas Gregor68d10c42009-05-18 19:08:47 +0000357}
358
359//===----------------------------------------------------------------------===/
360// Objective-C statements
361//===----------------------------------------------------------------------===/
362Sema::OwningStmtResult
363TemplateStmtInstantiator::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
364 // FIXME: Implement this
365 assert(false && "Cannot instantiate an Objective-C @finally statement");
366 return SemaRef.StmtError();
367}
368
369Sema::OwningStmtResult
370TemplateStmtInstantiator::VisitObjCAtSynchronizedStmt(
371 ObjCAtSynchronizedStmt *S) {
372 // FIXME: Implement this
373 assert(false && "Cannot instantiate an Objective-C @synchronized statement");
374 return SemaRef.StmtError();
375}
376
377Sema::OwningStmtResult
378TemplateStmtInstantiator::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
379 // FIXME: Implement this
380 assert(false && "Cannot instantiate an Objective-C @try statement");
381 return SemaRef.StmtError();
382}
383
384Sema::OwningStmtResult
385TemplateStmtInstantiator::VisitObjCForCollectionStmt(
386 ObjCForCollectionStmt *S) {
387 // FIXME: Implement this
388 assert(false && "Cannot instantiate an Objective-C \"for\" statement");
389 return SemaRef.StmtError();
390}
391
392Sema::OwningStmtResult
393TemplateStmtInstantiator::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
394 // FIXME: Implement this
395 assert(false && "Cannot instantiate an Objective-C @throw statement");
396 return SemaRef.StmtError();
397}
398
399Sema::OwningStmtResult
400TemplateStmtInstantiator::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
401 // FIXME: Implement this
402 assert(false && "Cannot instantiate an Objective-C @catch statement");
403 return SemaRef.StmtError();
404}
405
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000406Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) {
407 Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs);
408 if (Result.isInvalid())
409 return SemaRef.StmtError();
410
411 return SemaRef.Owned(Result.takeAs<Stmt>());
412}
413
414Sema::OwningStmtResult
415Sema::InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor50557a72009-05-15 20:47:12 +0000416 if (!S)
417 return Owned((Stmt *)0);
418
Douglas Gregorb70ccad2009-05-15 18:22:25 +0000419 TemplateStmtInstantiator Instantiator(*this, TemplateArgs);
420 return Instantiator.Visit(S);
421}
Douglas Gregor66b46be2009-05-20 22:57:03 +0000422
423Sema::OwningStmtResult
424Sema::InstantiateCompoundStmt(CompoundStmt *S,
425 const TemplateArgumentList &TemplateArgs,
426 bool isStmtExpr) {
427 if (!S)
428 return Owned((Stmt *)0);
429
430 TemplateStmtInstantiator Instantiator(*this, TemplateArgs);
431 ASTOwningVector<&ActionBase::DeleteStmt> Statements(*this);
432 for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end();
433 B != BEnd; ++B) {
434 OwningStmtResult Result = Instantiator.Visit(*B);
435 if (Result.isInvalid())
436 return StmtError();
437
438 Statements.push_back(Result.takeAs<Stmt>());
439 }
440
441 return ActOnCompoundStmt(S->getLBracLoc(), S->getRBracLoc(),
Douglas Gregor45db71d2009-05-21 16:25:11 +0000442 move_arg(Statements), isStmtExpr);
Douglas Gregor66b46be2009-05-20 22:57:03 +0000443}