[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 |
---|
[3d26610] | 12 | // Last Modified On : Wed May 30 18:04:38 2018 |
---|
| 13 | // Update Count : 148 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[d180746] | 16 | |
---|
[984dce6] | 17 | #include "TypedefTable.h" |
---|
[2f0a0678] | 18 | #include <cassert> // for assert |
---|
[51b7345] | 19 | |
---|
| 20 | #if 0 |
---|
| 21 | #include <iostream> |
---|
[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 | |
---|
[7fdb94e] | 29 | TypedefTable::~TypedefTable() { |
---|
| 30 | if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { |
---|
[3d26610] | 31 | std::cerr << "scope failure " << kindTable.currentScope() << endl; |
---|
[7fdb94e] | 32 | } // if |
---|
| 33 | } // TypedefTable::~TypedefTable |
---|
| 34 | |
---|
| 35 | bool TypedefTable::exists( const string & identifier ) { |
---|
| 36 | return kindTable.find( identifier ) != kindTable.end(); |
---|
| 37 | } // TypedefTable::exists |
---|
[984dce6] | 38 | |
---|
[7fdb94e] | 39 | int TypedefTable::isKind( const string & identifier ) const { |
---|
| 40 | KindTable::const_iterator posn = kindTable.find( identifier ); |
---|
[2f0a0678] | 41 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. |
---|
[7fdb94e] | 42 | if ( posn == kindTable.end() ) return IDENTIFIER; |
---|
| 43 | return posn->second; |
---|
| 44 | } // TypedefTable::isKind |
---|
[45161b4d] | 45 | |
---|
[2f0a0678] | 46 | void TypedefTable::changeKind( const string & identifier, int kind ) { |
---|
[7fdb94e] | 47 | KindTable::iterator posn = kindTable.find( identifier ); |
---|
[2f0a0678] | 48 | if ( posn != kindTable.end() ) posn->second = kind; // exists => update |
---|
[7fdb94e] | 49 | } // TypedefTable::changeKind |
---|
[721f17a] | 50 | |
---|
[45161b4d] | 51 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by |
---|
[7fdb94e] | 52 | // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the |
---|
| 53 | // name is explicitly used. |
---|
| 54 | void TypedefTable::makeTypedef( const string & name ) { |
---|
[45161b4d] | 55 | if ( ! typedefTable.exists( name ) ) { |
---|
[3d26610] | 56 | typedefTable.addToEnclosingScope( name, TYPEDEFname /*, "MTD"*/ ); |
---|
[45161b4d] | 57 | } // if |
---|
[7fdb94e] | 58 | } // TypedefTable::makeTypedef |
---|
| 59 | |
---|
[3d26610] | 60 | void TypedefTable::addToScope( const std::string & identifier, int kind /*, const char * locn*/ ) { |
---|
| 61 | auto scope = kindTable.currentScope(); |
---|
| 62 | debugPrint( cerr << "Adding at " /* << locn */ << " " << identifier << " as kind " << kind << " scope " << scope << endl ); |
---|
| 63 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
| 64 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
| 65 | } // TypedefTable::addToScope |
---|
| 66 | |
---|
| 67 | void TypedefTable::addToEnclosingScope( const std::string & identifier, int kind /*, const char * locn*/ ) { |
---|
[7fdb94e] | 68 | assert( kindTable.currentScope() >= 1 ); |
---|
| 69 | auto scope = kindTable.currentScope() - 1; |
---|
[3d26610] | 70 | debugPrint( cerr << "Adding2 at " /* << locn */ << " " << identifier << " as kind " << kind << " scope " << scope << endl ); |
---|
[7fdb94e] | 71 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
[2f0a0678] | 72 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
[7fdb94e] | 73 | } // TypedefTable::addToEnclosingScope |
---|
[51b7345] | 74 | |
---|
[de62360d] | 75 | void TypedefTable::enterScope() { |
---|
[7fdb94e] | 76 | kindTable.beginScope(); |
---|
[3d26610] | 77 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << endl ); |
---|
| 78 | debugPrint( print() ); |
---|
[7fdb94e] | 79 | } // TypedefTable::enterScope |
---|
[51b7345] | 80 | |
---|
[de62360d] | 81 | void TypedefTable::leaveScope() { |
---|
[3d26610] | 82 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl ); |
---|
| 83 | debugPrint( print() ); |
---|
[7fdb94e] | 84 | kindTable.endScope(); |
---|
| 85 | } // TypedefTable::leaveScope |
---|
| 86 | |
---|
[3d26610] | 87 | void TypedefTable::print( void ) const { |
---|
| 88 | KindTable::size_type scope = kindTable.currentScope(); |
---|
| 89 | debugPrint( cerr << "[" << scope << "]" ); |
---|
| 90 | for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { |
---|
| 91 | while ( i.get_level() != scope ) { |
---|
| 92 | --scope; |
---|
| 93 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
| 94 | } // while |
---|
| 95 | debugPrint( cerr << " " << (*i).first << ":" << (*i).second ); |
---|
| 96 | } // for |
---|
| 97 | while ( scope > 0 ) { |
---|
| 98 | --scope; |
---|
| 99 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
| 100 | } |
---|
| 101 | debugPrint( cerr << endl ); |
---|
| 102 | } |
---|
[b87a5ed] | 103 | |
---|
| 104 | // Local Variables: // |
---|
| 105 | // tab-width: 4 // |
---|
| 106 | // mode: c++ // |
---|
| 107 | // compile-command: "make install" // |
---|
| 108 | // End: // |
---|