1 | #include "FunctionChecker.h"
|
---|
2 | #include "FunctionFixer.h"
|
---|
3 | #include "SemanticError.h"
|
---|
4 |
|
---|
5 | #include <algorithm>
|
---|
6 | #include <iostream>
|
---|
7 | #include <cassert>
|
---|
8 |
|
---|
9 | namespace Tuples {
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 | void checkFunctions( std::list< Declaration * > translationUnit )
|
---|
13 | {
|
---|
14 | FunctionChecker fchk(true);
|
---|
15 | TupleDistrib td;
|
---|
16 | FunctionFixer ff;
|
---|
17 |
|
---|
18 | mutateAll( translationUnit , fchk );
|
---|
19 | mutateAll( translationUnit , ff );
|
---|
20 | mutateAll( translationUnit , td );
|
---|
21 | return;
|
---|
22 | }
|
---|
23 |
|
---|
24 | FunctionChecker::FunctionChecker( bool _topLevel, UniqueName *_nameGen ) : topLevel( _topLevel ), nameGen( _nameGen ) {
|
---|
25 | if( topLevel) {
|
---|
26 | assert( !nameGen );
|
---|
27 | nameGen = new UniqueName("_MVR_");
|
---|
28 | } else
|
---|
29 | assert( nameGen );
|
---|
30 | }
|
---|
31 |
|
---|
32 | FunctionChecker::~FunctionChecker() {
|
---|
33 | if( topLevel) {
|
---|
34 | delete nameGen;
|
---|
35 | nameGen = 0;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | Statement* FunctionChecker::mutate(ExprStmt *exprStmt) {
|
---|
40 | exprStmt->set_expr( maybeMutate( exprStmt->get_expr(), *this ) );
|
---|
41 | if ( !tempExpr.empty() ) {
|
---|
42 | assert ( !temporaries.empty() );
|
---|
43 | CompoundStmt *newBlock = new CompoundStmt( std::list< Label >() );
|
---|
44 | // declarations
|
---|
45 | for( std::list< ObjectDecl *>::iterator d = temporaries.begin(); d != temporaries.end(); ++d )
|
---|
46 | newBlock->get_kids().push_back( new DeclStmt( std::list<Label>(), *d ) );
|
---|
47 | // new expression statements
|
---|
48 | for( std::list< Expression *>::iterator e = tempExpr.begin(); e != tempExpr.end(); ++e )
|
---|
49 | newBlock->get_kids().push_back( new ExprStmt( std::list<Label>(), *e ) );
|
---|
50 |
|
---|
51 | newBlock->get_kids().push_back( exprStmt );
|
---|
52 | return newBlock;
|
---|
53 | } else
|
---|
54 | return exprStmt;
|
---|
55 | }
|
---|
56 |
|
---|
57 | Expression* FunctionChecker::mutate(ApplicationExpr *applicationExpr) {
|
---|
58 | if ( topLevel )
|
---|
59 | ; // In top level of Functionchecker
|
---|
60 |
|
---|
61 | if ( applicationExpr->get_results().size() > 1 ) {
|
---|
62 | for ( std::list< Type *>::iterator res = applicationExpr->get_results().begin(); res != applicationExpr->get_results().end(); res++ )
|
---|
63 | temporaries.push_back( new ObjectDecl(nameGen->newName(),Declaration::Auto,LinkageSpec::AutoGen, 0, (*res)->clone(), 0 ) );
|
---|
64 |
|
---|
65 | assert( ! temporaries.empty() );
|
---|
66 | }
|
---|
67 |
|
---|
68 | applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) );
|
---|
69 |
|
---|
70 | std::list< Expression * > newArgs;
|
---|
71 | for( std::list< Expression *>::iterator e = applicationExpr->get_args().begin(); e != applicationExpr->get_args().end(); ++e ) {
|
---|
72 | FunctionChecker rec( false, nameGen );
|
---|
73 | (*e)->acceptMutator( rec );
|
---|
74 |
|
---|
75 | if ( !rec.temporaries.empty() ) {
|
---|
76 | TupleExpr *lhs = new TupleExpr;
|
---|
77 | std::list< Expression * > &tmem = lhs->get_exprs();
|
---|
78 | for( std::list<ObjectDecl *>::iterator d = rec.temporaries.begin(); d != rec.temporaries.end(); ++d ) {
|
---|
79 | tmem.push_back( new VariableExpr( *d ) );
|
---|
80 | newArgs.push_back( new VariableExpr( *d ) );
|
---|
81 | }
|
---|
82 |
|
---|
83 | // construct tuple assignment
|
---|
84 | std::list<Expression *> args;
|
---|
85 | args.push_back( new AddressExpr(lhs) );
|
---|
86 | args.push_back( *e );
|
---|
87 | tempExpr.push_back( new UntypedExpr( new NameExpr("?=?"), args ) );
|
---|
88 |
|
---|
89 | temporaries.splice( temporaries.end(), rec.temporaries );
|
---|
90 | } else
|
---|
91 | newArgs.push_back( *e );
|
---|
92 | // percolate to recursive calls
|
---|
93 | }
|
---|
94 |
|
---|
95 | applicationExpr->get_args().clear();
|
---|
96 | std::copy( newArgs.begin(), newArgs.end(), back_inserter(applicationExpr->get_args()) );
|
---|
97 |
|
---|
98 | return applicationExpr;
|
---|
99 | }
|
---|
100 |
|
---|
101 | Expression* TupleDistrib::mutate(UntypedExpr *expr) {
|
---|
102 | if( NameExpr *assgnop = dynamic_cast< NameExpr * >(expr->get_function()) ) {
|
---|
103 | if( assgnop->get_name() == std::string("?=?") ) {
|
---|
104 | std::list<Expression *> &args = expr->get_args();
|
---|
105 | assert(args.size() == 2);
|
---|
106 | //if args.front() points to a tuple and if args.back() is already resolved
|
---|
107 | if( AddressExpr *addr = dynamic_cast<AddressExpr *>(args.front()) )
|
---|
108 | if( TupleExpr *lhs = dynamic_cast<TupleExpr *>(addr->get_arg()) )
|
---|
109 | if( ApplicationExpr *rhs = dynamic_cast<ApplicationExpr *>( args.back() ) ) {
|
---|
110 | for ( std::list<Expression *>::iterator tc = lhs->get_exprs().begin(); tc != lhs->get_exprs().end(); ++tc )
|
---|
111 | rhs->get_args().push_back( new AddressExpr( *tc ) );
|
---|
112 | return rhs; // XXX
|
---|
113 | }
|
---|
114 | } else
|
---|
115 | assert(false); // It's not an assignment, shouldn't be here
|
---|
116 | }
|
---|
117 | return expr;
|
---|
118 | }
|
---|
119 |
|
---|
120 | } // namespace Tuples
|
---|