1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: TypedefTable.cc,v 1.7 2004/09/14 17:21:12 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include <map> |
---|
9 | #include <list> |
---|
10 | #include "TypedefTable.h" |
---|
11 | #include <cassert> |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | #if 0 |
---|
15 | #include <iostream> |
---|
16 | # define debugPrint(x) cerr << x |
---|
17 | #else |
---|
18 | # define debugPrint(x) |
---|
19 | #endif |
---|
20 | |
---|
21 | TypedefTable::TypedefTable() |
---|
22 | : currentScope(0) |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | bool |
---|
27 | TypedefTable::isKind(string identifier, kind_t kind) const |
---|
28 | { |
---|
29 | tableType::const_iterator id_pos = table.find(identifier); |
---|
30 | if (id_pos == table.end()) { |
---|
31 | return true; |
---|
32 | } else { |
---|
33 | return (*((*id_pos).second.begin())).kind == kind; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | bool |
---|
38 | TypedefTable::isIdentifier(string identifier) const |
---|
39 | { |
---|
40 | return isKind(identifier, ID); |
---|
41 | } |
---|
42 | |
---|
43 | bool |
---|
44 | TypedefTable::isTypedef(string identifier) const |
---|
45 | { |
---|
46 | return isKind(identifier, TD); |
---|
47 | } |
---|
48 | |
---|
49 | bool |
---|
50 | TypedefTable::isTypegen(string identifier) const |
---|
51 | { |
---|
52 | return isKind(identifier, TG); |
---|
53 | } |
---|
54 | |
---|
55 | void |
---|
56 | TypedefTable::addToScope(const std::string &identifier, kind_t kind, int scope) |
---|
57 | { |
---|
58 | if( currentContext != "" && scope == contextScope ) { |
---|
59 | DeferredEntry entry = { identifier, kind }; |
---|
60 | contexts[currentContext].push_back( entry ); |
---|
61 | } else { |
---|
62 | debugPrint( "Adding " << identifier << " as type " << kind << " scope " << scope << " from scope " << currentScope << endl ); |
---|
63 | Entry newEntry = { scope, kind }; |
---|
64 | tableType::iterator curPos = table.find(identifier); |
---|
65 | if (curPos == table.end()) { |
---|
66 | list<Entry> newList; |
---|
67 | newList.push_front(newEntry); |
---|
68 | table[identifier] = newList; |
---|
69 | } else { |
---|
70 | list<Entry>::iterator listPos = (*curPos).second.begin(); |
---|
71 | while( listPos != (*curPos).second.end() && listPos->scope > scope ) { |
---|
72 | listPos++; |
---|
73 | } |
---|
74 | (*curPos).second.insert(listPos, newEntry); |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | void |
---|
80 | TypedefTable::addToCurrentScope(const std::string &identifier, kind_t kind) |
---|
81 | { |
---|
82 | addToScope( identifier, kind, currentScope ); |
---|
83 | } |
---|
84 | |
---|
85 | void |
---|
86 | TypedefTable::addToCurrentScope(kind_t kind) |
---|
87 | { |
---|
88 | addToCurrentScope( nextIdentifiers.top(), kind ); |
---|
89 | } |
---|
90 | |
---|
91 | void |
---|
92 | TypedefTable::addToEnclosingScope(const std::string &identifier, kind_t kind) |
---|
93 | { |
---|
94 | assert( currentScope >= 1 ); |
---|
95 | addToScope( identifier, kind, currentScope - 1 ); |
---|
96 | } |
---|
97 | |
---|
98 | void |
---|
99 | TypedefTable::addToEnclosingScope(kind_t kind) |
---|
100 | { |
---|
101 | addToEnclosingScope( nextIdentifiers.top(), kind ); |
---|
102 | } |
---|
103 | |
---|
104 | void |
---|
105 | TypedefTable::addToEnclosingScope2(const std::string &identifier, kind_t kind) |
---|
106 | { |
---|
107 | assert( currentScope >= 2 ); |
---|
108 | addToScope( identifier, kind, currentScope - 2 ); |
---|
109 | } |
---|
110 | |
---|
111 | void |
---|
112 | TypedefTable::addToEnclosingScope2(kind_t kind) |
---|
113 | { |
---|
114 | addToEnclosingScope2( nextIdentifiers.top(), kind ); |
---|
115 | } |
---|
116 | |
---|
117 | void |
---|
118 | TypedefTable::setNextIdentifier( const std::string &identifier ) |
---|
119 | { |
---|
120 | nextIdentifiers.top() = identifier; |
---|
121 | } |
---|
122 | |
---|
123 | void |
---|
124 | TypedefTable::openContext( std::string contextName ) |
---|
125 | { |
---|
126 | map< string, deferListType >::iterator i = contexts.find( contextName ); |
---|
127 | if( i != contexts.end() ) { |
---|
128 | deferListType &entries = i->second; |
---|
129 | for (deferListType::iterator i = entries.begin(); i != entries.end(); i++) { |
---|
130 | addToEnclosingScope( i->identifier, i->kind ); |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void |
---|
136 | TypedefTable::enterScope(void) |
---|
137 | { |
---|
138 | currentScope += 1; |
---|
139 | deferListStack.push( deferListType() ); |
---|
140 | nextIdentifiers.push( "" ); |
---|
141 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl ); |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | TypedefTable::leaveScope(void) |
---|
146 | { |
---|
147 | debugPrint( "Leaving scope " << currentScope << endl ); |
---|
148 | for (tableType::iterator i = table.begin(); i != table.end(); i++) { |
---|
149 | list<Entry> &declList = (*i).second; |
---|
150 | while (!declList.empty() && declList.front().scope == currentScope) { |
---|
151 | declList.pop_front(); |
---|
152 | } |
---|
153 | if( declList.empty() ) { |
---|
154 | table.erase( i ); |
---|
155 | } |
---|
156 | } |
---|
157 | currentScope -= 1; |
---|
158 | for (deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++) { |
---|
159 | addToCurrentScope( i->identifier, i->kind ); |
---|
160 | } |
---|
161 | deferListStack.pop(); |
---|
162 | debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); |
---|
163 | nextIdentifiers.pop(); |
---|
164 | } |
---|
165 | |
---|
166 | void |
---|
167 | TypedefTable::enterContext( std::string contextName ) |
---|
168 | { |
---|
169 | currentContext = contextName; |
---|
170 | contextScope = currentScope; |
---|
171 | } |
---|
172 | |
---|
173 | void |
---|
174 | TypedefTable::leaveContext(void) |
---|
175 | { |
---|
176 | currentContext = ""; |
---|
177 | } |
---|
178 | |
---|
179 | void |
---|
180 | TypedefTable::print(void) const |
---|
181 | { |
---|
182 | for (tableType::const_iterator i = table.begin(); i != table.end(); i++) { |
---|
183 | debugPrint( (*i).first << ": " ); |
---|
184 | list<Entry> declList = (*i).second; |
---|
185 | for (list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++) { |
---|
186 | debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); |
---|
187 | } |
---|
188 | debugPrint( endl ); |
---|
189 | } |
---|
190 | } |
---|