1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: Occurs.cc,v 1.2 2005/08/29 20:14:16 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include <set> |
---|
9 | #include <algorithm> |
---|
10 | #include <iterator> |
---|
11 | #include "SynTree/Type.h" |
---|
12 | #include "SynTree/Visitor.h" |
---|
13 | #include "TypeEnvironment.h" |
---|
14 | |
---|
15 | |
---|
16 | namespace ResolvExpr { |
---|
17 | |
---|
18 | class Occurs : public Visitor |
---|
19 | { |
---|
20 | public: |
---|
21 | Occurs( std::string varName, const TypeEnvironment &env ); |
---|
22 | |
---|
23 | bool get_result() const { return result; } |
---|
24 | |
---|
25 | virtual void visit( TypeInstType *typeInst ); |
---|
26 | |
---|
27 | private: |
---|
28 | bool result; |
---|
29 | std::set< std::string > eqvVars; |
---|
30 | const TypeEnvironment &env; |
---|
31 | }; |
---|
32 | |
---|
33 | bool |
---|
34 | occurs( Type *type, std::string varName, const TypeEnvironment &env ) |
---|
35 | { |
---|
36 | Occurs occur( varName, env ); |
---|
37 | type->accept( occur ); |
---|
38 | return occur.get_result(); |
---|
39 | } |
---|
40 | |
---|
41 | Occurs::Occurs( std::string varName, const TypeEnvironment &env ) |
---|
42 | : result( false ), env( env ) |
---|
43 | { |
---|
44 | EqvClass eqvClass; |
---|
45 | if( env.lookup( varName, eqvClass ) ) { |
---|
46 | eqvVars = eqvClass.vars; |
---|
47 | } else { |
---|
48 | eqvVars.insert( varName ); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | void |
---|
53 | Occurs::visit( TypeInstType *typeInst ) |
---|
54 | { |
---|
55 | EqvClass eqvClass; |
---|
56 | /// std::cout << "searching for vars: "; |
---|
57 | /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cout, " " ) ); |
---|
58 | /// std::cout << std::endl; |
---|
59 | if( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) { |
---|
60 | result = true; |
---|
61 | } else if( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
62 | if( eqvClass.type ) { |
---|
63 | /// std::cout << typeInst->get_name() << " is bound to"; |
---|
64 | /// eqvClass.type->print( std::cout ); |
---|
65 | /// std::cout << std::endl; |
---|
66 | eqvClass.type->accept( *this ); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | } // namespace ResolvExpr |
---|