[51b7345] | 1 | /* |
---|
| 2 | * This file is part of the Cforall project |
---|
| 3 | * |
---|
| 4 | * $Id: IdTable.cc,v 1.6 2005/08/29 20:14:17 rcbilson Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include <cassert> |
---|
| 9 | |
---|
| 10 | #include "SynTree/Declaration.h" |
---|
| 11 | #include "ResolvExpr/typeops.h" |
---|
| 12 | #include "Indexer.h" |
---|
| 13 | #include "Mangler.h" |
---|
| 14 | #include "IdTable.h" |
---|
| 15 | #include "SemanticError.h" |
---|
| 16 | |
---|
| 17 | using std::string; |
---|
| 18 | |
---|
| 19 | namespace SymTab { |
---|
| 20 | |
---|
| 21 | IdTable::IdTable() |
---|
| 22 | : scopeLevel( 0 ) |
---|
| 23 | { |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | void |
---|
| 27 | IdTable::enterScope() |
---|
| 28 | { |
---|
| 29 | scopeLevel++; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | void |
---|
| 33 | IdTable::leaveScope() |
---|
| 34 | { |
---|
[a32b204] | 35 | for ( OuterTableType::iterator outer = table.begin(); outer != table.end(); ++outer ) { |
---|
| 36 | for ( InnerTableType::iterator inner = outer->second.begin(); inner != outer->second.end(); ++inner ) { |
---|
[51b7345] | 37 | std::stack< DeclEntry >& entry = inner->second; |
---|
[a32b204] | 38 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
[51b7345] | 39 | entry.pop(); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | scopeLevel--; |
---|
| 45 | assert( scopeLevel >= 0 ); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void |
---|
| 49 | IdTable::addDecl( DeclarationWithType *decl ) |
---|
| 50 | { |
---|
| 51 | const string &name = decl->get_name(); |
---|
| 52 | string manglename; |
---|
[a32b204] | 53 | if ( decl->get_linkage() == LinkageSpec::C ) { |
---|
[51b7345] | 54 | manglename = name; |
---|
| 55 | } else { |
---|
| 56 | manglename = Mangler::mangle( decl ); |
---|
| 57 | } |
---|
| 58 | InnerTableType &declTable = table[ name ]; |
---|
| 59 | InnerTableType::iterator it = declTable.find( manglename ); |
---|
[a32b204] | 60 | if ( it == declTable.end() ) { |
---|
[51b7345] | 61 | declTable[ manglename ].push( DeclEntry( decl, scopeLevel ) ); |
---|
| 62 | } else { |
---|
| 63 | std::stack< DeclEntry >& entry = it->second; |
---|
[a32b204] | 64 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
| 65 | if ( decl->get_linkage() != LinkageSpec::C || ResolvExpr::typesCompatible( decl->get_type(), entry.top().first->get_type(), Indexer() ) ) { |
---|
[51b7345] | 66 | FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( decl ); |
---|
| 67 | FunctionDecl *old = dynamic_cast< FunctionDecl* >( entry.top().first ); |
---|
[a32b204] | 68 | if ( newentry && old && newentry->get_statements() && old->get_statements() ) { |
---|
[51b7345] | 69 | throw SemanticError( "duplicate function definition for ", decl ); |
---|
| 70 | } else { |
---|
| 71 | ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( decl ); |
---|
| 72 | ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( entry.top().first ); |
---|
[a32b204] | 73 | if ( newobj && oldobj && newobj->get_init() && oldobj->get_init() ) { |
---|
[51b7345] | 74 | throw SemanticError( "duplicate definition for ", decl ); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | } else { |
---|
| 78 | throw SemanticError( "duplicate definition for ", decl ); |
---|
| 79 | } |
---|
| 80 | } else { |
---|
| 81 | declTable[ manglename ].push( DeclEntry( decl, scopeLevel ) ); |
---|
| 82 | } |
---|
| 83 | } |
---|
[bdd516a] | 84 | // ensure the set of routines with C linkage cannot be overloaded |
---|
[a32b204] | 85 | for ( InnerTableType::iterator i = declTable.begin(); i != declTable.end(); ++i ) { |
---|
| 86 | if ( ! i->second.empty() && i->second.top().first->get_linkage() == LinkageSpec::C && declTable.size() > 1 ) { |
---|
[bdd516a] | 87 | InnerTableType::iterator j = i; |
---|
[a32b204] | 88 | for ( j++; j != declTable.end(); ++j ) { |
---|
| 89 | if ( ! j->second.empty() && j->second.top().first->get_linkage() == LinkageSpec::C ) { |
---|
[bdd516a] | 90 | throw SemanticError( "invalid overload of C function " ); |
---|
| 91 | } |
---|
| 92 | } |
---|
[51b7345] | 93 | } |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void |
---|
| 98 | IdTable::lookupId( const std::string &id, std::list< DeclarationWithType* >& decls ) const |
---|
| 99 | { |
---|
| 100 | OuterTableType::const_iterator outer = table.find( id ); |
---|
[a32b204] | 101 | if ( outer == table.end() ) return; |
---|
[51b7345] | 102 | const InnerTableType &declTable = outer->second; |
---|
[a32b204] | 103 | for ( InnerTableType::const_iterator it = declTable.begin(); it != declTable.end(); ++it ) { |
---|
[51b7345] | 104 | const std::stack< DeclEntry >& entry = it->second; |
---|
[a32b204] | 105 | if ( ! entry.empty() ) { |
---|
[51b7345] | 106 | decls.push_back( entry.top().first ); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[bdd516a] | 111 | DeclarationWithType* IdTable::lookupId( const std::string &id) const { |
---|
| 112 | DeclarationWithType* result = 0; |
---|
| 113 | int depth = -1; |
---|
| 114 | |
---|
| 115 | OuterTableType::const_iterator outer = table.find( id ); |
---|
[a32b204] | 116 | if ( outer == table.end() ) return 0; |
---|
[bdd516a] | 117 | const InnerTableType &declTable = outer->second; |
---|
[a32b204] | 118 | for ( InnerTableType::const_iterator it = declTable.begin(); it != declTable.end(); ++it ) { |
---|
[bdd516a] | 119 | const std::stack< DeclEntry >& entry = it->second; |
---|
[a32b204] | 120 | if ( ! entry.empty() && entry.top().second > depth ) { |
---|
[bdd516a] | 121 | result = entry.top().first; |
---|
| 122 | depth = entry.top().second; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | return result; |
---|
| 126 | } |
---|
| 127 | |
---|
[51b7345] | 128 | void |
---|
| 129 | IdTable::dump( std::ostream &os ) const |
---|
| 130 | { |
---|
[a32b204] | 131 | for ( OuterTableType::const_iterator outer = table.begin(); outer != table.end(); ++outer ) { |
---|
| 132 | for ( InnerTableType::const_iterator inner = outer->second.begin(); inner != outer->second.end(); ++inner ) { |
---|
[bdd516a] | 133 | #if 0 |
---|
| 134 | const std::stack< DeclEntry >& entry = inner->second; |
---|
[a32b204] | 135 | if ( ! entry.empty() ) { // && entry.top().second == scopeLevel ) { |
---|
[bdd516a] | 136 | os << outer->first << " (" << inner->first << ") (" << entry.top().second << ")" << std::endl; |
---|
| 137 | } else { |
---|
| 138 | os << outer->first << " (" << inner->first << ") ( entry-empty)" << std::endl; |
---|
| 139 | } |
---|
| 140 | #endif |
---|
| 141 | #if 0 |
---|
| 142 | std::stack<DeclEntry> stack = inner->second; |
---|
| 143 | os << "dumping a stack" << std::endl; |
---|
[a32b204] | 144 | while (! stack.empty()) { |
---|
[bdd516a] | 145 | DeclEntry d = stack.top(); |
---|
| 146 | os << outer->first << " (" << inner->first << ") (" << d.second << ") " << std::endl; |
---|
| 147 | stack.pop(); |
---|
| 148 | } |
---|
| 149 | #endif |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | #if 0 |
---|
[a32b204] | 153 | for ( OuterTableType::const_iterator outer = table.begin(); outer != table.end(); ++outer ) { |
---|
| 154 | for ( InnerTableType::const_iterator inner = outer->second.begin(); inner != outer->second.end(); ++inner ) { |
---|
[51b7345] | 155 | const std::stack< DeclEntry >& entry = inner->second; |
---|
[a32b204] | 156 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
[51b7345] | 157 | os << outer->first << " (" << inner->first << ") (" << scopeLevel << ")" << std::endl; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
[bdd516a] | 161 | #endif |
---|
[51b7345] | 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | } // namespace SymTab |
---|