[a32b204] | 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 | //
|
---|
[103b264] | 7 | // Occurs.cc --
|
---|
[a32b204] | 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 | //
|
---|
[51b73452] | 15 |
|
---|
[ea6332d] | 16 | #include <set> // for set, _Rb_tree_const_iterator
|
---|
| 17 | #include <string> // for string
|
---|
| 18 |
|
---|
[103b264] | 19 | #include "Common/PassVisitor.h"
|
---|
[ea6332d] | 20 | #include "SynTree/Type.h" // for TypeInstType, Type
|
---|
| 21 | #include "TypeEnvironment.h" // for EqvClass, TypeEnvironment
|
---|
[51b73452] | 22 |
|
---|
| 23 | namespace ResolvExpr {
|
---|
[103b264] | 24 | struct Occurs : public WithVisitorRef<Occurs> {
|
---|
[a32b204] | 25 | Occurs( std::string varName, const TypeEnvironment &env );
|
---|
[85dac33] | 26 | void previsit( const TypeInstType * typeInst );
|
---|
[103b264] | 27 |
|
---|
[a32b204] | 28 | bool result;
|
---|
| 29 | std::set< std::string > eqvVars;
|
---|
[103b264] | 30 | const TypeEnvironment &tenv;
|
---|
[a32b204] | 31 | };
|
---|
[51b73452] | 32 |
|
---|
[85dac33] | 33 | bool occurs( const Type *type, const std::string & varName, const TypeEnvironment &env ) {
|
---|
[103b264] | 34 | PassVisitor<Occurs> occur( varName, env );
|
---|
[a32b204] | 35 | type->accept( occur );
|
---|
[103b264] | 36 | return occur.pass.result;
|
---|
[a32b204] | 37 | }
|
---|
[51b73452] | 38 |
|
---|
[103b264] | 39 | Occurs::Occurs( std::string varName, const TypeEnvironment & env ) : result( false ), tenv( env ) {
|
---|
[00ac42e] | 40 | if ( const EqvClass *eqvClass = tenv.lookup( varName ) ) {
|
---|
| 41 | eqvVars = eqvClass->vars;
|
---|
[a32b204] | 42 | } else {
|
---|
| 43 | eqvVars.insert( varName );
|
---|
| 44 | } // if
|
---|
| 45 | }
|
---|
[51b73452] | 46 |
|
---|
[85dac33] | 47 | void Occurs::previsit( const TypeInstType * typeInst ) {
|
---|
[00ac42e] | 48 | /// std::cerr << "searching for vars: ";
|
---|
[103b264] | 49 | /// std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cerr, " " ) );
|
---|
| 50 | /// std::cerr << std::endl;
|
---|
[a32b204] | 51 | if ( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) {
|
---|
| 52 | result = true;
|
---|
[00ac42e] | 53 | } else if ( const EqvClass *eqvClass = tenv.lookup( typeInst->get_name() ) ) {
|
---|
| 54 | if ( eqvClass->type ) {
|
---|
[103b264] | 55 | /// std::cerr << typeInst->get_name() << " is bound to";
|
---|
| 56 | /// eqvClass.type->print( std::cerr );
|
---|
| 57 | /// std::cerr << std::endl;
|
---|
[00ac42e] | 58 | eqvClass->type->accept( *visitor );
|
---|
[a32b204] | 59 | } // if
|
---|
| 60 | } // if
|
---|
| 61 | }
|
---|
[51b73452] | 62 | } // namespace ResolvExpr
|
---|
[a32b204] | 63 |
|
---|
| 64 | // Local Variables: //
|
---|
| 65 | // tab-width: 4 //
|
---|
| 66 | // mode: c++ //
|
---|
| 67 | // compile-command: "make install" //
|
---|
| 68 | // End: //
|
---|