| 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 | 
|---|
| 12 | // Last Modified On : Sun May 17 12:07:59 2015 | 
|---|
| 13 | // Update Count     : 2 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <strstream> | 
|---|
| 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 { | 
|---|
| 25 | RenameVars global_renamer; | 
|---|
| 26 |  | 
|---|
| 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 ); | 
|---|
| 47 | ///   std::cout << "do pointer" << std::endl; | 
|---|
| 48 | maybeAccept( pointerType->get_base(), *this ); | 
|---|
| 49 | ///   std::cout << "done pointer" << std::endl; | 
|---|
| 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 ); | 
|---|
| 62 | ///   std::cout << "return vals" << std::endl; | 
|---|
| 63 | acceptAll( functionType->get_returnVals(), *this ); | 
|---|
| 64 | ///   std::cout << functionType->get_parameters().size() << " parameters" << std::endl; | 
|---|
| 65 | acceptAll( functionType->get_parameters(), *this ); | 
|---|
| 66 | ///   std::cout << "done function" << std::endl; | 
|---|
| 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 ); | 
|---|
| 97 | ///   std::cout << "instance of type " << instType->get_name() << std::endl; | 
|---|
| 98 | std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() ); | 
|---|
| 99 | if ( i != mapStack.front().end() ) { | 
|---|
| 100 | ///     std::cout << "found name " << i->second << std::endl; | 
|---|
| 101 | instType->set_name( i->second ); | 
|---|
| 102 | } else { | 
|---|
| 103 | ///     std::cout << "no name found" << std::endl; | 
|---|
| 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() ) { | 
|---|
| 117 | ///     std::cout << "type with forall: "; | 
|---|
| 118 | ///     type->print( std::cout ); | 
|---|
| 119 | ///     std::cout << std::endl; | 
|---|
| 120 | mapStack.push_front( mapStack.front() ); | 
|---|
| 121 | for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { | 
|---|
| 122 | std::ostrstream output; | 
|---|
| 123 | output << "_" << level << "_" << (*i)->get_name(); | 
|---|
| 124 | std::string newname( output.str(), output.pcount() ); | 
|---|
| 125 | mapStack.front()[ (*i)->get_name() ] = newname; | 
|---|
| 126 | (*i)->set_name( newname ); | 
|---|
| 127 | level++; | 
|---|
| 128 | acceptAll( (*i)->get_assertions(), *this ); | 
|---|
| 129 | } // for | 
|---|
| 130 | } // if | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void RenameVars::typeAfter( Type *type ) { | 
|---|
| 134 | if ( ! type->get_forall().empty() ) { | 
|---|
| 135 | mapStack.pop_front(); | 
|---|
| 136 | } // if | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | } // namespace ResolvExpr | 
|---|
| 140 |  | 
|---|
| 141 | // Local Variables: // | 
|---|
| 142 | // tab-width: 4 // | 
|---|
| 143 | // mode: c++ // | 
|---|
| 144 | // compile-command: "make install" // | 
|---|
| 145 | // End: // | 
|---|