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 | { |
---|
35 | for ( OuterTableType::iterator outer = table.begin(); outer != table.end(); ++outer ) { |
---|
36 | for ( InnerTableType::iterator inner = outer->second.begin(); inner != outer->second.end(); ++inner ) { |
---|
37 | std::stack< DeclEntry >& entry = inner->second; |
---|
38 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
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; |
---|
53 | if ( decl->get_linkage() == LinkageSpec::C ) { |
---|
54 | manglename = name; |
---|
55 | } else { |
---|
56 | manglename = Mangler::mangle( decl ); |
---|
57 | } |
---|
58 | InnerTableType &declTable = table[ name ]; |
---|
59 | InnerTableType::iterator it = declTable.find( manglename ); |
---|
60 | if ( it == declTable.end() ) { |
---|
61 | declTable[ manglename ].push( DeclEntry( decl, scopeLevel ) ); |
---|
62 | } else { |
---|
63 | std::stack< DeclEntry >& entry = it->second; |
---|
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() ) ) { |
---|
66 | FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( decl ); |
---|
67 | FunctionDecl *old = dynamic_cast< FunctionDecl* >( entry.top().first ); |
---|
68 | if ( newentry && old && newentry->get_statements() && old->get_statements() ) { |
---|
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 ); |
---|
73 | if ( newobj && oldobj && newobj->get_init() && oldobj->get_init() ) { |
---|
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 | } |
---|
84 | // ensure the set of routines with C linkage cannot be overloaded |
---|
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 ) { |
---|
87 | InnerTableType::iterator j = i; |
---|
88 | for ( j++; j != declTable.end(); ++j ) { |
---|
89 | if ( ! j->second.empty() && j->second.top().first->get_linkage() == LinkageSpec::C ) { |
---|
90 | throw SemanticError( "invalid overload of C function " ); |
---|
91 | } |
---|
92 | } |
---|
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 ); |
---|
101 | if ( outer == table.end() ) return; |
---|
102 | const InnerTableType &declTable = outer->second; |
---|
103 | for ( InnerTableType::const_iterator it = declTable.begin(); it != declTable.end(); ++it ) { |
---|
104 | const std::stack< DeclEntry >& entry = it->second; |
---|
105 | if ( ! entry.empty() ) { |
---|
106 | decls.push_back( entry.top().first ); |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
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 ); |
---|
116 | if ( outer == table.end() ) return 0; |
---|
117 | const InnerTableType &declTable = outer->second; |
---|
118 | for ( InnerTableType::const_iterator it = declTable.begin(); it != declTable.end(); ++it ) { |
---|
119 | const std::stack< DeclEntry >& entry = it->second; |
---|
120 | if ( ! entry.empty() && entry.top().second > depth ) { |
---|
121 | result = entry.top().first; |
---|
122 | depth = entry.top().second; |
---|
123 | } |
---|
124 | } |
---|
125 | return result; |
---|
126 | } |
---|
127 | |
---|
128 | void |
---|
129 | IdTable::dump( std::ostream &os ) const |
---|
130 | { |
---|
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 ) { |
---|
133 | #if 0 |
---|
134 | const std::stack< DeclEntry >& entry = inner->second; |
---|
135 | if ( ! entry.empty() ) { // && entry.top().second == scopeLevel ) { |
---|
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; |
---|
144 | while (! stack.empty()) { |
---|
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 |
---|
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 ) { |
---|
155 | const std::stack< DeclEntry >& entry = inner->second; |
---|
156 | if ( ! entry.empty() && entry.top().second == scopeLevel ) { |
---|
157 | os << outer->first << " (" << inner->first << ") (" << scopeLevel << ")" << std::endl; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | #endif |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | } // namespace SymTab |
---|