source: translator/Parser.old/TypedefTable.cc @ 3c70d38

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 3c70d38 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

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