Skip to content

Commit af2e8bd

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

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/json-symtab-language/json_symtab_language.cpp

+61
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_symbol_types(symbol_table);
3335
return false;
3436
}
3537
catch(const std::string &str)
@@ -39,6 +41,65 @@ bool json_symtab_languaget::typecheck(
3941
}
4042
}
4143

44+
void json_symtab_languaget::follow_symbol_types(
45+
irept &irep,
46+
const namespacet &ns)
47+
{
48+
if(irep.id() == ID_symbol)
49+
{
50+
const symbolt &symbol = ns.lookup(irep);
51+
52+
if(symbol.is_type)
53+
{
54+
ns.follow_symbol(irep);
55+
}
56+
}
57+
58+
for(irept &sub : irep.get_sub())
59+
{
60+
follow_symbol_types(sub, ns);
61+
}
62+
63+
for(auto &entry : irep.get_named_sub())
64+
{
65+
irept &sub = entry.second;
66+
67+
follow_symbol_types(sub, ns);
68+
}
69+
}
70+
71+
void json_symtab_languaget::follow_symbol_types(symbol_tablet &symbol_table)
72+
{
73+
const namespacet ns(symbol_table);
74+
75+
typedef symbol_tablet::symbolst symbolst;
76+
symbolst &symbols = symbol_table.symbols;
77+
78+
for(auto &entry : symbols)
79+
{
80+
symbolt &symbol = entry.second;
81+
82+
// Modify entries in place
83+
follow_symbol_types(symbol.type, ns);
84+
follow_symbol_types(symbol.value, ns);
85+
}
86+
87+
// Remove type entries from the symbol table
88+
for(symbolst::iterator it = symbols.begin(); it != symbols.end();)
89+
{
90+
const symbolt &symbol = it->second;
91+
92+
if(symbol.is_type)
93+
{
94+
it = symbols.erase(it);
95+
}
96+
else
97+
{
98+
it++;
99+
}
100+
}
101+
}
102+
42103
void json_symtab_languaget::show_parse(std::ostream &out)
43104
{
44105
parsed_json_file.output(out);

src/json-symtab-language/json_symtab_language.h

+3
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_symbol_types(symbol_tablet &symbol_table);
64+
void follow_symbol_types(irept &irep, const namespacet &ns);
65+
6366
jsont parsed_json_file;
6467
};
6568

0 commit comments

Comments
 (0)