[51b7345] | 1 | #include <map> |
---|
| 2 | #include <list> |
---|
| 3 | #include "TypedefTable.h" |
---|
| 4 | #include <cassert> |
---|
| 5 | using namespace std; |
---|
| 6 | |
---|
| 7 | #if 0 |
---|
| 8 | #include <iostream> |
---|
| 9 | # define debugPrint(x) cerr << x |
---|
| 10 | #else |
---|
| 11 | # define debugPrint(x) |
---|
| 12 | #endif |
---|
| 13 | |
---|
[8c17ab0] | 14 | TypedefTable::TypedefTable() : currentScope(0) {} |
---|
[51b7345] | 15 | |
---|
[8c17ab0] | 16 | bool TypedefTable::isKind(string identifier, kind_t kind) const { |
---|
[51b7345] | 17 | tableType::const_iterator id_pos = table.find(identifier); |
---|
| 18 | if (id_pos == table.end()) { |
---|
| 19 | return true; |
---|
| 20 | } else { |
---|
| 21 | return (*((*id_pos).second.begin())).kind == kind; |
---|
| 22 | } |
---|
| 23 | } |
---|
| 24 | |
---|
[8c17ab0] | 25 | bool TypedefTable::isIdentifier(string identifier) const { |
---|
[51b7345] | 26 | return isKind(identifier, ID); |
---|
| 27 | } |
---|
| 28 | |
---|
[8c17ab0] | 29 | bool TypedefTable::isTypedef(string identifier) const { |
---|
[51b7345] | 30 | return isKind(identifier, TD); |
---|
| 31 | } |
---|
| 32 | |
---|
[8c17ab0] | 33 | bool TypedefTable::isTypegen(string identifier) const { |
---|
[51b7345] | 34 | return isKind(identifier, TG); |
---|
| 35 | } |
---|
| 36 | |
---|
[8c17ab0] | 37 | void TypedefTable::addToScope(const std::string &identifier, kind_t kind, int scope) { |
---|
| 38 | if ( currentContext != "" && scope == contextScope ) { |
---|
| 39 | DeferredEntry entry = { identifier, kind }; |
---|
| 40 | contexts[currentContext].push_back( entry ); |
---|
[51b7345] | 41 | } else { |
---|
[8c17ab0] | 42 | debugPrint( "Adding " << identifier << " as type " << kind << " scope " << scope << " from scope " << currentScope << endl ); |
---|
| 43 | Entry newEntry = { scope, kind }; |
---|
| 44 | tableType::iterator curPos = table.find(identifier); |
---|
| 45 | if (curPos == table.end()) { |
---|
| 46 | list<Entry> newList; |
---|
| 47 | newList.push_front(newEntry); |
---|
| 48 | table[identifier] = newList; |
---|
| 49 | } else { |
---|
| 50 | list<Entry>::iterator listPos = (*curPos).second.begin(); |
---|
| 51 | while( listPos != (*curPos).second.end() && listPos->scope > scope ) { |
---|
| 52 | listPos++; |
---|
| 53 | } |
---|
| 54 | (*curPos).second.insert(listPos, newEntry); |
---|
| 55 | } |
---|
[51b7345] | 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
[8c17ab0] | 59 | void TypedefTable::addToCurrentScope(const std::string &identifier, kind_t kind) { |
---|
| 60 | addToScope( identifier, kind, currentScope ); |
---|
[51b7345] | 61 | } |
---|
| 62 | |
---|
[8c17ab0] | 63 | void TypedefTable::addToCurrentScope(kind_t kind) { |
---|
| 64 | addToCurrentScope( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 65 | } |
---|
| 66 | |
---|
[8c17ab0] | 67 | void TypedefTable::addToEnclosingScope(const std::string &identifier, kind_t kind) { |
---|
| 68 | assert( currentScope >= 1 ); |
---|
| 69 | addToScope( identifier, kind, currentScope - 1 ); |
---|
[51b7345] | 70 | } |
---|
| 71 | |
---|
[8c17ab0] | 72 | void TypedefTable::addToEnclosingScope(kind_t kind) { |
---|
| 73 | addToEnclosingScope( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 74 | } |
---|
| 75 | |
---|
[8c17ab0] | 76 | void TypedefTable::addToEnclosingScope2(const std::string &identifier, kind_t kind) { |
---|
| 77 | assert( currentScope >= 2 ); |
---|
| 78 | addToScope( identifier, kind, currentScope - 2 ); |
---|
[51b7345] | 79 | } |
---|
| 80 | |
---|
[8c17ab0] | 81 | void TypedefTable::addToEnclosingScope2(kind_t kind) { |
---|
| 82 | addToEnclosingScope2( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 83 | } |
---|
| 84 | |
---|
[8c17ab0] | 85 | void TypedefTable::setNextIdentifier( const std::string &identifier ) { |
---|
| 86 | nextIdentifiers.top() = identifier; |
---|
[51b7345] | 87 | } |
---|
| 88 | |
---|
[8c17ab0] | 89 | void TypedefTable::openContext( std::string contextName ) { |
---|
| 90 | map< string, deferListType >::iterator i = contexts.find( contextName ); |
---|
| 91 | if ( i != contexts.end() ) { |
---|
| 92 | deferListType &entries = i->second; |
---|
| 93 | for (deferListType::iterator i = entries.begin(); i != entries.end(); i++) { |
---|
| 94 | addToEnclosingScope( i->identifier, i->kind ); |
---|
| 95 | } |
---|
[51b7345] | 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
[8c17ab0] | 99 | void TypedefTable::enterScope(void) { |
---|
[51b7345] | 100 | currentScope += 1; |
---|
| 101 | deferListStack.push( deferListType() ); |
---|
| 102 | nextIdentifiers.push( "" ); |
---|
| 103 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl ); |
---|
| 104 | } |
---|
| 105 | |
---|
[8c17ab0] | 106 | void TypedefTable::leaveScope(void) { |
---|
[51b7345] | 107 | debugPrint( "Leaving scope " << currentScope << endl ); |
---|
[0b8cd722] | 108 | for (tableType::iterator i = table.begin(); i != table.end(); ) { |
---|
[51b7345] | 109 | list<Entry> &declList = (*i).second; |
---|
| 110 | while (!declList.empty() && declList.front().scope == currentScope) { |
---|
| 111 | declList.pop_front(); |
---|
| 112 | } |
---|
[0b8cd722] | 113 | if ( declList.empty() ) { // standard idom for erasing during traversal |
---|
| 114 | table.erase( i++ ); |
---|
| 115 | } else ++i; |
---|
[51b7345] | 116 | } |
---|
| 117 | currentScope -= 1; |
---|
| 118 | for (deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++) { |
---|
[8c17ab0] | 119 | addToCurrentScope( i->identifier, i->kind ); |
---|
[51b7345] | 120 | } |
---|
| 121 | deferListStack.pop(); |
---|
| 122 | debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); |
---|
| 123 | nextIdentifiers.pop(); |
---|
| 124 | } |
---|
| 125 | |
---|
[8c17ab0] | 126 | void TypedefTable::enterContext( std::string contextName ) { |
---|
| 127 | currentContext = contextName; |
---|
| 128 | contextScope = currentScope; |
---|
[51b7345] | 129 | } |
---|
| 130 | |
---|
[8c17ab0] | 131 | void TypedefTable::leaveContext(void) { |
---|
| 132 | currentContext = ""; |
---|
[51b7345] | 133 | } |
---|
| 134 | |
---|
[8c17ab0] | 135 | void TypedefTable::print(void) const { |
---|
[51b7345] | 136 | for (tableType::const_iterator i = table.begin(); i != table.end(); i++) { |
---|
| 137 | debugPrint( (*i).first << ": " ); |
---|
| 138 | list<Entry> declList = (*i).second; |
---|
| 139 | for (list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++) { |
---|
| 140 | debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); |
---|
| 141 | } |
---|
| 142 | debugPrint( endl ); |
---|
| 143 | } |
---|
| 144 | } |
---|