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