source: src/Parser/TypedefTable.cc@ 5aa708c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 5aa708c was 721f17a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

fix OT_LABELADDRESS warning, parse genric types

  • Property mode set to 100644
File size: 5.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 : Rodolfo G. Esteves
10// Created On : Sat May 16 15:20:13 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jun 26 07:30:16 2015
13// Update Count : 19
14//
15
16#include <map>
17#include <list>
18#include "TypedefTable.h"
19#include <cassert>
20using namespace std;
21
22#if 0
23#include <iostream>
24#define debugPrint( x ) cerr << x
25#else
26#define debugPrint( x )
27#endif
28
29TypedefTable::TypedefTable() : currentScope( 0 ) {}
30
31void TypedefTable::changeKind( const string &identifier, kind_t kind ) {
32 tableType::iterator id_pos = table.find( identifier );
33 if ( id_pos == table.end() ) {
34 return;
35 } else {
36 (*((*id_pos ).second.begin())).kind = kind;
37 return;
38 } // if
39}
40
41bool TypedefTable::isKind( const string &identifier, kind_t kind ) const {
42 tableType::const_iterator id_pos = table.find( identifier );
43 if ( id_pos == table.end() ) {
44 return true;
45 } else {
46 return (*((*id_pos ).second.begin())).kind == kind;
47 } // if
48}
49
50bool TypedefTable::isIdentifier( const string &identifier ) const {
51 return isKind( identifier, ID );
52}
53
54bool TypedefTable::isTypedef( const string &identifier ) const {
55 return isKind( identifier, TD );
56}
57
58bool TypedefTable::isTypegen( const string &identifier ) const {
59 return isKind( identifier, TG );
60}
61
62void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) {
63 if ( currentContext != "" && scope == contextScope ) {
64 DeferredEntry entry = { identifier, kind };
65 contexts[currentContext].push_back( entry );
66 } else {
67 debugPrint( "Adding " << identifier << " as kind " << kind << " scope " << scope << " from scope " << currentScope << endl );
68 Entry newEntry = { scope, kind };
69 tableType::iterator curPos = table.find( identifier );
70 if ( curPos == table.end()) {
71 list<Entry> newList;
72 newList.push_front( newEntry );
73 table[identifier] = newList;
74 } else {
75 list<Entry>::iterator listPos = (*curPos ).second.begin();
76 while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) {
77 listPos++;
78 } // while
79 (*curPos ).second.insert( listPos, newEntry );
80 } // if
81 } // if
82}
83
84void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) {
85 addToScope( identifier, kind, currentScope );
86}
87
88void TypedefTable::addToCurrentScope( kind_t kind ) {
89 addToCurrentScope( nextIdentifiers.top(), kind );
90}
91
92void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) {
93 assert( currentScope >= 1 );
94 addToScope( identifier, kind, currentScope - 1 );
95}
96
97void TypedefTable::addToEnclosingScope( kind_t kind ) {
98 addToEnclosingScope( nextIdentifiers.top(), kind );
99}
100
101void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) {
102 assert( currentScope >= 2 );
103 addToScope( identifier, kind, currentScope - 2 );
104}
105
106void TypedefTable::addToEnclosingScope2( kind_t kind ) {
107 addToEnclosingScope2( nextIdentifiers.top(), kind );
108}
109
110void TypedefTable::setNextIdentifier( const std::string &identifier ) {
111 nextIdentifiers.top() = identifier;
112}
113
114void TypedefTable::openContext( const std::string &contextName ) {
115 map< string, deferListType >::iterator i = contexts.find( contextName );
116 if ( i != contexts.end() ) {
117 deferListType &entries = i->second;
118 for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) {
119 addToEnclosingScope( i->identifier, i->kind );
120 } // for
121 } // if
122}
123
124void TypedefTable::enterScope() {
125 currentScope += 1;
126 deferListStack.push( deferListType() );
127 nextIdentifiers.push( "" );
128 debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
129}
130
131void TypedefTable::leaveScope() {
132 debugPrint( "Leaving scope " << currentScope << endl );
133 for ( tableType::iterator i = table.begin(); i != table.end(); ) {
134 list<Entry> &declList = (*i).second;
135 while ( ! declList.empty() && declList.front().scope == currentScope ) {
136 declList.pop_front();
137 }
138 if ( declList.empty() ) { // standard idom for erasing during traversal
139 table.erase( i++ );
140 } else
141 ++i;
142 } // for
143 currentScope -= 1;
144 for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++ ) {
145 addToCurrentScope( i->identifier, i->kind );
146 } // for
147 deferListStack.pop();
148 debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl );
149 nextIdentifiers.pop();
150}
151
152void TypedefTable::enterContext( const std::string &contextName ) {
153 currentContext = contextName;
154 contextScope = currentScope;
155}
156
157void TypedefTable::leaveContext() {
158 currentContext = "";
159}
160
161void TypedefTable::print( void ) const {
162 for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) {
163 debugPrint( (*i ).first << ": " );
164 list<Entry> declList = (*i).second;
165 for ( list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++ ) {
166 debugPrint( "(" << (*j).scope << " " << (*j).kind << ") " );
167 }
168 debugPrint( endl );
169 } // for
170}
171
172// Local Variables: //
173// tab-width: 4 //
174// mode: c++ //
175// compile-command: "make install" //
176// End: //
Note: See TracBrowser for help on using the repository browser.