source: src/Parser/TypedefTable.cc @ 6b1c4f2

ast-experimental
Last change on this file since 6b1c4f2 was c468150, checked in by Andrew Beach <ajbeach@…>, 19 months ago

Split up ParseNode?.h so that headers match implementation. May have a bit less to include total because of it.

  • Property mode set to 100644
File size: 6.0 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
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
29using namespace std;
30
31#if 0
32#define debugPrint( code ) code
33
34static 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
49TypedefTable::~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
56bool TypedefTable::exists( const string & identifier ) const {
57        return kindTable.find( identifier ) != kindTable.end();
58} // TypedefTable::exists
59
60bool TypedefTable::existsCurr( const string & identifier ) const {
61        return kindTable.findAt( kindTable.currentScope() - 1, identifier ) != kindTable.end();
62} // TypedefTable::exists
63
64int 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.
74void TypedefTable::makeTypedef( const string & name, int kind ) {
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        if ( ! typedefTable.exists( name ) ) {
83                typedefTable.addToEnclosingScope( name, kind, "MTD" );
84        } // if
85} // TypedefTable::makeTypedef
86
87void TypedefTable::makeTypedef( const string & name ) {
88        return makeTypedef( name, TYPEDEFname );
89} // TypedefTable::makeTypedef
90
91void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
92        KindTable::size_type scope = kindTable.currentScope();
93        debugPrint( cerr << "Adding current at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl );
94        kindTable.insertAt( scope, identifier, kind );
95} // TypedefTable::addToScope
96
97void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
98        KindTable::size_type scope = kindTable.currentScope() - 1 - kindTable.getNote( kindTable.currentScope() - 1 ).level;
99//      size_type scope = level - kindTable.getNote( kindTable.currentScope() - 1 ).level;
100        debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl );
101        pair< KindTable::iterator, bool > ret = kindTable.insertAt( scope, identifier, kind );
102        if ( ! ret.second ) ret.first->second = kind;           // exists => update
103} // TypedefTable::addToEnclosingScope
104
105void TypedefTable::enterScope() {
106        kindTable.beginScope( (Note){ 0, false } );
107        debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() );
108} // TypedefTable::enterScope
109
110void TypedefTable::leaveScope() {
111        debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() );
112        kindTable.endScope();
113} // TypedefTable::leaveScope
114
115void TypedefTable::up( bool forall ) {
116        level += 1;
117        kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() };
118        debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; );
119} // TypedefTable::up
120
121void TypedefTable::down() {
122        level -= 1;
123        debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; );
124} // TypedefTable::down
125
126void TypedefTable::print( void ) const {
127        KindTable::size_type scope = kindTable.currentScope();
128        debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
129        for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) {
130                while ( i.get_level() != scope ) {
131                        --scope;
132                        debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
133                } // while
134                debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) );
135        } // for
136        while ( scope > 0 ) {
137                --scope;
138                debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
139        } // while
140        debugPrint( cerr << endl );
141} // TypedefTable::print
142
143// Local Variables: //
144// tab-width: 4 //
145// mode: c++ //
146// compile-command: "make install" //
147// End: //
Note: See TracBrowser for help on using the repository browser.