Skip to content

Commit 222e732

Browse files
committed
Replace Irep JSON-wrapper with a recursive datatype
This uses containers-of-pointers because Ada recursive containers appear to be quite difficult.
1 parent 7aa43fb commit 222e732

File tree

5 files changed

+61
-26
lines changed

5 files changed

+61
-26
lines changed

gnat2goto/driver/tree_walk.adb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ with Stand; use Stand;
33
with Treepr; use Treepr;
44
with Namet; use Namet;
55

6-
with GNATCOLL.JSON; use GNATCOLL.JSON;
7-
86
with Iinfo; use Iinfo;
97
with Irep_Helpers; use Irep_Helpers;
108
with Uint_To_Binary; use Uint_To_Binary;
@@ -227,9 +225,7 @@ package body Tree_Walk is
227225
else Make_Irep_Code_Block);
228226

229227
-- Append the HSS_Rep block to the Decls_Rep one:
230-
for I in Integer range 1 .. Length (HSS_Rep.Sub) loop
231-
Append (Decls_Rep.Sub, Get (HSS_Rep.Sub, I));
232-
end loop;
228+
Irep_Vectors.Append (Decls_Rep.Sub, HSS_Rep.Sub);
233229
return Decls_Rep;
234230
end Do_Subprogram_Or_Block;
235231

irep_utils/src/iinfo.adb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,34 @@ package body Iinfo is
3232
end Trivial;
3333

3434
function Irep_To_Json (Ir : Irep) return JSON_Value is
35-
ToRet : JSON_Value := Create_Object;
36-
35+
Ret_Sub : JSON_Array := Empty_Array;
36+
Ret_Named_Sub : JSON_Value := Create_Object;
37+
Ret_Comment : JSON_Value := Create_Object;
3738
begin
39+
for Sub of Ir.Sub loop
40+
Append (Ret_Sub, Irep_To_Json (Sub.All));
41+
end loop;
42+
for C in Ir.Named_Sub.Iterate loop
43+
Set_Field (Ret_Named_Sub, To_String (Irep_Maps.Key (C)), Irep_To_Json (Irep_Maps.Element (C).All));
44+
end loop;
45+
for C in Ir.Comment.Iterate loop
46+
Set_Field (Ret_Comment, To_String (Irep_Maps.Key (C)), Irep_To_Json (Irep_Maps.Element (C).All));
47+
end loop;
48+
3849
return R : Json_Value := Create_Object do
3950
R.Set_Field ("id", Ir.Id);
40-
R.Set_Field ("sub", Ir.Sub);
41-
R.Set_Field ("namedSub", Ir.Named_Sub);
42-
R.Set_Field ("comment", Ir.Comment);
51+
R.Set_Field ("sub", Ret_Sub);
52+
R.Set_Field ("namedSub", Ret_Named_Sub);
53+
R.Set_Field ("comment", Ret_Comment);
4354
end return;
4455
end Irep_To_Json;
4556

57+
function Alloc_Clone (Ir : Irep) return Irep_Ptr is
58+
begin
59+
return new Irep'(Id => Ir.Id,
60+
Sub => Ir.Sub,
61+
Named_Sub => Ir.Named_Sub,
62+
Comment => Ir.Comment);
63+
end Alloc_Clone;
64+
4665
end Iinfo;

irep_utils/src/iinfo.ads

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
with GNATCOLL.JSON; use GNATCOLL.JSON;
2-
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
1+
with GNATCOLL.JSON; use GNATCOLL.JSON;
2+
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
3+
with Ada.Strings.Unbounded.Hash;
4+
with Ada.Containers.Vectors;
5+
with Ada.Containers.Hashed_Maps;
36

47
package Iinfo is
58

9+
type Irep;
10+
type Irep_Ptr is access Irep;
11+
12+
package Irep_Maps is new Ada.Containers.Hashed_Maps
13+
(Key_Type => Unbounded_String,
14+
Element_Type => Irep_Ptr,
15+
Hash => Ada.Strings.Unbounded.Hash,
16+
Equivalent_Keys => "=");
17+
18+
package Irep_Vectors is new Ada.Containers.Vectors
19+
(Element_Type => Irep_Ptr,
20+
Index_Type => Positive);
21+
622
type Irep is record
723
Id : Unbounded_String;
8-
Sub : JSON_Array := Empty_Array;
9-
Named_Sub : JSON_Value := Create_Object;
10-
Comment : JSON_Value := Create_Object;
24+
Sub : Irep_Vectors.Vector;
25+
Named_Sub : Irep_Maps.Map;
26+
Comment : Irep_Maps.Map;
1127
end record;
1228

