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 30 18:04:38 2018 |
---|
13 | // Update Count : 148 |
---|
14 | // |
---|
15 | |
---|
16 | |
---|
17 | #include "TypedefTable.h" |
---|
18 | #include <cassert> // for assert |
---|
19 | |
---|
20 | #if 0 |
---|
21 | #include <iostream> |
---|
22 | #define debugPrint( code ) code |
---|
23 | #else |
---|
24 | #define debugPrint( code ) |
---|
25 | #endif |
---|
26 | |
---|
27 | using namespace std; // string, iostream |
---|
28 | |
---|
29 | TypedefTable::~TypedefTable() { |
---|
30 | if ( ! SemanticErrorThrow && kindTable.currentScope() != 0 ) { |
---|
31 | std::cerr << "scope failure " << kindTable.currentScope() << endl; |
---|
32 | } // if |
---|
33 | } // TypedefTable::~TypedefTable |
---|
34 | |
---|
35 | bool TypedefTable::exists( const string & identifier ) { |
---|
36 | return kindTable.find( identifier ) != kindTable.end(); |
---|
37 | } // TypedefTable::exists |
---|
38 | |
---|
39 | int TypedefTable::isKind( const string & identifier ) const { |
---|
40 | KindTable::const_iterator posn = kindTable.find( identifier ); |
---|
41 | // Name lookup defaults to identifier, and then the identifier's kind is set by the parser. |
---|
42 | if ( posn == kindTable.end() ) return IDENTIFIER; |
---|
43 | return posn->second; |
---|
44 | } // TypedefTable::isKind |
---|
45 | |
---|
46 | void TypedefTable::changeKind( const string & identifier, int kind ) { |
---|
47 | KindTable::iterator posn = kindTable.find( identifier ); |
---|
48 | if ( posn != kindTable.end() ) posn->second = kind; // exists => update |
---|
49 | } // TypedefTable::changeKind |
---|
50 | |
---|
51 | // SKULLDUGGERY: Generate a typedef for the aggregate name so the aggregate does not have to be qualified by |
---|
52 | // "struct". Only generate the typedef, if the name is not in use. The typedef is implicitly (silently) removed if the |
---|
53 | // name is explicitly used. |
---|
54 | void TypedefTable::makeTypedef( const string & name ) { |
---|
55 | if ( ! typedefTable.exists( name ) ) { |
---|
56 | typedefTable.addToEnclosingScope( name, TYPEDEFname /*, "MTD"*/ ); |
---|
57 | } // if |
---|
58 | } // TypedefTable::makeTypedef |
---|
59 | |
---|
60 | void TypedefTable::addToScope( const std::string & identifier, int kind /*, const char * locn*/ ) { |
---|
61 | auto scope = kindTable.currentScope(); |
---|
62 | debugPrint( cerr << "Adding at " /* << locn */ << " " << identifier << " as kind " << kind << " scope " << scope << endl ); |
---|
63 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
64 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
65 | } // TypedefTable::addToScope |
---|
66 | |
---|
67 | void TypedefTable::addToEnclosingScope( const std::string & identifier, int kind /*, const char * locn*/ ) { |
---|
68 | assert( kindTable.currentScope() >= 1 ); |
---|
69 | auto scope = kindTable.currentScope() - 1; |
---|
70 | debugPrint( cerr << "Adding2 at " /* << locn */ << " " << identifier << " as kind " << kind << " scope " << scope << endl ); |
---|
71 | auto ret = kindTable.insertAt( scope, identifier, kind ); |
---|
72 | if ( ! ret.second ) ret.first->second = kind; // exists => update |
---|
73 | } // TypedefTable::addToEnclosingScope |
---|
74 | |
---|
75 | void TypedefTable::enterScope() { |
---|
76 | kindTable.beginScope(); |
---|
77 | debugPrint( cerr << "Entering scope " << kindTable.currentScope() << endl ); |
---|
78 | debugPrint( print() ); |
---|
79 | } // TypedefTable::enterScope |
---|
80 | |
---|
81 | void TypedefTable::leaveScope() { |
---|
82 | debugPrint( cerr << "Leaving scope " << kindTable.currentScope() << endl ); |
---|
83 | debugPrint( print() ); |
---|
84 | kindTable.endScope(); |
---|
85 | } // TypedefTable::leaveScope |
---|
86 | |
---|
87 | void TypedefTable::print( void ) const { |
---|
88 | KindTable::size_type scope = kindTable.currentScope(); |
---|
89 | debugPrint( cerr << "[" << scope << "]" ); |
---|
90 | for ( KindTable::const_iterator i = kindTable.begin(); i != kindTable.end(); i++ ) { |
---|
91 | while ( i.get_level() != scope ) { |
---|
92 | --scope; |
---|
93 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
94 | } // while |
---|
95 | debugPrint( cerr << " " << (*i).first << ":" << (*i).second ); |
---|
96 | } // for |
---|
97 | while ( scope > 0 ) { |
---|
98 | --scope; |
---|
99 | debugPrint( cerr << endl << "[" << scope << "]" ); |
---|
100 | } |
---|
101 | debugPrint( cerr << endl ); |
---|
102 | } |
---|
103 | |
---|
104 | // Local Variables: // |
---|
105 | // tab-width: 4 // |
---|
106 | // mode: c++ // |
---|
107 | // compile-command: "make install" // |
---|
108 | // End: // |
---|