| [6eb8948] | 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 | // TupleAssignment.cc --
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // Author           : Rodolfo G. Esteves
 | 
|---|
 | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [c0aa336] | 12 | // Last Modified On : Wed Feb  1 16:40:40 2017
 | 
|---|
 | 13 | // Update Count     : 3
 | 
|---|
| [6eb8948] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include <iterator>
 | 
|---|
 | 17 | #include <iostream>
 | 
|---|
 | 18 | #include <cassert>
 | 
|---|
 | 19 | #include "Tuples.h"
 | 
|---|
| [f006f01] | 20 | #include "GenPoly/DeclMutator.h"
 | 
|---|
 | 21 | #include "SynTree/Mutator.h"
 | 
|---|
| [6eb8948] | 22 | #include "SynTree/Statement.h"
 | 
|---|
| [f006f01] | 23 | #include "SynTree/Declaration.h"
 | 
|---|
 | 24 | #include "SynTree/Type.h"
 | 
|---|
| [3c13c03] | 25 | #include "SynTree/Expression.h"
 | 
|---|
 | 26 | #include "SynTree/Initializer.h"
 | 
|---|
| [f006f01] | 27 | #include "SymTab/Mangler.h"
 | 
|---|
 | 28 | #include "Common/ScopedMap.h"
 | 
|---|
| [1132b62] | 29 | #include "ResolvExpr/typeops.h"
 | 
|---|
| [f0121d7] | 30 | #include "InitTweak/GenInit.h"
 | 
|---|
| [6eb8948] | 31 | 
 | 
