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