| [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 | 
 | 
|---|
| [ad51cc2] | 21 | #include "Common/PassVisitor.h"
 | 
|---|
| [ea6332d] | 22 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| [51b73452] | 23 | #include "RenameVars.h"
 | 
|---|
| [ea6332d] | 24 | #include "SynTree/Declaration.h"   // for DeclarationWithType, TypeDecl, Dec...
 | 
|---|
 | 25 | #include "SynTree/Expression.h"    // for Expression
 | 
|---|
 | 26 | #include "SynTree/Type.h"          // for Type, TypeInstType, TraitInstType
 | 
|---|
 | 27 | #include "SynTree/Visitor.h"       // for acceptAll, maybeAccept
 | 
|---|
| [51b73452] | 28 | 
 | 
|---|
 | 29 | namespace ResolvExpr {
 | 
|---|
| [ad51cc2] | 30 |         namespace {
 | 
|---|
 | 31 |                 struct RenameVars {
 | 
|---|
 | 32 |                         RenameVars();
 | 
|---|
 | 33 |                         void reset();
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |                         void previsit( TypeInstType * instType );
 | 
|---|
 | 36 |                         void previsit( Type * );
 | 
|---|
 | 37 |                         void postvisit( Type * );
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 |                   private:
 | 
|---|
 | 40 |                         int level, resetCount;
 | 
|---|
 | 41 |                         std::list< std::map< std::string, std::string > > mapStack;
 | 
|---|
 | 42 |                 };
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 |                 PassVisitor<RenameVars> global_renamer;
 | 
|---|
 | 45 |         } // namespace
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |         void renameTyVars( Type * t ) {
 | 
|---|
 | 48 |                 t->accept( global_renamer );
 | 
|---|
 | 49 |         }
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |         void resetTyVarRenaming() {
 | 
|---|
 | 52 |                 global_renamer.pass.reset();
 | 
|---|
 | 53 |         }
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |         namespace {
 | 
|---|
 | 56 |                 RenameVars::RenameVars() : level( 0 ), resetCount( 0 ) {
 | 
|---|
 | 57 |                         mapStack.push_front( std::map< std::string, std::string >() );
 | 
|---|
 | 58 |                 }
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 |                 void RenameVars::reset() {
 | 
|---|
 | 61 |                         level = 0;
 | 
|---|
 | 62 |                         resetCount++;
 | 
|---|
 | 63 |                 }
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 |                 void RenameVars::previsit( TypeInstType * instType ) {
 | 
|---|
 | 66 |                         previsit( (Type *)instType );
 | 
|---|
 | 67 |                         std::map< std::string, std::string >::const_iterator i = mapStack.front().find( instType->name );
 | 
|---|
 | 68 |                         if ( i != mapStack.front().end() ) {
 | 
|---|
 | 69 |                                 instType->name = i->second;
 | 
|---|
 | 70 |                         } // if
 | 
|---|
 | 71 |                 }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |                 void RenameVars::previsit( Type * type ) {
 | 
|---|
 | 74 |                         if ( ! type->forall.empty() ) {
 | 
|---|
 | 75 |                                 // copies current name mapping into new mapping
 | 
|---|
 | 76 |                                 mapStack.push_front( mapStack.front() );
 | 
|---|
 | 77 |                                 // renames all "forall" type names to `_${level}_${name}'
 | 
|---|
 | 78 |                                 for ( auto td : type->forall ) {
 | 
|---|
 | 79 |                                         std::ostringstream output;
 | 
|---|
 | 80 |                                         output << "_" << resetCount << "_" << level << "_" << td->name;
 | 
|---|
 | 81 |                                         std::string newname( output.str() );
 | 
|---|
 | 82 |                                         mapStack.front()[ td->get_name() ] = newname;
 | 
|---|
 | 83 |                                         td->name = newname;
 | 
|---|
 | 84 |                                         // ditto for assertion names, the next level in
 | 
|---|
 | 85 |                                         level++;
 | 
|---|
 | 86 |                                         // acceptAll( td->assertions, *this );
 | 
|---|
 | 87 |                                 } // for
 | 
|---|
 | 88 |                         } // if
 | 
|---|
 | 89 |                 }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |                 void RenameVars::postvisit( Type * type ) {
 | 
|---|
 | 92 |                         // clears name mapping added by typeBefore()
 | 
|---|
 | 93 |                         if ( ! type->forall.empty() ) {
 | 
|---|
 | 94 |                                 mapStack.pop_front();
 | 
|---|
 | 95 |                         } // if
 | 
|---|
 | 96 |                 }
 | 
|---|
 | 97 |         } // namespace
 | 
|---|
| [51b73452] | 98 | } // namespace ResolvExpr
 | 
|---|
| [a32b204] | 99 | 
 | 
|---|
 | 100 | // Local Variables: //
 | 
|---|
 | 101 | // tab-width: 4 //
 | 
|---|
 | 102 | // mode: c++ //
 | 
|---|
 | 103 | // compile-command: "make install" //
 | 
|---|
 | 104 | // End: //
 | 
|---|