source: src/Parser/TypedefTable.cc @ 446740a

Last change on this file since 446740a was 6cef439, checked in by Andrew Beach <ajbeach@…>, 3 months ago

Return 'TypeData? *' from some parse rules. Moved TypeData? construction over to that file.

  • Property mode set to 100644
File size: 6.2 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 : 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
23struct TypeData;
24
25#include "ExpressionNode.h"                                                             // for LabelNode
26#include "ParserTypes.h"                                                                // for Token
27#include "StatementNode.h"                                                              // for CondCtl, ForCtrl
28// This (generated) header must come late as it is missing includes.
29#include "parser.hh"                                                                    // for IDENTIFIER, TYPEDEFname, TYPEGENname
30
31using namespace std;
32
33#if 0
34#define debugPrint( code ) code
35
36static 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
47#else
48#define debugPrint( code )
49#endif
50
51TypedefTable::~TypedefTable() {
52        if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) {
53                cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl;
54                abort();
55        } // if
56} // TypedefTable::~TypedefTable
57
58bool TypedefTable::exists( const string & identifier ) const {
59        return kindTable.find( identifier ) != kindTable.end();
60} // TypedefTable::exists
61
62bool TypedefTable::existsCurr( const string & identifier ) const {
63        return kindTable.findAt( kindTable.currentScope() - 1, identifier ) != kindTable.end();
64} // TypedefTable::exists
65
66int TypedefTable::isKind( const string & identifier ) const {
67        KindTable::const_iterator posn = kindTable.find( identifier );
68        // Name lookup defaults to identifier, and then the identifier's kind is set by the parser.
69        if ( posn == kindTable.end() ) return IDENTIFIER;
70        return posn->second;
71} // TypedefTable::isKind
72
73// SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by
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.
76void TypedefTable::makeTypedef( const string & name, int kind, const char * locn __attribute__((unused)) ) {
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//        }
84        debugPrint( cerr << "Make typedef at " << locn << " \"" << name << "\" as " << kindName( kind ) << " scope " << kindTable.currentScope() << endl );
85        if ( ! typedefTable.exists( name ) ) {
86                typedefTable.addToEnclosingScope( name, kind, "MTD" );
87        } // if
88} // TypedefTable::makeTypedef
89
90void 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" );
93} // TypedefTable::makeTypedef
94
95void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
96        KindTable::size_type scope = kindTable.currentScope();
97        debugPrint( cerr << "Adding current at " << locn << " \"" << identifier << "\" as " << kindName( kind ) << " scope " << scope << endl );
98        kindTable.insertAt( scope, identifier, kind );
99} // TypedefTable::addToScope
100
101void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
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;
104        debugPrint( cerr << "Adding enclosing at " << locn << " \"" << identifier << "\" as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl );
105        pair< KindTable::iterator, bool > ret = kindTable.insertAt( scope, identifier, kind );
106        if ( ! ret.second ) ret.first->second = kind;           // exists => update
107} // TypedefTable::addToEnclosingScope
108
109void TypedefTable::enterScope() {
110        kindTable.beginScope( (Note){ 0, false } );
111        debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() );
112} // TypedefTable::enterScope
113
114void TypedefTable::leaveScope() {
115        debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() );
116        kindTable.endScope();
117} // TypedefTable::leaveScope
118
119void TypedefTable::up( bool forall ) {
120        level += 1;
121        kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() };
122        debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; );
123} // TypedefTable::up
124
125void TypedefTable::down() {
126        level -= 1;
127        debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; );
128} // TypedefTable::down
129
130void TypedefTable::print( void ) const {
131        KindTable::size_type scope = kindTable.currentScope();
132        debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
133        for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) {
134                while ( i.get_level() != scope ) {
135                        --scope;
136                        debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
137                } // while
138                debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) );
139        } // for
140        while ( scope > 0 ) {
141                --scope;
142                debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
143        } // while
144        debugPrint( cerr << endl );
145} // TypedefTable::print
146
147// Local Variables: //
148// tab-width: 4 //
149// mode: c++ //
150// compile-command: "make install" //
151// End: //
Note: See TracBrowser for help on using the repository browser.