11
11
#include " json_symtab_language.h"
12
12
#include < json/json_parser.h>
13
13
#include < util/json_symbol_table.h>
14
+ #include < util/namespace.h>
14
15
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
15
20
bool json_symtab_languaget::parse (
16
21
std::istream &instream,
17
22
const std::string &path)
18
23
{
19
24
return parse_json (instream, path, get_message_handler (), parsed_json_file);
20
25
}
21
26
27
+ // / Typecheck a goto program in json form.
28
+ // / \param symbol_table: The symbol table containing symbols read from file.
29
+ // / \param module: A useless parameter, there for interface consistency.
30
+ // / \return: boolean signifying success or failure of the typechecking.
22
31
bool json_symtab_languaget::typecheck (
23
32
symbol_tablet &symbol_table,
24
33
const std::string &module)
25
34
{
26
35
try
27
36
{
28
37
symbol_table_from_json (parsed_json_file, symbol_table);
38
+ follow_type_symbols (symbol_table);
29
39
return false ;
30
40
}
31
41
catch (const std::string &str)
@@ -35,6 +45,75 @@ bool json_symtab_languaget::typecheck(
35
45
}
36
46
}
37
47
48
+ // / Follow type symbols present in the irep using the passed irep
49
+ // / as the root for this operation.
50
+ // / \param irep: An irep `irep` to use as a root for the recursive following.
51
+ // / \param ns: The namespace to use for symbol following.
52
+ void json_symtab_languaget::follow_type_symbols (
53
+ irept &irep,
54
+ const namespacet &ns)
55
+ {
56
+ while (irep.id () == ID_symbol)
57
+ {
58
+ const symbolt &symbol =
59
+ ns.lookup (to_symbol_expr (static_cast <exprt const &>(irep)));
60
+
61
+ if (symbol.is_type && !symbol.type .is_nil ())
62
+ {
63
+ irep = symbol.type ;
64
+ }
65
+ else
66
+ {
67
+ break ;
68
+ }
69
+ }
70
+
71
+ for (irept &sub : irep.get_sub ())
72
+ {
73
+ follow_type_symbols (sub, ns);
74
+ }
75
+
76
+ for (auto &entry : irep.get_named_sub ())
77
+ {
78
+ irept &sub = entry.second ;
79
+
80
+ follow_type_symbols (sub, ns);
81
+ }
82
+ }
83
+
84
+ // / Follow type symbols present in every symbol in the symbol table.
85
+ // / \param symbol_table: The symbol table `symbol_table` that has been
86
+ // / produced as part of the parsing and the
87
+ // / typechecking of the goto program in json form.
88
+ void json_symtab_languaget::follow_type_symbols (symbol_tablet &symbol_table)
89
+ {
90
+ const namespacet ns (symbol_table);
91
+
92
+ std::vector<irep_idt> type_symbol_names;
93
+
94
+ for (auto &entry : symbol_table)
95
+ {
96
+ symbolt &symbol = symbol_table.get_writeable_ref (entry.first );
97
+
98
+ if (symbol.is_type )
99
+ {
100
+ type_symbol_names.push_back (symbol.name );
101
+ }
102
+
103
+ // Modify entries in place
104
+ follow_type_symbols (symbol.type , ns);
105
+ follow_type_symbols (symbol.value , ns);
106
+ }
107
+
108
+ for (const irep_idt &id : type_symbol_names)
109
+ {
110
+ symbol_table.remove (id);
111
+ }
112
+ }
113
+
114
+ // / Output the result of the parsed json file to the output stream
115
+ // / passed as a parameter to this function.
116
+ // / \param out: The stream to use to output the parsed_json_file.
38
117
void json_symtab_languaget::show_parse (std::ostream &out)
39
118
{
40
119
parsed_json_file.output (out);
0 commit comments