| 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 : Tue May 22 08:40:01 2018 | 
|---|
| 13 | // Update Count     : 121 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #include "TypedefTable.h" | 
|---|
| 18 | #include <cassert>                                                                              // for assert | 
|---|
| 19 |  | 
|---|
| 20 | #if 0 | 
|---|
| 21 | #include <iostream> | 
|---|
| 22 | #define debugPrint( x ) cerr << x | 
|---|
| 23 | #else | 
|---|
| 24 | #define debugPrint( x ) | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | using namespace std;                                                                    // string, iostream | 
|---|
| 28 |  | 
|---|
| 29 | TypedefTable::~TypedefTable() { | 
|---|
| 30 | if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { | 
|---|
| 31 | // std::cerr << "scope failure " << kindTable.currentScope() << endl; | 
|---|
| 32 | } // if | 
|---|
| 33 | } // TypedefTable::~TypedefTable | 
|---|
| 34 |  | 
|---|
| 35 | bool TypedefTable::exists( const string & identifier ) { | 
|---|
| 36 | return kindTable.find( identifier ) != kindTable.end(); | 
|---|
| 37 | } // TypedefTable::exists | 
|---|
| 38 |  | 
|---|
| 39 | int TypedefTable::isKind( const string & identifier ) const { | 
|---|
| 40 | KindTable::const_iterator posn = kindTable.find( identifier ); | 
|---|
| 41 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. | 
|---|
| 42 | if ( posn == kindTable.end() ) return IDENTIFIER; | 
|---|
| 43 | return posn->second; | 
|---|
| 44 | } // TypedefTable::isKind | 
|---|
| 45 |  | 
|---|
| 46 | void TypedefTable::changeKind( const string & identifier, int kind ) { | 
|---|
| 47 | KindTable::iterator posn = kindTable.find( identifier ); | 
|---|
| 48 | if ( posn != kindTable.end() ) posn->second = kind;     // exists => update | 
|---|
| 49 | } // TypedefTable::changeKind | 
|---|
| 50 |  | 
|---|
| 51 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by | 
|---|
| 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 ) { | 
|---|
| 55 | if ( ! typedefTable.exists( name ) ) { | 
|---|
| 56 | typedefTable.addToEnclosingScope( name, TYPEDEFname ); | 
|---|
| 57 | } // if | 
|---|
| 58 | } // TypedefTable::makeTypedef | 
|---|
| 59 |  | 
|---|
| 60 | void TypedefTable::addToEnclosingScope( const std::string & identifier, int kind ) { | 
|---|
| 61 | assert( kindTable.currentScope() >= 1 ); | 
|---|
| 62 | auto scope = kindTable.currentScope() - 1; | 
|---|
| 63 | debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << endl ); | 
|---|
| 64 | auto ret = kindTable.insertAt( scope, identifier, kind ); | 
|---|
| 65 | if ( ! ret.second ) ret.first->second = kind;           // exists => update | 
|---|
| 66 | } // TypedefTable::addToEnclosingScope | 
|---|
| 67 |  | 
|---|
| 68 | void TypedefTable::enterScope() { | 
|---|
| 69 | kindTable.beginScope(); | 
|---|
| 70 | debugPrint( "Entering scope " << kindTable.currentScope() << endl ); | 
|---|
| 71 | } // TypedefTable::enterScope | 
|---|
| 72 |  | 
|---|
| 73 | void TypedefTable::leaveScope() { | 
|---|
| 74 | debugPrint( "Leaving scope " << kindTable.currentScope() << endl ); | 
|---|
| 75 | kindTable.endScope(); | 
|---|
| 76 | } // TypedefTable::leaveScope | 
|---|
| 77 |  | 
|---|
| 78 | // void TypedefTable::print( void ) const { | 
|---|
| 79 | //      for ( KindTable::const_iterator i = table.begin(); i != table.end(); i++) { | 
|---|
| 80 | //              debugPrint( (*i ).first << ": " ); | 
|---|
| 81 | //              list< Entry > declList = (*i).second; | 
|---|
| 82 | //              for ( list< Entry >::const_iterator j = declList.begin(); j != declList.end(); j++ ) { | 
|---|
| 83 | //                      debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " ); | 
|---|
| 84 | //              } | 
|---|
| 85 | //              debugPrint( endl ); | 
|---|
| 86 | //      } // for | 
|---|
| 87 | // } | 
|---|
| 88 |  | 
|---|
| 89 | // Local Variables: // | 
|---|
| 90 | // tab-width: 4 // | 
|---|
| 91 | // mode: c++ // | 
|---|
| 92 | // compile-command: "make install" // | 
|---|
| 93 | // End: // | 
|---|