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