[0dd3a2f] | 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 | // StackTable.cc --
|
---|
| 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 21:45:15 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[a08ba92] | 12 | // Last Modified On : Tue May 19 16:51:53 2015
|
---|
| 13 | // Update Count : 3
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #include <cassert>
|
---|
| 17 |
|
---|
| 18 | #include "StackTable.h"
|
---|
| 19 |
|
---|
| 20 | namespace SymTab {
|
---|
[a08ba92] | 21 | template< typename Element, typename ConflictFunction >
|
---|
| 22 | StackTable< Element, ConflictFunction >::StackTable() : scopeLevel( 0 ) {
|
---|
[0dd3a2f] | 23 | }
|
---|
[51b73452] | 24 |
|
---|
[a08ba92] | 25 | template< typename Element, typename ConflictFunction >
|
---|
| 26 | void StackTable< Element, ConflictFunction >::enterScope() {
|
---|
[0dd3a2f] | 27 | scopeLevel++;
|
---|
[a08ba92] | 28 | }
|
---|
[51b73452] | 29 |
|
---|
[a08ba92] | 30 | template< typename Element, typename ConflictFunction >
|
---|
| 31 | void StackTable< Element, ConflictFunction >::leaveScope() {
|
---|
[0dd3a2f] | 32 | for ( typename TableType::iterator it = table.begin(); it != table.end(); ++it ) {
|
---|
| 33 | std::stack< Entry >& entry = it->second;
|
---|
| 34 | if ( ! entry.empty() && entry.top().second == scopeLevel ) {
|
---|
| 35 | entry.pop();
|
---|
| 36 | } // if
|
---|
| 37 | } // for
|
---|
| 38 | scopeLevel--;
|
---|
| 39 | assert( scopeLevel >= 0 );
|
---|
[a08ba92] | 40 | }
|
---|
[51b73452] | 41 |
|
---|
[a08ba92] | 42 | template< typename Element, typename ConflictFunction >
|
---|
| 43 | void StackTable< Element, ConflictFunction >::add( Element *type ) {
|
---|
[0dd3a2f] | 44 | std::stack< Entry >& entry = table[ type->get_name() ];
|
---|
| 45 | if ( ! entry.empty() && entry.top().second == scopeLevel ) {
|
---|
| 46 | entry.top().first = conflictFunction( entry.top().first, type );
|
---|
| 47 | } else {
|
---|
| 48 | entry.push( Entry( type, scopeLevel ) );
|
---|
| 49 | } // if
|
---|
[a08ba92] | 50 | }
|
---|
[51b73452] | 51 |
|
---|
[a08ba92] | 52 | template< typename Element, typename ConflictFunction >
|
---|
| 53 | void StackTable< Element, ConflictFunction >::add( std::string fwdDeclName ) {
|
---|
[0dd3a2f] | 54 | add( new Element( fwdDeclName ) );
|
---|
[a08ba92] | 55 | }
|
---|
[51b73452] | 56 |
|
---|
[a08ba92] | 57 | template< typename Element, typename ConflictFunction >
|
---|
| 58 | Element *StackTable< Element, ConflictFunction >::lookup( std::string id ) const {
|
---|
[0dd3a2f] | 59 | typename TableType::const_iterator it = table.find( id );
|
---|
| 60 | if ( it == table.end() ) {
|
---|
| 61 | return 0;
|
---|
| 62 | } else if ( ! it->second.empty() ) {
|
---|
| 63 | return it->second.top().first;
|
---|
| 64 | } else {
|
---|
| 65 | return 0;
|
---|
| 66 | } // if
|
---|
[a08ba92] | 67 | }
|
---|
[51b73452] | 68 |
|
---|
[a08ba92] | 69 | template< typename Element, typename ConflictFunction >
|
---|
| 70 | void StackTable< Element, ConflictFunction >::dump( std::ostream &os ) const {
|
---|
[0dd3a2f] | 71 | for ( typename TableType::const_iterator it = table.begin(); it != table.end(); ++it ) {
|
---|
| 72 | const std::stack< Entry >& entry = it->second;
|
---|
| 73 | if ( ! entry.empty() && entry.top().second == scopeLevel ) {
|
---|
| 74 | os << it->first << std::endl;
|
---|
| 75 | } // if
|
---|
| 76 | } // for
|
---|
[a08ba92] | 77 | }
|
---|
[0dd3a2f] | 78 | } // namespace SymTab
|
---|
| 79 |
|
---|
| 80 | // Local Variables: //
|
---|
| 81 | // tab-width: 4 //
|
---|
| 82 | // mode: c++ //
|
---|
| 83 | // compile-command: "make install" //
|
---|
| 84 | // End: //
|
---|