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