with Atree; use Atree;
with Einfo; use Einfo;
with Exp_Util; use Exp_Util;
with Nmake; use Nmake;
with Namet; use Namet;
with Nlists; use Nlists;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Util; use Sem_Util;
with Sinfo; use Sinfo;
with Snames; use Snames;
with Stand; use Stand;
with Stringt; use Stringt;
with Tbuild; use Tbuild;
package body Exp_Smem is
Insert_Node : Node_Id;
procedure Add_Read_Before (N : Node_Id);
procedure Add_Write_After (N : Node_Id);
procedure Build_Full_Name
(E : in Entity_Id;
N : out String_Id);
function On_Lhs_Of_Assignment (N : Node_Id) return Boolean;
function Is_Out_Actual (N : Node_Id) return Boolean;
procedure Add_Read_Before (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ent : constant Node_Id := Entity (N);
begin
if Present (Shared_Var_Read_Proc (Ent)) then
Insert_Action (N,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (Shared_Var_Read_Proc (Ent), Loc),
Parameter_Associations => Empty_List));
end if;
end Add_Read_Before;
procedure Add_Shared_Var_Lock_Procs (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Obj : constant Entity_Id := Entity (Expression (First_Actual (N)));
Inode : Node_Id;
Vnm : String_Id;
begin
Inode := N;
while Nkind (Inode) /= N_Procedure_Call_Statement
and then Nkind (Inode) in N_Subexpr
loop
Inode := Parent (Inode);
end loop;
Build_Full_Name (Obj, Vnm);
Insert_Before_And_Analyze (Inode,
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (RTE (RE_Shared_Var_Lock), Loc),
Parameter_Associations => New_List (
Make_String_Literal (Loc, Vnm))));
Insert_Before_And_Analyze (Inode,
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Shared_Var_Read_Proc (Obj), Loc)));
Insert_After_And_Analyze (Inode,
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (RTE (RE_Shared_Var_Unlock), Loc),
Parameter_Associations => New_List (
Make_String_Literal (Loc, Vnm))));
if Nkind (N) = N_Procedure_Call_Statement then
Insert_After_And_Analyze (Inode,
Make_Procedure_Call_Statement (Loc,
Name => New_Occurrence_Of (Shared_Var_Assign_Proc (Obj), Loc)));
end if;
end Add_Shared_Var_Lock_Procs;
procedure Add_Write_After (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ent : constant Node_Id := Entity (N);
begin
if Present (Shared_Var_Assign_Proc (Ent)) then
Insert_After_And_Analyze (Insert_Node,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (Shared_Var_Assign_Proc (Ent), Loc),
Parameter_Associations => Empty_List));
end if;
end Add_Write_After;
procedure Build_Full_Name
(E : in Entity_Id;
N : out String_Id)
is
procedure Build_Name (E : Entity_Id);
procedure Build_Name (E : Entity_Id) is
begin
if Scope (E) /= Standard_Standard then
Build_Name (Scope (E));
Store_String_Char ('.');
end if;
Get_Decoded_Name_String (Chars (E));
Store_String_Chars (Name_Buffer (1 .. Name_Len));
end Build_Name;
begin
Start_String;
Build_Name (E);
N := End_String;
end Build_Full_Name;
procedure Expand_Shared_Passive_Variable (N : Node_Id) is
Typ : constant Entity_Id := Etype (N);
begin
if Is_Limited_Type (Typ) or else Is_Concurrent_Type (Typ) then
return;
elsif On_Lhs_Of_Assignment (N) then
Add_Write_After (N);
elsif Is_Out_Actual (N) then
Add_Read_Before (N);
Add_Write_After (N);
else
Add_Read_Before (N);
end if;
end Expand_Shared_Passive_Variable;
function Is_Out_Actual (N : Node_Id) return Boolean is
Parnt : constant Node_Id := Parent (N);
Formal : Entity_Id;
Call : Node_Id;
Actual : Node_Id;
begin
if (Nkind (Parnt) = N_Indexed_Component
or else
Nkind (Parnt) = N_Selected_Component)
and then N = Prefix (Parnt)
then
return Is_Out_Actual (Parnt);
elsif Nkind (Parnt) = N_Parameter_Association
and then N = Explicit_Actual_Parameter (Parnt)
then
Call := Parent (Parnt);
elsif Nkind (Parnt) = N_Procedure_Call_Statement then
Call := Parnt;
else
return False;
end if;
Actual := First_Actual (Call);
Formal := First_Formal (Entity (Name (Call)));
loop
if Actual = N then
if Ekind (Formal) /= E_In_Parameter then
Insert_Node := Call;
return True;
else
return False;
end if;
else
Actual := Next_Actual (Actual);
Formal := Next_Formal (Formal);
end if;
end loop;
end Is_Out_Actual;
procedure Make_Shared_Var_Procs (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ent : constant Entity_Id := Defining_Identifier (N);
Typ : constant Entity_Id := Etype (Ent);
Vnm : String_Id;
Atr : Node_Id;
Assign_Proc : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name (Chars (Ent), 'A'));
Read_Proc : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_External_Name (Chars (Ent), 'R'));
S : Entity_Id;
begin
Build_Full_Name (Ent, Vnm);
Set_Is_Shared_Passive (Ent, False);
S := Make_Defining_Identifier (Loc, Name_uS);
Atr :=
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Write,
Expressions => New_List (
New_Reference_To (S, Loc),
New_Occurrence_Of (Ent, Loc)));
Set_OK_For_Stream (Atr, True);
Insert_After_And_Analyze (N,
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Assign_Proc),
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => S,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (
Make_Assignment_Statement (Loc,
Name => New_Reference_To (S, Loc),
Expression =>
Make_Function_Call (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Shared_Var_WOpen), Loc),
Parameter_Associations => New_List (
Make_String_Literal (Loc, Vnm)))),
Atr,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of (RTE (RE_Shared_Var_Close), Loc),
Parameter_Associations =>
New_List (New_Reference_To (S, Loc)))))));
S := Make_Defining_Identifier (Loc, Name_uS);
Atr :=
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Read,
Expressions => New_List (
New_Reference_To (S, Loc),
New_Occurrence_Of (Ent, Loc)));
Set_OK_For_Stream (Atr, True);
Insert_After_And_Analyze (N,
Make_Subprogram_Body (Loc,
Specification =>
Make_Procedure_Specification (Loc,
Defining_Unit_Name => Read_Proc),
Declarations => New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => S,
Object_Definition =>
New_Occurrence_Of (RTE (RE_Stream_Access), Loc))),
Handled_Statement_Sequence =>
Make_Handled_Sequence_Of_Statements (Loc,
Statements => New_List (
Make_Assignment_Statement (Loc,
Name => New_Reference_To (S, Loc),
Expression =>
Make_Function_Call (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Shared_Var_ROpen), Loc),
Parameter_Associations => New_List (
Make_String_Literal (Loc, Vnm)))),
Make_Implicit_If_Statement (N,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd => New_Reference_To (S, Loc),
Right_Opnd => Make_Null (Loc)),
Then_Statements => New_List (
Atr,
Make_Procedure_Call_Statement (Loc,
Name =>
New_Occurrence_Of
(RTE (RE_Shared_Var_Close), Loc),
Parameter_Associations =>
New_List (New_Reference_To (S, Loc)))))))));
Set_Is_Shared_Passive (Ent, True);
Set_Shared_Var_Assign_Proc (Ent, Assign_Proc);
Set_Shared_Var_Read_Proc (Ent, Read_Proc);
end Make_Shared_Var_Procs;
function On_Lhs_Of_Assignment (N : Node_Id) return Boolean is
P : constant Node_Id := Parent (N);
begin
if Nkind (P) = N_Assignment_Statement then
if N = Name (P) then
Insert_Node := P;
return True;
else
return False;
end if;
elsif (Nkind (P) = N_Indexed_Component
or else
Nkind (P) = N_Selected_Component)
and then N = Prefix (P)
then
return On_Lhs_Of_Assignment (P);
else
return False;
end if;
end On_Lhs_Of_Assignment;
end Exp_Smem;