| [0dd3a2f] | 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 | //
 | 
|---|
| [3b58d91] | 7 | // TupleExpr.cc --
 | 
|---|
| [0dd3a2f] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [615a096] | 12 | // Last Modified On : Fri Mar 17 09:42:29 2017
 | 
|---|
 | 13 | // Update Count     : 3
 | 
|---|
| [0dd3a2f] | 14 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
| [ea6332d] | 16 | #include <cassert>              // for assert, safe_dynamic_cast, assertf
 | 
|---|
 | 17 | #include <iterator>             // for next
 | 
|---|
 | 18 | #include <list>                 // for list, _List_iterator
 | 
|---|
 | 19 | #include <ostream>              // for ostream, operator<<, basic_ostream, endl
 | 
|---|
 | 20 | #include <string>               // for operator<<, string, char_traits
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include "Common/utility.h"     // for cloneAll, deleteAll, printAll, toString
 | 
|---|
 | 23 | #include "Declaration.h"        // for ObjectDecl
 | 
|---|
 | 24 | #include "Expression.h"         // for Expression, TupleExpr, TupleIndexExpr
 | 
|---|
 | 25 | #include "SynTree/Label.h"      // for Label, noLabels
 | 
|---|
 | 26 | #include "SynTree/Statement.h"  // for CompoundStmt, DeclStmt, ExprStmt, Sta...
 | 
|---|
 | 27 | #include "Tuples/Tuples.h"      // for makeTupleType
 | 
|---|
 | 28 | #include "Type.h"               // for TupleType, Type
 | 
|---|
| [51b73452] | 29 | 
 | 
|---|
| [907eccb] | 30 | UntypedTupleExpr::UntypedTupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
 | 
|---|
 | 31 | }
 | 
|---|
 | 32 | 
 | 