|---|
 | 32 | namespace Tuples {
 | 
|---|
| [3c13c03] | 33 |         namespace {
 | 
|---|
| [5f5083e] | 34 |                 class MemberTupleExpander final : public Mutator {
 | 
|---|
| [bf32bb8] | 35 |                 public:
 | 
|---|
 | 36 |                         typedef Mutator Parent;
 | 
|---|
| [5f5083e] | 37 |                         using Parent::mutate;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 |                         virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override;
 | 
|---|
| [bf32bb8] | 40 |                 };
 | 
|---|
 | 41 | 
 | 
|---|
| [5f5083e] | 42 |                 class UniqueExprExpander final : public GenPoly::DeclMutator {
 | 
|---|
| [3c13c03] | 43 |                 public:
 | 
|---|
 | 44 |                         typedef GenPoly::DeclMutator Parent;
 | 
|---|
| [5f5083e] | 45 |                         using Parent::mutate;
 | 
|---|
| [141b786] | 46 | 
 | 
|---|
| [5f5083e] | 47 |                         virtual Expression * mutate( UniqueExpr * unqExpr ) override;
 | 
|---|
| [141b786] | 48 | 
 | 
|---|
 | 49 |                         std::map< int, Expression * > decls; // not vector, because order added may not be increasing order
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |                         ~UniqueExprExpander() {
 | 
|---|
 | 52 |                                 for ( std::pair<const int, Expression *> & p : decls ) {
 | 
|---|
 | 53 |                                         delete p.second;
 | 
|---|
 | 54 |                                 }
 | 
|---|
 | 55 |                         }
 | 
|---|
| [3c13c03] | 56 |                 };
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |                 class TupleAssignExpander : public Mutator {
 | 
|---|
 | 59 |                 public:
 | 
|---|
 | 60 |                         typedef Mutator Parent;
 | 
|---|
| [5f5083e] | 61 |                         using Parent::mutate;
 | 
|---|
 | 62 | 
 | 
|---|
| [3c13c03] | 63 |                         virtual Expression * mutate( TupleAssignExpr * tupleExpr );
 | 
|---|
 | 64 |                 };
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |                 class TupleTypeReplacer : public GenPoly::DeclMutator {
 | 
|---|
 | 67 |                   public:
 | 
|---|
 | 68 |                         typedef GenPoly::DeclMutator Parent;
 | 
|---|
| [5f5083e] | 69 |                         using Parent::mutate;
 | 
|---|
| [3c13c03] | 70 | 
 | 
|---|
| [5f5083e] | 71 |                         virtual Type * mutate( TupleType * tupleType ) override;
 | 
|---|
| [3c13c03] | 72 | 
 | 
|---|
| [5f5083e] | 73 |                         virtual CompoundStmt * mutate( CompoundStmt * stmt ) override {
 | 
|---|
| [3c13c03] | 74 |                                 typeMap.beginScope();
 | 
|---|
 | 75 |                                 stmt = Parent::mutate( stmt );
 | 
|---|
 | 76 |                                 typeMap.endScope();
 | 
|---|
 | 77 |                                 return stmt;
 | 
|---|
 | 78 |                         }
 | 
|---|
 | 79 |                   private:
 | 
|---|
 | 80 |                         ScopedMap< std::string, StructDecl * > typeMap;
 | 
|---|
 | 81 |                 };
 | 
|---|
 | 82 | 
 | 
|---|
| [5f5083e] | 83 |                 class TupleIndexExpander final : public Mutator {
 | 
|---|
| [3c13c03] | 84 |                 public:
 | 
|---|
 | 85 |                         typedef Mutator Parent;
 | 
|---|
| [5f5083e] | 86 |                         using Parent::mutate;
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |                         virtual Expression * mutate( TupleIndexExpr * tupleExpr ) override;
 | 
|---|
| [3c13c03] | 89 |                 };
 | 
|---|
 | 90 | 
 | 
|---|
| [5f5083e] | 91 |                 class TupleExprExpander final : public Mutator {
 | 
|---|
| [3c13c03] | 92 |                 public:
 | 
|---|
 | 93 |                         typedef Mutator Parent;
 | 
|---|
| [5f5083e] | 94 |                         using Parent::mutate;
 | 
|---|
| [d9fa60a] | 95 | 
 | 
|---|
| [5f5083e] | 96 |                         virtual Expression * mutate( TupleExpr * tupleExpr ) override;
 | 
|---|
| [3c13c03] | 97 |                 };
 | 
|---|
 | 98 |         }
 | 
|---|
| [f006f01] | 99 | 
 | 
|---|
| [bf32bb8] | 100 |         void expandMemberTuples( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 101 |                 MemberTupleExpander expander;
 | 
|---|
 | 102 |                 mutateAll( translationUnit, expander );
 | 
|---|
 | 103 |         }
 | 
|---|
 | 104 | 
 | 
|---|
| [aefcc3b] | 105 |         void expandUniqueExpr( std::list< Declaration * > & translationUnit ) {
 | 
|---|
| [3c13c03] | 106 |                 UniqueExprExpander unqExpander;
 | 
|---|
 | 107 |                 unqExpander.mutateDeclarationList( translationUnit );
 | 
|---|
| [aefcc3b] | 108 |         }
 | 
|---|
| [3c13c03] | 109 | 
 | 
|---|
| [aefcc3b] | 110 |         void expandTuples( std::list< Declaration * > & translationUnit ) {
 | 
|---|
| [3c13c03] | 111 |                 TupleAssignExpander assnExpander;
 | 
|---|
 | 112 |                 mutateAll( translationUnit, assnExpander );
 | 
|---|
| [f006f01] | 113 | 
 | 
|---|
 | 114 |                 TupleTypeReplacer replacer;
 | 
|---|
 | 115 |                 replacer.mutateDeclarationList( translationUnit );
 | 
|---|
| [3c13c03] | 116 | 
 | 
|---|
 | 117 |                 TupleIndexExpander idxExpander;
 | 
|---|
 | 118 |                 mutateAll( translationUnit, idxExpander );
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |                 TupleExprExpander exprExpander;
 | 
|---|
 | 121 |                 mutateAll( translationUnit, exprExpander );
 | 
|---|
 | 122 |         }
 | 
|---|
 | 123 | 
 | 
|---|
| [bf32bb8] | 124 |         namespace {
 | 
|---|
 | 125 |                 /// given a expression representing the member and an expression representing the aggregate,
 | 
|---|
 | 126 |                 /// reconstructs a flattened UntypedMemberExpr with the right precedence
 | 
|---|
| [141b786] | 127 |                 Expression * reconstructMemberExpr( Expression * member, Expression * aggr ) {
 | 
|---|
| [bf32bb8] | 128 |                         if ( UntypedMemberExpr * memberExpr = dynamic_cast< UntypedMemberExpr * >( member ) ) {
 | 
|---|
 | 129 |                                 // construct a new UntypedMemberExpr with the correct structure , and recursively
 | 
|---|
 | 130 |                                 // expand that member expression.
 | 
|---|
 | 131 |                                 MemberTupleExpander expander;
 | 
|---|
 | 132 |                                 UntypedMemberExpr * newMemberExpr = new UntypedMemberExpr( memberExpr->get_member(), new UntypedMemberExpr( memberExpr->get_aggregate(), aggr->clone() ) );
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |                                 memberExpr->set_member(nullptr);
 | 
|---|
 | 135 |                                 memberExpr->set_aggregate(nullptr);
 | 
|---|
 | 136 |                                 delete memberExpr;
 | 
|---|
 | 137 |                                 return newMemberExpr->acceptMutator( expander );
 | 
|---|
 | 138 |                         } else {
 | 
|---|
 | 139 |                                 // not a member expression, so there is nothing to do but attach and return
 | 
|---|
 | 140 |                                 return new UntypedMemberExpr( member, aggr->clone() );
 | 
|---|
 | 141 |                         }
 | 
|---|
 | 142 |                 }
 | 
|---|
 | 143 |         }
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |         Expression * MemberTupleExpander::mutate( UntypedMemberExpr * memberExpr ) {
 | 
|---|
| [907eccb] | 146 |                 if ( UntypedTupleExpr * tupleExpr = dynamic_cast< UntypedTupleExpr * > ( memberExpr->get_member() ) ) {
 | 
|---|
| [141b786] | 147 |                         Expression * aggr = memberExpr->get_aggregate()->clone()->acceptMutator( *this );
 | 
|---|
 | 148 |                         // aggregate expressions which might be impure must be wrapped in unique expressions
 | 
|---|
 | 149 |                         // xxx - if there's a member-tuple expression nested in the aggregate, this currently generates the wrong code if a UniqueExpr is not used, and it's purely an optimization to remove the UniqueExpr
 | 
|---|
 | 150 |                         // if ( Tuples::maybeImpure( memberExpr->get_aggregate() ) ) aggr = new UniqueExpr( aggr );
 | 
|---|
 | 151 |                         aggr = new UniqueExpr( aggr );
 | 
|---|
| [bf32bb8] | 152 |                         for ( Expression *& expr : tupleExpr->get_exprs() ) {
 | 
|---|
| [141b786] | 153 |                                 expr = reconstructMemberExpr( expr, aggr );
 | 
|---|
| [bf32bb8] | 154 |                         }
 | 
|---|
| [141b786] | 155 |                         delete aggr;
 | 
|---|
| [bf32bb8] | 156 |                         return tupleExpr;
 | 
|---|
 | 157 |                 } else {
 | 
|---|
| [f0121d7] | 158 |                         // there may be a tuple expr buried in the aggregate
 | 
|---|
 | 159 |                         // xxx - this is a memory leak
 | 
|---|
 | 160 |                         return new UntypedMemberExpr( memberExpr->get_member()->clone(), memberExpr->get_aggregate()->acceptMutator( *this ) );
 | 
|---|
| [bf32bb8] | 161 |                 }
 | 
|---|
 | 162 |         }
 | 
|---|
 | 163 | 
 | 
|---|
| [3c13c03] | 164 |         Expression * UniqueExprExpander::mutate( UniqueExpr * unqExpr ) {
 | 
|---|
 | 165 |                 unqExpr = safe_dynamic_cast< UniqueExpr * > ( Parent::mutate( unqExpr ) );
 | 
|---|
| [141b786] | 166 |                 const int id = unqExpr->get_id();
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |                 // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID,
 | 
|---|
 | 169 |                 // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable.
 | 
|---|
 | 170 |                 if ( ! decls.count( id ) ) {
 | 
|---|
 | 171 |                         Expression * assignUnq;
 | 
|---|
 | 172 |                         Expression * var = unqExpr->get_var();
 | 
|---|
 | 173 |                         if ( unqExpr->get_object() ) {
 | 
|---|
 | 174 |                                 // an object was generated to represent this unique expression -- it should be added to the list of declarations now
 | 
|---|
 | 175 |                                 addDeclaration( unqExpr->get_object() );
 | 
|---|
 | 176 |                                 unqExpr->set_object( nullptr );
 | 
|---|
 | 177 |                                 // steal the expr from the unqExpr
 | 
|---|
 | 178 |                                 assignUnq = UntypedExpr::createAssign( unqExpr->get_var()->clone(), unqExpr->get_expr() );
 | 
|---|
 | 179 |                                 unqExpr->set_expr( nullptr );
 | 
|---|
 | 180 |                         } else {
 | 
|---|
 | 181 |                                 // steal the already generated assignment to var from the unqExpr - this has been generated by FixInit
 | 
|---|
 | 182 |                                 Expression * expr = unqExpr->get_expr();
 | 
|---|
 | 183 |                                 CommaExpr * commaExpr = safe_dynamic_cast< CommaExpr * >( expr );
 | 
|---|
 | 184 |                                 assignUnq = commaExpr->get_arg1();
 | 
|---|
 | 185 |                                 commaExpr->set_arg1( nullptr );
 | 
|---|
 | 186 |                         }
 | 
|---|
 | 187 |                         BasicType * boolType = new BasicType( Type::Qualifiers(), BasicType::Bool );
 | 
|---|
 | 188 |                         ObjectDecl * finished = new ObjectDecl( toString( "_unq_expr_finished_", id ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, nullptr, new BasicType( Type::Qualifiers(), BasicType::Bool ), new SingleInit( new ConstantExpr( Constant( boolType->clone(), "0" ) ), noDesignators ) );
 | 
|---|
 | 189 |                         addDeclaration( finished );
 | 
|---|
 | 190 |                         // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N))
 | 
|---|
 | 191 |                         // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code.
 | 
|---|
 | 192 |                         Expression * assignFinished = UntypedExpr::createAssign( new VariableExpr(finished), new ConstantExpr( Constant( boolType->clone(), "1" ) ) );
 | 
|---|
 | 193 |                         ConditionalExpr * condExpr = new ConditionalExpr( new VariableExpr( finished ), var->clone(),
 | 
|---|
 | 194 |                                 new CommaExpr( new CommaExpr( assignUnq, assignFinished ), var->clone() ) );
 | 
|---|
 | 195 |                         condExpr->set_result( var->get_result()->clone() );
 | 
|---|
| [d5556a3] | 196 |                         condExpr->set_env( maybeClone( unqExpr->get_env() ) );
 | 
|---|
| [141b786] | 197 |                         decls[id] = condExpr;
 | 
|---|
| [3c13c03] | 198 |                 }
 | 
|---|
| [141b786] | 199 |                 delete unqExpr;
 | 
|---|
 | 200 |                 return decls[id]->clone();
 | 
|---|
| [6eb8948] | 201 |         }
 | 
|---|
 | 202 | 
 | 
|---|
| [3c13c03] | 203 |         Expression * TupleAssignExpander::mutate( TupleAssignExpr * assnExpr ) {
 | 
|---|
| [141b786] | 204 |                 assnExpr = safe_dynamic_cast< TupleAssignExpr * >( Parent::mutate( assnExpr ) );
 | 
|---|
| [d5556a3] | 205 |                 StmtExpr * ret = assnExpr->get_stmtExpr();
 | 
|---|
 | 206 |                 assnExpr->set_stmtExpr( nullptr );
 | 
|---|
 | 207 |                 // move env to StmtExpr
 | 
|---|
 | 208 |                 ret->set_env( assnExpr->get_env() );
 | 
|---|
 | 209 |                 assnExpr->set_env( nullptr );
 | 
|---|
| [3c13c03] | 210 |                 delete assnExpr;
 | 
|---|
| [d5556a3] | 211 |                 return ret;
 | 
|---|
| [6eb8948] | 212 |         }
 | 
|---|
 | 213 | 
 | 
|---|
| [f006f01] | 214 |         Type * TupleTypeReplacer::mutate( TupleType * tupleType ) {
 | 
|---|
 | 215 |                 std::string mangleName = SymTab::Mangler::mangleType( tupleType );
 | 
|---|
| [d9fa60a] | 216 |                 tupleType = safe_dynamic_cast< TupleType * > ( Parent::mutate( tupleType ) );
 | 
|---|
| [f006f01] | 217 |                 if ( ! typeMap.count( mangleName ) ) {
 | 
|---|
 | 218 |                         // generate struct type to replace tuple type
 | 
|---|
| [d5556a3] | 219 |                         // xxx - should fix this to only generate one tuple struct for each number of type parameters
 | 
|---|
| [f006f01] | 220 |                         StructDecl * decl = new StructDecl( "_tuple_type_" + mangleName );
 | 
|---|
 | 221 |                         decl->set_body( true );
 | 
|---|
| [d9fa60a] | 222 |                         for ( size_t i = 0; i < tupleType->size(); ++i ) {
 | 
|---|
 | 223 |                                 TypeDecl * tyParam = new TypeDecl( toString("tuple_param_", i), DeclarationNode::NoStorageClass, nullptr, TypeDecl::Any );
 | 
|---|
 | 224 |                                 decl->get_members().push_back( new ObjectDecl( toString("field_", i), DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, new TypeInstType( Type::Qualifiers(), tyParam->get_name(), tyParam ), nullptr ) );
 | 
|---|
 | 225 |                                 decl->get_parameters().push_back( tyParam );
 | 
|---|
| [f006f01] | 226 |                         }
 | 
|---|
| [4c8621ac] | 227 |                         if ( tupleType->size() == 0 ) {
 | 
|---|
 | 228 |                                 // empty structs are not standard C. Add a dummy field to empty tuples to silence warnings when a compound literal Tuple0 is created.
 | 
|---|
 | 229 |                                 decl->get_members().push_back( new ObjectDecl( "dummy", DeclarationNode::NoStorageClass, LinkageSpec::C, nullptr, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), nullptr ) );
 | 
|---|
 | 230 |                         }
 | 
|---|
| [f006f01] | 231 |                         typeMap[mangleName] = decl;
 | 
|---|
 | 232 |                         addDeclaration( decl );
 | 
|---|
 | 233 |                 }
 | 
|---|
| [d9fa60a] | 234 |                 Type::Qualifiers qualifiers = tupleType->get_qualifiers();
 | 
|---|
 | 235 | 
 | 
|---|
 | 236 |                 StructDecl * decl = typeMap[mangleName];
 | 
|---|
 | 237 |                 StructInstType * newType = new StructInstType( qualifiers, decl );
 | 
|---|
 | 238 |                 for ( Type * t : *tupleType ) {
 | 
|---|
 | 239 |                         newType->get_parameters().push_back( new TypeExpr( t->clone() ) );
 | 
|---|
 | 240 |                 }
 | 
|---|
 | 241 |                 delete tupleType;
 | 
|---|
 | 242 |                 return newType;
 | 
|---|
| [f006f01] | 243 |         }
 | 
|---|
 | 244 | 
 | 
|---|
| [3c13c03] | 245 |         Expression * TupleIndexExpander::mutate( TupleIndexExpr * tupleExpr ) {
 | 
|---|
 | 246 |                 Expression * tuple = maybeMutate( tupleExpr->get_tuple(), *this );
 | 
|---|
 | 247 |                 assert( tuple );
 | 
|---|
 | 248 |                 tupleExpr->set_tuple( nullptr );
 | 
|---|
 | 249 |                 unsigned int idx = tupleExpr->get_index();
 | 
|---|
| [d5556a3] | 250 |                 TypeSubstitution * env = tupleExpr->get_env();
 | 
|---|
 | 251 |                 tupleExpr->set_env( nullptr );
 | 
|---|
| [3c13c03] | 252 |                 delete tupleExpr;
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 |                 StructInstType * type = safe_dynamic_cast< StructInstType * >( tuple->get_result() );
 | 
|---|
 | 255 |                 StructDecl * structDecl = type->get_baseStruct();
 | 
|---|
 | 256 |                 assert( structDecl->get_members().size() > idx );
 | 
|---|
 | 257 |                 Declaration * member = *std::next(structDecl->get_members().begin(), idx);
 | 
|---|
| [d5556a3] | 258 |                 MemberExpr * memExpr = new MemberExpr( safe_dynamic_cast< DeclarationWithType * >( member ), tuple );
 | 
|---|
 | 259 |                 memExpr->set_env( env );
 | 
|---|
 | 260 |                 return memExpr;
 | 
|---|
| [3c13c03] | 261 |         }
 | 
|---|
 | 262 | 
 | 
|---|
| [d5556a3] | 263 |         Expression * replaceTupleExpr( Type * result, const std::list< Expression * > & exprs, TypeSubstitution * env ) {
 | 
|---|
| [65660bd] | 264 |                 if ( result->isVoid() ) {
 | 
|---|
 | 265 |                         // void result - don't need to produce a value for cascading - just output a chain of comma exprs
 | 
|---|
 | 266 |                         assert( ! exprs.empty() );
 | 
|---|
 | 267 |                         std::list< Expression * >::const_iterator iter = exprs.begin();
 | 
|---|
| [d5556a3] | 268 |                         Expression * expr = new CastExpr( *iter++ );
 | 
|---|
| [65660bd] | 269 |                         for ( ; iter != exprs.end(); ++iter ) {
 | 
|---|
| [d5556a3] | 270 |                                 expr = new CommaExpr( expr, new CastExpr( *iter ) );
 | 
|---|
| [65660bd] | 271 |                         }
 | 
|---|
| [d5556a3] | 272 |                         expr->set_env( env );
 | 
|---|
| [65660bd] | 273 |                         return expr;
 | 
|---|
 | 274 |                 } else {
 | 
|---|
 | 275 |                         // typed tuple expression - produce a compound literal which performs each of the expressions
 | 
|---|
 | 276 |                         // as a distinct part of its initializer - the produced compound literal may be used as part of
 | 
|---|
 | 277 |                         // another expression
 | 
|---|
 | 278 |                         std::list< Initializer * > inits;
 | 
|---|
 | 279 |                         for ( Expression * expr : exprs ) {
 | 
|---|
 | 280 |                                 inits.push_back( new SingleInit( expr ) );
 | 
|---|
 | 281 |                         }
 | 
|---|
| [d5556a3] | 282 |                         Expression * expr = new CompoundLiteralExpr( result, new ListInit( inits ) );
 | 
|---|
 | 283 |                         expr->set_env( env );
 | 
|---|
 | 284 |                         return expr;
 | 
|---|
| [3c13c03] | 285 |                 }
 | 
|---|
 | 286 |         }
 | 
|---|
 | 287 | 
 | 
|---|
| [65660bd] | 288 |         Expression * TupleExprExpander::mutate( TupleExpr * tupleExpr ) {
 | 
|---|
| [bf32bb8] | 289 |                 // recursively expand sub-tuple-expressions
 | 
|---|
 | 290 |                 tupleExpr = safe_dynamic_cast<TupleExpr *>(Parent::mutate(tupleExpr));
 | 
|---|
| [65660bd] | 291 |                 Type * result = tupleExpr->get_result();
 | 
|---|
 | 292 |                 std::list< Expression * > exprs = tupleExpr->get_exprs();
 | 
|---|
 | 293 |                 assert( result );
 | 
|---|
| [d5556a3] | 294 |                 TypeSubstitution * env = tupleExpr->get_env();
 | 
|---|
| [65660bd] | 295 | 
 | 
|---|
| [bf32bb8] | 296 |                 // remove data from shell and delete it
 | 
|---|
| [65660bd] | 297 |                 tupleExpr->set_result( nullptr );
 | 
|---|
 | 298 |                 tupleExpr->get_exprs().clear();
 | 
|---|
| [d5556a3] | 299 |                 tupleExpr->set_env( nullptr );
 | 
|---|
| [65660bd] | 300 |                 delete tupleExpr;
 | 
|---|
 | 301 | 
 | 
|---|
| [d5556a3] | 302 |                 return replaceTupleExpr( result, exprs, env );
 | 
|---|
| [65660bd] | 303 |         }
 | 
|---|
 | 304 | 
 | 
|---|
 | 305 |         Type * makeTupleType( const std::list< Expression * > & exprs ) {
 | 
|---|
 | 306 |                 // produce the TupleType which aggregates the types of the exprs
 | 
|---|
| [c0aa336] | 307 |                 TupleType *tupleType = new TupleType( Type::Qualifiers(true, true, true, true, true) );
 | 
|---|
| [3c13c03] | 308 |                 Type::Qualifiers &qualifiers = tupleType->get_qualifiers();
 | 
|---|
 | 309 |                 for ( Expression * expr : exprs ) {
 | 
|---|
 | 310 |                         assert( expr->get_result() );
 | 
|---|
| [65660bd] | 311 |                         if ( expr->get_result()->isVoid() ) {
 | 
|---|
 | 312 |                                 // if the type of any expr is void, the type of the entire tuple is void
 | 
|---|
 | 313 |                                 delete tupleType;
 | 
|---|
 | 314 |                                 return new VoidType( Type::Qualifiers() );
 | 
|---|
 | 315 |                         }
 | 
|---|
| [3c13c03] | 316 |                         Type * type = expr->get_result()->clone();
 | 
|---|
 | 317 |                         tupleType->get_types().push_back( type );
 | 
|---|
| [65660bd] | 318 |                         // the qualifiers on the tuple type are the qualifiers that exist on all component types
 | 
|---|
| [3c13c03] | 319 |                         qualifiers &= type->get_qualifiers();
 | 
|---|
 | 320 |                 } // for
 | 
|---|
| [907eccb] | 321 |                 if ( exprs.empty() ) qualifiers = Type::Qualifiers();
 | 
|---|
| [3c13c03] | 322 |                 return tupleType;
 | 
|---|
 | 323 |         }
 | 
|---|
| [65660bd] | 324 | 
 | 
|---|
| [8bf784a] | 325 |         TypeInstType * isTtype( Type * type ) {
 | 
|---|
 | 326 |                 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( type ) ) {
 | 
|---|
 | 327 |                         if ( inst->get_baseType()->get_kind() == TypeDecl::Ttype ) {
 | 
|---|
 | 328 |                                 return inst;
 | 
|---|
 | 329 |                         }
 | 
|---|
 | 330 |                 }
 | 
|---|
 | 331 |                 return nullptr;
 | 
|---|
 | 332 |         }
 | 
