[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 | // |
---|
[c92bdcc] | 7 | // TypedefTable.cpp -- |
---|
[b87a5ed] | 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 |
---|
[fc95df3] | 12 | // Last Modified On : Wed Jul 12 06:11:28 2023 |
---|
| 13 | // Update Count : 276 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[d180746] | 16 | |
---|
[c92bdcc] | 17 | #include "TypedefTable.hpp" |
---|
[c468150] | 18 | |
---|
[fc95df3] | 19 | #include <cassert> // for assert |
---|
| 20 | #include <string> // for string |
---|
| 21 | #include <iostream> // for iostream |
---|
[c468150] | 22 | |
---|
[6cef439] | 23 | struct TypeData; |
---|
| 24 | |
---|
[c92bdcc] | 25 | #include "ExpressionNode.hpp" // for LabelNode |
---|
| 26 | #include "ParserTypes.hpp" // for Token |
---|
| 27 | #include "StatementNode.hpp" // for CondCtl, ForCtrl |
---|
[c468150] | 28 | // This (generated) header must come late as it is missing includes. |
---|
[fc95df3] | 29 | #include "parser.hh" // for IDENTIFIER, TYPEDEFname, TYPEGENname |
---|
[c468150] | 30 | |
---|
[66406f3] | 31 | using namespace std; |
---|
[51b7345] | 32 | |
---|
| 33 | #if 0 |
---|
[3d26610] | 34 | #define debugPrint( code ) code |
---|
[c468150] | 35 | |
---|
| 36 | static const char *kindName( int kind ) { |
---|
| 37 | switch ( kind ) { |
---|
| 38 | case IDENTIFIER: return "identifier"; |
---|
| 39 | case TYPEDIMname: return "typedim"; |
---|
| 40 | case TYPEDEFname: return "typedef"; |
---|
| 41 | case TYPEGENname: return "typegen"; |
---|
| 42 | default: |
---|
| 43 | cerr << "Error: cfa-cpp internal error, invalid kind of identifier" << endl; |
---|
| 44 | abort(); |
---|
| 45 | } // switch |
---|
| 46 | } // kindName |
---|
[51b7345] | 47 | #else |
---|
[3d26610] | 48 | #define debugPrint( code ) |
---|
[51b7345] | 49 | #endif |
---|
| 50 | |
---|
[7fdb94e] | 51 | TypedefTable::~TypedefTable() { |
---|
| 52 | if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { |
---|
[a1c9ddd] | 53 | cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl; |
---|
| 54 | abort(); |
---|
[7fdb94e] | 55 | } // if |
---|
| 56 | } // TypedefTable::~TypedefTable |
---|
| 57 | |
---|
[60a8062] | 58 | bool TypedefTable::exists( const string & identifier ) const { |
---|
[7fdb94e] | 59 | return kindTable.find( identifier ) != kindTable.end(); |
---|
| 60 | } // TypedefTable::exists |
---|
[984dce6] | 61 | |
---|
[60a8062] | 62 | bool TypedefTable::existsCurr( const string & identifier ) const { |
---|
[c4f68dc] | 63 | return kindTable.findAt( kindTable.currentScope() - 1, identifier ) != kindTable.end(); |
---|
| 64 | } // TypedefTable::exists |
---|
| 65 | |
---|
[7fdb94e] | 66 | int TypedefTable::isKind( const string & identifier ) const { |
---|
| 67 | KindTable::const_iterator posn = kindTable.find( identifier ); |
---|
[2f0a0678] | 68 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. |
---|
[7fdb94e] | 69 | if ( posn == kindTable.end() ) return IDENTIFIER; |
---|
| 70 | return posn->second; |
---|
| 71 | } // TypedefTable::isKind |
---|
[45161b4d] | 72 | |
---|
| 73 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by |
---|
[7fdb94e] | 74 | // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the |
---|
| 75 | // name is explicitly used. |
---|
[fc95df3] | 76 | void TypedefTable::makeTypedef( const string & name, int kind, const char * locn __attribute__((unused)) ) { |
---|
[a1c9ddd] | 77 | // Check for existence is necessary to handle: |
---|
| 78 | // struct Fred {}; |
---|
| 79 | // void Fred(); |
---|
| 80 | // void fred() { |
---|
| 81 | // struct Fred act; // do not add as type in this scope |
---|
| 82 | // Fred(); |
---|
| 83 | // } |
---|
[fc95df3] | 84 | debugPrint( cerr << "Make typedef at " << locn << " \"" << name << "\" as " << kindName( kind ) << " scope " << kindTable.currentScope() << endl ); |
---|
[45161b4d] | 85 | if ( ! typedefTable.exists( name ) ) { |
---|
[a1c9ddd] | 86 | typedefTable.addToEnclosingScope( name, kind, "MTD" ); |
---|
[45161b4d] | 87 | } // if |
---|
[7fdb94e] | 88 | } // TypedefTable::makeTypedef |
---|
| 89 | |
---|
[fc95df3] | 90 | void TypedefTable::makeTypedef( const string & name, const char * locn __attribute__((unused)) ) { |
---|
| 91 | debugPrint( cerr << "Make typedef at " << locn << " \"" << name << " scope " << kindTable.currentScope() << endl ); |
---|
| 92 | return makeTypedef( name, TYPEDEFname, "makeTypede" ); |
---|
[c468150] | 93 | } // TypedefTable::makeTypedef |
---|
| 94 | |
---|
[a1c9ddd] | 95 | void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { |
---|
[66406f3] | 96 | KindTable::size_type scope = kindTable.currentScope(); |
---|
[fc95df3] | 97 | debugPrint( cerr << "Adding current at " << locn << " \"" << identifier << "\" as " << kindName( kind ) << " scope " << scope << endl ); |
---|
[fc20514] | 98 | kindTable.insertAt( scope, identifier, kind ); |
---|
[3d26610] | 99 | } // TypedefTable::addToScope |
---|
| 100 | |
---|
[a1c9ddd] | 101 | void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { |
---|
[66406f3] | 102 | KindTable::size_type scope = kindTable.currentScope() - 1 - kindTable.getNote( kindTable.currentScope() - 1 ).level; |
---|
| 103 | // size_type scope = level - kindTable.getNote( kindTable.currentScope() - 1 ).level; |
---|
[fc95df3] | 104 | debugPrint( cerr << "Adding enclosing at " << locn << " \"" << identifier << "\" as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl ); |
---|
[66406f3] | 105 | pair< KindTable::iterator, bool > ret = kindTable.insertAt( scope, identifier, kind ); |
---|
[6259c7e] | 106 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
[7fdb94e] | 107 | } // TypedefTable::addToEnclosingScope |
---|
[51b7345] | 108 | |
---|
[de62360d] | 109 | void TypedefTable::enterScope() { |
---|
[fc20514] | 110 | kindTable.beginScope( (Note){ 0, false } ); |
---|
| 111 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() ); |
---|
[7fdb94e] | 112 | } // TypedefTable::enterScope |
---|
[51b7345] | 113 | |
---|
[de62360d] | 114 | void TypedefTable::leaveScope() { |
---|
[3d56d15b] | 115 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() ); |
---|
[7fdb94e] | 116 | kindTable.endScope(); |
---|
| 117 | } // TypedefTable::leaveScope |
---|
| 118 | |
---|
[fc20514] | 119 | void TypedefTable::up( bool forall ) { |
---|
| 120 | level += 1; |
---|
[26ef3b2] | 121 | kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() }; |
---|
[fc20514] | 122 | debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; ); |
---|
| 123 | } // TypedefTable::up |
---|
| 124 | |
---|
| 125 | void TypedefTable::down() { |
---|
| 126 | level -= 1; |
---|
| 127 | debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; ); |
---|
| 128 | } // TypedefTable::down |
---|
| 129 | |
---|
[3d26610] | 130 | void TypedefTable::print( void ) const { |
---|
| 131 | KindTable::size_type scope = kindTable.currentScope(); |
---|
[fc20514] | 132 | debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); |
---|
[3d26610] | 133 | for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { |
---|
| 134 | while ( i.get_level() != scope ) { |
---|
| 135 | --scope; |
---|
[fc20514] | 136 | debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); |
---|
[3d26610] | 137 | } // while |
---|
[a1c9ddd] | 138 | debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) ); |
---|
[3d26610] | 139 | } // for |
---|
| 140 | while ( scope > 0 ) { |
---|
| 141 | --scope; |
---|
[fc20514] | 142 | debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" ); |
---|
[3d56d15b] | 143 | } // while |
---|
[3d26610] | 144 | debugPrint( cerr << endl ); |
---|
[3d56d15b] | 145 | } // TypedefTable::print |
---|
[b87a5ed] | 146 | |
---|
| 147 | // Local Variables: // |
---|
| 148 | // tab-width: 4 // |
---|
| 149 | // mode: c++ // |
---|
| 150 | // compile-command: "make install" // |
---|
| 151 | // End: // |
---|