[51b7345] | 1 | /* |
---|
| 2 | * This file is part of the Cforall project |
---|
| 3 | * |
---|
| 4 | * $Id: StackTable.cc,v 1.3 2002/09/19 21:02:44 rcbilson Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include <cassert> |
---|
| 9 | |
---|
| 10 | #include "StackTable.h" |
---|
| 11 | |
---|
| 12 | namespace SymTab { |
---|
| 13 | |
---|
| 14 | template< typename Element, typename ConflictFunction > |
---|
| 15 | StackTable< Element, ConflictFunction >::StackTable() |
---|
| 16 | : scopeLevel( 0 ) |
---|
| 17 | { |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | template< typename Element, typename ConflictFunction > |
---|
| 21 | void |
---|
| 22 | StackTable< Element, ConflictFunction >::enterScope() |
---|
| 23 | { |
---|
| 24 | scopeLevel++; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | template< typename Element, typename ConflictFunction > |
---|
| 28 | void |
---|
| 29 | StackTable< Element, ConflictFunction >::leaveScope() |
---|
| 30 | { |
---|
| 31 | for( typename TableType::iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 32 | std::stack< Entry >& entry = it->second; |
---|
| 33 | if( !entry.empty() && entry.top().second == scopeLevel ) { |
---|
| 34 | entry.pop(); |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | scopeLevel--; |
---|
| 39 | assert( scopeLevel >= 0 ); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | template< typename Element, typename ConflictFunction > |
---|
| 43 | void |
---|
| 44 | StackTable< Element, ConflictFunction >::add( Element *type ) |
---|
| 45 | { |
---|
| 46 | std::stack< Entry >& entry = table[ type->get_name() ]; |
---|
| 47 | if( !entry.empty() && entry.top().second == scopeLevel ) { |
---|
| 48 | entry.top().first = conflictFunction( entry.top().first, type ); |
---|
| 49 | } else { |
---|
| 50 | entry.push( Entry( type, scopeLevel ) ); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | template< typename Element, typename ConflictFunction > |
---|
| 55 | void |
---|
| 56 | StackTable< Element, ConflictFunction >::add( std::string fwdDeclName ) |
---|
| 57 | { |
---|
| 58 | add( new Element( fwdDeclName ) ); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | template< typename Element, typename ConflictFunction > |
---|
| 62 | Element * |
---|
| 63 | StackTable< Element, ConflictFunction >::lookup( std::string id ) const |
---|
| 64 | { |
---|
| 65 | typename TableType::const_iterator it = table.find( id ); |
---|
| 66 | if( it == table.end() ) { |
---|
| 67 | return 0; |
---|
| 68 | } else if( !it->second.empty() ) { |
---|
| 69 | return it->second.top().first; |
---|
| 70 | } else { |
---|
| 71 | return 0; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | template< typename Element, typename ConflictFunction > |
---|
| 76 | void |
---|
| 77 | StackTable< Element, ConflictFunction >::dump( std::ostream &os ) const |
---|
| 78 | { |
---|
| 79 | for( typename TableType::const_iterator it = table.begin(); it != table.end(); ++it ) { |
---|
| 80 | const std::stack< Entry >& entry = it->second; |
---|
| 81 | if( !entry.empty() && entry.top().second == scopeLevel ) { |
---|
| 82 | os << it->first << std::endl; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | } // namespace SymTab |
---|