[2bb4a01] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // Stmt.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
[1e97287] | 10 | // Created On : Wed May 8 13:00:00 2019 |
---|
[7edd5c1] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Tue May 3 15:18:20 2022 |
---|
| 13 | // Update Count : 4 |
---|
[2bb4a01] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Stmt.hpp" |
---|
| 17 | |
---|
[7edd5c1] | 18 | #include "Copy.hpp" |
---|
[2bb4a01] | 19 | #include "DeclReplacer.hpp" |
---|
[e9b44489] | 20 | #include "Type.hpp" |
---|
[2bb4a01] | 21 | |
---|
[1e97287] | 22 | namespace ast { |
---|
| 23 | |
---|
[2bb4a01] | 24 | // --- CompoundStmt |
---|
[7edd5c1] | 25 | CompoundStmt::CompoundStmt( const CompoundStmt& other ) : Stmt(other), kids() { |
---|
| 26 | // Statements can have weak references to them, if that happens inserting |
---|
| 27 | // the original node into the new list will put the original node in a |
---|
| 28 | // bad state, where it cannot be mutated. To avoid this, just perform an |
---|
| 29 | // additional shallow copy on the statement. |
---|
| 30 | for ( const Stmt * kid : other.kids ) { |
---|
| 31 | if ( kid->isReferenced() ) { |
---|
| 32 | kids.emplace_back( ast::shallowCopy( kid ) ); |
---|
| 33 | } else { |
---|
| 34 | kids.emplace_back( kid ); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
[e9b44489] | 38 | // when cloning a compound statement, we may end up cloning declarations which |
---|
| 39 | // are referred to by VariableExprs throughout the block. Cloning a VariableExpr |
---|
| 40 | // does a shallow copy, so the VariableExpr will end up pointing to the original |
---|
| 41 | // declaration. If the original declaration is deleted, e.g. because the original |
---|
| 42 | // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case, |
---|
| 43 | // find all DeclarationWithType nodes (since a VariableExpr must point to a |
---|
| 44 | // DeclarationWithType) in the original CompoundStmt and map them to the cloned |
---|
| 45 | // node in the new CompoundStmt ('this'), then replace the Declarations referred to |
---|
| 46 | // by each VariableExpr according to the constructed map. Note that only the declarations |
---|
| 47 | // in the current level are collected into the map, because child CompoundStmts will |
---|
| 48 | // recursively execute this routine. There may be more efficient ways of doing |
---|
| 49 | // this. |
---|
| 50 | DeclReplacer::DeclMap declMap; |
---|
| 51 | auto origit = other.kids.begin(); |
---|
| 52 | for ( const Stmt * s : kids ) { |
---|
| 53 | assert( origit != other.kids.end() ); |
---|
| 54 | const Stmt * origStmt = *origit++; |
---|
| 55 | if ( const DeclStmt * declStmt = dynamic_cast< const DeclStmt * >( s ) ) { |
---|
| 56 | const DeclStmt * origDeclStmt = strict_dynamic_cast< const DeclStmt * >( origStmt ); |
---|
| 57 | if ( const DeclWithType * dwt = dynamic_cast< const DeclWithType * > ( declStmt->decl.get() ) ) { |
---|
| 58 | const DeclWithType * origdwt = strict_dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() ); |
---|
| 59 | assert( dwt->name == origdwt->name ); |
---|
| 60 | declMap[ origdwt ] = dwt; |
---|
| 61 | } else assert( ! dynamic_cast< const DeclWithType * > ( origDeclStmt->decl.get() ) ); |
---|
| 62 | } else assert( ! dynamic_cast< const DeclStmt * > ( s ) ); |
---|
| 63 | } |
---|
| 64 | if ( ! declMap.empty() ) { |
---|
| 65 | DeclReplacer::replace( this, declMap ); |
---|
| 66 | } |
---|
[1e97287] | 67 | } |
---|
| 68 | |
---|
| 69 | // --- BranchStmt |
---|
[6180274] | 70 | BranchStmt::BranchStmt( const CodeLocation& loc, Kind kind, Label target, const std::vector<Label>&& labels ) |
---|
| 71 | : Stmt(loc, std::move(labels)), originalTarget(target), target(target), kind(kind) { |
---|
[1e97287] | 72 | // Make sure a syntax error hasn't slipped through. |
---|
| 73 | assert( Goto != kind || !target.empty() ); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | const char * BranchStmt::kindNames[] = { |
---|
| 77 | "Goto", "Break", "Continue", "FallThrough", "FallThroughDefault" |
---|
[87701b6] | 78 | }; |
---|
[2bb4a01] | 79 | |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // Local Variables: // |
---|
| 83 | // tab-width: 4 // |
---|
| 84 | // mode: c++ // |
---|
| 85 | // compile-command: "make install" // |
---|
| 86 | // End: // |
---|