source: translator/Parser/TypedefTable.cc @ 51b7345

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 51b7345 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

initial commit

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