| 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 ) { | 
|---|
| 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 |  | 
|---|
| 87 | void RenameVars::visit( TraitInstType *aggregateUseType ) { | 
|---|
| 88 | typeBefore( aggregateUseType ); | 
|---|
| 89 | acceptAll( aggregateUseType->get_parameters(), *this ); | 
|---|
| 90 | acceptAll( aggregateUseType->get_members(), *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 << "_" << 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: // | 
|---|