1 | #include <list> |
---|
2 | #include <vector> |
---|
3 | #include <cassert> |
---|
4 | #include <algorithm> |
---|
5 | |
---|
6 | #include "FunctionFixer.h" |
---|
7 | |
---|
8 | namespace Tuples { |
---|
9 | DeclarationWithType *FunctionFixer::mutate( FunctionDecl *functionDecl ) { |
---|
10 | functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) ); |
---|
11 | mutateAll( functionDecl->get_oldDecls(), *this ); |
---|
12 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) ); |
---|
13 | index.visit( functionDecl ); |
---|
14 | rets.clear(); |
---|
15 | return functionDecl; |
---|
16 | } |
---|
17 | |
---|
18 | Type *FunctionFixer::mutate( FunctionType *functionType ) |
---|
19 | { |
---|
20 | typedef std::list< DeclarationWithType * > Decls; |
---|
21 | |
---|
22 | if ( functionType->get_returnVals().size() <= 1 ) return functionType; |
---|
23 | std::copy( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), back_inserter(rets) ); |
---|
24 | |
---|
25 | Type::Qualifiers qual; |
---|
26 | for ( Decls::iterator i = rets.begin(); i != rets.end(); i++ ) { |
---|
27 | (*i)->set_type( new PointerType( qual, (*i)->get_type() ) ); |
---|
28 | functionType->get_parameters().push_back( *i ); |
---|
29 | } |
---|
30 | |
---|
31 | functionType->get_returnVals() = *(new std::list< DeclarationWithType * >()); |
---|
32 | |
---|
33 | functionType->set_isVarArgs( false ); |
---|
34 | return functionType; |
---|
35 | } |
---|
36 | |
---|
37 | Statement *FunctionFixer::mutate( ReturnStmt *retStmt ) |
---|
38 | { |
---|
39 | bool tupleReturn = false; |
---|
40 | Expression *rhs = 0; |
---|
41 | // also check if returning multiple values |
---|
42 | if( CastExpr *cst = dynamic_cast<CastExpr *>( retStmt->get_expr() ) ) { |
---|
43 | if( ApplicationExpr *app = dynamic_cast<ApplicationExpr *>( cst->get_arg() ) ) { |
---|
44 | if( app->get_results().size() > 1 ) { // doesn't need to be ApplicationExpr |
---|
45 | tupleReturn = true; |
---|
46 | rhs = app; |
---|
47 | } |
---|
48 | } else if( TupleExpr *t = dynamic_cast<TupleExpr *>( cst->get_arg() ) ) { |
---|
49 | tupleReturn = true; |
---|
50 | assert( rets.size() == t->get_exprs().size() ); // stupid check, resolve expression |
---|
51 | rhs = t; |
---|
52 | } |
---|
53 | |
---|
54 | if( tupleReturn ) { |
---|
55 | assert ( rhs != 0 ); |
---|
56 | std::list< Expression * > lhs; |
---|
57 | for( std::list< DeclarationWithType * >::iterator d = rets.begin(); d != rets.end(); ++d ) { |
---|
58 | std::list<Expression *> largs; |
---|
59 | largs.push_back(new VariableExpr( *d )); |
---|
60 | Expression *exp = ResolvExpr::resolveInVoidContext( new CastExpr( new UntypedExpr(new NameExpr("*?"), largs), (*d)->get_type()), |
---|
61 | index ); |
---|
62 | lhs.push_back(exp); |
---|
63 | } |
---|
64 | std::list< Expression * > args; |
---|
65 | TupleExpr *tlhs = new TupleExpr; tlhs->set_exprs( lhs ); |
---|
66 | args.push_back( new AddressExpr(tlhs) ); |
---|
67 | args.push_back(rhs); |
---|
68 | |
---|
69 | return new ExprStmt( std::list< Label>(), new UntypedExpr( new NameExpr("?=?"), args ) ); |
---|
70 | } |
---|
71 | } |
---|
72 | /* |
---|
73 | else |
---|
74 | std::cerr << "Empty return statement" << std::endl; |
---|
75 | */ |
---|
76 | |
---|
77 | return retStmt; |
---|
78 | } |
---|
79 | |
---|
80 | Expression* FunctionFixer::mutate( VariableExpr *variableExpr ) { |
---|
81 | if ( rets.empty() ) return variableExpr; |
---|
82 | mutateAll( variableExpr->get_results(), *this ); |
---|
83 | if( std::find( rets.begin(), rets.end(), variableExpr->get_var() ) != rets.end() ) |
---|
84 | // if ( PointerType *ptr = dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) ) { |
---|
85 | if ( dynamic_cast<PointerType *>(variableExpr->get_var()->get_type()) != 0 ) { |
---|
86 | std::list<Expression *> largs; |
---|
87 | largs.push_back( new AddressExpr(variableExpr) ); |
---|
88 | Expression *expr = ResolvExpr::resolveInVoidContext( /*new CastExpr(*/new UntypedExpr( new NameExpr( "*?" ), largs )/*, |
---|
89 | ptr->get_base()),*/, index); |
---|
90 | if( ApplicationExpr *app = dynamic_cast< ApplicationExpr * >( expr ) ) { |
---|
91 | assert( app->get_args().size() == 1 ); |
---|
92 | app->get_args().pop_front(); |
---|
93 | app->get_args().push_back( variableExpr ); |
---|
94 | return app; |
---|
95 | } |
---|
96 | } |
---|
97 | return variableExpr; |
---|
98 | } |
---|
99 | |
---|
100 | /* |
---|
101 | Expression* FunctionFixer::mutate(ApplicationExpr *applicationExpr) { |
---|
102 | std::cerr << "In Application Expression" << std::endl; |
---|
103 | mutateAll( applicationExpr->get_results(), *this ); |
---|
104 | applicationExpr->set_function( maybeMutate( applicationExpr->get_function(), *this ) ); |
---|
105 | mutateAll( applicationExpr->get_args(), *this ); |
---|
106 | return applicationExpr; |
---|
107 | } |
---|
108 | */ |
---|
109 | } // namespace Tuples |
---|