source: src/Parser/TypedefTable.cc @ 0a73148

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 0a73148 was 0a73148, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix conflict

  • Property mode set to 100644
File size: 5.1 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 : Fri Jul 13 18:35:54 2018
13// Update Count     : 257
14//
15
16
17#include "TypedefTable.h"
18#include <cassert>                                                                              // for assert
19#include <iostream>
20
21#if 0
22#define debugPrint( code ) code
23#else
24#define debugPrint( code )
25#endif
26
27using namespace std;                                                                    // string, iostream
28
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
42TypedefTable::~TypedefTable() {
43        if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) {
44                cerr << "Error: cfa-cpp internal error, scope failure " << kindTable.currentScope() << endl;
45                abort();
46        } // if
47} // TypedefTable::~TypedefTable
48
49bool TypedefTable::exists( const string & identifier ) {
50        return kindTable.find( identifier ) != kindTable.end();
51} // TypedefTable::exists
52
53int TypedefTable::isKind( const string & identifier ) const {
54        KindTable::const_iterator posn = kindTable.find( identifier );
55        // Name lookup defaults to identifier, and then the identifier's kind is set by the parser.
56        if ( posn == kindTable.end() ) return IDENTIFIER;
57        return posn->second;
58} // TypedefTable::isKind
59
60// SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by
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.
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//        }
71        if ( ! typedefTable.exists( name ) ) {
72                typedefTable.addToEnclosingScope( name, kind, "MTD" );
73        } // if
74} // TypedefTable::makeTypedef
75
76void TypedefTable::addToScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
77        auto scope = kindTable.currentScope();
78        debugPrint( cerr << "Adding current at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << endl );
79        kindTable.insertAt( scope, identifier, kind );
80} // TypedefTable::addToScope
81
82void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) {
83        auto scope = kindTable.currentScope() - 1 - kindTable.getNote( kindTable.currentScope() - 1 ).level;
84//      auto scope = level - kindTable.getNote( kindTable.currentScope() - 1 ).level;
85        debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << " note " << kindTable.getNote( kindTable.currentScope() - 1 ).level << endl );
86        auto ret = kindTable.insertAt( scope, identifier, kind );
87        if ( ! ret.second ) ret.first->second = kind;   // exists => update
88} // TypedefTable::addToEnclosingScope
89
90void TypedefTable::enterScope() {
91        kindTable.beginScope( (Note){ 0, false } );
92        debugPrint( cerr << "Entering scope " << kindTable.currentScope() << " level " << level << endl; print() );
93} // TypedefTable::enterScope
94
95void TypedefTable::leaveScope() {
96        debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() );
97        kindTable.endScope();
98} // TypedefTable::leaveScope
99
100void TypedefTable::up( bool forall ) {
101        level += 1;
102        kindTable.getNote( kindTable.currentScope() ) = (Note){ level, forall || getEnclForall() };
103        debugPrint( cerr << "Up " << " level " << level << " note " << kindTable.getNote( level ).level << ", " << kindTable.getNote( level ).forall << endl; );
104} // TypedefTable::up
105
106void TypedefTable::down() {
107        level -= 1;
108        debugPrint( cerr << "Down " << " level " << level << " note " << kindTable.getNote( level ).level << endl; );
109} // TypedefTable::down
110
111void TypedefTable::print( void ) const {
112        KindTable::size_type scope = kindTable.currentScope();
113        debugPrint( cerr << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
114        for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) {
115                while ( i.get_level() != scope ) {
116                        --scope;
117                        debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
118                } // while
119                debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) );
120        } // for
121        while ( scope > 0 ) {
122                --scope;
123                debugPrint( cerr << endl << "[" << scope << "] " << kindTable.getNote( scope ).level << ", " << kindTable.getNote( scope ).forall << ":" );
124        } // while
125        debugPrint( cerr << endl );
126} // TypedefTable::print
127
128// Local Variables: //
129// tab-width: 4 //
130// mode: c++ //
131// compile-command: "make install" //
132// End: //
Note: See TracBrowser for help on using the repository browser.