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