Changes in / [172d9342:f6964ef]
- Location:
- src/AST
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r172d9342 rf6964ef 10 10 // Created On : Thu May 09 15::37::05 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri May 17 1 2:13:00 201913 // Update Count : 312 // Last Modified On : Fri May 17 16:01:00 2019 13 // Update Count : 4 14 14 // 15 15 … … 41 41 //================================================================================================ 42 42 class ConverterNewToOld : public ast::Visitor { 43 BaseSyntaxNode * node = nullptr; 44 45 template<typename T> 46 struct Getter { 47 ConverterNewToOld & visitor; 48 49 template<typename U> 50 T * accept1( const ast::ptr<U> & ptr ) { 51 ptr->accept( visitor ); 52 T * ret = strict_dynamic_cast< T * >( visitor.node ); 53 visitor.node = nullptr; 54 return ret; 55 } 56 57 template<typename U> 58 std::list< T * > acceptL( const U & container ) { 59 std::list< T * > ret; 60 for (auto ptr : container ) { 61 ret.emplace_back( accept1( ptr ) ); 62 } 63 return ret; 64 } 65 }; 66 67 template<typename T> 68 Getter<T> get() { 69 return Getter<T>{ *this }; 70 } 71 72 Label makeLabel(Statement * labelled, const ast::Label& label) { 73 return Label( 74 label.name, 75 labelled, 76 get<Attribute>().acceptL(label.attributes) 77 ); 78 } 79 80 template<template <class...> class C> 81 std::list<Label> makeLabelL(Statement * labelled, const C<ast::Label>& labels) { 82 std::list<Label> ret; 83 for (auto label : labels) { 84 ret.push_back( makeLabel(labelled, label) ); 85 } 86 return ret; 87 } 88 43 89 public: 44 template<typename T> 45 T * get() { 46 T * ret = strict_dynamic_cast< T * >( node ); 47 node = nullptr; 48 return ret; 90 Declaration * decl( const ast::Decl * declNode ) { 91 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) ); 49 92 } 50 93 51 94 private: 52 BaseSyntaxNode * node = nullptr;53 54 // All the visit functions:55 56 95 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 57 96 (void)node; … … 105 144 106 145 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final { 107 (void)node; 146 auto stmt = new CompoundStmt( get<Statement>().acceptL( node->kids ) ); 147 stmt->location = node->location; 148 stmt->labels = makeLabelL( stmt, node->labels ); 149 this->node = stmt; 108 150 return nullptr; 109 151 } 110 152 111 153 const ast::Stmt * visit( const ast::ExprStmt * node ) override final { 112 (void)node; 154 auto stmt = new ExprStmt( get<Expression>().accept1( node->expr ) ); 155 stmt->location = node->location; 156 stmt->labels = makeLabelL( stmt, node->labels ); 157 this->node = stmt; 113 158 return nullptr; 114 159 } 115 160 116 161 const ast::Stmt * visit( const ast::AsmStmt * node ) override final { 117 (void)node; 162 auto stmt = new AsmStmt( 163 node->isVolatile, 164 get<Expression>().accept1( node->instruction ), 165 get<Expression>().acceptL( node->output ), 166 get<Expression>().acceptL( node->input ), 167 get<ConstantExpr>().acceptL( node->clobber ), 168 makeLabelL( nullptr, node->gotoLabels ) // What are these labelling? 169 ); 170 stmt->location = node->location; 171 stmt->labels = makeLabelL( stmt, node->labels ); 172 this->node = stmt; 118 173 return nullptr; 119 174 } 120 175 121 176 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final { 122 (void)node; 177 auto stmt = new DirectiveStmt( node->directive ); 178 stmt->location = node->location; 179 stmt->labels = makeLabelL( stmt, node->labels ); 180 this->node = stmt; 123 181 return nullptr; 124 182 } 125 183 126 184 const ast::Stmt * visit( const ast::IfStmt * node ) override final { 127 (void)node; 185 auto stmt = new IfStmt( 186 get<Expression>().accept1( node->cond ), 187 get<Statement>().accept1( node->thenPart ), 188 get<Statement>().accept1( node->elsePart ), 189 get<Statement>().acceptL( node->inits ) 190 ); 191 stmt->location = node->location; 192 stmt->labels = makeLabelL( stmt, node->labels ); 193 this->node = stmt; 194 return nullptr; 195 } 196 197 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final { 198 auto stmt = new SwitchStmt( 199 get<Expression>().accept1( node->cond ), 200 get<Statement>().acceptL( node->stmts ) 201 ); 202 stmt->location = node->location; 203 stmt->labels = makeLabelL( stmt, node->labels ); 204 this->node = stmt; 205 return nullptr; 206 } 207 208 const ast::Stmt * visit( const ast::CaseStmt * node ) override final { 209 auto stmt = new CaseStmt( 210 get<Expression>().accept1( node->cond ), 211 get<Statement>().acceptL( node->stmts ), 212 node->isDefault() 213 ); 214 stmt->location = node->location; 215 stmt->labels = makeLabelL( stmt, node->labels ); 216 this->node = stmt; 128 217 return nullptr; 129 218 } 130 219 131 220 const ast::Stmt * visit( const ast::WhileStmt * node ) override final { 132 (void)node; 221 auto inits = get<Statement>().acceptL( node->inits ); 222 auto stmt = new WhileStmt( 223 get<Expression>().accept1( node->cond ), 224 get<Statement>().accept1( node->body ), 225 inits, 226 node->isDoWhile 227 ); 228 stmt->location = node->location; 229 stmt->labels = makeLabelL( stmt, node->labels ); 230 this->node = stmt; 133 231 return nullptr; 134 232 } 135 233 136 234 const ast::Stmt * visit( const ast::ForStmt * node ) override final { 137 (void)node; 138 return nullptr; 139 } 140 141 const ast::Stmt * visit( const ast::SwitchStmt * node ) override final { 142 (void)node; 143 return nullptr; 144 } 145 146 const ast::Stmt * visit( const ast::CaseStmt * node ) override final { 147 (void)node; 235 auto stmt = new ForStmt( 236 get<Statement>().acceptL( node->inits ), 237 get<Expression>().accept1( node->cond ), 238 get<Expression>().accept1( node->inc ), 239 get<Statement>().accept1( node->body ) 240 ); 241 stmt->location = node->location; 242 stmt->labels = makeLabelL( stmt, node->labels ); 243 this->node = stmt; 148 244 return nullptr; 149 245 } 150 246 151 247 const ast::Stmt * visit( const ast::BranchStmt * node ) override final { 152 (void)node; 248 BranchStmt * stmt; 249 if (node->computedTarget) { 250 stmt = new BranchStmt( get<Expression>().accept1( node->computedTarget ), 251 BranchStmt::Goto ); 252 } else { 253 BranchStmt::Type type; 254 switch (node->kind) { 255 #define CASE(n) \ 256 case ast::BranchStmt::n: \ 257 type = BranchStmt::n; \ 258 break 259 CASE(Goto); 260 CASE(Break); 261 CASE(Continue); 262 CASE(FallThrough); 263 CASE(FallThroughDefault); 264 #undef CASE 265 default: 266 assertf(false, "Invalid ast::BranchStmt::Kind: %d\n", node->kind); 267 } 268 269 // The labels here are also weird. 270 stmt = new BranchStmt( makeLabel( nullptr, node->originalTarget ), type ); 271 stmt->target = makeLabel( stmt, node->target ); 272 } 273 stmt->location = node->location; 274 stmt->labels = makeLabelL( stmt, node->labels ); 275 this->node = stmt; 153 276 return nullptr; 154 277 } 155 278 156 279 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final { 157 (void)node; 280 auto stmt = new ReturnStmt( get<Expression>().accept1( node->expr ) ); 281 stmt->location = node->location; 282 stmt->labels = makeLabelL( stmt, node->labels ); 283 this->node = stmt; 158 284 return nullptr; 159 285 } 160 286 161 287 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final { 162 (void)node; 288 ThrowStmt::Kind kind; 289 switch (node->kind) { 290 case ast::ThrowStmt::Terminate: 291 kind = ThrowStmt::Terminate; 292 break; 293 case ast::ThrowStmt::Resume: 294 kind = ThrowStmt::Resume; 295 break; 296 default: 297 assertf(false, "Invalid ast::ThrowStmt::Kind: %d\n", node->kind); 298 } 299 auto stmt = new ThrowStmt( 300 kind, 301 get<Expression>().accept1( node->expr ), 302 get<Expression>().accept1( node->target ) 303 ); 304 stmt->location = node->location; 305 stmt->labels = makeLabelL( stmt, node->labels ); 306 this->node = stmt; 163 307 return nullptr; 164 308 } 165 309 166 310 const ast::Stmt * visit( const ast::TryStmt * node ) override final { 167 (void)node; 311 auto handlers = get<CatchStmt>().acceptL( node->handlers ); 312 auto stmt = new TryStmt( 313 get<CompoundStmt>().accept1( node->body ), 314 handlers, 315 get<FinallyStmt>().accept1( node->finally ) 316 ); 317 stmt->location = node->location; 318 stmt->labels = makeLabelL( stmt, node->labels ); 319 this->node = stmt; 168 320 return nullptr; 169 321 } 170 322 171 323 const ast::Stmt * visit( const ast::CatchStmt * node ) override final { 172 (void)node; 324 CatchStmt::Kind kind; 325 switch (node->kind) { 326 case ast::CatchStmt::Terminate: 327 kind = CatchStmt::Terminate; 328 break; 329 case ast::CatchStmt::Resume: 330 kind = CatchStmt::Resume; 331 break; 332 default: 333 assertf(false, "Invalid ast::CatchStmt::Kind: %d\n", node->kind); 334 } 335 auto stmt = new CatchStmt( 336 kind, 337 get<Declaration>().accept1( node->decl ), 338 get<Expression>().accept1( node->cond ), 339 get<Statement>().accept1( node->body ) 340 ); 341 stmt->location = node->location; 342 stmt->labels = makeLabelL( stmt, node->labels ); 343 this->node = stmt; 173 344 return nullptr; 174 345 } 175 346 176 347 const ast::Stmt * visit( const ast::FinallyStmt * node ) override final { 177 (void)node; 348 auto stmt = new FinallyStmt( get<CompoundStmt>().accept1( node->body ) ); 349 stmt->location = node->location; 350 stmt->labels = makeLabelL( stmt, node->labels ); 351 this->node = stmt; 178 352 return nullptr; 179 353 } 180 354 181 355 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final { 182 (void)node; 356 auto stmt = new WaitForStmt; 357 stmt->clauses.reserve( node->clauses.size() ); 358 for ( auto clause : node->clauses ) { 359 stmt->clauses.push_back({{ 360 get<Expression>().accept1( clause.target.function ), 361 get<Expression>().acceptL( clause.target.arguments ), 362 }, 363 get<Statement>().accept1( clause.stmt ), 364 get<Expression>().accept1( clause.cond ), 365 }); 366 } 367 stmt->timeout = { 368 get<Expression>().accept1( node->timeout.time ), 369 get<Statement>().accept1( node->timeout.stmt ), 370 get<Expression>().accept1( node->timeout.cond ), 371 }; 372 stmt->orelse = { 373 get<Statement>().accept1( node->orElse.stmt ), 374 get<Expression>().accept1( node->orElse.cond ), 375 }; 376 stmt->location = node->location; 377 stmt->labels = makeLabelL( stmt, node->labels ); 378 this->node = stmt; 183 379 return nullptr; 184 380 } 185 381 186 382 const ast::Stmt * visit( const ast::WithStmt * node ) override final { 187 (void)node; 383 auto stmt = new WithStmt( 384 get<Expression>().acceptL( node->exprs ), 385 get<Statement>().accept1( node->stmt ) 386 ); 387 stmt->location = node->location; 388 stmt->labels = makeLabelL( stmt, node->labels ); 389 this->node = stmt; 188 390 return nullptr; 189 391 } 190 392 191 393 const ast::NullStmt * visit( const ast::NullStmt * node ) override final { 192 (void)node; 394 auto stmt = new NullStmt(); 395 stmt->location = node->location; 396 stmt->labels = makeLabelL( stmt, node->labels ); 397 this->node = stmt; 193 398 return nullptr; 194 399 } 195 400 196 401 const ast::Stmt * visit( const ast::DeclStmt * node ) override final { 197 (void)node; 402 auto stmt = new DeclStmt( get<Declaration>().accept1( node->decl ) ); 403 stmt->location = node->location; 404 stmt->labels = makeLabelL( stmt, node->labels ); 405 this->node = stmt; 198 406 return nullptr; 199 407 } … … 514 722 std::list< Declaration * > decls; 515 723 for(auto d : translationUnit) { 516 d->accept( c ); 517 decls.emplace_back( c.get<Declaration>() ); 724 decls.emplace_back( c.decl( d ) ); 518 725 delete d; 519 726 } -
src/AST/Stmt.hpp
r172d9342 rf6964ef 10 10 // Created On : Wed May 8 13:00:00 2019 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri May 17 1 0:03:00 201913 // Update Count : 412 // Last Modified On : Fri May 17 12:45:00 2019 13 // Update Count : 5 14 14 // 15 15 … … 175 175 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {} 176 176 177 bool isDefault() { return !cond; }177 bool isDefault() const { return !cond; } 178 178 179 179 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); }
Note: See TracChangeset
for help on using the changeset viewer.