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 | // TupleExpr.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed Aug 14 14:34:00 2019 |
---|
13 | // Update Count : 5 |
---|
14 | // |
---|
15 | |
---|
16 | #include <cassert> // for assert, strict_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 |
---|
26 | #include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt, Sta... |
---|
27 | #include "Tuples/Tuples.h" // for makeTupleType |
---|
28 | #include "Type.h" // for TupleType, Type |
---|
29 | |
---|
30 | UntypedTupleExpr::UntypedTupleExpr( const std::list< Expression * > & exprs ) : Expression(), 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, Indenter indent ) const { |
---|
42 | os << "Untyped Tuple:" << std::endl; |
---|
43 | printAll( exprs, os, indent+1 ); |
---|
44 | Expression::print( os, indent ); |
---|
45 | } |
---|
46 | |
---|
47 | TupleExpr::TupleExpr( const std::list< Expression * > & exprs ) : Expression(), exprs( exprs ) { |
---|
48 | set_result( Tuples::makeTupleType( exprs ) ); |
---|
49 | } |
---|
50 | |
---|
51 | TupleExpr::TupleExpr( const TupleExpr &other ) : Expression( other ) { |
---|
52 | cloneAll( other.exprs, exprs ); |
---|
53 | } |
---|
54 | |
---|
55 | TupleExpr::~TupleExpr() { |
---|
56 | deleteAll( exprs ); |
---|
57 | } |
---|
58 | |
---|
59 | bool TupleExpr::get_lvalue() const { |
---|
60 | return result->get_lvalue(); |
---|
61 | } |
---|
62 | |
---|
63 | void TupleExpr::print( std::ostream &os, Indenter indent ) const { |
---|
64 | os << "Tuple:" << std::endl; |
---|
65 | printAll( exprs, os, indent+1 ); |
---|
66 | Expression::print( os, indent ); |
---|
67 | } |
---|
68 | |
---|
69 | TupleIndexExpr::TupleIndexExpr( Expression * tuple, unsigned int index ) : tuple( tuple ), index( index ) { |
---|
70 | TupleType * type = strict_dynamic_cast< TupleType * >( tuple->get_result() ); |
---|
71 | 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() ); |
---|
72 | set_result( (*std::next( type->get_types().begin(), index ))->clone() ); |
---|
73 | // like MemberExpr, TupleIndexExpr is always an lvalue |
---|
74 | get_result()->set_lvalue( true ); |
---|
75 | } |
---|
76 | |
---|
77 | TupleIndexExpr::TupleIndexExpr( const TupleIndexExpr &other ) : Expression( other ), tuple( other.tuple->clone() ), index( other.index ) { |
---|
78 | } |
---|
79 | |
---|
80 | TupleIndexExpr::~TupleIndexExpr() { |
---|
81 | delete tuple; |
---|
82 | } |
---|
83 | |
---|
84 | bool TupleIndexExpr::get_lvalue() const { |
---|
85 | assert( result->get_lvalue() ); |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | void TupleIndexExpr::print( std::ostream &os, Indenter indent ) const { |
---|
90 | os << "Tuple Index Expression, with tuple:" << std::endl; |
---|
91 | os << indent+1; |
---|
92 | tuple->print( os, indent+1 ); |
---|
93 | os << indent+1 << "with index: " << index << std::endl; |
---|
94 | Expression::print( os, indent ); |
---|
95 | } |
---|
96 | |
---|
97 | TupleAssignExpr::TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls ) : Expression() { |
---|
98 | // convert internally into a StmtExpr which contains the declarations and produces the tuple of the assignments |
---|
99 | set_result( Tuples::makeTupleType( assigns ) ); |
---|
100 | CompoundStmt * compoundStmt = new CompoundStmt(); |
---|
101 | std::list< Statement * > & stmts = compoundStmt->get_kids(); |
---|
102 | for ( ObjectDecl * obj : tempDecls ) { |
---|
103 | stmts.push_back( new DeclStmt( obj ) ); |
---|
104 | } |
---|
105 | TupleExpr * tupleExpr = new TupleExpr( assigns ); |
---|
106 | assert( tupleExpr->get_result() ); |
---|
107 | stmts.push_back( new ExprStmt( tupleExpr ) ); |
---|
108 | stmtExpr = new StmtExpr( compoundStmt ); |
---|
109 | } |
---|
110 | |
---|
111 | TupleAssignExpr::TupleAssignExpr( const TupleAssignExpr &other ) : Expression( other ) { |
---|
112 | assert( other.stmtExpr ); |
---|
113 | stmtExpr = other.stmtExpr->clone(); |
---|
114 | } |
---|
115 | |
---|
116 | TupleAssignExpr::TupleAssignExpr( |
---|
117 | StmtExpr * s ) |
---|
118 | : Expression(), stmtExpr(s) { |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | TupleAssignExpr::~TupleAssignExpr() { |
---|
123 | delete stmtExpr; |
---|
124 | } |
---|
125 | |
---|
126 | void TupleAssignExpr::print( std::ostream &os, Indenter indent ) const { |
---|
127 | os << "Tuple Assignment Expression, with stmt expr:" << std::endl; |
---|
128 | os << indent+1; |
---|
129 | stmtExpr->print( os, indent+1 ); |
---|
130 | Expression::print( os, indent ); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | // Local Variables: // |
---|
136 | // tab-width: 4 // |
---|
137 | // mode: c++ // |
---|
138 | // compile-command: "make install" // |
---|
139 | // End: // |
---|