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 Jun 22 06:14:39 2018 |
---|
13 | // Update Count : 206 |
---|
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 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 | |
---|
42 | TypedefTable::~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 | |
---|
49 | bool TypedefTable::exists( const string & identifier ) { |
---|
50 | return kindTable.find( identifier ) != kindTable.end(); |
---|
51 | } // TypedefTable::exists |
---|
52 | |
---|
53 | int 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. |
---|
63 | void 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 | |
---|
76 | void 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 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
80 | //if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
81 | assert( ret.first->second == kind ); // exists |
---|
82 | } // TypedefTable::addToScope |
---|
83 | |
---|
84 | void TypedefTable::addToEnclosingScope( const string & identifier, int kind, const char * locn __attribute__((unused)) ) { |
---|
85 | assert( kindTable.currentScope() >= 1 + level ); |
---|
86 | auto scope = kindTable.currentScope() - 1 - level; |
---|
87 | debugPrint( cerr << "Adding enclosing at " << locn << " " << identifier << " as " << kindName( kind ) << " scope " << scope << " level " << level << endl ); |
---|
88 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
89 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
90 | } // TypedefTable::addToEnclosingScope |
---|
91 | |
---|
92 | void TypedefTable::enterScope() { |
---|
93 | kindTable.beginScope(0); |
---|
94 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << endl; print() ); |
---|
95 | } // TypedefTable::enterScope |
---|
96 | |
---|
97 | void TypedefTable::leaveScope() { |
---|
98 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl; print() ); |
---|
99 | kindTable.endScope(); |
---|
100 | } // TypedefTable::leaveScope |
---|
101 | |
---|
102 | void TypedefTable::print( void ) const { |
---|
103 | KindTable::size_type scope = kindTable.currentScope(); |
---|
104 | debugPrint( cerr << "[" << scope << "]" ); |
---|
105 | for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { |
---|
106 | while ( i.get_level() != scope ) { |
---|
107 | --scope; |
---|
108 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
109 | } // while |
---|
110 | debugPrint( cerr << " " << (*i).first << ":" << kindName( (*i).second ) ); |
---|
111 | } // for |
---|
112 | while ( scope > 0 ) { |
---|
113 | --scope; |
---|
114 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
115 | } // while |
---|
116 | debugPrint( cerr << endl ); |
---|
117 | } // TypedefTable::print |
---|
118 | |
---|
119 | // Local Variables: // |
---|
120 | // tab-width: 4 // |
---|
121 | // mode: c++ // |
---|
122 | // compile-command: "make install" // |
---|
123 | // End: // |
---|