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 | |
| 28 | public: |
| 29 | typedef Sema::OwningExprResult OwningExprResult; |
| 30 | typedef Sema::OwningStmtResult OwningStmtResult; |
| 31 | |
| 32 | TemplateStmtInstantiator(Sema &SemaRef, |
| 33 | const TemplateArgumentList &TemplateArgs) |
| 34 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs) { } |
| 35 | |
| 36 | // FIXME: Once we get closer to completion, replace these |
| 37 | // manually-written declarations with automatically-generated ones |
| 38 | // from clang/AST/StmtNodes.def. |
| 39 | OwningStmtResult VisitDeclStmt(DeclStmt *S); |
| 40 | OwningStmtResult VisitNullStmt(NullStmt *S); |
| 41 | OwningStmtResult VisitCompoundStmt(CompoundStmt *S); |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 42 | OwningStmtResult VisitIfStmt(IfStmt *S); |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 43 | OwningStmtResult VisitExpr(Expr *E); |
| 44 | OwningStmtResult VisitLabelStmt(LabelStmt *S); |
| 45 | OwningStmtResult VisitGotoStmt(GotoStmt *S); |
| 46 | OwningStmtResult VisitReturnStmt(ReturnStmt *S); |
| 47 | |
| 48 | // Base case. I'm supposed to ignore this. |
| 49 | OwningStmtResult VisitStmt(Stmt *S) { |
| 50 | S->dump(); |
| 51 | assert(false && "Cannot instantiate this kind of statement"); |
| 52 | return SemaRef.StmtError(); |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitDeclStmt(DeclStmt *S) { |
| 58 | llvm::SmallVector<Decl *, 8> Decls; |
| 59 | for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); |
| 60 | D != DEnd; ++D) { |
| 61 | Decl *Instantiated = SemaRef.InstantiateDecl(*D, SemaRef.CurContext, |
| 62 | TemplateArgs); |
| 63 | if (!Instantiated) |
| 64 | return SemaRef.StmtError(); |
| 65 | |
| 66 | Decls.push_back(Instantiated); |
| 67 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(cast<VarDecl>(*D), |
| 68 | cast<VarDecl>(Instantiated)); |
| 69 | } |
| 70 | |
| 71 | return SemaRef.Owned(new (SemaRef.Context) DeclStmt( |
| 72 | DeclGroupRef::Create(SemaRef.Context, |
| 73 | &Decls[0], |
| 74 | Decls.size()), |
| 75 | S->getStartLoc(), |
| 76 | S->getEndLoc())); |
| 77 | } |
| 78 | |
| 79 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitNullStmt(NullStmt *S) { |
| 80 | return SemaRef.Owned(S->Clone(SemaRef.Context)); |
| 81 | } |
| 82 | |
| 83 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitLabelStmt(LabelStmt *S) { |
| 84 | OwningStmtResult SubStmt = Visit(S->getSubStmt()); |
| 85 | |
| 86 | if (SubStmt.isInvalid()) |
| 87 | return SemaRef.StmtError(); |
| 88 | |
| 89 | // FIXME: Pass the real colon loc in. |
| 90 | return SemaRef.ActOnLabelStmt(S->getIdentLoc(), S->getID(), SourceLocation(), |
| 91 | move(SubStmt)); |
| 92 | } |
| 93 | |
| 94 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitGotoStmt(GotoStmt *S) { |
| 95 | return SemaRef.ActOnGotoStmt(S->getGotoLoc(), S->getLabelLoc(), |
| 96 | S->getLabel()->getID()); |
| 97 | } |
| 98 | |
| 99 | Sema::OwningStmtResult |
| 100 | TemplateStmtInstantiator::VisitReturnStmt(ReturnStmt *S) { |
Anders Carlsson | e28be43 | 2009-05-15 20:29:28 +0000 | [diff] [blame] | 101 | Sema::OwningExprResult Result = SemaRef.ExprEmpty(); |
| 102 | if (Expr *E = S->getRetValue()) { |
| 103 | Result = SemaRef.InstantiateExpr(E, TemplateArgs); |
| 104 | |
| 105 | if (Result.isInvalid()) |
| 106 | return SemaRef.StmtError(); |
| 107 | } |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 108 | |
| 109 | return SemaRef.ActOnReturnStmt(S->getReturnLoc(), move(Result)); |
| 110 | } |
| 111 | |
| 112 | Sema::OwningStmtResult |
| 113 | TemplateStmtInstantiator::VisitCompoundStmt(CompoundStmt *S) { |
| 114 | // FIXME: We need an *easy* RAII way to delete these statements if |
| 115 | // something goes wrong. |
| 116 | llvm::SmallVector<Stmt *, 16> Statements; |
| 117 | |
| 118 | for (CompoundStmt::body_iterator B = S->body_begin(), BEnd = S->body_end(); |
| 119 | B != BEnd; ++B) { |
| 120 | OwningStmtResult Result = Visit(*B); |
| 121 | if (Result.isInvalid()) { |
| 122 | // FIXME: This should be handled by an RAII destructor. |
| 123 | for (unsigned I = 0, N = Statements.size(); I != N; ++I) |
| 124 | Statements[I]->Destroy(SemaRef.Context); |
| 125 | return SemaRef.StmtError(); |
| 126 | } |
| 127 | |
| 128 | Statements.push_back(Result.takeAs<Stmt>()); |
| 129 | } |
| 130 | |
| 131 | return SemaRef.Owned( |
| 132 | new (SemaRef.Context) CompoundStmt(SemaRef.Context, |
| 133 | &Statements[0], |
| 134 | Statements.size(), |
| 135 | S->getLBracLoc(), |
| 136 | S->getRBracLoc())); |
| 137 | } |
| 138 | |
Douglas Gregor | d06f6ca | 2009-05-15 18:53:42 +0000 | [diff] [blame] | 139 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitIfStmt(IfStmt *S) { |
| 140 | // Instantiate the condition |
| 141 | OwningExprResult Cond = SemaRef.InstantiateExpr(S->getCond(), TemplateArgs); |
| 142 | if (Cond.isInvalid()) |
| 143 | return SemaRef.StmtError(); |
| 144 | |
| 145 | // Instantiate the "then" branch. |
| 146 | OwningStmtResult Then = SemaRef.InstantiateStmt(S->getThen(), TemplateArgs); |
| 147 | if (Then.isInvalid()) |
| 148 | return SemaRef.StmtError(); |
| 149 | |
| 150 | // Instantiate the "else" branch. |
| 151 | OwningStmtResult Else = SemaRef.InstantiateStmt(S->getElse(), TemplateArgs); |
| 152 | if (Else.isInvalid()) |
| 153 | return SemaRef.StmtError(); |
| 154 | |
| 155 | return SemaRef.ActOnIfStmt(S->getIfLoc(), move(Cond), move(Then), |
| 156 | S->getElseLoc(), move(Else)); |
| 157 | } |
| 158 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 159 | Sema::OwningStmtResult TemplateStmtInstantiator::VisitExpr(Expr *E) { |
| 160 | Sema::OwningExprResult Result = SemaRef.InstantiateExpr(E, TemplateArgs); |
| 161 | if (Result.isInvalid()) |
| 162 | return SemaRef.StmtError(); |
| 163 | |
| 164 | return SemaRef.Owned(Result.takeAs<Stmt>()); |
| 165 | } |
| 166 | |
| 167 | Sema::OwningStmtResult |
| 168 | Sema::InstantiateStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 50557a7 | 2009-05-15 20:47:12 +0000 | [diff] [blame^] | 169 | if (!S) |
| 170 | return Owned((Stmt *)0); |
| 171 | |
Douglas Gregor | b70ccad | 2009-05-15 18:22:25 +0000 | [diff] [blame] | 172 | TemplateStmtInstantiator Instantiator(*this, TemplateArgs); |
| 173 | return Instantiator.Visit(S); |
| 174 | } |