|---|
 | 33 | UntypedTupleExpr::UntypedTupleExpr( const UntypedTupleExpr &other ) : Expression( other ) {
 | 
|---|
 | 34 |         cloneAll( other.exprs, exprs );
 | 
|---|
 | 35 | }
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | UntypedTupleExpr::~UntypedTupleExpr() {
 | 
|---|
 | 38 |         deleteAll( exprs );
 | 
|---|
 | 39 | }
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | void UntypedTupleExpr::print( std::ostream &os, int indent ) const {
 | 
|---|
 | 42 |         os << "Untyped Tuple:" << std::endl;
 | 
|---|
 | 43 |         printAll( exprs, os, indent+2 );
 | 
|---|
 | 44 |         Expression::print( os, indent );
 | 
|---|
 | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
| [3c13c03] | 47 | TupleExpr::TupleExpr( const std::list< Expression * > & exprs, Expression *_aname ) : Expression( _aname ), exprs( exprs ) {
 | 
|---|
| [907eccb] | 48 |         set_result( Tuples::makeTupleType( exprs ) );
 | 
|---|
| [51b73452] | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
| [0dd3a2f] | 51 | TupleExpr::TupleExpr( const TupleExpr &other ) : Expression( other ) {
 | 
|---|
 | 52 |         cloneAll( other.exprs, exprs );
 | 
|---|
| [51b73452] | 53 | }
 | 
|---|
 | 54 | 
 | 
|---|
| [0dd3a2f] | 55 | TupleExpr::~TupleExpr() {
 | 
|---|
 | 56 |         deleteAll( exprs );
 | 
|---|
| [51b73452] | 57 | }
 | 
|---|
 | 58 | 
 | 
|---|
| [0dd3a2f] | 59 | void TupleExpr::print( std::ostream &os, int indent ) const {
 | 
|---|
| [aa8f9df] | 60 |         os << "Tuple:" << std::endl;
 | 
|---|
| [0dd3a2f] | 61 |         printAll( exprs, os, indent+2 );
 | 
|---|
 | 62 |         Expression::print( os, indent );
 | 
|---|
| [51b73452] | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
| [3c13c03] | 65 | TupleIndexExpr::TupleIndexExpr( Expression * tuple, unsigned int index ) : tuple( tuple ), index( index )  {
 | 
|---|
| [aa8f9df] | 66 |         TupleType * type = safe_dynamic_cast< TupleType * >( tuple->get_result() );
 | 
|---|
| [6c3a988f] | 67 |         assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested index %d in expr %s", type->size(), index, toString( tuple ).c_str() );
 | 
|---|
| [3c13c03] | 68 |         set_result( (*std::next( type->get_types().begin(), index ))->clone() );
 | 
|---|
| [5ccb10d] | 69 |         // like MemberExpr, TupleIndexExpr is always an lvalue
 | 
|---|
 | 70 |         get_result()->set_lvalue( true );
 | 
|---|
| [3b58d91] | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | TupleIndexExpr::TupleIndexExpr( const TupleIndexExpr &other ) : Expression( other ), tuple( other.tuple->clone() ), index( other.index ) {
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | TupleIndexExpr::~TupleIndexExpr() {
 | 
|---|
 | 77 |         delete tuple;
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | void TupleIndexExpr::print( std::ostream &os, int indent ) const {
 | 
|---|
| [aa8f9df] | 81 |         os << "Tuple Index Expression, with tuple:" << std::endl;
 | 
|---|
| [3c13c03] | 82 |         os << std::string( indent+2, ' ' );
 | 
|---|
| [3b58d91] | 83 |         tuple->print( os, indent+2 );
 | 
|---|
 | 84 |         os << std::string( indent+2, ' ' ) << "with index: " << index << std::endl;
 | 
|---|
 | 85 |         Expression::print( os, indent );
 | 
|---|
 | 86 | }
 | 
|---|
 | 87 | 
 | 
|---|
| [d5556a3] | 88 | TupleAssignExpr::TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname ) : Expression( _aname ) {
 | 
|---|
 | 89 |         // convert internally into a StmtExpr which contains the declarations and produces the tuple of the assignments
 | 
|---|
| [65660bd] | 90 |         set_result( Tuples::makeTupleType( assigns ) );
 | 
|---|
| [d5556a3] | 91 |         CompoundStmt * compoundStmt = new CompoundStmt( noLabels );
 | 
|---|
 | 92 |         std::list< Statement * > & stmts = compoundStmt->get_kids();
 | 
|---|
 | 93 |         for ( ObjectDecl * obj : tempDecls ) {
 | 
|---|
 | 94 |                 stmts.push_back( new DeclStmt( noLabels, obj ) );
 | 
|---|
 | 95 |         }
 | 
|---|
 | 96 |         TupleExpr * tupleExpr = new TupleExpr( assigns );
 | 
|---|
 | 97 |         assert( tupleExpr->get_result() );
 | 
|---|
 | 98 |         stmts.push_back( new ExprStmt( noLabels, tupleExpr ) );
 | 
|---|
 | 99 |         stmtExpr = new StmtExpr( compoundStmt );
 | 
|---|
| [6eb8948] | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [23bb1b9] | 102 | TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ) {
 | 
|---|
| [d5556a3] | 103 |         assert( other.stmtExpr );
 | 
|---|
 | 104 |         stmtExpr = other.stmtExpr->clone();
 | 
|---|
| [6eb8948] | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 | TupleAssignExpr::~TupleAssignExpr() {
 | 
|---|
| [d5556a3] | 108 |         delete stmtExpr;
 | 
|---|
| [6eb8948] | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | void TupleAssignExpr::print( std::ostream &os, int indent ) const {
 | 
|---|
| [d5556a3] | 112 |         os << "Tuple Assignment Expression, with stmt expr:" << std::endl;
 | 
|---|
 | 113 |         os << std::string( indent+2, ' ' );
 | 
|---|
 | 114 |         stmtExpr->print( os, indent+4 );
 | 
|---|
| [6eb8948] | 115 |         Expression::print( os, indent );
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | 
 | 
|---|
| [0dd3a2f] | 120 | // Local Variables: //
 | 
|---|
 | 121 | // tab-width: 4 //
 | 
|---|
 | 122 | // mode: c++ //
 | 
|---|
 | 123 | // compile-command: "make install" //
 | 
|---|
 | 124 | // End: //
 | 
|---|