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)
@@ -23,13 +28,18 @@ bool json_symtab_languaget::parse(
23
28
parsed_json_file);
24
29
}
25
30
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.
26
35
bool json_symtab_languaget::typecheck (
27
36
symbol_tablet &symbol_table,
28
37
const std::string &module)
29
38
{
30
39
try
31
40
{
32
41
symbol_table_from_json (parsed_json_file, symbol_table);
42
+ follow_type_symbols (symbol_table);
33
43
return false ;
34
44
}
35
45
catch (const std::string &str)
@@ -39,6 +49,62 @@ bool json_symtab_languaget::typecheck(
39
49
}
40
50
}
41
51
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.
42
108
void json_symtab_languaget::show_parse (std::ostream &out)
43
109
{
44
110
parsed_json_file.output (out);
0 commit comments