| 1 | #include <cassert>
|
|---|
| 2 |
|
|---|
| 3 | #include "StackTable.h"
|
|---|
| 4 |
|
|---|
| 5 | namespace SymTab {
|
|---|
| 6 | template< typename Element, typename ConflictFunction >
|
|---|
| 7 | StackTable< Element, ConflictFunction >::StackTable() : scopeLevel( 0 )
|
|---|
| 8 | {}
|
|---|
| 9 |
|
|---|
| 10 | template< typename Element, typename ConflictFunction >
|
|---|
| 11 | void StackTable< Element, ConflictFunction >::enterScope() {
|
|---|
| 12 | scopeLevel++;
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 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;
|
|---|
| 19 | if ( !entry.empty() && entry.top().second == scopeLevel ) {
|
|---|
| 20 | entry.pop();
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | scopeLevel--;
|
|---|
| 24 | assert( scopeLevel >= 0 );
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | template< typename Element, typename ConflictFunction >
|
|---|
| 28 | void StackTable< Element, ConflictFunction >::add( Element *type ) {
|
|---|
| 29 | std::stack< Entry >& entry = table[ type->get_name() ];
|
|---|
| 30 | if ( !entry.empty() && entry.top().second == scopeLevel ) {
|
|---|
| 31 | entry.top().first = conflictFunction( entry.top().first, type );
|
|---|
| 32 | } else {
|
|---|
| 33 | entry.push( Entry( type, scopeLevel ) );
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | template< typename Element, typename ConflictFunction >
|
|---|
| 38 | void StackTable< Element, ConflictFunction >::add( std::string fwdDeclName ) {
|
|---|
| 39 | add( new Element( fwdDeclName ) );
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 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;
|
|---|
| 47 | } else if ( !it->second.empty() ) {
|
|---|
| 48 | return it->second.top().first;
|
|---|
| 49 | } else {
|
|---|
| 50 | return 0;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 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;
|
|---|
| 58 | if ( !entry.empty() && entry.top().second == scopeLevel ) {
|
|---|
| 59 | os << it->first << std::endl;
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | } // SymTab
|
|---|