[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 |
---|
[4040425] | 12 | // Last Modified On : Wed Mar 2 17:36:32 2016 |
---|
| 13 | // Update Count : 5 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[5f2f2d7] | 16 | #include <sstream> |
---|
[51b7345] | 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; |
---|
[51b7345] | 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 ); |
---|
| 47 | maybeAccept( pointerType->get_base(), *this ); |
---|
| 48 | typeAfter( pointerType ); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void RenameVars::visit( ArrayType *arrayType ) { |
---|
| 52 | typeBefore( arrayType ); |
---|
| 53 | maybeAccept( arrayType->get_dimension(), *this ); |
---|
| 54 | maybeAccept( arrayType->get_base(), *this ); |
---|
| 55 | typeAfter( arrayType ); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void RenameVars::visit( FunctionType *functionType ) { |
---|
| 59 | typeBefore( functionType ); |
---|
| 60 | acceptAll( functionType->get_returnVals(), *this ); |
---|
| 61 | acceptAll( functionType->get_parameters(), *this ); |
---|
| 62 | typeAfter( functionType ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void RenameVars::visit( StructInstType *aggregateUseType ) { |
---|
| 66 | typeBefore( aggregateUseType ); |
---|
| 67 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 68 | typeAfter( aggregateUseType ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void RenameVars::visit( UnionInstType *aggregateUseType ) { |
---|
| 72 | typeBefore( aggregateUseType ); |
---|
| 73 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 74 | typeAfter( aggregateUseType ); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void RenameVars::visit( EnumInstType *aggregateUseType ) { |
---|
| 78 | typeBefore( aggregateUseType ); |
---|
| 79 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 80 | typeAfter( aggregateUseType ); |
---|
| 81 | } |
---|
| 82 | |
---|
[4040425] | 83 | void RenameVars::visit( TraitInstType *aggregateUseType ) { |
---|
[a32b204] | 84 | typeBefore( aggregateUseType ); |
---|
| 85 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 86 | acceptAll( aggregateUseType->get_members(), *this ); |
---|
| 87 | typeAfter( aggregateUseType ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void RenameVars::visit( TypeInstType *instType ) { |
---|
| 91 | typeBefore( instType ); |
---|
| 92 | std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() ); |
---|
| 93 | if ( i != mapStack.front().end() ) { |
---|
| 94 | instType->set_name( i->second ); |
---|
| 95 | } else { |
---|
| 96 | } // if |
---|
| 97 | acceptAll( instType->get_parameters(), *this ); |
---|
| 98 | typeAfter( instType ); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void RenameVars::visit( TupleType *tupleType ) { |
---|
| 102 | typeBefore( tupleType ); |
---|
| 103 | acceptAll( tupleType->get_types(), *this ); |
---|
| 104 | typeAfter( tupleType ); |
---|
| 105 | } |
---|
| 106 | |
---|
[44b7088] | 107 | void RenameVars::visit( VarArgsType *varArgsType ) { |
---|
| 108 | typeBefore( varArgsType ); |
---|
| 109 | typeAfter( varArgsType ); |
---|
| 110 | } |
---|
| 111 | |
---|
[a32b204] | 112 | void RenameVars::typeBefore( Type *type ) { |
---|
| 113 | if ( ! type->get_forall().empty() ) { |
---|
[0f19d763] | 114 | // copies current name mapping into new mapping |
---|
[a32b204] | 115 | mapStack.push_front( mapStack.front() ); |
---|
[0f19d763] | 116 | // renames all "forall" type names to `_${level}_${name}' |
---|
[a32b204] | 117 | for ( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
[5f2f2d7] | 118 | std::ostringstream output; |
---|
[a32b204] | 119 | output << "_" << level << "_" << (*i)->get_name(); |
---|
[5f2f2d7] | 120 | std::string newname( output.str() ); |
---|
[a32b204] | 121 | mapStack.front()[ (*i)->get_name() ] = newname; |
---|
| 122 | (*i)->set_name( newname ); |
---|
[0f19d763] | 123 | // ditto for assertion names, the next level in |
---|
[a32b204] | 124 | level++; |
---|
| 125 | acceptAll( (*i)->get_assertions(), *this ); |
---|
| 126 | } // for |
---|
| 127 | } // if |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void RenameVars::typeAfter( Type *type ) { |
---|
[0f19d763] | 131 | // clears name mapping added by typeBefore() |
---|
[a32b204] | 132 | if ( ! type->get_forall().empty() ) { |
---|
| 133 | mapStack.pop_front(); |
---|
| 134 | } // if |
---|
| 135 | } |
---|
[51b7345] | 136 | |
---|
| 137 | } // namespace ResolvExpr |
---|
[a32b204] | 138 | |
---|
| 139 | // Local Variables: // |
---|
| 140 | // tab-width: 4 // |
---|
| 141 | // mode: c++ // |
---|
| 142 | // compile-command: "make install" // |
---|
| 143 | // End: // |
---|