Changes in / [f6964ef:172d9342]
- Location:
- src/AST
- Files:
-
- 2 edited
-
Convert.cpp (modified) (4 diffs)
-
Stmt.hpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
rf6964ef r172d9342 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 6:01:00 201913 // Update Count : 412 // Last Modified On : Fri May 17 12:13:00 2019 13 // Update Count : 3 14 14 // 15 15 … … 41 41 //================================================================================================ 42 42 class ConverterNewToOld : public ast::Visitor { 43 public: 44 template<typename T> 45 T * get() { 46 T * ret = strict_dynamic_cast< T * >( node ); 47 node = nullptr; 48 return ret; 49 } 50 51 private: 43 52 BaseSyntaxNode * node = nullptr; 44 53 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 89 public: 90 Declaration * decl( const ast::Decl * declNode ) { 91 return get<Declaration>().accept1( ast::ptr<ast::Decl>( declNode ) ); 92 } 93 94 private: 54 // All the visit functions: 55 95 56 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 96 57 (void)node; … … 144 105 145 106 const ast::CompoundStmt * visit( const ast::CompoundStmt * node ) override final { 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; 107 (void)node; 150 108 return nullptr; 151 109 } 152 110 153 111 const ast::Stmt * visit( const ast::ExprStmt * node ) override final { 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; 112 (void)node; 158 113 return nullptr; 159 114 } 160 115 161 116 const ast::Stmt * visit( const ast::AsmStmt * node ) override final { 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; 117 (void)node; 173 118 return nullptr; 174 119 } 175 120 176 121 const ast::Stmt * visit( const ast::DirectiveStmt * node ) override final { 177 auto stmt = new DirectiveStmt( node->directive ); 178 stmt->location = node->location; 179 stmt->labels = makeLabelL( stmt, node->labels ); 180 this->node = stmt; 122 (void)node; 181 123 return nullptr; 182 124 } 183 125 184 126 const ast::Stmt * visit( const ast::IfStmt * node ) override final { 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; 127 (void)node; 128 return nullptr; 129 } 130 131 const ast::Stmt * visit( const ast::WhileStmt * node ) override final { 132 (void)node; 133 return nullptr; 134 } 135 136 const ast::Stmt * visit( const ast::ForStmt * node ) override final { 137 (void)node; 194 138 return nullptr; 195 139 } 196 140 197 141 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; 142 (void)node; 205 143 return nullptr; 206 144 } 207 145 208 146 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; 217 return nullptr; 218 } 219 220 const ast::Stmt * visit( const ast::WhileStmt * node ) override final { 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; 231 return nullptr; 232 } 233 234 const ast::Stmt * visit( const ast::ForStmt * node ) override final { 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; 147 (void)node; 244 148 return nullptr; 245 149 } 246 150 247 151 const ast::Stmt * visit( const ast::BranchStmt * node ) override final { 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; 152 (void)node; 276 153 return nullptr; 277 154 } 278 155 279 156 const ast::Stmt * visit( const ast::ReturnStmt * node ) override final { 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; 157 (void)node; 284 158 return nullptr; 285 159 } 286 160 287 161 const ast::Stmt * visit( const ast::ThrowStmt * node ) override final { 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; 162 (void)node; 307 163 return nullptr; 308 164 } 309 165 310 166 const ast::Stmt * visit( const ast::TryStmt * node ) override final { 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; 167 (void)node; 320 168 return nullptr; 321 169 } 322 170 323 171 const ast::Stmt * visit( const ast::CatchStmt * node ) override final { 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; 172 (void)node; 344 173 return nullptr; 345 174 } 346 175 347 176 const ast::Stmt * visit( const ast::FinallyStmt * node ) override final { 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; 177 (void)node; 352 178 return nullptr; 353 179 } 354 180 355 181 const ast::Stmt * visit( const ast::WaitForStmt * node ) override final { 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; 182 (void)node; 379 183 return nullptr; 380 184 } 381 185 382 186 const ast::Stmt * visit( const ast::WithStmt * node ) override final { 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; 187 (void)node; 390 188 return nullptr; 391 189 } 392 190 393 191 const ast::NullStmt * visit( const ast::NullStmt * node ) override final { 394 auto stmt = new NullStmt(); 395 stmt->location = node->location; 396 stmt->labels = makeLabelL( stmt, node->labels ); 397 this->node = stmt; 192 (void)node; 398 193 return nullptr; 399 194 } 400 195 401 196 const ast::Stmt * visit( const ast::DeclStmt * node ) override final { 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; 197 (void)node; 406 198 return nullptr; 407 199 } … … 722 514 std::list< Declaration * > decls; 723 515 for(auto d : translationUnit) { 724 decls.emplace_back( c.decl( d ) ); 516 d->accept( c ); 517 decls.emplace_back( c.get<Declaration>() ); 725 518 delete d; 726 519 } -
src/AST/Stmt.hpp
rf6964ef r172d9342 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 2:45:00 201913 // Update Count : 512 // Last Modified On : Fri May 17 10:03:00 2019 13 // Update Count : 4 14 14 // 15 15 … … 175 175 : Stmt(loc, std::move(labels)), cond(cond), stmts(std::move(stmts)) {} 176 176 177 bool isDefault() const{ return !cond; }177 bool isDefault() { 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.