Skip to content

Commit e262b18

Browse files
committed
Resolve symbol types in symbol table
1 parent 7e72b44 commit e262b18

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/json-symtab-language/json_symtab_language.cpp

Lines changed: 53 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,57 @@ 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+
typedef symbol_tablet::symbolst symbolst;
68+
symbolst &symbols = symbol_table.symbols;
69+
70+
for(auto &entry : symbols)
71+
{
72+
symbolt &symbol = entry.second;
73+
74+
// Modify entries in place
75+
follow_type_symbols(symbol.type, ns);
76+
follow_type_symbols(symbol.value, ns);
77+
}
78+
79+
// Remove type entries from the symbol table
80+
for(symbolst::iterator it = symbols.begin(); it != symbols.end();)
81+
{
82+
const symbolt &symbol = it->second;
83+
84+
if(symbol.is_type)
85+
{
86+
it = symbols.erase(it);
87+
}
88+
else
89+
{
90+
it++;
91+
}
92+
}
93+
}
94+
4295
void json_symtab_languaget::show_parse(std::ostream &out)
4396
{
4497
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/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

0 commit comments

Comments
 (0)