| 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>
|
|---|
| 20 | using namespace std;
|
|---|
| 21 |
|
|---|
| 22 | #if 0
|
|---|
| 23 | #include <iostream>
|
|---|
| 24 | #define debugPrint( x ) cerr << x
|
|---|
| 25 | #else
|
|---|
| 26 | #define debugPrint( x )
|
|---|
| 27 | #endif
|
|---|
| 28 |
|
|---|
| 29 | TypedefTable::TypedefTable() : currentScope( 0 ) {}
|
|---|
| 30 |
|
|---|
| 31 | void 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 |
|
|---|
| 41 | bool 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 |
|
|---|
| 50 | bool TypedefTable::isIdentifier( const string &identifier ) const {
|
|---|
| 51 | return isKind( identifier, ID );
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | bool TypedefTable::isTypedef( const string &identifier ) const {
|
|---|
| 55 | return isKind( identifier, TD );
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | bool TypedefTable::isTypegen( const string &identifier ) const {
|
|---|
| 59 | return isKind( identifier, TG );
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | void 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 |
|
|---|
| 84 | void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) {
|
|---|
| 85 | addToScope( identifier, kind, currentScope );
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void TypedefTable::addToCurrentScope( kind_t kind ) {
|
|---|
| 89 | addToCurrentScope( nextIdentifiers.top(), kind );
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) {
|
|---|
| 93 | assert( currentScope >= 1 );
|
|---|
| 94 | addToScope( identifier, kind, currentScope - 1 );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void TypedefTable::addToEnclosingScope( kind_t kind ) {
|
|---|
| 98 | addToEnclosingScope( nextIdentifiers.top(), kind );
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) {
|
|---|
| 102 | assert( currentScope >= 2 );
|
|---|
| 103 | addToScope( identifier, kind, currentScope - 2 );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void TypedefTable::addToEnclosingScope2( kind_t kind ) {
|
|---|
| 107 | addToEnclosingScope2( nextIdentifiers.top(), kind );
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | void TypedefTable::setNextIdentifier( const std::string &identifier ) {
|
|---|
| 111 | nextIdentifiers.top() = identifier;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | void 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 |
|
|---|
| 124 | void 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 |
|
|---|
| 131 | void 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 |
|
|---|
| 152 | void TypedefTable::enterContext( const std::string &contextName ) {
|
|---|
| 153 | currentContext = contextName;
|
|---|
| 154 | contextScope = currentScope;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void TypedefTable::leaveContext() {
|
|---|
| 158 | currentContext = "";
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void 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: //
|
|---|