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.cpp --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Sun May 17 12:05:18 2015
|
---|
11 | // Last Modified By : Andrew Beach
|
---|
12 | // Last Modified On : Thr Jun 20 17:39:00 2019
|
---|
13 | // Update Count : 8
|
---|
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, ostr...
|
---|
19 | #include <utility> // for pair
|
---|
20 |
|
---|
21 | #include "AST/Pass.hpp"
|
---|
22 | #include "AST/Type.hpp"
|
---|
23 | #include "Common/ScopedMap.hpp"
|
---|
24 | #include "Common/SemanticError.hpp" // for SemanticError
|
---|
25 | #include "RenameVars.hpp"
|
---|
26 |
|
---|
27 | #include "AST/Copy.hpp"
|
---|
28 |
|
---|
29 | namespace ResolvExpr {
|
---|
30 |
|
---|
31 | namespace {
|
---|
32 |
|
---|
33 | class 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;
|
---|
41 | public:
|
---|
42 | void reset() {
|
---|
43 | level = 0;
|
---|
44 | ++resetCount;
|
---|
45 | }
|
---|
46 |
|
---|
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 | }
|
---|
63 |
|
---|
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);
|
---|
84 | }
|
---|
85 | idMap[ td->name ] = ast::TypeEnvKey( *mut );
|
---|
86 |
|
---|
87 | td = mut;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return mutType;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void closeLevel( const ast::FunctionType * type ) {
|
---|
94 | if ( type->forall.empty() ) return;
|
---|
95 | idMap.endScope();
|
---|
96 | }
|
---|
97 | };
|
---|
98 |
|
---|
99 | // Global State:
|
---|
100 | RenamingData renaming;
|
---|
101 |
|
---|
102 | struct RenameVars final : public ast::PureVisitor /*: public ast::WithForallSubstitutor*/ {
|
---|
103 | RenameMode mode;
|
---|
104 |
|
---|
105 | const ast::FunctionType * previsit( const ast::FunctionType * type ) {
|
---|
106 | return renaming.openLevel( type, mode );
|
---|
107 | }
|
---|
108 |
|
---|
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 | };
|
---|
130 |
|
---|
131 | } // namespace
|
---|
132 |
|
---|
133 | const ast::Type * renameTyVars( const ast::Type * t, RenameMode mode, bool reset ) {
|
---|
134 | ast::Pass<RenameVars> renamer;
|
---|
135 | renamer.core.mode = mode;
|
---|
136 | if (mode == GEN_USAGE && reset) {
|
---|
137 | renaming.nextUsage();
|
---|
138 | }
|
---|
139 | return t->accept( renamer );
|
---|
140 | }
|
---|
141 |
|
---|
142 | void resetTyVarRenaming() {
|
---|
143 | renaming.reset();
|
---|
144 | renaming.nextUsage();
|
---|
145 | }
|
---|
146 |
|
---|
147 | } // namespace ResolvExpr
|
---|
148 |
|
---|
149 | // Local Variables: //
|
---|
150 | // tab-width: 4 //
|
---|
151 | // mode: c++ //
|
---|
152 | // compile-command: "make install" //
|
---|
153 | // End: //
|
---|