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