| [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 | //
 | 
|---|
 | 7 | // RenameVars.cc -- 
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Sun May 17 12:05:18 2015
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [5f2f2d7] | 12 | // Last Modified On : Mon Jun  8 14:51:35 2015
 | 
|---|
 | 13 | // Update Count     : 4
 | 
|---|
| [a32b204] | 14 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
| [5f2f2d7] | 16 | #include <sstream>
 | 
|---|
| [51b73452] | 17 | 
 | 
|---|
 | 18 | #include "RenameVars.h"
 | 
|---|
 | 19 | #include "SynTree/Visitor.h"
 | 
|---|
 | 20 | #include "SynTree/Type.h"
 | 
|---|
 | 21 | #include "SynTree/Declaration.h"
 | 
|---|
 | 22 | #include "SynTree/Expression.h"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | namespace ResolvExpr {
 | 
|---|
| [a32b204] | 25 |         RenameVars global_renamer;
 | 
|---|
| [51b73452] | 26 | 
 | 
|---|
| [a32b204] | 27 |         RenameVars::RenameVars() : level( 0 ) {
 | 
|---|
 | 28 |                 mapStack.push_front( std::map< std::string, std::string >() );
 | 
|---|
 | 29 |         }
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 |         void RenameVars::reset() {
 | 
|---|
 | 32 |                 level = 0;
 | 
|---|
 | 33 |         }
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |         void RenameVars::visit( VoidType *voidType ) {
 | 
|---|
 | 36 |                 typeBefore( voidType );
 | 
|---|
 | 37 |                 typeAfter( voidType );
 | 
|---|
 | 38 |         }
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 |         void RenameVars::visit( BasicType *basicType ) {
 | 
|---|
 | 41 |                 typeBefore( basicType );
 | 
|---|
 | 42 |                 typeAfter( basicType );
 | 
|---|
 | 43 |         }
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 |         void RenameVars::visit( PointerType *pointerType ) {
 | 
|---|
 | 46 |                 typeBefore( pointerType );
 | 
|---|
| [51b73452] | 47 | ///   std::cout << "do pointer" << std::endl;
 | 
|---|
| [a32b204] | 48 |                 maybeAccept( pointerType->get_base(), *this );
 | 
|---|
| [51b73452] | 49 | ///   std::cout << "done pointer" << std::endl;
 | 
|---|
| [a32b204] | 50 |                 typeAfter( pointerType );
 | 
|---|
 | 51 |         }
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |         void RenameVars::visit( ArrayType *arrayType ) {
 | 
|---|
 | 54 |                 typeBefore( arrayType );
 | 
|---|
 | 55 |                 maybeAccept( arrayType->get_dimension(), *this );
 | 
|---|
 | 56 |                 maybeAccept( arrayType->get_base(), *this );
 | 
|---|
 | 57 |                 typeAfter( arrayType );
 | 
|---|
 | 58 |         }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |         void RenameVars::visit( FunctionType *functionType ) {
 | 
|---|
 | 61 |                 typeBefore( functionType );
 | 
|---|
| [51b73452] | 62 | ///   std::cout << "return vals" << std::endl;
 | 
|---|
| [a32b204] | 63 |                 acceptAll( functionType->get_returnVals(), *this );
 | 
|---|
| [51b73452] | 64 | ///   std::cout << functionType->get_parameters().size() << " parameters" << std::endl;
 | 
|---|
| [a32b204] | 65 |                 acceptAll( functionType->get_parameters(), *this );
 | 
|---|
| [51b73452] | 66 | ///   std::cout << "done function" << std::endl;
 | 
|---|
| [a32b204] | 67 |                 typeAfter( functionType );
 | 
|---|
 | 68 |         }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |         void RenameVars::visit( StructInstType *aggregateUseType ) {
 | 
|---|
 | 71 |                 typeBefore( aggregateUseType );
 | 
|---|
 | 72 |                 acceptAll( aggregateUseType->get_parameters(), *this );
 | 
|---|
 | 73 |                 typeAfter( aggregateUseType );
 | 
|---|
 | 74 |         }
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |         void RenameVars::visit( UnionInstType *aggregateUseType ) {
 | 
|---|
 | 77 |                 typeBefore( aggregateUseType );
 | 
|---|
 | 78 |                 acceptAll( aggregateUseType->get_parameters(), *this );
 | 
|---|
 | 79 |                 typeAfter( aggregateUseType );
 | 
|---|
 | 80 |         }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |         void RenameVars::visit( EnumInstType *aggregateUseType ) {
 | 
|---|
 | 83 |                 typeBefore( aggregateUseType );
 | 
|---|
 | 84 |                 acceptAll( aggregateUseType->get_parameters(), *this );
 | 
|---|
 | 85 |                 typeAfter( aggregateUseType );
 | 
|---|
 | 86 |         }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |         void RenameVars::visit( ContextInstType *aggregateUseType ) {
 | 
|---|
 | 89 |                 typeBefore( aggregateUseType );
 | 
|---|
 | 90 |                 acceptAll( aggregateUseType->get_parameters(), *this );
 | 
|---|
 | 91 |                 acceptAll( aggregateUseType->get_members(), *this );
 | 
|---|
 | 92 |                 typeAfter( aggregateUseType );
 | 
|---|
 | 93 |         }
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |         void RenameVars::visit( TypeInstType *instType ) {
 | 
|---|
 | 96 |                 typeBefore( instType );
 | 
|---|
| [51b73452] | 97 | ///   std::cout << "instance of type " << instType->get_name() << std::endl;
 | 
|---|
| [a32b204] | 98 |                 std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() );
 | 
|---|
 | 99 |                 if ( i != mapStack.front().end() ) {
 | 
|---|
| [51b73452] | 100 | ///     std::cout << "found name " << i->second << std::endl;
 | 
|---|
| [a32b204] | 101 |                         instType->set_name( i->second );
 | 
|---|
 | 102 |                 } else {
 | 
|---|
| [51b73452] | 103 | ///     std::cout << "no name found" << std::endl;
 | 
|---|
| [a32b204] | 104 |                 } // if
 | 
|---|
 | 105 |                 acceptAll( instType->get_parameters(), *this );
 | 
|---|
 | 106 |                 typeAfter( instType );
 | 
|---|
 | 107 |         }
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |         void RenameVars::visit( TupleType *tupleType ) {
 | 
|---|
 | 110 |                 typeBefore( tupleType );
 | 
|---|
 | 111 |                 acceptAll( tupleType->get_types(), *this );
 | 
|---|
 | 112 |                 typeAfter( tupleType );
 | 
|---|
 | 113 |         }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |         void RenameVars::typeBefore( Type *type ) {
 | 
|---|
 | 116 |                 if ( ! type->get_forall().empty() ) {
 | 
|---|
| [51b73452] | 117 | ///     std::cout << "type with forall: ";
 | 
|---|
 | 118 | ///     type->print( std::cout );
 | 
|---|
 | 119 | ///     std::cout << std::endl;
 | 
|---|
| [0f19d763] | 120 |                         // copies current name mapping into new mapping
 | 
|---|
| [a32b204] | 121 |                         mapStack.push_front( mapStack.front() );
 | 
|---|
| [0f19d763] | 122 |                         // renames all "forall" type names to `_${level}_${name}'
 | 
|---|
| [a32b204] | 123 |                         for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
 | 
|---|
| [5f2f2d7] | 124 |                                 std::ostringstream output;
 | 
|---|
| [a32b204] | 125 |                                 output << "_" << level << "_" << (*i)->get_name();
 | 
|---|
| [5f2f2d7] | 126 |                                 std::string newname( output.str() );
 | 
|---|
| [a32b204] | 127 |                                 mapStack.front()[ (*i)->get_name() ] = newname;
 | 
|---|
 | 128 |                                 (*i)->set_name( newname );
 | 
|---|
| [0f19d763] | 129 |                                 // ditto for assertion names, the next level in
 | 
|---|
| [a32b204] | 130 |                                 level++;
 | 
|---|
 | 131 |                                 acceptAll( (*i)->get_assertions(), *this );
 | 
|---|
 | 132 |                         } // for
 | 
|---|
 | 133 |                 } // if
 | 
|---|
 | 134 |         }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |         void RenameVars::typeAfter( Type *type ) {
 | 
|---|
| [0f19d763] | 137 |                 // clears name mapping added by typeBefore()
 | 
|---|
| [a32b204] | 138 |                 if ( ! type->get_forall().empty() ) {
 | 
|---|
 | 139 |                         mapStack.pop_front();
 | 
|---|
 | 140 |                 } // if
 | 
|---|
 | 141 |         }
 | 
|---|
| [51b73452] | 142 | 
 | 
|---|
 | 143 | } // namespace ResolvExpr
 | 
|---|
| [a32b204] | 144 | 
 | 
|---|
 | 145 | // Local Variables: //
 | 
|---|
 | 146 | // tab-width: 4 //
 | 
|---|
 | 147 | // mode: c++ //
 | 
|---|
 | 148 | // compile-command: "make install" //
 | 
|---|
 | 149 | // End: //
 | 
|---|