| 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           : Peter A. Buhr | 
|---|
| 10 | // Created On       : Sat May 16 15:20:13 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Wed Jul 25 15:32:35 2018 | 
|---|
| 13 | // Update Count     : 258 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #include "TypedefTable.h" | 
|---|
| 18 | #include <cassert>                                                                              // for assert | 
|---|
| 19 | #include <iostream> | 
|---|
| 20 |  | 
|---|
| 21 | #if 0 | 
|---|
| 22 | #define debugPrint( code ) code | 
|---|
| 23 | #else | 
|---|
| 24 | #define debugPrint( code ) | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std;                                                                    // string, iostream | 
|---|
| 28 |  | 
|---|
| 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 | ) | 
|---|
| 41 |  | 
|---|
| 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 ); | 
|---|
| 59 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. | 
|---|
| 60 | if ( posn == kindTable.end() ) return IDENTIFIER; | 
|---|
| 61 | return posn->second; | 
|---|
| 62 | } // TypedefTable::isKind | 
|---|
| 63 |  | 
|---|
| 64 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by | 
|---|
| 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 | //        } | 
|---|
| 75 | if ( ! typedefTable.exists( name ) ) { | 
|---|
| 76 | typedefTable.addToEnclosingScope( name, kind, "MTD" ); | 
|---|
| 77 | } // if | 
|---|
| 78 | } // TypedefTable::makeTypedef | 
|---|
| 79 |  | 
|---|
| 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 | 
|---|
| 85 |  | 
|---|
| 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 | 
|---|
| 93 |  | 
|---|
| 94 | void TypedefTable::enterScope() { | 
|---|
| 95 | kindTable.beginScope( (Note){ 0, false } ); | 
|---|
| 96 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() ); | 
|---|
| 97 | } // TypedefTable::enterScope | 
|---|
| 98 |  | 
|---|
| 99 | void TypedefTable::leaveScope() { | 
|---|
| 100 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() ); | 
|---|
| 101 | kindTable.endScope(); | 
|---|
| 102 | } // TypedefTable::leaveScope | 
|---|
| 103 |  | 
|---|
| 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 | 
|---|
| 109 |  | 
|---|
| 110 | void TypedefTable::down() { | 
|---|
| 111 | level -= 1; | 
|---|
| 112 | debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; ); | 
|---|
| 113 | } // TypedefTable::down | 
|---|
| 114 |  | 
|---|
| 115 | void TypedefTable::print( void ) const { | 
|---|
| 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 ) ); | 
|---|
| 124 | } // for | 
|---|
| 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 | 
|---|
| 131 |  | 
|---|
| 132 | // Local Variables: // | 
|---|
| 133 | // tab-width: 4 // | 
|---|
| 134 | // mode: c++ // | 
|---|
| 135 | // compile-command: "make install" // | 
|---|
| 136 | // End: // | 
|---|