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>
|
---|
17 | #include <algorithm>
|
---|
18 | #include <iterator>
|
---|
19 | #include "SynTree/Type.h"
|
---|
20 | #include "SynTree/Visitor.h"
|
---|
21 | #include "TypeEnvironment.h"
|
---|
22 |
|
---|
23 | namespace ResolvExpr {
|
---|
24 | class Occurs : public Visitor {
|
---|
25 | public:
|
---|
26 | Occurs( std::string varName, const TypeEnvironment &env );
|
---|
27 | bool get_result() const { return result; }
|
---|
28 | virtual void visit( TypeInstType *typeInst );
|
---|
29 | private:
|
---|
30 | bool result;
|
---|
31 | std::set< std::string > eqvVars;
|
---|
32 | const TypeEnvironment &env;
|
---|
33 | };
|
---|
34 |
|
---|
35 | bool occurs( Type *type, std::string varName, const TypeEnvironment &env ) {
|
---|
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 ) : result( false ), env( env ) {
|
---|
42 | EqvClass eqvClass;
|
---|
43 | if ( env.lookup( varName, eqvClass ) ) {
|
---|
44 | eqvVars = eqvClass.vars;
|
---|
45 | } else {
|
---|
46 | eqvVars.insert( varName );
|
---|
47 | } // if
|
---|
48 | }
|
---|
49 |
|
---|
50 | void Occurs::visit( TypeInstType *typeInst ) {
|
---|
51 | EqvClass eqvClass;
|
---|
52 | /// std::cout << "searching for vars: ";
|
---|
53 | /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cout, " " ) );
|
---|
54 | /// std::cout << std::endl;
|
---|
55 | if ( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) {
|
---|
56 | result = true;
|
---|
57 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
|
---|
58 | if ( eqvClass.type ) {
|
---|
59 | /// std::cout << typeInst->get_name() << " is bound to";
|
---|
60 | /// eqvClass.type->print( std::cout );
|
---|
61 | /// std::cout << std::endl;
|
---|
62 | eqvClass.type->accept( *this );
|
---|
63 | } // if
|
---|
64 | } // if
|
---|
65 | }
|
---|
66 | } // namespace ResolvExpr
|
---|
67 |
|
---|
68 | // Local Variables: //
|
---|
69 | // tab-width: 4 //
|
---|
70 | // mode: c++ //
|
---|
71 | // compile-command: "make install" //
|
---|
72 | // End: //
|
---|