source: translator/Parser/TypedefTable.cc @ 3848e0e

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 3848e0e was 8c17ab0, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add quoted identifiers, add compilation include directory, reformatted some files

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