[b87a5ed] | 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 |
---|
[721f17a] | 12 | // Last Modified On : Fri Jun 26 07:30:16 2015 |
---|
| 13 | // Update Count : 19 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <map> |
---|
| 17 | #include <list> |
---|
| 18 | #include "TypedefTable.h" |
---|
| 19 | #include <cassert> |
---|
| 20 | using namespace std; |
---|
| 21 | |
---|
| 22 | #if 0 |
---|
| 23 | #include <iostream> |
---|
[721f17a] | 24 | #define debugPrint( x ) cerr << x |
---|
[51b7345] | 25 | #else |
---|
[b87a5ed] | 26 | #define debugPrint( x ) |
---|
[51b7345] | 27 | #endif |
---|
| 28 | |
---|
[b87a5ed] | 29 | TypedefTable::TypedefTable() : currentScope( 0 ) {} |
---|
[51b7345] | 30 | |
---|
[721f17a] | 31 | void TypedefTable::changeKind( const string &identifier, kind_t kind ) { |
---|
| 32 | tableType::iterator id_pos = table.find( identifier ); |
---|
| 33 | if ( id_pos == table.end() ) { |
---|
| 34 | return; |
---|
| 35 | } else { |
---|
| 36 | (*((*id_pos ).second.begin())).kind = kind; |
---|
| 37 | return; |
---|
| 38 | } // if |
---|
| 39 | } |
---|
| 40 | |
---|
[de62360d] | 41 | bool TypedefTable::isKind( const string &identifier, kind_t kind ) const { |
---|
[b87a5ed] | 42 | tableType::const_iterator id_pos = table.find( identifier ); |
---|
[de62360d] | 43 | if ( id_pos == table.end() ) { |
---|
[b87a5ed] | 44 | return true; |
---|
| 45 | } else { |
---|
| 46 | return (*((*id_pos ).second.begin())).kind == kind; |
---|
[de62360d] | 47 | } // if |
---|
[51b7345] | 48 | } |
---|
| 49 | |
---|
[de62360d] | 50 | bool TypedefTable::isIdentifier( const string &identifier ) const { |
---|
[b87a5ed] | 51 | return isKind( identifier, ID ); |
---|
[51b7345] | 52 | } |
---|
| 53 | |
---|
[de62360d] | 54 | bool TypedefTable::isTypedef( const string &identifier ) const { |
---|
[b87a5ed] | 55 | return isKind( identifier, TD ); |
---|
[51b7345] | 56 | } |
---|
| 57 | |
---|
[de62360d] | 58 | bool TypedefTable::isTypegen( const string &identifier ) const { |
---|
[b87a5ed] | 59 | return isKind( identifier, TG ); |
---|
[51b7345] | 60 | } |
---|
| 61 | |
---|
[b87a5ed] | 62 | void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) { |
---|
| 63 | if ( currentContext != "" && scope == contextScope ) { |
---|
| 64 | DeferredEntry entry = { identifier, kind }; |
---|
| 65 | contexts[currentContext].push_back( entry ); |
---|
[8c17ab0] | 66 | } else { |
---|
[721f17a] | 67 | debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << " from scope " << currentScope << endl ); |
---|
[b87a5ed] | 68 | Entry newEntry = { scope, kind }; |
---|
| 69 | tableType::iterator curPos = table.find( identifier ); |
---|
| 70 | if ( curPos == table.end()) { |
---|
| 71 | list<Entry> newList; |
---|
| 72 | newList.push_front( newEntry ); |
---|
| 73 | table[identifier] = newList; |
---|
| 74 | } else { |
---|
| 75 | list<Entry>::iterator listPos = (*curPos ).second.begin(); |
---|
[a32b204] | 76 | while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) { |
---|
[b87a5ed] | 77 | listPos++; |
---|
[de62360d] | 78 | } // while |
---|
[b87a5ed] | 79 | (*curPos ).second.insert( listPos, newEntry ); |
---|
[de62360d] | 80 | } // if |
---|
| 81 | } // if |
---|
[51b7345] | 82 | } |
---|
| 83 | |
---|
[b87a5ed] | 84 | void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) { |
---|
| 85 | addToScope( identifier, kind, currentScope ); |
---|
[51b7345] | 86 | } |
---|
| 87 | |
---|
[b87a5ed] | 88 | void TypedefTable::addToCurrentScope( kind_t kind ) { |
---|
| 89 | addToCurrentScope( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 90 | } |
---|
| 91 | |
---|
[b87a5ed] | 92 | void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) { |
---|
| 93 | assert( currentScope >= 1 ); |
---|
| 94 | addToScope( identifier, kind, currentScope - 1 ); |
---|
[51b7345] | 95 | } |
---|
| 96 | |
---|
[b87a5ed] | 97 | void TypedefTable::addToEnclosingScope( kind_t kind ) { |
---|
| 98 | addToEnclosingScope( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 99 | } |
---|
| 100 | |
---|
[b87a5ed] | 101 | void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) { |
---|
| 102 | assert( currentScope >= 2 ); |
---|
| 103 | addToScope( identifier, kind, currentScope - 2 ); |
---|
[51b7345] | 104 | } |
---|
| 105 | |
---|
[b87a5ed] | 106 | void TypedefTable::addToEnclosingScope2( kind_t kind ) { |
---|
| 107 | addToEnclosingScope2( nextIdentifiers.top(), kind ); |
---|
[51b7345] | 108 | } |
---|
| 109 | |
---|
[8c17ab0] | 110 | void TypedefTable::setNextIdentifier( const std::string &identifier ) { |
---|
[b87a5ed] | 111 | nextIdentifiers.top() = identifier; |
---|
[51b7345] | 112 | } |
---|
| 113 | |
---|
[de62360d] | 114 | void TypedefTable::openContext( const std::string &contextName ) { |
---|
[b87a5ed] | 115 | map< string, deferListType >::iterator i = contexts.find( contextName ); |
---|
| 116 | if ( i != contexts.end() ) { |
---|
| 117 | deferListType &entries = i->second; |
---|
| 118 | for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) { |
---|
| 119 | addToEnclosingScope( i->identifier, i->kind ); |
---|
[de62360d] | 120 | } // for |
---|
| 121 | } // if |
---|
[51b7345] | 122 | } |
---|
| 123 | |
---|
[de62360d] | 124 | void TypedefTable::enterScope() { |
---|
[b87a5ed] | 125 | currentScope += 1; |
---|
| 126 | deferListStack.push( deferListType() ); |
---|
| 127 | nextIdentifiers.push( "" ); |
---|
| 128 | debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl ); |
---|
[51b7345] | 129 | } |
---|
| 130 | |
---|
[de62360d] | 131 | void TypedefTable::leaveScope() { |
---|
[b87a5ed] | 132 | debugPrint( "Leaving scope " << currentScope << endl ); |
---|
| 133 | for ( tableType::iterator i = table.begin(); i != table.end(); ) { |
---|
[721f17a] | 134 | list<Entry> &declList = (*i).second; |
---|
[a08ba92] | 135 | while ( ! declList.empty() && declList.front().scope == currentScope ) { |
---|
[b87a5ed] | 136 | declList.pop_front(); |
---|
| 137 | } |
---|
[721f17a] | 138 | if ( declList.empty() ) { // standard idom for erasing during traversal |
---|
[b87a5ed] | 139 | table.erase( i++ ); |
---|
[721f17a] | 140 | } else |
---|
| 141 | ++i; |
---|
[de62360d] | 142 | } // for |
---|
[b87a5ed] | 143 | currentScope -= 1; |
---|
[721f17a] | 144 | for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++ ) { |
---|
[b87a5ed] | 145 | addToCurrentScope( i->identifier, i->kind ); |
---|
[de62360d] | 146 | } // for |
---|
[b87a5ed] | 147 | deferListStack.pop(); |
---|
| 148 | debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); |
---|
| 149 | nextIdentifiers.pop(); |
---|
[51b7345] | 150 | } |
---|
| 151 | |
---|
[de62360d] | 152 | void TypedefTable::enterContext( const std::string &contextName ) { |
---|
[b87a5ed] | 153 | currentContext = contextName; |
---|
| 154 | contextScope = currentScope; |
---|
[51b7345] | 155 | } |
---|
| 156 | |
---|
[de62360d] | 157 | void TypedefTable::leaveContext() { |
---|
[b87a5ed] | 158 | currentContext = ""; |
---|
[51b7345] | 159 | } |
---|
| 160 | |
---|
[b87a5ed] | 161 | void TypedefTable::print( void ) const { |
---|
| 162 | for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) { |
---|
| 163 | debugPrint( (*i ).first << ": " ); |
---|
[721f17a] | 164 | list<Entry> declList = (*i).second; |
---|
[b87a5ed] | 165 | for ( list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++ ) { |
---|
[721f17a] | 166 | debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); |
---|
[b87a5ed] | 167 | } |
---|
| 168 | debugPrint( endl ); |
---|
[de62360d] | 169 | } // for |
---|
[51b7345] | 170 | } |
---|
[b87a5ed] | 171 | |
---|
| 172 | // Local Variables: // |
---|
| 173 | // tab-width: 4 // |
---|
| 174 | // mode: c++ // |
---|
| 175 | // compile-command: "make install" // |
---|
| 176 | // End: // |
---|