source: src/Parser/TypedefTable.cc @ 4612bb0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 4612bb0 was 4612bb0, checked in by Aaron Moss <a3moss@…>, 6 years ago

Add push operations for ScopedMap? notes

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[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
[3d56d15b]12// Last Modified On : Fri Jun 22 06:14:39 2018
13// Update Count     : 206
[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]27using namespace std;                                                                    // string, iostream
[51b7345]28
[a1c9ddd]29debugPrint(
30static 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]42TypedefTable::~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
49bool TypedefTable::exists( const string & identifier ) {
50        return kindTable.find( identifier ) != kindTable.end();
51} // TypedefTable::exists
[984dce6]52
[7fdb94e]53int 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]63void 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]76void 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 );
[3d56d15b]80        //if ( ! ret.second ) ret.first->second = kind;         // exists => update
81        assert( ret.first->second == kind );                            // exists
[3d26610]82} // TypedefTable::addToScope
83
[a1c9ddd]84void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
[3d56d15b]85        assert( kindTable.currentScope() >= 1 + level );
86        auto scope = kindTable.currentScope() - 1 - level;
87        debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << endl );
[7fdb94e]88        auto ret = kindTable.insertAt( scope, identifier, kind );
[2f0a0678]89        if ( ! ret.second ) ret.first->second = kind;           // exists => update
[7fdb94e]90} // TypedefTable::addToEnclosingScope
[51b7345]91
[de62360d]92void TypedefTable::enterScope() {
[4612bb0]93        kindTable.beginScope(0);
[3d56d15b]94        debugPrint( cerr << "Entering scope " << kindTable.currentScope() << endl; print() );
[7fdb94e]95} // TypedefTable::enterScope
[51b7345]96
[de62360d]97void TypedefTable::leaveScope() {
[3d56d15b]98        debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() );
[7fdb94e]99        kindTable.endScope();
100} // TypedefTable::leaveScope
101
[3d26610]102void TypedefTable::print( void ) const {
103        KindTable::size_type scope = kindTable.currentScope();
104        debugPrint( cerr << "[" << scope << "]" );
105        for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) {
106                while ( i.get_level() != scope ) {
107                        --scope;
108                        debugPrint( cerr << endl << "[" << scope << "]" );
109                } // while
[a1c9ddd]110                debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) );
[3d26610]111        } // for
112        while ( scope > 0 ) {
113                --scope;
114                debugPrint( cerr << endl << "[" << scope << "]" );
[3d56d15b]115        } // while
[3d26610]116        debugPrint( cerr << endl );
[3d56d15b]117} // TypedefTable::print
[b87a5ed]118
119// Local Variables: //
120// tab-width: 4 //
121// mode: c++ //
122// compile-command: "make install" //
123// End: //
Note: See TracBrowser for help on using the repository browser.