| 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 |                 if ( const EqvClass *eqvClass = tenv.lookup( varName ) ) {
 | 
|---|
| 41 |                         eqvVars = eqvClass->vars;
 | 
|---|
| 42 |                 } else {
 | 
|---|
| 43 |                         eqvVars.insert( varName );
 | 
|---|
| 44 |                 } // if
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         void Occurs::previsit( TypeInstType * typeInst ) {
 | 
|---|
| 48 |                 ///   std::cerr << "searching for vars: ";
 | 
|---|
| 49 | ///   std::copy( eqvVars.begin(), eqvVars.end(), std::ostream_iterator< std::string >( std::cerr, " " ) );
 | 
|---|
| 50 | ///   std::cerr << std::endl;
 | 
|---|
| 51 |                 if ( eqvVars.find( typeInst->get_name() ) != eqvVars.end() ) {
 | 
|---|
| 52 |                         result = true;
 | 
|---|
| 53 |                 } else if ( const EqvClass *eqvClass = tenv.lookup( typeInst->get_name() ) ) {
 | 
|---|
| 54 |                         if ( eqvClass->type ) {
 | 
|---|
| 55 | ///       std::cerr << typeInst->get_name() << " is bound to";
 | 
|---|
| 56 | ///       eqvClass.type->print( std::cerr );
 | 
|---|
| 57 | ///       std::cerr << std::endl;
 | 
|---|
| 58 |                                 eqvClass->type->accept( *visitor );
 | 
|---|
| 59 |                         } // if
 | 
|---|
| 60 |                 } // if
 | 
|---|
| 61 |         }
 | 
|---|
| 62 | } // namespace ResolvExpr
 | 
|---|
| 63 | 
 | 
|---|
| 64 | // Local Variables: //
 | 
|---|
| 65 | // tab-width: 4 //
 | 
|---|
| 66 | // mode: c++ //
 | 
|---|
| 67 | // compile-command: "make install" //
 | 
|---|
| 68 | // End: //
 | 
|---|