1329
package Trivial is
@@ -18,5 +34,6 @@ package Iinfo is
1834
end Trivial;
1935

2036
function Irep_To_Json (Ir : Irep) return JSON_Value;
37+
function Alloc_Clone (Ir : Irep) return Irep_Ptr;
2138

2239
end Iinfo;

irep_utils/src/irep_helpers.adb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11

22
with GNATCOLL.JSON; use GNATCOLL.JSON;
3+
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
34
with Iinfo; use Iinfo;
5+
with Irep_Schemata; use Irep_Schemata;
46

57
package body Irep_Helpers is
6-
8+
79
function To_Code_Block (Input : Irep_Code) return Irep_Code_Block is
810
-- TODO: auto-synthesise getters
9-
Input_Statement : constant String := Get (Get (Input.Named_Sub, "statement"), "id");
11+
Statement_Irep : constant Irep :=
12+
Irep_Maps.Element (Input.Named_Sub, To_Unbounded_String ("statement")).all;
13+
Input_Statement : constant Unbounded_String := Statement_Irep.Id;
1014
begin
1115
if (Input_Statement = "block") then
1216
return Irep_Code_Block (Input);

irep_utils/src/irep_specs_to_ada.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
outbody = open(sys.argv[2] + ".adb", "w")
1515

1616
outspec.write("with Iinfo; use Iinfo;\n")
17-
outspec.write("with GNATCOLL.JSON; use GNATCOLL.JSON;\n")
1817
outspec.write("\n")
1918
outspec.write("package Irep_Schemata is\n")
2019

21-
outbody.write("with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;\n")
20+
outbody.write("with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;\n")
2221
outbody.write("\n")
2322
outbody.write("package body Irep_Schemata is\n")
2423

2524
def write_set_field(obj, field, value, indent_lvl=0):
26-
rv = (indent * indent_lvl) + "Set_Field ("
25+
rv = (indent * indent_lvl) + "Irep_Maps.Insert ("
2726
offset = len(rv)
2827
rv += obj + ",\n"
29-
rv += (" " * offset) + '"%s",\n' % field
30-
rv += (" " * offset) + 'Irep_To_Json (%s));\n' % value
28+
rv += (" " * offset) + 'To_Unbounded_String ("%s"),\n' % field
29+
rv += (" " * offset) + 'Alloc_Clone (%s));\n' % value
3130
return rv
3231

3332
def to_ada_identifier(s):
@@ -120,15 +119,15 @@ def ada_from_schema(schema_name, schema):
120119
continue
121120

122121
for subname in subnames:
123-
body = "Set_Element (Irep_To_Modify.Sub, %d, Irep_To_Json (%s));" % \
122+
body = "Irep_Vectors.Replace_Element (Irep_To_Modify.Sub, %d, Alloc_Clone (%s));" % \
124123
(i + 1, ada_argument_conversion_from_schema(sub, "Value"))
125124
write_set_method(subname, schema_name, ada_type_from_schema(sub), body)
126125

127126
if "number" in sub:
128127
assert sub["number"] == "*"
129128
body = "Irep_To_Modify.Sub := Value;"
130-
write_set_method(subname + "s", schema_name, "JSON_Array", body)
131-
add_body = "Append (Irep_To_Modify.Sub, Irep_To_Json (%s));" % \
129+
write_set_method(subname + "s", schema_name, "Irep_Vectors.Vector", body)
130+
add_body = "Irep_Vectors.Append (Irep_To_Modify.Sub, Alloc_Clone (%s));" % \
132131
(ada_argument_conversion_from_schema(sub, "Value"))
133132
write_mutator_method("Add", subname, schema_name, ada_type_from_schema(sub), add_body)
134133

@@ -206,7 +205,7 @@ def get_constant_assignments(schema):
206205
if n_required_operands != 0:
207206
outbody.write(indent + indent + "-- Add null values for required operands\n")
208207
for i in range(n_required_operands):
209-
outbody.write(indent + indent + "Append (Ret.Sub, Irep_To_Json (Trivial.Trivial_Irep (\"\")));\n")
208+
outbody.write(indent + indent + "Irep_Vectors.Append (Ret.Sub, Alloc_Clone (Trivial.Trivial_Irep (\"\")));\n")
210209
constant_assignments = get_constant_assignments(schema)
211210
if len(constant_assignments) != 0:
212211
outbody.write(indent + indent + "-- Set constant members:\n")

0 commit comments

Comments
 (0)