| 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 |
|
|---|
| 14 | TypedefTable::TypedefTable() : currentScope(0) {}
|
|---|
| 15 |
|
|---|
| 16 | bool TypedefTable::isKind(string identifier, kind_t kind) const {
|
|---|
| 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 |
|
|---|
| 25 | bool TypedefTable::isIdentifier(string identifier) const {
|
|---|
| 26 | return isKind(identifier, ID);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | bool TypedefTable::isTypedef(string identifier) const {
|
|---|
| 30 | return isKind(identifier, TD);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | bool TypedefTable::isTypegen(string identifier) const {
|
|---|
| 34 | return isKind(identifier, TG);
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 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 );
|
|---|
| 41 | } else {
|
|---|
| 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 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void TypedefTable::addToCurrentScope(const std::string &identifier, kind_t kind) {
|
|---|
| 60 | addToScope( identifier, kind, currentScope );
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | void TypedefTable::addToCurrentScope(kind_t kind) {
|
|---|
| 64 | addToCurrentScope( nextIdentifiers.top(), kind );
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void TypedefTable::addToEnclosingScope(const std::string &identifier, kind_t kind) {
|
|---|
| 68 | assert( currentScope >= 1 );
|
|---|
| 69 | addToScope( identifier, kind, currentScope - 1 );
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void TypedefTable::addToEnclosingScope(kind_t kind) {
|
|---|
| 73 | addToEnclosingScope( nextIdentifiers.top(), kind );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | void TypedefTable::addToEnclosingScope2(const std::string &identifier, kind_t kind) {
|
|---|
| 77 | assert( currentScope >= 2 );
|
|---|
| 78 | addToScope( identifier, kind, currentScope - 2 );
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | void TypedefTable::addToEnclosingScope2(kind_t kind) {
|
|---|
| 82 | addToEnclosingScope2( nextIdentifiers.top(), kind );
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void TypedefTable::setNextIdentifier( const std::string &identifier ) {
|
|---|
| 86 | nextIdentifiers.top() = identifier;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 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 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | void TypedefTable::enterScope(void) {
|
|---|
| 100 | currentScope += 1;
|
|---|
| 101 | deferListStack.push( deferListType() );
|
|---|
| 102 | nextIdentifiers.push( "" );
|
|---|
| 103 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void TypedefTable::leaveScope(void) {
|
|---|
| 107 | debugPrint( "Leaving scope " << currentScope << endl );
|
|---|
| 108 | for (tableType::iterator i = table.begin(); i != table.end(); ) {
|
|---|
| 109 | list<Entry> &declList = (*i).second;
|
|---|
| 110 | while (!declList.empty() && declList.front().scope == currentScope) {
|
|---|
| 111 | declList.pop_front();
|
|---|
| 112 | }
|
|---|
| 113 | if ( declList.empty() ) { // standard idom for erasing during traversal
|
|---|
| 114 | table.erase( i++ );
|
|---|
| 115 | } else ++i;
|
|---|
| 116 | }
|
|---|
| 117 | currentScope -= 1;
|
|---|
| 118 | for (deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++) {
|
|---|
| 119 | addToCurrentScope( i->identifier, i->kind );
|
|---|
| 120 | }
|
|---|
| 121 | deferListStack.pop();
|
|---|
| 122 | debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl );
|
|---|
| 123 | nextIdentifiers.pop();
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void TypedefTable::enterContext( std::string contextName ) {
|
|---|
| 127 | currentContext = contextName;
|
|---|
| 128 | contextScope = currentScope;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void TypedefTable::leaveContext(void) {
|
|---|
| 132 | currentContext = "";
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | void TypedefTable::print(void) const {
|
|---|
| 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 | }
|
|---|