source: src/Parser/TypedefTable.cc @ afdb74b

ADTast-experimental
Last change on this file since afdb74b was 702e826, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Pre-translation pass on the parser. Entirely code readability improvements, no behaviour (on a larger scale) should be effected.

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