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