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