|---|
 | 333 | 
 | 
|---|
| [65660bd] | 334 |         namespace {
 | 
|---|
 | 335 |                 /// determines if impurity (read: side-effects) may exist in a piece of code. Currently gives a very crude approximation, wherein any function call expression means the code may be impure
 | 
|---|
 | 336 |                 class ImpurityDetector : public Visitor {
 | 
|---|
 | 337 |                 public:
 | 
|---|
 | 338 |                         typedef Visitor Parent;
 | 
|---|
 | 339 |                         virtual void visit( ApplicationExpr * appExpr ) { maybeImpure = true;   }
 | 
|---|
 | 340 |                         virtual void visit( UntypedExpr * untypedExpr ) { maybeImpure = true; }
 | 
|---|
 | 341 |                         bool maybeImpure = false;
 | 
|---|
 | 342 |                 };
 | 
|---|
 | 343 |         } // namespace
 | 
|---|
 | 344 | 
 | 
|---|
 | 345 |         bool maybeImpure( Expression * expr ) {
 | 
|---|
 | 346 |                 ImpurityDetector detector;
 | 
|---|
 | 347 |                 expr->accept( detector );
 | 
|---|
 | 348 |                 return detector.maybeImpure;
 | 
|---|
 | 349 |         }
 | 
|---|
| [6eb8948] | 350 | } // namespace Tuples
 | 
|---|
 | 351 | 
 | 
|---|
 | 352 | // Local Variables: //
 | 
|---|
 | 353 | // tab-width: 4 //
 | 
|---|
 | 354 | // mode: c++ //
 | 
|---|
 | 355 | // compile-command: "make install" //
 | 
|---|
 | 356 | // End: //
 | 
|---|