| 1 | /*
|
|---|
| 2 | * This file is part of the Cforall project
|
|---|
| 3 | *
|
|---|
| 4 | * $Id: RenameVars.cc,v 1.4 2005/08/29 20:14:16 rcbilson Exp $
|
|---|
| 5 | *
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include <strstream>
|
|---|
| 9 |
|
|---|
| 10 | #include "RenameVars.h"
|
|---|
| 11 | #include "SynTree/Visitor.h"
|
|---|
| 12 | #include "SynTree/Type.h"
|
|---|
| 13 | #include "SynTree/Declaration.h"
|
|---|
| 14 | #include "SynTree/Expression.h"
|
|---|
| 15 |
|
|---|
| 16 | namespace ResolvExpr {
|
|---|
| 17 |
|
|---|
| 18 | RenameVars global_renamer;
|
|---|
| 19 |
|
|---|
| 20 | RenameVars::RenameVars()
|
|---|
| 21 | : level( 0 )
|
|---|
| 22 | {
|
|---|
| 23 | mapStack.push_front( std::map< std::string, std::string >() );
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void
|
|---|
| 27 | RenameVars::reset()
|
|---|
| 28 | {
|
|---|
| 29 | level = 0;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void
|
|---|
| 33 | RenameVars::visit( VoidType *voidType )
|
|---|
| 34 | {
|
|---|
| 35 | typeBefore( voidType );
|
|---|
| 36 | typeAfter( voidType );
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | void
|
|---|
| 40 | RenameVars::visit( BasicType *basicType )
|
|---|
| 41 | {
|
|---|
| 42 | typeBefore( basicType );
|
|---|
| 43 | typeAfter( basicType );
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | void
|
|---|
| 47 | RenameVars::visit( PointerType *pointerType )
|
|---|
| 48 | {
|
|---|
| 49 | typeBefore( pointerType );
|
|---|
| 50 | /// std::cout << "do pointer" << std::endl;
|
|---|
| 51 | maybeAccept( pointerType->get_base(), *this );
|
|---|
| 52 | /// std::cout << "done pointer" << std::endl;
|
|---|
| 53 | typeAfter( pointerType );
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void
|
|---|
| 57 | RenameVars::visit( ArrayType *arrayType )
|
|---|
| 58 | {
|
|---|
| 59 | typeBefore( arrayType );
|
|---|
| 60 | maybeAccept( arrayType->get_dimension(), *this );
|
|---|
| 61 | maybeAccept( arrayType->get_base(), *this );
|
|---|
| 62 | typeAfter( arrayType );
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void
|
|---|
| 66 | RenameVars::visit( FunctionType *functionType )
|
|---|
| 67 | {
|
|---|
| 68 | typeBefore( functionType );
|
|---|
| 69 | /// std::cout << "return vals" << std::endl;
|
|---|
| 70 | acceptAll( functionType->get_returnVals(), *this );
|
|---|
| 71 | /// std::cout << functionType->get_parameters().size() << " parameters" << std::endl;
|
|---|
| 72 | acceptAll( functionType->get_parameters(), *this );
|
|---|
| 73 | /// std::cout << "done function" << std::endl;
|
|---|
| 74 | typeAfter( functionType );
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void
|
|---|
| 78 | RenameVars::visit( StructInstType *aggregateUseType )
|
|---|
| 79 | {
|
|---|
| 80 | typeBefore( aggregateUseType );
|
|---|
| 81 | acceptAll( aggregateUseType->get_parameters(), *this );
|
|---|
| 82 | typeAfter( aggregateUseType );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void
|
|---|
| 86 | RenameVars::visit( UnionInstType *aggregateUseType )
|
|---|
| 87 | {
|
|---|
| 88 | typeBefore( aggregateUseType );
|
|---|
| 89 | acceptAll( aggregateUseType->get_parameters(), *this );
|
|---|
| 90 | typeAfter( aggregateUseType );
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void
|
|---|
| 94 | RenameVars::visit( EnumInstType *aggregateUseType )
|
|---|
| 95 | {
|
|---|
| 96 | typeBefore( aggregateUseType );
|
|---|
| 97 | acceptAll( aggregateUseType->get_parameters(), *this );
|
|---|
| 98 | typeAfter( aggregateUseType );
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void
|
|---|
| 102 | RenameVars::visit( ContextInstType *aggregateUseType )
|
|---|
| 103 | {
|
|---|
| 104 | typeBefore( aggregateUseType );
|
|---|
| 105 | acceptAll( aggregateUseType->get_parameters(), *this );
|
|---|
| 106 | acceptAll( aggregateUseType->get_members(), *this );
|
|---|
| 107 | typeAfter( aggregateUseType );
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | void
|
|---|
| 111 | RenameVars::visit( TypeInstType *instType )
|
|---|
| 112 | {
|
|---|
| 113 | typeBefore( instType );
|
|---|
| 114 | /// std::cout << "instance of type " << instType->get_name() << std::endl;
|
|---|
| 115 | std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->get_name() );
|
|---|
| 116 | if( i != mapStack.front().end() ) {
|
|---|
| 117 | /// std::cout << "found name " << i->second << std::endl;
|
|---|
| 118 | instType->set_name( i->second );
|
|---|
| 119 | } else {
|
|---|
| 120 | /// std::cout << "no name found" << std::endl;
|
|---|
| 121 | }
|
|---|
| 122 | acceptAll( instType->get_parameters(), *this );
|
|---|
| 123 | typeAfter( instType );
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void
|
|---|
| 127 | RenameVars::visit( TupleType *tupleType )
|
|---|
| 128 | {
|
|---|
| 129 | typeBefore( tupleType );
|
|---|
| 130 | acceptAll( tupleType->get_types(), *this );
|
|---|
| 131 | typeAfter( tupleType );
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void
|
|---|
| 135 | RenameVars::typeBefore( Type *type )
|
|---|
| 136 | {
|
|---|
| 137 | if( !type->get_forall().empty() ) {
|
|---|
| 138 | /// std::cout << "type with forall: ";
|
|---|
| 139 | /// type->print( std::cout );
|
|---|
| 140 | /// std::cout << std::endl;
|
|---|
| 141 | mapStack.push_front( mapStack.front() );
|
|---|
| 142 | for( std::list< TypeDecl* >::iterator i = type->get_forall().begin(); i != type->get_forall().end(); ++i ) {
|
|---|
| 143 | std::ostrstream output;
|
|---|
| 144 | output << "_" << level << "_" << (*i)->get_name();
|
|---|
| 145 | std::string newname( output.str(), output.pcount() );
|
|---|
| 146 | mapStack.front()[ (*i)->get_name() ] = newname;
|
|---|
| 147 | (*i)->set_name( newname );
|
|---|
| 148 | level++;
|
|---|
| 149 | acceptAll( (*i)->get_assertions(), *this );
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | void
|
|---|
| 155 | RenameVars::typeAfter( Type *type )
|
|---|
| 156 | {
|
|---|
| 157 | if( !type->get_forall().empty() ) {
|
|---|
| 158 | mapStack.pop_front();
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | } // namespace ResolvExpr
|
|---|