Skip to content

Commit 61f2883

Browse files
committed
Fix discriminated record
by giving the struct. component the right name and by forcing the initialisation of the constraints.
1 parent 16d1d42 commit 61f2883

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

gnat2goto/driver/tree_walk.adb

+29-4
Original file line numberDiff line numberDiff line change
@@ -3123,7 +3123,16 @@ package body Tree_Walk is
31233123
Variant_Disc_Value := Disc_Actual;
31243124
end if;
31253125

3126-
New_Expr := Do_Expression (Disc_Actual);
3126+
if Present (Disc_Actual) then
3127+
New_Expr := Do_Expression (Disc_Actual);
3128+
else
3129+
-- Default initialize to 0
3130+
New_Expr := Make_Constant_Expr
3131+
(Source_Location => Get_Source_Location (E),
3132+
I_Type => Do_Type_Reference (Etype (Iter)),
3133+
Range_Check => False,
3134+
Value => "0");
3135+
end if;
31273136
Append_Struct_Member (Ret, New_Expr);
31283137
-- Substitute uses of the discriminant in the record
31293138
-- initialiser for its actual value:
@@ -3196,7 +3205,7 @@ package body Tree_Walk is
31963205
elsif Ekind (E) in Record_Kind then
31973206
return Make_Record_Default_Initialiser (E, DCs);
31983207
else
3199-
return Report_Unhandled_Node_Irep (N, "Make_Default_Initialiser",
3208+
return Report_Unhandled_Node_Irep (E, "Make_Default_Initialiser",
32003209
"Unknown Ekind");
32013210
end if;
32023211
end Make_Default_Initialiser;
@@ -3214,7 +3223,10 @@ package body Tree_Walk is
32143223

32153224
if Has_Init_Expression (N) or Present (Expression (N)) then
32163225
Init_Expr := Do_Expression (Expression (N));
3217-
elsif Needs_Default_Initialisation (Etype (Defined)) then
3226+
elsif Needs_Default_Initialisation (Etype (Defined)) or
3227+
(Present (Object_Definition (N)) and then
3228+
Nkind (Object_Definition (N)) = N_Subtype_Indication)
3229+
then
32183230
declare
32193231
Defn : constant Node_Id := Object_Definition (N);
32203232
Discriminant_Constraint : constant Node_Id :=
@@ -4458,8 +4470,21 @@ package body Tree_Walk is
44584470
function Do_Selected_Component (N : Node_Id) return Irep is
44594471
Root : constant Irep := Do_Expression (Prefix (N));
44604472
Component : constant Entity_Id := Entity (Selector_Name (N));
4473+
4474+
-- Example:
4475+
-- struct Foo { int a; };
4476+
-- struct Foo bar;
4477+
-- bar.a = 5;
4478+
--
4479+
-- The Unique_Name of the struct-component in declaration is Foo__a. But
4480+
-- when parsing the assignment the component is that of the entity bar,
4481+
-- thus it's unique-name is bar__a: which is not present in Foo. That's
4482+
-- why we have to use the component of the original-record to get the
4483+
-- right name.
4484+
Orig_Component : constant Entity_Id :=
4485+
Original_Record_Component (Component);
44614486
Component_Type : constant Irep := Do_Type_Reference (Etype (Component));
4462-
Component_Name : constant String := Unique_Name (Component);
4487+
Component_Name : constant String := Unique_Name (Orig_Component);
44634488
Source_Location : constant Irep := Get_Source_Location (N);
44644489
begin
44654490
if Do_Discriminant_Check (N) then

testsuite/gnat2goto/tests/discriminated_record/discriminated_record.adb

+4
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ procedure Discriminated_Record is
1515
begin
1616
Inst1.A (5) := 1;
1717
Inst1.B (10) := 2;
18+
19+
pragma Assert (Inst1.A (5) = 1);
20+
Inst1.B (6) := 3;
21+
pragma Assert (Inst1.A (5) = 1);
1822
end Discriminated_Record;
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
[discriminated_record.assertion.1] line 19 assertion Inst1.A (5) = 1: SUCCESS
2+
[discriminated_record.assertion.2] line 21 assertion Inst1.A (5) = 1: SUCCESS
13
VERIFICATION SUCCESSFUL

0 commit comments

Comments
 (0)