Changeset 90152a4 for src/Parser/TypedefTable.cc
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Parser/TypedefTable.cc
rf9feab8 r90152a4 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // TypedefTable.cc -- 7 // TypedefTable.cc -- 8 8 // 9 // Author : Rodolfo G. Esteves9 // Author : Peter A. Buhr 10 10 // Created On : Sat May 16 15:20:13 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Aug 15 18:24:42 201613 // Update Count : 25 12 // Last Modified On : Wed Jul 25 15:32:35 2018 13 // Update Count : 258 14 14 // 15 15 16 #include <ext/alloc_traits.h> // for __alloc_traits<>::value_type17 #include <cassert> // for assert18 #include <list> // for list, _List_iterator, list<>::iterator19 #include <map> // for _Rb_tree_iterator, _Rb_tree_const_it...20 #include <memory> // for allocator_traits<>::value_type21 #include <utility> // for pair22 16 23 #include "Parser/ParserTypes.h" // for typedefTable24 #include "Parser/parser.hh" // for IDENTIFIER25 17 #include "TypedefTable.h" 26 27 using namespace std; 18 #include <cassert> // for assert 19 #include <iostream> 28 20 29 21 #if 0 30 #include <iostream> 31 32 #define debugPrint( x ) cerr << x 22 #define debugPrint( code ) code 33 23 #else 34 #define debugPrint( x)24 #define debugPrint( code ) 35 25 #endif 36 26 37 TypedefTable::TypedefTable() : currentScope( 0 ) {} 27 using namespace std; // string, iostream 38 28 39 bool TypedefTable::exists( const string &identifier ) { 40 return table.count( identifier ) > 0; 41 } 29 debugPrint( 30 static const char *kindName( int kind ) { 31 switch ( kind ) { 32 case IDENTIFIER: return "identifier"; 33 case TYPEDEFname: return "typedef"; 34 case TYPEGENname: return "typegen"; 35 default: 36 cerr << "Error: cfa-cpp internal error, invalid kind of identifier" << endl; 37 abort(); 38 } // switch 39 } // kindName 40 ) 42 41 43 int TypedefTable::isKind( const string &identifier ) const { 44 tableType::const_iterator id_pos = table.find( identifier ); 42 TypedefTable::~TypedefTable() { 43 if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { 44 cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl; 45 abort(); 46 } // if 47 } // TypedefTable::~TypedefTable 48 49 bool TypedefTable::exists( const string & identifier ) { 50 return kindTable.find( identifier ) != kindTable.end(); 51 } // TypedefTable::exists 52 53 bool TypedefTable::existsCurr( const string & identifier ) { 54 return kindTable.findAt( kindTable.currentScope() - 1, identifier ) != kindTable.end(); 55 } // TypedefTable::exists 56 57 int TypedefTable::isKind( const string & identifier ) const { 58 KindTable::const_iterator posn = kindTable.find( identifier ); 45 59 // 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 50 void TypedefTable::changeKind( const string &identifier, kind_t kind ) { 51 tableType::iterator id_pos = table.find( identifier ); 52 if ( id_pos == table.end() ) return; 53 id_pos->second.begin()->kind = kind; 54 } 60 if ( posn == kindTable.end() ) return IDENTIFIER; 61 return posn->second; 62 } // TypedefTable::isKind 55 63 56 64 // 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 ) { 65 // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the 66 // name is explicitly used. 67 void TypedefTable::makeTypedef( const string & name, int kind ) { 68 // Check for existence is necessary to handle: 69 // struct Fred {}; 70 // void Fred(); 71 // void fred() { 72 // struct Fred act; // do not add as type in this scope 73 // Fred(); 74 // } 60 75 if ( ! typedefTable.exists( name ) ) { 61 typedefTable.addToEnclosingScope( name, TypedefTable::TD);76 typedefTable.addToEnclosingScope( name, kind, "MTD" ); 62 77 } // if 63 } 78 } // TypedefTable::makeTypedef 64 79 65 void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) { 66 if ( currentTrait != "" && scope == contextScope ) { 67 DeferredEntry entry = { identifier, kind }; 68 contexts[currentTrait].push_back( entry ); 69 } else { 70 debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << " from scope " << currentScope << endl ); 71 Entry newEntry = { scope, kind }; 72 tableType::iterator curPos = table.find( identifier ); 73 if ( curPos == table.end()) { 74 list< Entry > newList; 75 newList.push_front( newEntry ); 76 table[identifier] = newList; 77 } else { 78 list< Entry >::iterator listPos = (*curPos ).second.begin(); 79 while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) { 80 listPos++; 81 } // while 82 (*curPos ).second.insert( listPos, newEntry ); 83 } // if 84 } // if 85 } 80 void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { 81 auto scope = kindTable.currentScope(); 82 debugPrint( cerr << "Adding current at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl ); 83 kindTable.insertAt( scope, identifier, kind ); 84 } // TypedefTable::addToScope 86 85 87 void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) { 88 addToScope( identifier, kind, currentScope ); 89 } 90 91 void TypedefTable::addToCurrentScope( kind_t kind ) { 92 addToCurrentScope( nextIdentifiers.top(), kind ); 93 } 94 95 void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) { 96 assert( currentScope >= 1 ); 97 addToScope( identifier, kind, currentScope - 1 ); 98 } 99 100 void TypedefTable::addToEnclosingScope( kind_t kind ) { 101 addToEnclosingScope( nextIdentifiers.top(), kind ); 102 } 103 104 void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) { 105 assert( currentScope >= 2 ); 106 addToScope( identifier, kind, currentScope - 2 ); 107 } 108 109 void TypedefTable::addToEnclosingScope2( kind_t kind ) { 110 addToEnclosingScope2( nextIdentifiers.top(), kind ); 111 } 112 113 void TypedefTable::setNextIdentifier( const std::string &identifier ) { 114 nextIdentifiers.top() = identifier; 115 } 116 117 void TypedefTable::openTrait( const std::string &contextName ) { 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 ); 123 } // for 124 } // if 125 } 86 void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { 87 auto scope = kindTable.currentScope() - 1 - kindTable.getNote( kindTable.currentScope() - 1 ).level; 88 // auto scope = level - kindTable.getNote( kindTable.currentScope() - 1 ).level; 89 debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl ); 90 auto ret = kindTable.insertAt( scope, identifier, kind ); 91 if ( ! ret.second ) ret.first->second = kind; // exists => update 92 } // TypedefTable::addToEnclosingScope 126 93 127 94 void TypedefTable::enterScope() { 128 currentScope += 1; 129 deferListStack.push( deferListType() ); 130 nextIdentifiers.push( "" ); 131 debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl ); 132 } 95 kindTable.beginScope( (Note){ 0, false } ); 96 debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() ); 97 } // TypedefTable::enterScope 133 98 134 99 void TypedefTable::leaveScope() { 135 debugPrint( "Leaving scope " << currentScope << endl ); 136 for ( tableType::iterator i = table.begin(); i != table.end(); ) { 137 list< Entry > &declList = (*i).second; 138 while ( ! declList.empty() && declList.front().scope == currentScope ) { 139 declList.pop_front(); 140 } 141 if ( declList.empty() ) { // standard idom for erasing during traversal 142 table.erase( i++ ); 143 } else 144 ++i; 145 } // for 146 currentScope -= 1; 147 for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++ ) { 148 addToCurrentScope( i->identifier, i->kind ); 149 } // for 150 deferListStack.pop(); 151 debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl ); 152 nextIdentifiers.pop(); 153 } 100 debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() ); 101 kindTable.endScope(); 102 } // TypedefTable::leaveScope 154 103 155 void TypedefTable::enterTrait( const std::string &contextName ) { 156 currentTrait = contextName; 157 contextScope = currentScope; 158 } 104 void TypedefTable::up( bool forall ) { 105 level += 1; 106 kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() }; 107 debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; ); 108 } // TypedefTable::up 159 109 160 void TypedefTable::leaveTrait() { 161 currentTrait = ""; 162 } 110 void TypedefTable::down() { 111 level -= 1; 112 debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; ); 113 } // TypedefTable::down 163 114 164 115 void TypedefTable::print( void ) const { 165 for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) { 166 debugPrint( (*i ).first << ": " ); 167 list< Entry > declList = (*i).second; 168 for ( list< Entry >::const_iterator j = declList.begin(); j != declList.end(); j++ ) { 169 debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); 170 } 171 debugPrint( endl ); 116 KindTable::size_type scope = kindTable.currentScope(); 117 debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); 118 for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { 119 while ( i.get_level() != scope ) { 120 --scope; 121 debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); 122 } // while 123 debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) ); 172 124 } // for 173 } 125 while ( scope > 0 ) { 126 --scope; 127 debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); 128 } // while 129 debugPrint( cerr << endl ); 130 } // TypedefTable::print 174 131 175 132 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.