source: src/ResolvExpr/RenameVars.cc @ 13de4478

Last change on this file since 13de4478 was 13de4478, checked in by Andrew Beach <ajbeach@…>, 8 weeks ago

Updated files in ResolvExpr? to the new indentation style. It seems the remaining places have reason to break from the style.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[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
[f5edcb4]11// Last Modified By : Andrew Beach
12// Last Modified On : Thr Jun 20 17:39:00 2019
13// Update Count     : 8
[a32b204]14//
[51b7345]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
[51b7345]20
[f5edcb4]21#include "AST/Pass.hpp"
22#include "AST/Type.hpp"
23#include "Common/ScopedMap.h"
[ea6332d]24#include "Common/SemanticError.h"  // for SemanticError
[51b7345]25#include "RenameVars.h"
26
[99da267]27#include "AST/Copy.hpp"
28
[51b7345]29namespace ResolvExpr {
[ad51cc2]30
[f5edcb4]31namespace {
[ad51cc2]32
[13de4478]33class RenamingData {
34        int level = 0;
35        int resetCount = 0;
36
37        int next_expr_id = 1;
38        int next_usage_id = 1;
39        ScopedMap< std::string, std::string > nameMap;
40        ScopedMap< std::string, ast::TypeEnvKey > idMap;
41public:
42        void reset() {
43                level = 0;
44                ++resetCount;
45        }
[3e5dd913]46
[13de4478]47        void nextUsage() {
48                ++next_usage_id;
49        }
50
51        const ast::TypeInstType * rename( const ast::TypeInstType * type ) {
52                auto it = idMap.find( type->name );
53                if ( it == idMap.end() ) return type;
54
55                // Unconditionally mutate because map will *always* have different name.
56                ast::TypeInstType * mut = ast::shallowCopy( type );
57                // Reconcile base node since some copies might have been made.
58                mut->base = it->second.base;
59                mut->formal_usage = it->second.formal_usage;
60                mut->expr_id = it->second.expr_id;
61                return mut;
62        }
[f5edcb4]63
[13de4478]64        const ast::FunctionType * openLevel( const ast::FunctionType * type, RenameMode mode ) {
65                if ( type->forall.empty() ) return type;
66                idMap.beginScope();
67
68                // Load new names from this forall clause and perform renaming.
69                auto mutType = ast::shallowCopy( type );
70                // assert( type == mutType && "mutated type must be unique from ForallSubstitutor" );
71                for ( auto & td : mutType->forall ) {
72                        auto mut = ast::shallowCopy( td.get() );
73                        // assert( td == mutDecl && "mutated decl must be unique from ForallSubstitutor" );
74
75                        if ( mode == GEN_EXPR_ID ) {
76                                mut->expr_id = next_expr_id;
77                                mut->formal_usage = -1;
78                                ++next_expr_id;
79                        } else if ( mode == GEN_USAGE ) {
80                                assertf( mut->expr_id, "unfilled expression id in generating candidate type" );
81                                mut->formal_usage = next_usage_id;
82                        } else {
83                                assert(false);
[f5edcb4]84                        }
[13de4478]85                        idMap[ td->name ] = ast::TypeEnvKey( *mut );
[e0e9a0b]86
[13de4478]87                        td = mut;
[ad51cc2]88                }
[898ae07]89
[13de4478]90                return mutType;
91        }
[f5edcb4]92
[13de4478]93        void closeLevel( const ast::FunctionType * type ) {
94                if ( type->forall.empty() ) return;
95                idMap.endScope();
96        }
97};
[f5edcb4]98
[13de4478]99// Global State:
100RenamingData renaming;
[f5edcb4]101
[13de4478]102struct RenameVars final : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
103        RenameMode mode;
[361bf01]104
[13de4478]105        const ast::FunctionType * previsit( const ast::FunctionType * type ) {
106                return renaming.openLevel( type, mode );
107        }
[361bf01]108
[13de4478]109        /*
110        const ast::StructInstType * previsit( const ast::StructInstType * type ) {
111                return renaming.openLevel( type );
112        }
113        const ast::UnionInstType * previsit( const ast::UnionInstType * type ) {
114                return renaming.openLevel( type );
115        }
116        const ast::TraitInstType * previsit( const ast::TraitInstType * type ) {
117                return renaming.openLevel( type );
118        }
119        */
120
121        const ast::TypeInstType * previsit( const ast::TypeInstType * type ) {
122                // Do not rename an actual type.
123                if ( mode == GEN_USAGE && !type->formal_usage ) return type;
124                return renaming.rename( type );
125        }
126        void postvisit( const ast::FunctionType * type ) {
127                renaming.closeLevel( type );
128        }
129};
[f5edcb4]130
131} // namespace
132
[7583c02]133const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
[0bd3faf]134        ast::Pass<RenameVars> renamer;
[3e5dd913]135        renamer.core.mode = mode;
[7583c02]136        if (mode == GEN_USAGE && reset) {
[3e5dd913]137                renaming.nextUsage();
138        }
139        return t->accept( renamer );
[f5edcb4]140}
141
142void resetTyVarRenaming() {
143        renaming.reset();
[7583c02]144        renaming.nextUsage();
[f5edcb4]145}
146
[51b7345]147} // namespace ResolvExpr
[a32b204]148
149// Local Variables: //
150// tab-width: 4 //
151// mode: c++ //
152// compile-command: "make install" //
153// End: //
Note: See TracBrowser for help on using the repository browser.