[51b7345] | 1 | #include <cassert> |
---|
| 2 | |
---|
| 3 | #include "StackTable.h" |
---|
| 4 | |
---|
| 5 | namespace SymTab { |
---|
[ea3eb06] | 6 | template< typename Element, typename ConflictFunction > |
---|
| 7 | StackTable< Element, ConflictFunction >::StackTable() : scopeLevel( 0 ) |
---|
| 8 | {} |
---|
[51b7345] | 9 | |
---|
[ea3eb06] | 10 | template< typename Element, typename ConflictFunction > |
---|
| 11 | void StackTable< Element, ConflictFunction >::enterScope() { |
---|
| 12 | scopeLevel++; |
---|
[51b7345] | 13 | } |
---|
| 14 | |
---|
[ea3eb06] | 15 | template< typename Element, typename ConflictFunction > |
---|
| 16 | void StackTable< Element, ConflictFunction >::leaveScope() { |
---|
| 17 | for ( typename TableType::iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 18 | std::stack< Entry >& entry = it->second; |
---|
[a32b204] | 19 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
[ea3eb06] | 20 | entry.pop(); |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | scopeLevel--; |
---|
| 24 | assert( scopeLevel >= 0 ); |
---|
| 25 | } |
---|
[51b7345] | 26 | |
---|
[ea3eb06] | 27 | template< typename Element, typename ConflictFunction > |
---|
| 28 | void StackTable< Element, ConflictFunction >::add( Element *type ) { |
---|
| 29 | std::stack< Entry >& entry = table[ type->get_name() ]; |
---|
[a32b204] | 30 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
[ea3eb06] | 31 | entry.top().first = conflictFunction( entry.top().first, type ); |
---|
| 32 | } else { |
---|
| 33 | entry.push( Entry( type, scopeLevel ) ); |
---|
| 34 | } |
---|
| 35 | } |
---|
[51b7345] | 36 | |
---|
[ea3eb06] | 37 | template< typename Element, typename ConflictFunction > |
---|
| 38 | void StackTable< Element, ConflictFunction >::add( std::string fwdDeclName ) { |
---|
| 39 | add( new Element( fwdDeclName ) ); |
---|
| 40 | } |
---|
[51b7345] | 41 | |
---|
[ea3eb06] | 42 | template< typename Element, typename ConflictFunction > |
---|
| 43 | Element *StackTable< Element, ConflictFunction >::lookup( std::string id ) const { |
---|
| 44 | typename TableType::const_iterator it = table.find( id ); |
---|
| 45 | if ( it == table.end() ) { |
---|
| 46 | return 0; |
---|
[a32b204] | 47 | } else if ( ! it->second.empty() ) { |
---|
[ea3eb06] | 48 | return it->second.top().first; |
---|
| 49 | } else { |
---|
| 50 | return 0; |
---|
| 51 | } |
---|
[51b7345] | 52 | } |
---|
| 53 | |
---|
[ea3eb06] | 54 | template< typename Element, typename ConflictFunction > |
---|
| 55 | void StackTable< Element, ConflictFunction >::dump( std::ostream &os ) const { |
---|
| 56 | for ( typename TableType::const_iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 57 | const std::stack< Entry >& entry = it->second; |
---|
[a32b204] | 58 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
[ea3eb06] | 59 | os << it->first << std::endl; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | } // SymTab |
---|