[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 | |
---|
[ea6332d] | 16 | #include <ext/alloc_traits.h> // for __alloc_traits<>::value_type |
---|
| 17 | #include <memory> // for allocator_traits<>::value_type |
---|
| 18 | #include <sstream> // for operator<<, basic_ostream, ostring... |
---|
| 19 | #include <utility> // for pair |
---|
[51b7345] | 20 | |
---|
[ea6332d] | 21 | #include "Common/SemanticError.h" // for SemanticError |
---|
[51b7345] | 22 | #include "RenameVars.h" |
---|
[ea6332d] | 23 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl, Dec... |
---|
| 24 | #include "SynTree/Expression.h" // for Expression |
---|
| 25 | #include "SynTree/Type.h" // for Type, TypeInstType, TraitInstType |
---|
| 26 | #include "SynTree/Visitor.h" // for acceptAll, maybeAccept |
---|
[51b7345] | 27 | |
---|
| 28 | namespace ResolvExpr { |
---|
[a32b204] | 29 | RenameVars global_renamer; |
---|
[51b7345] | 30 | |
---|
[a32b204] | 31 | RenameVars::RenameVars() : level( 0 ) { |
---|
| 32 | mapStack.push_front( std::map< std::string, std::string >() ); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void RenameVars::reset() { |
---|
| 36 | level = 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void RenameVars::visit( VoidType *voidType ) { |
---|
| 40 | typeBefore( voidType ); |
---|
| 41 | typeAfter( voidType ); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void RenameVars::visit( BasicType *basicType ) { |
---|
| 45 | typeBefore( basicType ); |
---|
| 46 | typeAfter( basicType ); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void RenameVars::visit( PointerType *pointerType ) { |
---|
| 50 | typeBefore( pointerType ); |
---|
| 51 | maybeAccept( pointerType->get_base(), *this ); |
---|
| 52 | typeAfter( pointerType ); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void RenameVars::visit( ArrayType *arrayType ) { |
---|
| 56 | typeBefore( arrayType ); |
---|
| 57 | maybeAccept( arrayType->get_dimension(), *this ); |
---|
| 58 | maybeAccept( arrayType->get_base(), *this ); |
---|
| 59 | typeAfter( arrayType ); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void RenameVars::visit( FunctionType *functionType ) { |
---|
| 63 | typeBefore( functionType ); |
---|
| 64 | acceptAll( functionType->get_returnVals(), *this ); |
---|
| 65 | acceptAll( functionType->get_parameters(), *this ); |
---|
| 66 | typeAfter( functionType ); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void RenameVars::visit( StructInstType *aggregateUseType ) { |
---|
| 70 | typeBefore( aggregateUseType ); |
---|
| 71 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 72 | typeAfter( aggregateUseType ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void RenameVars::visit( UnionInstType *aggregateUseType ) { |
---|
| 76 | typeBefore( aggregateUseType ); |
---|
| 77 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 78 | typeAfter( aggregateUseType ); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void RenameVars::visit( EnumInstType *aggregateUseType ) { |
---|
| 82 | typeBefore( aggregateUseType ); |
---|
| 83 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 84 | typeAfter( aggregateUseType ); |
---|
| 85 | } |
---|
| 86 | |
---|
[4040425] | 87 | void RenameVars::visit( TraitInstType *aggregateUseType ) { |
---|
[a32b204] | 88 | typeBefore( aggregateUseType ); |
---|
| 89 | acceptAll( aggregateUseType->get_parameters(), *this ); |
---|
| 90 | typeAfter( aggregateUseType ); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void RenameVars::visit( TypeInstType *instType ) { |
---|
| 94 | typeBefore( instType ); |
---|
| 95 | std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() ); |
---|
| 96 | if ( i != mapStack.front().end() ) { |
---|
| 97 | instType->set_name( i->second ); |
---|
| 98 | } else { |
---|
| 99 | } // if |
---|
| 100 | acceptAll( instType->get_parameters(), *this ); |
---|
| 101 | typeAfter( instType ); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | void RenameVars::visit( TupleType *tupleType ) { |
---|
| 105 | typeBefore( tupleType ); |
---|
| 106 | acceptAll( tupleType->get_types(), *this ); |
---|
| 107 | typeAfter( tupleType ); |
---|
| 108 | } |
---|
| 109 | |
---|
[44b7088] | 110 | void RenameVars::visit( VarArgsType *varArgsType ) { |
---|
| 111 | typeBefore( varArgsType ); |
---|
| 112 | typeAfter( varArgsType ); |
---|
| 113 | } |
---|
| 114 | |
---|
[89e6ffc] | 115 | void RenameVars::visit( ZeroType *zeroType ) { |
---|
| 116 | typeBefore( zeroType ); |
---|
| 117 | typeAfter( zeroType ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void RenameVars::visit( OneType *oneType ) { |
---|
| 121 | typeBefore( oneType ); |
---|
| 122 | typeAfter( oneType ); |
---|
| 123 | } |
---|
| 124 | |
---|
[a32b204] | 125 | void RenameVars::typeBefore( Type *type ) { |
---|
| 126 | if ( ! type->get_forall().empty() ) { |
---|
[0f19d763] | 127 | // copies current name mapping into new mapping |
---|
[a32b204] | 128 | mapStack.push_front( mapStack.front() ); |
---|
[0f19d763] | 129 | // renames all "forall" type names to `_${level}_${name}' |
---|
[8c49c0e] | 130 | for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) { |
---|
[5f2f2d7] | 131 | std::ostringstream output; |
---|
[a32b204] | 132 | output << "_" << level << "_" << (*i)->get_name(); |
---|
[5f2f2d7] | 133 | std::string newname( output.str() ); |
---|
[a32b204] | 134 | mapStack.front()[ (*i)->get_name() ] = newname; |
---|
| 135 | (*i)->set_name( newname ); |
---|
[0f19d763] | 136 | // ditto for assertion names, the next level in |
---|
[a32b204] | 137 | level++; |
---|
| 138 | acceptAll( (*i)->get_assertions(), *this ); |
---|
| 139 | } // for |
---|
| 140 | } // if |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void RenameVars::typeAfter( Type *type ) { |
---|
[0f19d763] | 144 | // clears name mapping added by typeBefore() |
---|
[a32b204] | 145 | if ( ! type->get_forall().empty() ) { |
---|
| 146 | mapStack.pop_front(); |
---|
| 147 | } // if |
---|
| 148 | } |
---|
[51b7345] | 149 | |
---|
| 150 | } // namespace ResolvExpr |
---|
[a32b204] | 151 | |
---|
| 152 | // Local Variables: // |
---|
| 153 | // tab-width: 4 // |
---|
| 154 | // mode: c++ // |
---|
| 155 | // compile-command: "make install" // |
---|
| 156 | // End: // |
---|