[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 | // |
---|
[8c49c0e] | 7 | // RenameVars.cc -- |
---|
[a32b204] | 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 | |
---|
[89e6ffc] | 112 | void RenameVars::visit( ZeroType *zeroType ) { |
---|
| 113 | typeBefore( zeroType ); |
---|
| 114 | typeAfter( zeroType ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void RenameVars::visit( OneType *oneType ) { |
---|
| 118 | typeBefore( oneType ); |
---|
| 119 | typeAfter( oneType ); |
---|
| 120 | } |
---|
| 121 | |
---|
[a32b204] | 122 | void RenameVars::typeBefore( Type *type ) { |
---|
| 123 | if ( ! type->get_forall().empty() ) { |
---|
[0f19d763] | 124 | // copies current name mapping into new mapping |
---|
[a32b204] | 125 | mapStack.push_front( mapStack.front() ); |
---|
[0f19d763] | 126 | // renames all "forall" type names to `_${level}_${name}' |
---|
[8c49c0e] | 127 | for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
[5f2f2d7] | 128 | std::ostringstream output; |
---|
[a32b204] | 129 | output << "_" << level << "_" << (*i)->get_name(); |
---|
[5f2f2d7] | 130 | std::string newname( output.str() ); |
---|
[a32b204] | 131 | mapStack.front()[ (*i)->get_name() ] = newname; |
---|
| 132 | (*i)->set_name( newname ); |
---|
[0f19d763] | 133 | // ditto for assertion names, the next level in |
---|
[a32b204] | 134 | level++; |
---|
| 135 | acceptAll( (*i)->get_assertions(), *this ); |
---|
| 136 | } // for |
---|
| 137 | } // if |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void RenameVars::typeAfter( Type *type ) { |
---|
[0f19d763] | 141 | // clears name mapping added by typeBefore() |
---|
[a32b204] | 142 | if ( ! type->get_forall().empty() ) { |
---|
| 143 | mapStack.pop_front(); |
---|
| 144 | } // if |
---|
| 145 | } |
---|
[51b7345] | 146 | |
---|
| 147 | } // namespace ResolvExpr |
---|
[a32b204] | 148 | |
---|
| 149 | // Local Variables: // |
---|
| 150 | // tab-width: 4 // |
---|
| 151 | // mode: c++ // |
---|
| 152 | // compile-command: "make install" // |
---|
| 153 | // End: // |
---|