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