Skip to content

Commit 5d0c656

Browse files
Merge pull request #2 from danpoe/feature/resolve-symbol-types-in-symbol-table
Resolve symbol types in symbol table
2 parents 676b1bd + 95af4f9 commit 5d0c656

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

src/json-symtab-language/json_symtab_language.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Author: Chris Smowton, [email protected]
1111
#include "json_symtab_language.h"
1212
#include <json/json_parser.h>
1313
#include <util/json_symbol_table.h>
14+
#include <util/namespace.h>
1415

1516
bool json_symtab_languaget::parse(
1617
std::istream &instream,
@@ -30,6 +31,7 @@ bool json_symtab_languaget::typecheck(
3031
try
3132
{
3233
symbol_table_from_json(parsed_json_file, symbol_table);
34+
follow_type_symbols(symbol_table);
3335
return false;
3436
}
3537
catch(const std::string &str)
@@ -39,6 +41,51 @@ bool json_symtab_languaget::typecheck(
3941
}
4042
}
4143

44+
void json_symtab_languaget::follow_type_symbols(
45+
irept &irep,
46+
const namespacet &ns)
47+
{
48+
ns.follow_type_symbol(irep);
49+
50+
for(irept &sub : irep.get_sub())
51+
{
52+
follow_type_symbols(sub, ns);
53+
}
54+
55+
for(auto &entry : irep.get_named_sub())
56+
{
57+
irept &sub = entry.second;
58+
59+
follow_type_symbols(sub, ns);
60+
}
61+
}
62+
63+
void json_symtab_languaget::follow_type_symbols(symbol_tablet &symbol_table)
64+
{
65+
const namespacet ns(symbol_table);
66+
67+
std::vector<irep_idt> type_symbol_names;
68+
69+
for(auto &entry : symbol_table)
70+
{
71+
symbolt &symbol = symbol_table.get_writeable_ref(entry.first);
72+
73+
if(symbol.is_type)
74+
{
75+
type_symbol_names.push_back(symbol.name);
76+
}
77+
78+
// Modify entries in place
79+
follow_type_symbols(symbol.type, ns);
80+
follow_type_symbols(symbol.value, ns);
81+
}
82+
83+
for(const irep_idt &id : type_symbol_names)
84+
{
85+
symbol_table.remove(id);
86+
}
87+
}
88+
4289
void json_symtab_languaget::show_parse(std::ostream &out)
4390
{
4491
parsed_json_file.output(out);

src/json-symtab-language/json_symtab_language.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class json_symtab_languaget:public languaget
6060
}
6161

6262
protected:
63+
void follow_type_symbols(symbol_tablet &symbol_table);
64+
void follow_type_symbols(irept &irep, const namespacet &ns);
65+
6366
jsont parsed_json_file;
6467
};
6568

src/util/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SRC = arith_tools.cpp \
3838
json_expr.cpp \
3939
json_irep.cpp \
4040
json_stream.cpp \
41+
json_symbol.cpp \
4142
json_symbol_table.cpp \
4243
json_symbol.cpp \
4344
lispexpr.cpp \

src/util/namespace.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ void namespace_baset::follow_symbol(irept &irep) const
6767
}
6868
}
6969

70+
void namespace_baset::follow_type_symbol(irept &irep) const
71+
{
72+
while(irep.id() == ID_symbol)
73+
{
74+
const symbolt &symbol = lookup(irep);
75+
76+
if(symbol.is_type && !symbol.type.is_nil())
77+
{
78+
irep = symbol.type;
79+
}
80+
else
81+
{
82+
break;
83+
}
84+
}
85+
}
86+
7087
const typet &namespace_baset::follow(const typet &src) const
7188
{
7289
if(src.id()!=ID_symbol)

src/util/namespace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class namespace_baset
4343
virtual ~namespace_baset();
4444

4545
void follow_symbol(irept &irep) const;
46+
void follow_type_symbol(irept &irep) const;
4647
void follow_macros(exprt &expr) const;
4748
const typet &follow(const typet &src) const;
4849

src/util/symbol_table_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class symbol_table_baset
179179
return &**this;
180180
}
181181

182-
symbolt &get_writeable_symbol(const irep_idt &identifier)
182+
symbolt &get_writeable_symbol()
183183
{
184184
if(on_get_writeable)
185185
on_get_writeable((*this)->first);

0 commit comments

Comments
 (0)