| 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 : Sat May 16 15:24:03 2015
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 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 | bool TypedefTable::isKind( string identifier, kind_t kind ) const {
 | 
|---|
| 32 |         tableType::const_iterator id_pos = table.find( identifier );
 | 
|---|
| 33 |         if ( id_pos == table.end()) {
 | 
|---|
| 34 |                 return true;
 | 
|---|
| 35 |         } else {
 | 
|---|
| 36 |                 return (*((*id_pos ).second.begin())).kind == kind;
 | 
|---|
| 37 |         }
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | bool TypedefTable::isIdentifier( string identifier ) const {
 | 
|---|
| 41 |         return isKind( identifier, ID );
 | 
|---|
| 42 | }
 | 
|---|
| 43 | 
 | 
|---|
| 44 | bool TypedefTable::isTypedef( string identifier ) const {
 | 
|---|
| 45 |         return isKind( identifier, TD );
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | bool TypedefTable::isTypegen( string identifier ) const {
 | 
|---|
| 49 |         return isKind( identifier, TG );
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void TypedefTable::addToScope( const std::string &identifier, kind_t kind, int scope ) {
 | 
|---|
| 53 |         if ( currentContext != "" && scope == contextScope ) {
 | 
|---|
| 54 |                 DeferredEntry entry = { identifier, kind };
 | 
|---|
| 55 |                 contexts[currentContext].push_back( entry );
 | 
|---|
| 56 |         } else {
 | 
|---|
| 57 |                 debugPrint( "Adding " << identifier << " as type " << kind << " scope " << scope << " from scope " << currentScope << endl );
 | 
|---|
| 58 |                 Entry newEntry = { scope, kind };
 | 
|---|
| 59 |                 tableType::iterator curPos = table.find( identifier );
 | 
|---|
| 60 |                 if ( curPos == table.end()) {
 | 
|---|
| 61 |                         list<Entry> newList;
 | 
|---|
| 62 |                         newList.push_front( newEntry );
 | 
|---|
| 63 |                         table[identifier] = newList;
 | 
|---|
| 64 |                 } else {
 | 
|---|
| 65 |                         list<Entry>::iterator listPos = (*curPos ).second.begin();
 | 
|---|
| 66 |                         while ( listPos != (*curPos ).second.end() && listPos->scope > scope ) {
 | 
|---|
| 67 |                                 listPos++;
 | 
|---|
| 68 |                         }
 | 
|---|
| 69 |                         (*curPos ).second.insert( listPos, newEntry );
 | 
|---|
| 70 |                 }
 | 
|---|
| 71 |         }
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | void TypedefTable::addToCurrentScope( const std::string &identifier, kind_t kind ) {
 | 
|---|
| 75 |         addToScope( identifier, kind, currentScope );
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void TypedefTable::addToCurrentScope( kind_t kind ) {
 | 
|---|
| 79 |         addToCurrentScope( nextIdentifiers.top(), kind );
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | void TypedefTable::addToEnclosingScope( const std::string &identifier, kind_t kind ) {
 | 
|---|
| 83 |         assert( currentScope >= 1 );
 | 
|---|
| 84 |         addToScope( identifier, kind, currentScope - 1 );
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | void TypedefTable::addToEnclosingScope( kind_t kind ) {
 | 
|---|
| 88 |         addToEnclosingScope( nextIdentifiers.top(), kind );
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | void TypedefTable::addToEnclosingScope2( const std::string &identifier, kind_t kind ) {
 | 
|---|
| 92 |         assert( currentScope >= 2 );
 | 
|---|
| 93 |         addToScope( identifier, kind, currentScope - 2 );
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | void TypedefTable::addToEnclosingScope2( kind_t kind ) {
 | 
|---|
| 97 |         addToEnclosingScope2( nextIdentifiers.top(), kind );
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | void TypedefTable::setNextIdentifier( const std::string &identifier ) {
 | 
|---|
| 101 |         nextIdentifiers.top() = identifier;
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void TypedefTable::openContext( std::string contextName ) {
 | 
|---|
| 105 |         map< string, deferListType >::iterator i = contexts.find( contextName );
 | 
|---|
| 106 |         if ( i != contexts.end() ) {
 | 
|---|
| 107 |                 deferListType &entries = i->second;
 | 
|---|
| 108 |                 for ( deferListType::iterator i = entries.begin(); i != entries.end(); i++) {
 | 
|---|
| 109 |                         addToEnclosingScope( i->identifier, i->kind );
 | 
|---|
| 110 |                 }
 | 
|---|
| 111 |         }
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | void TypedefTable::enterScope( void ) {
 | 
|---|
| 115 |         currentScope += 1;
 | 
|---|
| 116 |         deferListStack.push( deferListType() );
 | 
|---|
| 117 |         nextIdentifiers.push( "" );
 | 
|---|
| 118 |         debugPrint( "Entering scope " << currentScope << ", nextIdentifiers size is " << nextIdentifiers.size() << endl );
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | void TypedefTable::leaveScope( void ) {
 | 
|---|
| 122 |         debugPrint( "Leaving scope " << currentScope << endl );
 | 
|---|
| 123 |         for ( tableType::iterator i = table.begin(); i != table.end(); ) {
 | 
|---|
| 124 |                 list<Entry> &declList = (*i ).second;
 | 
|---|
| 125 |                 while ( ! declList.empty() && declList.front().scope == currentScope ) {
 | 
|---|
| 126 |                         declList.pop_front();
 | 
|---|
| 127 |                 }
 | 
|---|
| 128 |                 if ( declList.empty() ) {                       // standard idom for erasing during traversal
 | 
|---|
| 129 |                         table.erase( i++ );
 | 
|---|
| 130 |                 } else ++i;
 | 
|---|
| 131 |         }
 | 
|---|
| 132 |         currentScope -= 1;
 | 
|---|
| 133 |         for ( deferListType::iterator i = deferListStack.top().begin(); i != deferListStack.top().end(); i++) {
 | 
|---|
| 134 |                 addToCurrentScope( i->identifier, i->kind );
 | 
|---|
| 135 |         }
 | 
|---|
| 136 |         deferListStack.pop();
 | 
|---|
| 137 |         debugPrint( "nextIdentifiers size is " << nextIdentifiers.size() << " top is " << nextIdentifiers.top() << endl );
 | 
|---|
| 138 |         nextIdentifiers.pop();
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | void TypedefTable::enterContext( std::string contextName ) {
 | 
|---|
| 142 |         currentContext = contextName;
 | 
|---|
| 143 |         contextScope = currentScope;
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | void TypedefTable::leaveContext( void ) {
 | 
|---|
| 147 |         currentContext = "";
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void TypedefTable::print( void ) const {
 | 
|---|
| 151 |         for ( tableType::const_iterator i = table.begin(); i != table.end(); i++) {
 | 
|---|
| 152 |                 debugPrint( (*i ).first << ": " );
 | 
|---|
| 153 |                 list<Entry> declList = (*i ).second;
 | 
|---|
| 154 |                 for ( list<Entry>::const_iterator j = declList.begin(); j != declList.end(); j++ ) {
 | 
|---|
| 155 |                         debugPrint( "(" << (*j ).scope << " " << (*j).kind << ") " );
 | 
|---|
| 156 |                 }
 | 
|---|
| 157 |                 debugPrint( endl );
 | 
|---|
| 158 |         }
 | 
|---|
| 159 | }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | // Local Variables: //
 | 
|---|
| 162 | // tab-width: 4 //
 | 
|---|
| 163 | // mode: c++ //
 | 
|---|
| 164 | // compile-command: "make install" //
 | 
|---|
| 165 | // End: //
 | 
|---|