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