with Atree; use Atree;
with Einfo; use Einfo;
with Namet; use Namet;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Rtsfind; use Rtsfind;
with Sem_Util; use Sem_Util;
with Sinfo; use Sinfo;
with Snames; use Snames;
with Stand; use Stand;
with Tbuild; use Tbuild;
with Ttypes; use Ttypes;
with Uintp; use Uintp;
package body Exp_Strm is
procedure Build_Array_Read_Write_Procedure
(Nod : Node_Id;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : Entity_Id;
Nam : Name_Id);
procedure Build_Record_Read_Write_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : Entity_Id;
Nam : Name_Id);
procedure Build_Stream_Function
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Fnam : Entity_Id;
Decls : List_Id;
Stms : List_Id);
function Has_Stream_Standard_Rep (U_Type : Entity_Id) return Boolean;
function Make_Stream_Subprogram_Name
(Loc : Source_Ptr;
Typ : Entity_Id;
Nam : TSS_Name_Type) return Entity_Id;
function Stream_Base_Type (E : Entity_Id) return Entity_Id;
procedure Build_Array_Input_Function
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Fnam : out Entity_Id)
is
Dim : constant Pos := Number_Dimensions (Typ);
Lnam : Name_Id;
Hnam : Name_Id;
Decls : List_Id;
Ranges : List_Id;
Stms : List_Id;
Indx : Node_Id;
begin
Decls := New_List;
Ranges := New_List;
Indx := First_Index (Typ);
for J in 1 .. Dim loop
Lnam := New_External_Name ('L', J);
Hnam := New_External_Name ('H', J);
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Lnam),
Constant_Present => True,
Object_Definition => New_Occurrence_Of (Etype (Indx), Loc),
Expression =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
Attribute_Name => Name_Input,
Expressions => New_List (Make_Identifier (Loc, Name_S)))));
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Hnam),
Constant_Present => True,
Object_Definition =>
New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
Expression =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
Attribute_Name => Name_Input,
Expressions => New_List (Make_Identifier (Loc, Name_S)))));
Append_To (Ranges,
Make_Range (Loc,
Low_Bound => Make_Identifier (Loc, Lnam),
High_Bound => Make_Identifier (Loc, Hnam)));
Next_Index (Indx);
end loop;
if Is_Constrained (Stream_Base_Type (Typ)) then
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Object_Definition =>
New_Occurrence_Of (Stream_Base_Type (Typ), Loc)));
else
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Object_Definition =>
Make_Subtype_Indication (Loc,
Subtype_Mark =>
New_Occurrence_Of (Stream_Base_Type (Typ), Loc),
Constraint =>
Make_Index_Or_Discriminant_Constraint (Loc,
Constraints => Ranges))));
end if;
Stms := New_List (
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Read,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Identifier (Loc, Name_V))),
Make_Return_Statement (Loc,
Expression => Make_Identifier (Loc, Name_V)));
Fnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Input));
Build_Stream_Function (Loc, Typ, Decl, Fnam, Decls, Stms);
end Build_Array_Input_Function;
procedure Build_Array_Output_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Stms : List_Id;
Indx : Node_Id;
begin
Indx := First_Index (Typ);
Stms := New_List;
for J in 1 .. Number_Dimensions (Typ) loop
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_First,
Expressions => New_List (
Make_Integer_Literal (Loc, J))))));
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Stream_Base_Type (Etype (Indx)), Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Last,
Expressions => New_List (
Make_Integer_Literal (Loc, J))))));
Next_Index (Indx);
end loop;
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Identifier (Loc, Name_V))));
Pnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Output));
Build_Stream_Procedure (Loc, Typ, Decl, Pnam, Stms, False);
end Build_Array_Output_Procedure;
procedure Build_Array_Read_Procedure
(Nod : Node_Id;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Loc : constant Source_Ptr := Sloc (Nod);
begin
Pnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Read));
Build_Array_Read_Write_Procedure (Nod, Typ, Decl, Pnam, Name_Read);
end Build_Array_Read_Procedure;
procedure Build_Array_Read_Write_Procedure
(Nod : Node_Id;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : Entity_Id;
Nam : Name_Id)
is
Loc : constant Source_Ptr := Sloc (Nod);
Ndim : constant Pos := Number_Dimensions (Typ);
Ctyp : constant Entity_Id := Component_Type (Typ);
Stm : Node_Id;
Exl : List_Id;
RW : Entity_Id;
begin
Exl := New_List;
for J in 1 .. Ndim loop
Append_To (Exl, Make_Identifier (Loc, New_External_Name ('L', J)));
end loop;
Stm :=
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Stream_Base_Type (Ctyp), Loc),
Attribute_Name => Nam,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Indexed_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Expressions => Exl)));
if Nam = Name_Read then
RW := TSS (Base_Type (Ctyp), TSS_Stream_Read);
else
RW := TSS (Base_Type (Ctyp), TSS_Stream_Write);
end if;
if Present (RW)
and then not Is_Frozen (RW)
then
Set_Is_Frozen (RW);
end if;
for J in 1 .. Ndim loop
Stm :=
Make_Implicit_Loop_Statement (Nod,
Iteration_Scheme =>
Make_Iteration_Scheme (Loc,
Loop_Parameter_Specification =>
Make_Loop_Parameter_Specification (Loc,
Defining_Identifier =>
Make_Defining_Identifier (Loc,
Chars => New_External_Name ('L', Ndim - J + 1)),
Discrete_Subtype_Definition =>
Make_Attribute_Reference (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Attribute_Name => Name_Range,
Expressions => New_List (
Make_Integer_Literal (Loc, Ndim - J + 1))))),
Statements => New_List (Stm));
end loop;
Build_Stream_Procedure
(Loc, Typ, Decl, Pnam, New_List (Stm), Nam = Name_Read);
end Build_Array_Read_Write_Procedure;
procedure Build_Array_Write_Procedure
(Nod : Node_Id;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Loc : constant Source_Ptr := Sloc (Nod);
begin
Pnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Write));
Build_Array_Read_Write_Procedure (Nod, Typ, Decl, Pnam, Name_Write);
end Build_Array_Write_Procedure;
function Build_Elementary_Input_Call (N : Node_Id) return Node_Id is
Loc : constant Source_Ptr := Sloc (N);
P_Type : constant Entity_Id := Entity (Prefix (N));
U_Type : constant Entity_Id := Underlying_Type (P_Type);
Rt_Type : constant Entity_Id := Root_Type (U_Type);
FST : constant Entity_Id := First_Subtype (U_Type);
Strm : constant Node_Id := First (Expressions (N));
Targ : constant Node_Id := Next (Strm);
P_Size : Uint;
Res : Node_Id;
Lib_RE : RE_Id;
begin
if Is_Elementary_Type (FST) and then Has_Stream_Size_Clause (FST) then
P_Size := Static_Integer (Expression (Stream_Size_Clause (FST)));
else
P_Size := Esize (FST);
end if;
if Rt_Type = Standard_Boolean
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_I_B;
elsif Rt_Type = Standard_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_I_C;
elsif Rt_Type = Standard_Wide_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_I_WC;
elsif Rt_Type = Standard_Wide_Wide_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_I_WWC;
elsif Is_Floating_Point_Type (U_Type) then
if P_Size <= Standard_Short_Float_Size then
Lib_RE := RE_I_SF;
elsif P_Size <= Standard_Float_Size then
Lib_RE := RE_I_F;
elsif P_Size <= Standard_Long_Float_Size then
Lib_RE := RE_I_LF;
else
Lib_RE := RE_I_LLF;
end if;
elsif not Is_Unsigned_Type (FST)
and then
(Is_Fixed_Point_Type (U_Type)
or else
Is_Enumeration_Type (U_Type)
or else
(Is_Signed_Integer_Type (U_Type)
and then not Has_Biased_Representation (FST)))
then
if P_Size <= Standard_Short_Short_Integer_Size then
Lib_RE := RE_I_SSI;
elsif P_Size <= Standard_Short_Integer_Size then
Lib_RE := RE_I_SI;
elsif P_Size <= Standard_Integer_Size then
Lib_RE := RE_I_I;
elsif P_Size <= Standard_Long_Integer_Size then
Lib_RE := RE_I_LI;
else
Lib_RE := RE_I_LLI;
end if;
elsif Is_Modular_Integer_Type (U_Type)
or else Is_Fixed_Point_Type (U_Type)
or else Is_Enumeration_Type (U_Type)
or else Is_Signed_Integer_Type (U_Type)
then
if P_Size <= Standard_Short_Short_Integer_Size then
Lib_RE := RE_I_SSU;
elsif P_Size <= Standard_Short_Integer_Size then
Lib_RE := RE_I_SU;
elsif P_Size <= Standard_Integer_Size then
Lib_RE := RE_I_U;
elsif P_Size <= Standard_Long_Integer_Size then
Lib_RE := RE_I_LU;
else
Lib_RE := RE_I_LLU;
end if;
else pragma Assert (Is_Access_Type (U_Type));
if P_Size > System_Address_Size then
Lib_RE := RE_I_AD;
else
Lib_RE := RE_I_AS;
end if;
end if;
if Nkind (Targ) = N_Identifier
and then Is_Internal_Name (Chars (Targ))
and then Is_TSS (Scope (Entity (Targ)), TSS_Stream_Read)
then
Res :=
Unchecked_Convert_To (Base_Type (U_Type),
Make_Function_Call (Loc,
Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
Parameter_Associations => New_List (
Relocate_Node (Strm))));
Set_Do_Range_Check (Res);
if Base_Type (P_Type) /= Base_Type (U_Type) then
Res := Unchecked_Convert_To (Base_Type (P_Type), Res);
end if;
return Res;
else
return
Unchecked_Convert_To (P_Type,
Make_Function_Call (Loc,
Name => New_Occurrence_Of (RTE (Lib_RE), Loc),
Parameter_Associations => New_List (
Relocate_Node (Strm))));
end if;
end Build_Elementary_Input_Call;
function Build_Elementary_Write_Call (N : Node_Id) return Node_Id is
Loc : constant Source_Ptr := Sloc (N);
P_Type : constant Entity_Id := Entity (Prefix (N));
U_Type : constant Entity_Id := Underlying_Type (P_Type);
Rt_Type : constant Entity_Id := Root_Type (U_Type);
FST : constant Entity_Id := First_Subtype (U_Type);
Strm : constant Node_Id := First (Expressions (N));
Item : constant Node_Id := Next (Strm);
P_Size : Uint;
Lib_RE : RE_Id;
Libent : Entity_Id;
begin
if Is_Elementary_Type (FST) and then Has_Stream_Size_Clause (FST) then
P_Size := Static_Integer (Expression (Stream_Size_Clause (FST)));
else
P_Size := Esize (FST);
end if;
if Rt_Type = Standard_Boolean
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_W_B;
elsif Rt_Type = Standard_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_W_C;
elsif Rt_Type = Standard_Wide_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_W_WC;
elsif Rt_Type = Standard_Wide_Wide_Character
and then Has_Stream_Standard_Rep (U_Type)
then
Lib_RE := RE_W_WWC;
elsif Is_Floating_Point_Type (U_Type) then
if P_Size <= Standard_Short_Float_Size then
Lib_RE := RE_W_SF;
elsif P_Size <= Standard_Float_Size then
Lib_RE := RE_W_F;
elsif P_Size <= Standard_Long_Float_Size then
Lib_RE := RE_W_LF;
else
Lib_RE := RE_W_LLF;
end if;
elsif not Is_Unsigned_Type (FST)
and then
(Is_Fixed_Point_Type (U_Type)
or else
Is_Enumeration_Type (U_Type)
or else
(Is_Signed_Integer_Type (U_Type)
and then not Has_Biased_Representation (FST)))
then
if P_Size <= Standard_Short_Short_Integer_Size then
Lib_RE := RE_W_SSI;
elsif P_Size <= Standard_Short_Integer_Size then
Lib_RE := RE_W_SI;
elsif P_Size <= Standard_Integer_Size then
Lib_RE := RE_W_I;
elsif P_Size <= Standard_Long_Integer_Size then
Lib_RE := RE_W_LI;
else
Lib_RE := RE_W_LLI;
end if;
elsif Is_Modular_Integer_Type (U_Type)
or else Is_Fixed_Point_Type (U_Type)
or else Is_Enumeration_Type (U_Type)
or else Is_Signed_Integer_Type (U_Type)
then
if P_Size <= Standard_Short_Short_Integer_Size then
Lib_RE := RE_W_SSU;
elsif P_Size <= Standard_Short_Integer_Size then
Lib_RE := RE_W_SU;
elsif P_Size <= Standard_Integer_Size then
Lib_RE := RE_W_U;
elsif P_Size <= Standard_Long_Integer_Size then
Lib_RE := RE_W_LU;
else
Lib_RE := RE_W_LLU;
end if;
else pragma Assert (Is_Access_Type (U_Type));
if P_Size > System_Address_Size then
Lib_RE := RE_W_AD;
else
Lib_RE := RE_W_AS;
end if;
end if;
Libent := RTE (Lib_RE);
return
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Libent, Loc),
Parameter_Associations => New_List (
Relocate_Node (Strm),
Unchecked_Convert_To (Etype (Next_Formal (First_Formal (Libent))),
Relocate_Node (Item))));
end Build_Elementary_Write_Call;
procedure Build_Mutable_Record_Read_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Out_Formal : Node_Id;
Dcls : constant List_Id := New_List;
Stms : List_Id := New_List;
Disc : Entity_Id;
Tmp_For_Disc : Entity_Id;
Tmps_For_Discs : constant List_Id := New_List;
Cstr : constant List_Id := New_List;
Discriminant_Checks : constant List_Id := New_List;
Tmp : constant Entity_Id := Make_Defining_Identifier (Loc, Name_V);
Constrained_Stms : List_Id := New_List;
begin
Disc := First_Discriminant (Typ);
Pnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Read));
Out_Formal :=
Make_Selected_Component (Loc,
Prefix => New_Occurrence_Of (Pnam, Loc),
Selector_Name => Make_Identifier (Loc, Name_V));
while Present (Disc) loop
Tmp_For_Disc := Make_Defining_Identifier (Loc,
New_External_Name (Chars (Disc), "D"));
Append_To (Tmps_For_Discs,
Make_Object_Declaration (Loc,
Defining_Identifier => Tmp_For_Disc,
Object_Definition => New_Occurrence_Of (Etype (Disc), Loc)));
Set_No_Initialization (Last (Tmps_For_Discs));
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Etype (Disc), Loc),
Attribute_Name => Name_Read,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
New_Occurrence_Of (Tmp_For_Disc, Loc))));
Append_To (Cstr,
Make_Discriminant_Association (Loc,
Selector_Names => New_List (New_Occurrence_Of (Disc, Loc)),
Expression => New_Occurrence_Of (Tmp_For_Disc, Loc)));
Append_To (Discriminant_Checks,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd => New_Occurrence_Of (Tmp_For_Disc, Loc),
Right_Opnd =>
Make_Selected_Component (Loc,
Prefix => New_Copy_Tree (Out_Formal),
Selector_Name => New_Occurrence_Of (Disc, Loc))),
Reason => CE_Discriminant_Check_Failed));
Next_Discriminant (Disc);
end loop;
Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Read);
Append_To (Dcls,
Make_Object_Declaration (Loc,
Defining_Identifier => Tmp,
Object_Definition =>
Make_Subtype_Indication (Loc,
Subtype_Mark => New_Occurrence_Of (Typ, Loc),
Constraint =>
Make_Index_Or_Discriminant_Constraint (Loc,
Constraints => Cstr))));
Constrained_Stms := Statements (Handled_Statement_Sequence (Decl));
Append_To (Stms,
Make_Block_Statement (Loc,
Declarations => Dcls,
Handled_Statement_Sequence => Parent (Constrained_Stms)));
Append_To (Constrained_Stms,
Make_Implicit_If_Statement (Pnam,
Condition =>
Make_Attribute_Reference (Loc,
Prefix => New_Copy_Tree (Out_Formal),
Attribute_Name => Name_Constrained),
Then_Statements => Discriminant_Checks));
Append_To (Constrained_Stms,
Make_Assignment_Statement (Loc,
Name => Out_Formal,
Expression => Make_Identifier (Loc, Name_V)));
if Is_Unchecked_Union (Typ) then
Stms :=
New_List (
Make_Raise_Program_Error (Loc,
Reason => PE_Unchecked_Union_Restriction));
end if;
Set_Declarations (Decl, Tmps_For_Discs);
Set_Handled_Statement_Sequence (Decl,
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stms));
end Build_Mutable_Record_Read_Procedure;
procedure Build_Mutable_Record_Write_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Stms : List_Id;
Disc : Entity_Id;
begin
Stms := New_List;
Disc := First_Discriminant (Typ);
while Present (Disc) loop
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Etype (Disc), Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name => New_Occurrence_Of (Disc, Loc)))));
Next_Discriminant (Disc);
end loop;
Pnam :=
Make_Defining_Identifier (Loc,
Chars => Make_TSS_Name_Local (Typ, TSS_Stream_Write));
Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Write);
if Is_Unchecked_Union (Typ) then
Stms :=
New_List (
Make_Raise_Program_Error (Loc,
Reason => PE_Unchecked_Union_Restriction));
end if;
if Is_Non_Empty_List (
Statements (Handled_Statement_Sequence (Decl)))
then
Insert_List_Before
(First (Statements (Handled_Statement_Sequence (Decl))), Stms);
else
Set_Statements (Handled_Statement_Sequence (Decl), Stms);
end if;
end Build_Mutable_Record_Write_Procedure;
procedure Build_Record_Or_Elementary_Input_Function
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Fnam : out Entity_Id)
is
Cn : Name_Id;
J : Pos;
Decls : List_Id;
Constr : List_Id;
Stms : List_Id;
Discr : Entity_Id;
Odef : Node_Id;
begin
Decls := New_List;
Constr := New_List;
J := 1;
if Has_Discriminants (Typ) then
Discr := First_Discriminant (Typ);
while Present (Discr) loop
Cn := New_External_Name ('C', J);
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Cn),
Object_Definition =>
New_Occurrence_Of (Etype (Discr), Loc)));
Append_To (Decls,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Etype (Discr), Loc),
Attribute_Name => Name_Read,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Identifier (Loc, Cn))));
Append_To (Constr, Make_Identifier (Loc, Cn));
Next_Discriminant (Discr);
J := J + 1;
end loop;
Odef :=
Make_Subtype_Indication (Loc,
Subtype_Mark => New_Occurrence_Of (Typ, Loc),
Constraint =>
Make_Index_Or_Discriminant_Constraint (Loc,
Constraints => Constr));
else
Odef := New_Occurrence_Of (Typ, Loc);
end if;
Append_To (Decls,
Make_Object_Declaration (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Object_Definition => Odef));
Stms := New_List (
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Read,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Identifier (Loc, Name_V))),
Make_Return_Statement (Loc,
Expression => Make_Identifier (Loc, Name_V)));
Fnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Input);
Build_Stream_Function (Loc, Typ, Decl, Fnam, Decls, Stms);
end Build_Record_Or_Elementary_Input_Function;
procedure Build_Record_Or_Elementary_Output_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
Stms : List_Id;
Disc : Entity_Id;
begin
Stms := New_List;
if Has_Discriminants (Typ) then
Disc := First_Discriminant (Typ);
while Present (Disc) loop
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Stream_Base_Type (Etype (Disc)), Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name => New_Occurrence_Of (Disc, Loc)))));
Next_Discriminant (Disc);
end loop;
end if;
Append_To (Stms,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Identifier (Loc, Name_V))));
Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Output);
Build_Stream_Procedure (Loc, Typ, Decl, Pnam, Stms, False);
end Build_Record_Or_Elementary_Output_Procedure;
procedure Build_Record_Read_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
begin
Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Read);
Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Read);
end Build_Record_Read_Procedure;
procedure Build_Record_Read_Write_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : Entity_Id;
Nam : Name_Id)
is
Rdef : Node_Id;
Stms : List_Id;
Typt : Entity_Id;
In_Limited_Extension : Boolean := False;
function Make_Component_List_Attributes (CL : Node_Id) return List_Id;
function Make_Field_Attribute (C : Entity_Id) return Node_Id;
function Make_Field_Attributes (Clist : List_Id) return List_Id;
function Make_Component_List_Attributes (CL : Node_Id) return List_Id is
CI : constant List_Id := Component_Items (CL);
VP : constant Node_Id := Variant_Part (CL);
Result : List_Id;
Alts : List_Id;
V : Node_Id;
DC : Node_Id;
DCH : List_Id;
begin
Result := Make_Field_Attributes (CI);
if Present (VP) then
if Is_Unchecked_Union (Scope (Entity (Name (VP)))) then
return New_List (
Make_Raise_Program_Error (Sloc (VP),
Reason => PE_Unchecked_Union_Restriction));
end if;
V := First_Non_Pragma (Variants (VP));
Alts := New_List;
while Present (V) loop
DCH := New_List;
DC := First (Discrete_Choices (V));
while Present (DC) loop
Append_To (DCH, New_Copy_Tree (DC));
Next (DC);
end loop;
Append_To (Alts,
Make_Case_Statement_Alternative (Loc,
Discrete_Choices => DCH,
Statements =>
Make_Component_List_Attributes (Component_List (V))));
Next_Non_Pragma (V);
end loop;
Append_To (Result,
Make_Case_Statement (Loc,
Expression =>
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name =>
New_Occurrence_Of (Entity (Name (VP)), Loc)),
Alternatives => Alts));
end if;
return Result;
end Make_Component_List_Attributes;
function Make_Field_Attribute (C : Entity_Id) return Node_Id is
Field_Typ : constant Entity_Id := Stream_Base_Type (Etype (C));
TSS_Names : constant array (Name_Input .. Name_Write) of
TSS_Name_Type :=
(Name_Read => TSS_Stream_Read,
Name_Write => TSS_Stream_Write,
Name_Input => TSS_Stream_Input,
Name_Output => TSS_Stream_Output,
others => TSS_Null);
pragma Assert (TSS_Names (Nam) /= TSS_Null);
begin
if In_Limited_Extension
and then Is_Limited_Type (Field_Typ)
and then No (Find_Inherited_TSS (Field_Typ, TSS_Names (Nam)))
then
return Make_Null_Statement (Loc);
end if;
return
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Field_Typ, Loc),
Attribute_Name => Nam,
Expressions => New_List (
Make_Identifier (Loc, Name_S),
Make_Selected_Component (Loc,
Prefix => Make_Identifier (Loc, Name_V),
Selector_Name => New_Occurrence_Of (C, Loc))));
end Make_Field_Attribute;
function Make_Field_Attributes (Clist : List_Id) return List_Id is
Item : Node_Id;
Result : List_Id;
begin
Result := New_List;
if Present (Clist) then
Item := First (Clist);
while Present (Item) loop
if Nkind (Item) = N_Component_Declaration
and then
(Chars (Defining_Identifier (Item)) = Name_uParent
or else
not Is_Internal_Name (Chars (Defining_Identifier (Item))))
then
Append_To
(Result,
Make_Field_Attribute (Defining_Identifier (Item)));
end if;
Next (Item);
end loop;
end if;
return Result;
end Make_Field_Attributes;
begin
if Is_Protected_Type (Typ) then
Typt := Corresponding_Record_Type (Typ);
else
Typt := Typ;
end if;
Rdef := Type_Definition
(Declaration_Node (Base_Type (Underlying_Type (Typt))));
Stms := Empty_List;
if Nkind (Rdef) = N_Derived_Type_Definition then
Rdef := Record_Extension_Part (Rdef);
if Is_Limited_Type (Typt) then
In_Limited_Extension := True;
end if;
end if;
if Present (Component_List (Rdef)) then
Append_List_To (Stms,
Make_Component_List_Attributes (Component_List (Rdef)));
end if;
Build_Stream_Procedure
(Loc, Typ, Decl, Pnam, Stms, Nam = Name_Read);
end Build_Record_Read_Write_Procedure;
procedure Build_Record_Write_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : out Entity_Id)
is
begin
Pnam := Make_Stream_Subprogram_Name (Loc, Typ, TSS_Stream_Write);
Build_Record_Read_Write_Procedure (Loc, Typ, Decl, Pnam, Name_Write);
end Build_Record_Write_Procedure;
function Build_Stream_Attr_Profile
(Loc : Source_Ptr;
Typ : Entity_Id;
Nam : TSS_Name_Type) return List_Id
is
Profile : List_Id;
begin
Profile := New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
Parameter_Type =>
Make_Access_Definition (Loc,
Null_Exclusion_Present => True,
Subtype_Mark => New_Reference_To (
Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))));
if Nam /= TSS_Stream_Input then
Append_To (Profile,
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Out_Present => (Nam = TSS_Stream_Read),
Parameter_Type => New_Reference_To (Typ, Loc)));
end if;
return Profile;
end Build_Stream_Attr_Profile;
procedure Build_Stream_Function
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Fnam : Entity_Id;
Decls : List_Id;
Stms : List_Id)
is
Spec : Node_Id;
begin
Spec :=
Make_Function_Specification (Loc,
Defining_Unit_Name => Fnam,
Parameter_Specifications => New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
Parameter_Type =>
Make_Access_Definition (Loc,
Null_Exclusion_Present => True,
Subtype_Mark => New_Reference_To (
Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc)))),
Result_Definition => New_Occurrence_Of (Typ, Loc));
Decl :=
Make_Subprogram_Body (Loc,
Specification => Spec,
Declarations => Decls,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stms));
end Build_Stream_Function;
procedure Build_Stream_Procedure
(Loc : Source_Ptr;
Typ : Entity_Id;
Decl : out Node_Id;
Pnam : Entity_Id;
Stms : List_Id;
Outp : Boolean)
is
Spec : Node_Id;
begin
Spec :=
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Pnam,
Parameter_Specifications => New_List (
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_S),
Parameter_Type =>
Make_Access_Definition (Loc,
Null_Exclusion_Present => True,
Subtype_Mark => New_Reference_To (
Class_Wide_Type (RTE (RE_Root_Stream_Type)), Loc))),
Make_Parameter_Specification (Loc,
Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
Out_Present => Outp,
Parameter_Type => New_Occurrence_Of (Typ, Loc))));
Decl :=
Make_Subprogram_Body (Loc,
Specification => Spec,
Declarations => Empty_List,
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => Stms));
end Build_Stream_Procedure;
function Has_Stream_Standard_Rep (U_Type : Entity_Id) return Boolean is
begin
if Has_Non_Standard_Rep (U_Type) then
return False;
else
return
Esize (First_Subtype (U_Type)) = Esize (Root_Type (U_Type));
end if;
end Has_Stream_Standard_Rep;
function Make_Stream_Subprogram_Name
(Loc : Source_Ptr;
Typ : Entity_Id;
Nam : TSS_Name_Type) return Entity_Id
is
Sname : Name_Id;
begin
if Is_Tagged_Type (Typ) then
Sname := Make_TSS_Name (Typ, Nam);
else
Sname := Make_TSS_Name_Local (Typ, Nam);
end if;
return Make_Defining_Identifier (Loc, Sname);
end Make_Stream_Subprogram_Name;
function Stream_Base_Type (E : Entity_Id) return Entity_Id is
begin
if Is_Array_Type (E)
and then Is_First_Subtype (E)
then
return E;
else
return Base_Type (E);
end if;
end Stream_Base_Type;
end Exp_Strm;