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 | // XXX.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | /* |
---|
16 | * This file is part of the Cforall project |
---|
17 | * |
---|
18 | * $Id: FindFunction.cc,v 1.5 2005/08/29 20:14:13 rcbilson Exp $ |
---|
19 | * |
---|
20 | */ |
---|
21 | |
---|
22 | #include "FindFunction.h" |
---|
23 | #include "SynTree/Type.h" |
---|
24 | #include "SynTree/Declaration.h" |
---|
25 | #include "SynTree/Visitor.h" |
---|
26 | |
---|
27 | namespace GenPoly { |
---|
28 | |
---|
29 | class FindFunction : public Mutator |
---|
30 | { |
---|
31 | public: |
---|
32 | FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ); |
---|
33 | |
---|
34 | virtual Type *mutate( FunctionType *functionType ); |
---|
35 | virtual Type *mutate( PointerType *pointerType ); |
---|
36 | |
---|
37 | private: |
---|
38 | void handleForall( const std::list< TypeDecl* > &forall ); |
---|
39 | |
---|
40 | std::list< FunctionType* > &functions; |
---|
41 | TyVarMap tyVars; |
---|
42 | bool replaceMode; |
---|
43 | FindFunctionPredicate predicate; |
---|
44 | }; |
---|
45 | |
---|
46 | void |
---|
47 | findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) |
---|
48 | { |
---|
49 | FindFunction finder( functions, tyVars, false, predicate ); |
---|
50 | type->acceptMutator( finder ); |
---|
51 | } |
---|
52 | |
---|
53 | void |
---|
54 | findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) |
---|
55 | { |
---|
56 | FindFunction finder( functions, tyVars, true, predicate ); |
---|
57 | type = type->acceptMutator( finder ); |
---|
58 | } |
---|
59 | |
---|
60 | FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ) |
---|
61 | : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | void |
---|
66 | FindFunction::handleForall( const std::list< TypeDecl* > &forall ) |
---|
67 | { |
---|
68 | for ( std::list< TypeDecl* >::const_iterator i = forall.begin(); i != forall.end(); ++i ) { |
---|
69 | TyVarMap::iterator var = tyVars.find( (*i)->get_name() ); |
---|
70 | if ( var != tyVars.end() ) { |
---|
71 | tyVars.erase( var ); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | Type* |
---|
77 | FindFunction::mutate( FunctionType *functionType ) |
---|
78 | { |
---|
79 | TyVarMap oldTyVars = tyVars; |
---|
80 | handleForall( functionType->get_forall() ); |
---|
81 | mutateAll( functionType->get_returnVals(), *this ); |
---|
82 | Type *ret = functionType; |
---|
83 | if ( predicate( functionType, tyVars ) ) { |
---|
84 | functions.push_back( functionType ); |
---|
85 | if ( replaceMode ) { |
---|
86 | ret = new FunctionType( Type::Qualifiers(), true ); |
---|
87 | } |
---|
88 | } |
---|
89 | tyVars = oldTyVars; |
---|
90 | return ret; |
---|
91 | } |
---|
92 | |
---|
93 | Type * |
---|
94 | FindFunction::mutate( PointerType *pointerType ) |
---|
95 | { |
---|
96 | TyVarMap oldTyVars = tyVars; |
---|
97 | handleForall( pointerType->get_forall() ); |
---|
98 | Type *ret = Mutator::mutate( pointerType ); |
---|
99 | tyVars = oldTyVars; |
---|
100 | return ret; |
---|
101 | } |
---|
102 | |
---|
103 | } // namespace GenPoly |
---|
104 | // Local Variables: // |
---|
105 | // tab-width: 4 // |
---|
106 | // mode: c++ // |
---|
107 | // compile-command: "make install" // |
---|
108 | // End: // |
---|