Skip to content

Commit b776e3f

Browse files
danpoeNlightNFotis
authored andcommitted
Resolve type symbols in symbol table when loading from json
1 parent 4a76f81 commit b776e3f

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/json-symtab-language/json_symtab_language.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ 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

16+
/// Parse a goto program in json form.
17+
/// \param instream: The input stream
18+
/// \param path: A file path
19+
/// \return: boolean signifying success or failure of the parsing
1520
bool json_symtab_languaget::parse(
1621
std::istream &instream,
1722
const std::string &path)
@@ -23,13 +28,18 @@ bool json_symtab_languaget::parse(
2328
parsed_json_file);
2429
}
2530

31+
/// Typecheck a goto program in json form.
32+
/// \param symbol_table: The symbol table containing symbols read from file.
33+
/// \param module: A useless parameter, there for interface consistency.
34+
/// \return: boolean signifying success or failure of the typechecking.
2635
bool json_symtab_languaget::typecheck(
2736
symbol_tablet &symbol_table,
2837
const std::string &module)
2938
{
3039
try
3140
{
3241
symbol_table_from_json(parsed_json_file, symbol_table);
42+
follow_type_symbols(symbol_table);
3343
return false;
3444
}
3545
catch(const std::string &str)
@@ -39,6 +49,62 @@ bool json_symtab_languaget::typecheck(
3949
}
4050
}
4151

52+
/// Follow type symbols present in the irep using the passed irep
53+
/// as the root for this operation.
54+
/// \param irep: An irep `irep` to use as a root for the recursive following.
55+
/// \param ns: The namespace to use for symbol following.
56+
void json_symtab_languaget::follow_type_symbols(
57+
irept &irep,
58+
const namespacet &ns)
59+
{
60+
ns.follow_type_symbol(irep);
61+
62+
for(irept &sub : irep.get_sub())
63+
{
64+
follow_type_symbols(sub, ns);
65+
}
66+
67+
for(auto &entry : irep.get_named_sub())
68+
{
69+
irept &sub = entry.second;
70+
71+
follow_type_symbols(sub, ns);
72+
}
73+
}
74+
75+
/// Follow type symbols present in every symbol in the symbol table.
76+
/// \param symbol_table: The symbol table `symbol_table` that has been produced as part
77+
/// of the parsing and the typechecking of the goto program in json
78+
/// form.
79+
void json_symtab_languaget::follow_type_symbols(symbol_tablet &symbol_table)
80+
{
81+
const namespacet ns(symbol_table);
82+
83+
std::vector<irep_idt> type_symbol_names;
84+
85+
for(auto &entry : symbol_table)
86+
{
87+
symbolt &symbol = symbol_table.get_writeable_ref(entry.first);
88+
89+
if(symbol.is_type)
90+
{
91+
type_symbol_names.push_back(symbol.name);
92+
}
93+
94+
// Modify entries in place
95+
follow_type_symbols(symbol.type, ns);
96+
follow_type_symbols(symbol.value, ns);
97+
}
98+
99+
for(const irep_idt &id : type_symbol_names)
100+
{
101+
symbol_table.remove(id);
102+
}
103+
}
104+
105+
/// Output the result of the parsed json file to the output stream
106+
/// passed as a parameter to this function.
107+
/// \param out: The stream to use to output the parsed_json_file.
42108
void json_symtab_languaget::show_parse(std::ostream &out)
43109
{
44110
parsed_json_file.output(out);

src/json-symtab-language/json_symtab_language.h

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class json_symtab_languaget : public languaget
5656
}
5757

5858
protected:
59+
void follow_type_symbols(symbol_tablet &symbol_table);
60+
void follow_type_symbols(irept &irep, const namespacet &ns);
61+
5962
jsont parsed_json_file;
6063
};
6164

0 commit comments

Comments
 (0)