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 | // TypedefTable.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Sat May 16 15:20:13 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Mar 21 18:18:58 2016 |
---|
13 | // Update Count : 23 |
---|
14 | // |
---|
15 | |
---|
16 | #include <map> |
---|
17 | #include <list> |
---|
18 | #include <cassert> |
---|
19 | #include "TypedefTable.h" |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | #if 0 |
---|
23 | #include <iostream> |
---|
24 | #define debugPrint( x ) cerr << x |
---|
25 | #else |
---|
26 | #define debugPrint( x ) |
---|
27 | #endif |
---|
28 | |
---|
29 | TypedefTable::TypedefTable() : currentScope( 0 ) {} |
---|
30 | |
---|
31 | bool TypedefTable::exists( const string &identifier ) { |
---|
32 | return table.count( identifier ) > 0; |
---|
33 | } |
---|
34 | |
---|
35 | void TypedefTable::changeKind( const string &identifier, kind_t kind ) { |
---|
36 | tableType::iterator id_pos = table.find( identifier ); |
---|
37 | if ( id_pos == table.end() ) return; |
---|
38 | id_pos->second.begin()->kind = kind; |
---|
39 | } |
---|
40 | |
---|
41 | int TypedefTable::isKind( const string &identifier ) const { |
---|
42 | tableType::const_iterator id_pos = table.find( identifier ); |
---|
43 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. |
---|
44 | if ( id_pos == table.end() ) return IDENTIFIER; |
---|
45 | return id_pos->second.begin()->kind; |
---|
46 | } |
---|
47 | |
---|
48 | void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) { |
---|
49 | if ( currentTrait != "" && scope == contextScope ) { |
---|
50 | DeferredEntry entry = { identifier, kind }; |
---|
51 | contexts[currentTrait].push_back( entry ); |
---|
52 | } else { |
---|
53 | debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << " from scope " << currentScope << endl ); |
---|
54 | Entry newEntry = { scope, kind }; |
---|
55 | tableType::iterator curPos = table.find( identifier ); |
---|
56 | if ( curPos == table.end()) { |
---|
57 | list<Entry> newList; |
---|
58 | newList.push_front( newEntry ); |
---|
59 | table[identifier] = newList; |
---|
60 | } else { |
---|
61 | list<Entry>::iterator listPos = (*curPos ).second.begin(); |
---|
62 | while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) { |
---|
63 | listPos++; |
---|
64 | } // while |
---|
65 | (*curPos ).second.insert( listPos, newEntry ); |
---|
66 | } // if |
---|
67 | } // if |
---|
68 | } |
---|
69 | |
---|
70 | void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) { |
---|
71 | addToScope( identifier, kind, currentScope ); |
---|
72 | } |
---|
73 | |
---|
74 | void TypedefTable::addToCurrentScope( kind_t kind ) { |
---|
75 | addToCurrentScope( nextIdentifiers.top(), kind ); |
---|
76 | } |
---|
77 | |
---|
78 | void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) { |
---|
79 | assert( currentScope >= 1 ); |
---|
80 | addToScope( identifier, kind, currentScope - 1 ); |
---|
81 | } |
---|
82 | |
---|
83 | void TypedefTable::addToEnclosingScope( kind_t kind ) { |
---|
84 | addToEnclosingScope( nextIdentifiers.top(), kind ); |
---|
85 | } |
---|
86 | |
---|
87 | void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) { |
---|
88 | assert( currentScope >= 2 ); |
---|
89 | addToScope( identifier, kind, currentScope - 2 ); |
---|
90 | } |
---|
91 | |
---|
92 | void TypedefTable::addToEnclosingScope2( kind_t kind ) { |
---|
93 | addToEnclosingScope2( nextIdentifiers.top(), kind ); |
---|
94 | } |
---|
95 | |
---|
96 | void TypedefTable::setNextIdentifier( const std::string &identifier ) { |
---|
97 | nextIdentifiers.top() = identifier; |
---|
98 | } |
---|
99 | |
---|
100 | void TypedefTable::openTrait( const std::string &contextName ) { |
---|
101 | map< string, deferListType >::iterator i = contexts.find( contextName ); |
---|
102 | if ( i != contexts.end() ) { |
---|
103 | deferListType &entries = i->second; |
---|
104 | for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) { |
---|
105 | addToEnclosingScope( i->identifier, i->kind ); |
---|
106 | } // for |
---|
107 | } // if |
---|
108 | } |
---|
109 | |
---|
110 | void TypedefTable::enterScope() { |
---|
111 | currentScope += 1; |
---|
112 | deferListStack.push( deferListType() ); |
---|
113 | nextIdentifiers.push( "" ); |
---|
114 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl ); |
---|
115 | } |
---|
116 | |
---|
117 | void TypedefTable::leaveScope() { |
---|
118 | debugPrint( "Leaving scope " << currentScope << endl ); |
---|
119 | for ( tableType::iterator i = table.begin(); i != table.end(); ) { |
---|
120 | list<Entry> &declList = (*i).second; |
---|
121 | while ( ! declList.empty() && declList.front().scope == currentScope ) { |
---|
122 | declList.pop_front(); |
---|
123 | } |
---|
124 | if ( declList.empty() ) { // standard idom for erasing during traversal |
---|
125 | table.erase( i++ ); |
---|
126 | } else |
---|
127 | ++i; |
---|
128 | } // for |
---|
129 | currentScope -= 1; |
---|
130 | for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++ ) { |
---|
131 | addToCurrentScope( i->identifier, i->kind ); |
---|
132 | } // for |
---|
133 | deferListStack.pop(); |
---|
134 | debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); |
---|
135 | nextIdentifiers.pop(); |
---|
136 | } |
---|
137 | |
---|
138 | void TypedefTable::enterTrait( const std::string &contextName ) { |
---|
139 | currentTrait = contextName; |
---|
140 | contextScope = currentScope; |
---|
141 | } |
---|
142 | |
---|
143 | void TypedefTable::leaveTrait() { |
---|
144 | currentTrait = ""; |
---|
145 | } |
---|
146 | |
---|
147 | void TypedefTable::print( void ) const { |
---|
148 | for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) { |
---|
149 | debugPrint( (*i ).first << ": " ); |
---|
150 | list<Entry> declList = (*i).second; |
---|
151 | for ( list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++ ) { |
---|
152 | debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); |
---|
153 | } |
---|
154 | debugPrint( endl ); |
---|
155 | } // for |
---|
156 | } |
---|
157 | |
---|
158 | // Local Variables: // |
---|
159 | // tab-width: 4 // |
---|
160 | // mode: c++ // |
---|
161 | // compile-command: "make install" // |
---|
162 | // End: // |
---|