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 | // Occurs.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 09:47:41 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sun May 17 09:49:26 2015 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include <set> // for set, _Rb_tree_const_iterator |
---|
17 | #include <string> // for string |
---|
18 | |
---|
19 | #include "Common/PassVisitor.h" |
---|
20 | #include "SynTree/Type.h" // for TypeInstType, Type |
---|
21 | #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
22 | |
---|
23 | namespace ResolvExpr { |
---|
24 | struct Occurs : public WithVisitorRef<Occurs> { |
---|
25 | Occurs( std::string varName, const TypeEnvironment &env ); |
---|
26 | void previsit( TypeInstType * typeInst ); |
---|
27 | |
---|
28 | bool result; |
---|
29 | std::set< std::string > eqvVars; |
---|
30 | const TypeEnvironment &tenv; |
---|
31 | }; |
---|
32 | |
---|
33 | bool occurs( Type *type, std::string varName, const TypeEnvironment &env ) { |
---|
34 | PassVisitor<Occurs> occur( varName, env ); |
---|
35 | type->accept( occur ); |
---|
36 | return occur.pass.result; |
---|
37 | } |
---|
38 | |
---|
39 | Occurs::Occurs( std::string varName, const TypeEnvironment & env ) : result( false ), tenv( env ) { |
---|
40 | EqvClass eqvClass; |
---|
41 | if ( tenv.lookup( varName, eqvClass ) ) { |
---|
42 | eqvVars = eqvClass.vars; |
---|
43 | } else { |
---|
44 | eqvVars.insert( varName ); |
---|
45 | } // if |
---|
46 | } |
---|
47 | |
---|
48 | void Occurs::previsit( TypeInstType * typeInst ) { |
---|
49 | EqvClass eqvClass; |
---|
50 | /// std::cerr << "searching for vars: "; |
---|
51 | /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cerr, " " ) ); |
---|
52 | /// std::cerr << std::endl; |
---|
53 | if ( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) { |
---|
54 | result = true; |
---|
55 | } else if ( tenv.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
56 | if ( eqvClass.type ) { |
---|
57 | /// std::cerr << typeInst->get_name() << " is bound to"; |
---|
58 | /// eqvClass.type->print( std::cerr ); |
---|
59 | /// std::cerr << std::endl; |
---|
60 | eqvClass.type->accept( *visitor ); |
---|
61 | } // if |
---|
62 | } // if |
---|
63 | } |
---|
64 | } // namespace ResolvExpr |
---|
65 | |
---|
66 | // Local Variables: // |
---|
67 | // tab-width: 4 // |
---|
68 | // mode: c++ // |
---|
69 | // compile-command: "make install" // |
---|
70 | // End: // |
---|