Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | class VISIBILITY_HIDDEN TemplateStmtInstantiator |
| 24 | : public StmtVisitor<TemplateStmtInstantiator, Sema::OwningStmtResult> { |
| 25 | Sema &SemaRef; |
| 26 | const TemplateArgumentList &TemplateArgs; |
| 27 | |
Anders Carlsson | df3b208 | 2009-05-17 19:34:11 +0000 | [diff] [blame^] | 28 | template<typename T> |
| 29 | Sema::FullExprArg FullExpr(T &expr) { |
Anders Carlsson | a99fad8 | 2009-05-17 18:26:53 +0000 | [diff] [blame] | 30 | return SemaRef.FullExpr(expr); |
| 31 | } |
| 32 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 33 | 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 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 41 | // FIXME: Once we get closer to completion, replace these manually-written |
| 42 | // declarations with automatically-generated ones from |
| 43 | // clang/AST/StmtNodes.def. |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 44 | OwningStmtResult VisitDeclStmt(DeclStmt *S); |
| 45 | OwningStmtResult VisitNullStmt(NullStmt *S); |
| 46 | OwningStmtResult VisitCompoundStmt(CompoundStmt *S); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 47 | OwningStmtResult VisitCaseStmt(CaseStmt *S); |
| 48 | OwningStmtResult VisitDefaultStmt(DefaultStmt *S); |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 49 | OwningStmtResult VisitIfStmt(IfStmt *S); |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 50 | OwningStmtResult VisitSwitchStmt(SwitchStmt *S); |
Douglas Gregor | 4a2e204 | 2009-05-15 21:45:53 +0000 | [diff] [blame] | 51 | OwningStmtResult VisitWhileStmt(WhileStmt *S); |
Douglas Gregor | 9f3ca2a | 2009-05-15 21:56:04 +0000 | [diff] [blame] | 52 | OwningStmtResult VisitDoStmt(DoStmt *S); |
Douglas Gregor | 5831c6a | 2009-05-15 22:12:32 +0000 | [diff] [blame] | 53 | OwningStmtResult VisitForStmt(ForStmt *S); |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 54 | OwningStmtResult VisitExpr(Expr *E); |
| 55 | OwningStmtResult VisitLabelStmt(LabelStmt *S); |
| 56 | OwningStmtResult VisitGotoStmt(GotoStmt *S); |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 57 | OwningStmtResult VisitIndirectGotoStmt(IndirectGotoStmt *S); |
Douglas Gregor | 861ce31 | 2009-05-15 22:32:39 +0000 | [diff] [blame] | 58 | OwningStmtResult VisitBreakStmt(BreakStmt *S); |
| 59 | OwningStmtResult VisitContinueStmt(ContinueStmt *S); |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 60 | OwningStmtResult VisitReturnStmt(ReturnStmt *S); |
| 61 | |
| 62 | // Base case. I'm supposed to ignore this. |
| 63 | OwningStmtResult VisitStmt(Stmt *S) { |
| 64 | S->dump(); |
| 65 | assert(false && "Cannot instantiate this kind of statement"); |
| 66 | return SemaRef.StmtError(); |
| 67 | } |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitDeclStmt(DeclStmt *S) { |
| 72 | llvm::SmallVector<Decl *, 8> Decls; |
| 73 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 74 | D != DEnd; ++D) { |
| 75 | Decl *Instantiated = SemaRef.InstantiateDecl(*D, SemaRef.CurContext, |
| 76 | TemplateArgs); |
| 77 | if (!Instantiated) |
| 78 | return SemaRef.StmtError(); |
| 79 | |
| 80 | Decls.push_back(Instantiated); |
| 81 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(cast<VarDecl>(*D), |
| 82 | cast<VarDecl>(Instantiated)); |
| 83 | } |
| 84 | |
| 85 | return SemaRef.Owned(new (SemaRef.Context) DeclStmt( |
| 86 | DeclGroupRef::Create(SemaRef.Context, |
| 87 | &Decls[0], |
| 88 | Decls.size()), |
| 89 | S->getStartLoc(), |
| 90 | S->getEndLoc())); |
| 91 | } |
| 92 | |
| 93 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitNullStmt(NullStmt *S) { |
| 94 | return SemaRef.Owned(S->Clone(SemaRef.Context)); |
| 95 | } |
| 96 | |
| 97 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitLabelStmt(LabelStmt *S) { |
| 98 | OwningStmtResult SubStmt = Visit(S->getSubStmt()); |
| 99 | |
| 100 | if (SubStmt.isInvalid()) |
| 101 | return SemaRef.StmtError(); |
| 102 | |
| 103 | // FIXME: Pass the real colon loc in. |
| 104 | return SemaRef.ActOnLabelStmt(S->getIdentLoc(), S->getID(), SourceLocation(), |
| 105 | move(SubStmt)); |
| 106 | } |
| 107 | |
| 108 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitGotoStmt(GotoStmt *S) { |
| 109 | return SemaRef.ActOnGotoStmt(S->getGotoLoc(), S->getLabelLoc(), |
| 110 | S->getLabel()->getID()); |
| 111 | } |
| 112 | |
Douglas Gregor | 5f1b9e6 | 2009-05-16 00:20:29 +0000 | [diff] [blame] | 113 | Sema::OwningStmtResult |
| 114 | TemplateStmtInstantiator::VisitIndirectGotoStmt(IndirectGotoStmt *S) { |
| 115 | OwningExprResult Target = SemaRef.InstantiateExpr(S->getTarget(), |
| 116 | TemplateArgs); |
| 117 | if (Target.isInvalid()) |
| 118 | return SemaRef.StmtError(); |
| 119 | |
| 120 | return SemaRef.ActOnIndirectGotoStmt(S->getGotoLoc(), S->getStarLoc(), |
| 121 | move(Target)); |
| 122 | } |
| 123 | |
Douglas Gregor | 861ce31 | 2009-05-15 22:32:39 +0000 | [diff] [blame] | 124 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitBreakStmt(BreakStmt *S) { |
| 125 | return SemaRef.Owned(S->Clone(SemaRef.Context)); |
| 126 | } |
| 127 | |
| 128 | Sema::OwningStmtResult |
| 129 | TemplateStmtInstantiator::VisitContinueStmt(ContinueStmt *S) { |
| 130 | return SemaRef.Owned(S->Clone(SemaRef.Context)); |
| 131 | } |
| 132 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 133 | Sema::OwningStmtResult |
| 134 | TemplateStmtInstantiator::VisitReturnStmt(ReturnStmt *S) { |
Anders Carlsson | e28be43 | 2009-05-15 20:29:28 +0000 | [diff] [blame] | 135 | Sema::OwningExprResult Result = SemaRef.ExprEmpty(); |
| 136 | if (Expr *E = S->getRetValue()) { |
| 137 | Result = SemaRef.InstantiateExpr(E, TemplateArgs); |
| 138 | |
| 139 | if (Result.isInvalid()) |
| 140 | return SemaRef.StmtError(); |
| 141 | } |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 142 | |
| 143 | return SemaRef.ActOnReturnStmt(S->getReturnLoc(), move(Result)); |
| 144 | } |
| 145 | |
| 146 | Sema::OwningStmtResult |
| 147 | TemplateStmtInstantiator::VisitCompoundStmt(CompoundStmt *S) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 148 | // FIXME: We need an *easy* RAII way to delete these statements if something |
| 149 | // goes wrong. |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 150 | llvm::SmallVector<Stmt *, 16> Statements; |
| 151 | |
| 152 | for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end(); |
| 153 | B != BEnd; ++B) { |
| 154 | OwningStmtResult Result = Visit(*B); |
| 155 | if (Result.isInvalid()) { |
| 156 | // FIXME: This should be handled by an RAII destructor. |
| 157 | for (unsigned I = 0, N = Statements.size(); I != N; ++I) |
| 158 | Statements[I]->Destroy(SemaRef.Context); |
| 159 | return SemaRef.StmtError(); |
| 160 | } |
| 161 | |
| 162 | Statements.push_back(Result.takeAs<Stmt>()); |
| 163 | } |
| 164 | |
| 165 | return SemaRef.Owned( |
| 166 | new (SemaRef.Context) CompoundStmt(SemaRef.Context, |
| 167 | &Statements[0], |
| 168 | Statements.size(), |
| 169 | S->getLBracLoc(), |
| 170 | S->getRBracLoc())); |
| 171 | } |
| 172 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 173 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitCaseStmt(CaseStmt *S) { |
| 174 | // Instantiate left-hand case value. |
| 175 | OwningExprResult LHS = SemaRef.InstantiateExpr(S->getLHS(), TemplateArgs); |
| 176 | if (LHS.isInvalid()) |
| 177 | return SemaRef.StmtError(); |
| 178 | |
| 179 | // Instantiate right-hand case value (for the GNU case-range extension). |
| 180 | OwningExprResult RHS = SemaRef.InstantiateExpr(S->getRHS(), TemplateArgs); |
| 181 | if (RHS.isInvalid()) |
| 182 | return SemaRef.StmtError(); |
| 183 | |
| 184 | // Build the case statement. |
| 185 | OwningStmtResult Case = SemaRef.ActOnCaseStmt(S->getCaseLoc(), |
| 186 | move(LHS), |
| 187 | S->getEllipsisLoc(), |
| 188 | move(RHS), |
| 189 | S->getColonLoc()); |
| 190 | if (Case.isInvalid()) |
| 191 | return SemaRef.StmtError(); |
| 192 | |
| 193 | // Instantiate the statement following the case |
| 194 | OwningStmtResult SubStmt = SemaRef.InstantiateStmt(S->getSubStmt(), |
| 195 | TemplateArgs); |
| 196 | if (SubStmt.isInvalid()) |
| 197 | return SemaRef.StmtError(); |
| 198 | |
| 199 | SemaRef.ActOnCaseStmtBody(Case.get(), move(SubStmt)); |
| 200 | return move(Case); |
| 201 | } |
| 202 | |
| 203 | Sema::OwningStmtResult |
| 204 | TemplateStmtInstantiator::VisitDefaultStmt(DefaultStmt *S) { |
| 205 | // Instantiate the statement following the default case |
| 206 | OwningStmtResult SubStmt = SemaRef.InstantiateStmt(S->getSubStmt(), |
| 207 | TemplateArgs); |
| 208 | if (SubStmt.isInvalid()) |
| 209 | return SemaRef.StmtError(); |
| 210 | |
| 211 | return SemaRef.ActOnDefaultStmt(S->getDefaultLoc(), |
| 212 | S->getColonLoc(), |
| 213 | move(SubStmt), |
| 214 | /*CurScope=*/0); |
| 215 | } |
| 216 | |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 217 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) { |
| 218 | // Instantiate the condition |
| 219 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 220 | if (Cond.isInvalid()) |
| 221 | return SemaRef.StmtError(); |
| 222 | |
| 223 | // Instantiate the "then" branch. |
| 224 | OwningStmtResult Then = SemaRef.InstantiateStmt(S->getThen(), TemplateArgs); |
| 225 | if (Then.isInvalid()) |
| 226 | return SemaRef.StmtError(); |
| 227 | |
| 228 | // Instantiate the "else" branch. |
| 229 | OwningStmtResult Else = SemaRef.InstantiateStmt(S->getElse(), TemplateArgs); |
| 230 | if (Else.isInvalid()) |
| 231 | return SemaRef.StmtError(); |
| 232 | |
Anders Carlsson | a99fad8 | 2009-05-17 18:26:53 +0000 | [diff] [blame] | 233 | return SemaRef.ActOnIfStmt(S->getIfLoc(), FullExpr(Cond), move(Then), |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 234 | S->getElseLoc(), move(Else)); |
| 235 | } |
| 236 | |
Douglas Gregor | dbb26db | 2009-05-15 23:57:33 +0000 | [diff] [blame] | 237 | Sema::OwningStmtResult |
| 238 | TemplateStmtInstantiator::VisitSwitchStmt(SwitchStmt *S) { |
| 239 | // Instantiate the condition. |
| 240 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 241 | if (Cond.isInvalid()) |
| 242 | return SemaRef.StmtError(); |
| 243 | |
| 244 | // Start the switch statement itself. |
| 245 | OwningStmtResult Switch = SemaRef.ActOnStartOfSwitchStmt(move(Cond)); |
| 246 | if (Switch.isInvalid()) |
| 247 | return SemaRef.StmtError(); |
| 248 | |
| 249 | // Instantiate the body of the switch statement. |
| 250 | OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs); |
| 251 | if (Body.isInvalid()) |
| 252 | return SemaRef.StmtError(); |
| 253 | |
| 254 | // Complete the switch statement. |
| 255 | return SemaRef.ActOnFinishSwitchStmt(S->getSwitchLoc(), move(Switch), |
| 256 | move(Body)); |
| 257 | } |
| 258 | |
Douglas Gregor | 4a2e204 | 2009-05-15 21:45:53 +0000 | [diff] [blame] | 259 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitWhileStmt(WhileStmt *S) { |
| 260 | // Instantiate the condition |
| 261 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 262 | if (Cond.isInvalid()) |
| 263 | return SemaRef.StmtError(); |
| 264 | |
| 265 | // Instantiate the body |
| 266 | OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs); |
| 267 | if (Body.isInvalid()) |
| 268 | return SemaRef.StmtError(); |
| 269 | |
| 270 | return SemaRef.ActOnWhileStmt(S->getWhileLoc(), move(Cond), move(Body)); |
| 271 | } |
| 272 | |
Douglas Gregor | 9f3ca2a | 2009-05-15 21:56:04 +0000 | [diff] [blame] | 273 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitDoStmt(DoStmt *S) { |
| 274 | // Instantiate the condition |
| 275 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 276 | if (Cond.isInvalid()) |
| 277 | return SemaRef.StmtError(); |
| 278 | |
| 279 | // Instantiate the body |
| 280 | OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs); |
| 281 | if (Body.isInvalid()) |
| 282 | return SemaRef.StmtError(); |
| 283 | |
| 284 | return SemaRef.ActOnDoStmt(S->getDoLoc(), move(Body), S->getWhileLoc(), |
| 285 | move(Cond)); |
| 286 | } |
| 287 | |
Douglas Gregor | 5831c6a | 2009-05-15 22:12:32 +0000 | [diff] [blame] | 288 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitForStmt(ForStmt *S) { |
| 289 | // Instantiate the initialization statement |
| 290 | OwningStmtResult Init = SemaRef.InstantiateStmt(S->getInit(), TemplateArgs); |
| 291 | if (Init.isInvalid()) |
| 292 | return SemaRef.StmtError(); |
| 293 | |
| 294 | // Instantiate the condition |
| 295 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 296 | if (Cond.isInvalid()) |
| 297 | return SemaRef.StmtError(); |
| 298 | |
| 299 | // Instantiate the increment |
| 300 | OwningExprResult Inc = SemaRef.InstantiateExpr(S->getInc(), TemplateArgs); |
| 301 | if (Inc.isInvalid()) |
| 302 | return SemaRef.StmtError(); |
| 303 | |
| 304 | // Instantiate the body |
| 305 | OwningStmtResult Body = SemaRef.InstantiateStmt(S->getBody(), TemplateArgs); |
| 306 | if (Body.isInvalid()) |
| 307 | return SemaRef.StmtError(); |
| 308 | |
| 309 | return SemaRef.ActOnForStmt(S->getForLoc(), S->getLParenLoc(), |
| 310 | move(Init), move(Cond), move(Inc), |
| 311 | S->getRParenLoc(), move(Body)); |
| 312 | } |
| 313 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 314 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) { |
| 315 | Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs); |
| 316 | if (Result.isInvalid()) |
| 317 | return SemaRef.StmtError(); |
| 318 | |
| 319 | return SemaRef.Owned(Result.takeAs<Stmt>()); |
| 320 | } |
| 321 | |
| 322 | Sema::OwningStmtResult |
| 323 | Sema::InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 50557a7 | 2009-05-15 20:47:12 +0000 | [diff] [blame] | 324 | if (!S) |
| 325 | return Owned((Stmt *)0); |
| 326 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 327 | TemplateStmtInstantiator Instantiator(*this, TemplateArgs); |
| 328 | return Instantiator.Visit(S); |
| 329 | } |