[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 | // |
---|
[7fdb94e] | 9 | // Author : Peter A. Buhr |
---|
[b87a5ed] | 10 | // Created On : Sat May 16 15:20:13 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[a1c9ddd] | 12 | // Last Modified On : Thu Jun 7 13:17:56 2018 |
---|
| 13 | // Update Count : 192 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[d180746] | 16 | |
---|
[984dce6] | 17 | #include "TypedefTable.h" |
---|
[2f0a0678] | 18 | #include <cassert> // for assert |
---|
[a1c9ddd] | 19 | #include <iostream> |
---|
[51b7345] | 20 | |
---|
| 21 | #if 0 |
---|
[3d26610] | 22 | #define debugPrint( code ) code |
---|
[51b7345] | 23 | #else |
---|
[3d26610] | 24 | #define debugPrint( code ) |
---|
[51b7345] | 25 | #endif |
---|
| 26 | |
---|
[2f0a0678] | 27 | using namespace std; // string, iostream |
---|
[51b7345] | 28 | |
---|
[a1c9ddd] | 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 | |
---|
[7fdb94e] | 42 | TypedefTable::~TypedefTable() { |
---|
| 43 | if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { |
---|
[a1c9ddd] | 44 | cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl; |
---|
| 45 | abort(); |
---|
[7fdb94e] | 46 | } // if |
---|
| 47 | } // TypedefTable::~TypedefTable |
---|
| 48 | |
---|
| 49 | bool TypedefTable::exists( const string & identifier ) { |
---|
| 50 | return kindTable.find( identifier ) != kindTable.end(); |
---|
| 51 | } // TypedefTable::exists |
---|
[984dce6] | 52 | |
---|
[7fdb94e] | 53 | int TypedefTable::isKind( const string & identifier ) const { |
---|
| 54 | KindTable::const_iterator posn = kindTable.find( identifier ); |
---|
[2f0a0678] | 55 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. |
---|
[7fdb94e] | 56 | if ( posn == kindTable.end() ) return IDENTIFIER; |
---|
| 57 | return posn->second; |
---|
| 58 | } // TypedefTable::isKind |
---|
[45161b4d] | 59 | |
---|
| 60 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by |
---|
[7fdb94e] | 61 | // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the |
---|
| 62 | // name is explicitly used. |
---|
[a1c9ddd] | 63 | void TypedefTable::makeTypedef( const string & name, int kind ) { |
---|
| 64 | // Check for existence is necessary to handle: |
---|
| 65 | // struct Fred {}; |
---|
| 66 | // void Fred(); |
---|
| 67 | // void fred() { |
---|
| 68 | // struct Fred act; // do not add as type in this scope |
---|
| 69 | // Fred(); |
---|
| 70 | // } |
---|
[45161b4d] | 71 | if ( ! typedefTable.exists( name ) ) { |
---|
[a1c9ddd] | 72 | typedefTable.addToEnclosingScope( name, kind, "MTD" ); |
---|
[45161b4d] | 73 | } // if |
---|
[7fdb94e] | 74 | } // TypedefTable::makeTypedef |
---|
| 75 | |
---|
[a1c9ddd] | 76 | void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { |
---|
[3d26610] | 77 | auto scope = kindTable.currentScope(); |
---|
[a1c9ddd] | 78 | debugPrint( cerr << "Adding current at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl ); |
---|
[3d26610] | 79 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
| 80 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
| 81 | } // TypedefTable::addToScope |
---|
| 82 | |
---|
[a1c9ddd] | 83 | void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { |
---|
[7fdb94e] | 84 | assert( kindTable.currentScope() >= 1 ); |
---|
| 85 | auto scope = kindTable.currentScope() - 1; |
---|
[a1c9ddd] | 86 | debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl ); |
---|
[7fdb94e] | 87 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
[2f0a0678] | 88 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
[7fdb94e] | 89 | } // TypedefTable::addToEnclosingScope |
---|
[51b7345] | 90 | |
---|
[de62360d] | 91 | void TypedefTable::enterScope() { |
---|
[7fdb94e] | 92 | kindTable.beginScope(); |
---|
[3d26610] | 93 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << endl ); |
---|
| 94 | debugPrint( print() ); |
---|
[7fdb94e] | 95 | } // TypedefTable::enterScope |
---|
[51b7345] | 96 | |
---|
[de62360d] | 97 | void TypedefTable::leaveScope() { |
---|
[3d26610] | 98 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl ); |
---|
| 99 | debugPrint( print() ); |
---|
[7fdb94e] | 100 | kindTable.endScope(); |
---|
| 101 | } // TypedefTable::leaveScope |
---|
| 102 | |
---|
[3d26610] | 103 | void TypedefTable::print( void ) const { |
---|
| 104 | KindTable::size_type scope = kindTable.currentScope(); |
---|
| 105 | debugPrint( cerr << "[" << scope << "]" ); |
---|
| 106 | for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { |
---|
| 107 | while ( i.get_level() != scope ) { |
---|
| 108 | --scope; |
---|
| 109 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
| 110 | } // while |
---|
[a1c9ddd] | 111 | debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) ); |
---|
[3d26610] | 112 | } // for |
---|
| 113 | while ( scope > 0 ) { |
---|
| 114 | --scope; |
---|
| 115 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
| 116 | } |
---|
| 117 | debugPrint( cerr << endl ); |
---|
| 118 | } |
---|
[b87a5ed] | 119 | |
---|
| 120 | // Local Variables: // |
---|
| 121 | // tab-width: 4 // |
---|
| 122 | // mode: c++ // |
---|
| 123 | // compile-command: "make install" // |
---|
| 124 | // End: // |
---|