| 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 : Wed Mar  2 17:36:32 2016
 | 
|---|
| 13 | // Update Count     : 5
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 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
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| 22 | #include "RenameVars.h"
 | 
|---|
| 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
 | 
|---|
| 27 | 
 | 
|---|
| 28 | namespace ResolvExpr {
 | 
|---|
| 29 |         RenameVars global_renamer;
 | 
|---|
| 30 | 
 | 
|---|
| 31 |         RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
 | 
|---|
| 32 |                 mapStack.push_front( std::map< std::string, std::string >() );
 | 
|---|
| 33 |         }
 | 
|---|
| 34 | 
 | 
|---|
| 35 |         void RenameVars::reset() {
 | 
|---|
| 36 |                 level = 0;
 | 
|---|
| 37 |                 resetCount++;
 | 
|---|
| 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 | 
 | 
|---|
| 88 |         void RenameVars::visit( TraitInstType *aggregateUseType ) {
 | 
|---|
| 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 | 
 | 
|---|
| 111 |         void RenameVars::visit( VarArgsType *varArgsType ) {
 | 
|---|
| 112 |                 typeBefore( varArgsType );
 | 
|---|
| 113 |                 typeAfter( varArgsType );
 | 
|---|
| 114 |         }
 | 
|---|
| 115 | 
 | 
|---|
| 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 | 
 | 
|---|
| 126 |         void RenameVars::typeBefore( Type *type ) {
 | 
|---|
| 127 |                 if ( ! type->get_forall().empty() ) {
 | 
|---|
| 128 |                         // copies current name mapping into new mapping
 | 
|---|
| 129 |                         mapStack.push_front( mapStack.front() );
 | 
|---|
| 130 |                         // renames all "forall" type names to `_${level}_${name}'
 | 
|---|
| 131 |                         for ( Type::ForallList::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
 | 
|---|
| 132 |                                 std::ostringstream output;
 | 
|---|
| 133 |                                 output << "_" << resetCount << "_" << level << "_" << (*i)->get_name();
 | 
|---|
| 134 |                                 std::string newname( output.str() );
 | 
|---|
| 135 |                                 mapStack.front()[ (*i)->get_name() ] = newname;
 | 
|---|
| 136 |                                 (*i)->set_name( newname );
 | 
|---|
| 137 |                                 // ditto for assertion names, the next level in
 | 
|---|
| 138 |                                 level++;
 | 
|---|
| 139 |                                 acceptAll( (*i)->get_assertions(), *this );
 | 
|---|
| 140 |                         } // for
 | 
|---|
| 141 |                 } // if
 | 
|---|
| 142 |         }
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         void RenameVars::typeAfter( Type *type ) {
 | 
|---|
| 145 |                 // clears name mapping added by typeBefore()
 | 
|---|
| 146 |                 if ( ! type->get_forall().empty() ) {
 | 
|---|
| 147 |                         mapStack.pop_front();
 | 
|---|
| 148 |                 } // if
 | 
|---|
| 149 |         }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | } // namespace ResolvExpr
 | 
|---|
| 152 | 
 | 
|---|
| 153 | // Local Variables: //
 | 
|---|
| 154 | // tab-width: 4 //
 | 
|---|
| 155 | // mode: c++ //
 | 
|---|
| 156 | // compile-command: "make install" //
 | 
|---|
| 157 | // End: //
 | 
|---|