with Atree; use Atree;
with Casing; use Casing;
with Errout; use Errout;
with Fname; use Fname;
with Fname.UF; use Fname.UF;
with Lib; use Lib;
with Namet; use Namet;
with Opt; use Opt;
with Sinfo; use Sinfo;
with Sinput; use Sinput;
with Snames; use Snames;
with Uname; use Uname;
package body Restrict is
Restricted_Profile_Result : Boolean := False;
Restricted_Profile_Cached : Boolean := False;
procedure Restriction_Msg (Msg : String; R : String; N : Node_Id);
function Same_Unit (U1, U2 : Node_Id) return Boolean;
function Suppress_Restriction_Message (N : Node_Id) return Boolean;
function Abort_Allowed return Boolean is
begin
if Restrictions.Set (No_Abort_Statements)
and then Restrictions.Set (Max_Asynchronous_Select_Nesting)
and then Restrictions.Value (Max_Asynchronous_Select_Nesting) = 0
then
return False;
else
return True;
end if;
end Abort_Allowed;
procedure Check_Elaboration_Code_Allowed (N : Node_Id) is
begin
if Restrictions.Set (No_Elaboration_Code)
and then not Suppress_Restriction_Message (N)
then
Namet.Unlock;
Check_Restriction (Restriction_Id'(No_Elaboration_Code), N);
Namet.Lock;
end if;
end Check_Elaboration_Code_Allowed;
procedure Check_No_Implicit_Heap_Alloc (N : Node_Id) is
begin
Check_Restriction (Restriction_Id'(No_Implicit_Heap_Allocations), N);
end Check_No_Implicit_Heap_Alloc;
procedure Check_Restricted_Unit (U : Unit_Name_Type; N : Node_Id) is
begin
if Suppress_Restriction_Message (N) then
return;
elsif Is_Spec_Name (U) then
declare
Fnam : constant File_Name_Type :=
Get_File_Name (U, Subunit => False);
begin
if not Is_Predefined_File_Name (Fnam) then
return;
else
Get_Name_String (Fnam);
Name_Len := Name_Len - 4;
while Name_Len < 8 loop
Name_Len := Name_Len + 1;
Name_Buffer (Name_Len) := ' ';
end loop;
for J in Unit_Array'Range loop
if Name_Len = 8
and then Name_Buffer (1 .. 8) = Unit_Array (J).Filenm
then
Check_Restriction (Unit_Array (J).Res_Id, N);
end if;
end loop;
end if;
end;
end if;
end Check_Restricted_Unit;
procedure Check_Restriction
(R : Restriction_Id;
N : Node_Id;
V : Uint := Uint_Minus_1)
is
Rimage : constant String := Restriction_Id'Image (R);
VV : Integer;
procedure Update_Restrictions (Info : in out Restrictions_Info);
procedure Update_Restrictions (Info : in out Restrictions_Info) is
begin
if not Info.Violated (R) then
Info.Violated (R) := True;
if R in All_Parameter_Restrictions then
if VV < 0 then
Info.Unknown (R) := True;
Info.Count (R) := 1;
else
Info.Count (R) := VV;
end if;
end if;
elsif R in All_Parameter_Restrictions then
if VV < 0 then
Info.Unknown (R) := True;
elsif R in Checked_Max_Parameter_Restrictions then
Info.Count (R) := Integer'Max (Info.Count (R), VV);
elsif R in Checked_Add_Parameter_Restrictions then
declare
pragma Unsuppress (Overflow_Check);
begin
Info.Count (R) := Info.Count (R) + VV;
exception
when Constraint_Error =>
Info.Count (R) := Integer'Last;
Info.Unknown (R) := True;
end;
else
raise Program_Error;
end if;
end if;
end Update_Restrictions;
begin
if UI_Is_In_Int_Range (V) then
VV := Integer (UI_To_Int (V));
else
VV := -1;
end if;
pragma Assert (VV < 0 or else R in Checked_Val_Parameter_Restrictions);
if VV = 0 then
return;
end if;
Update_Restrictions (Restrictions);
if Current_Sem_Unit = Main_Unit
or else In_Extended_Main_Source_Unit (N)
then
Update_Restrictions (Main_Restrictions);
end if;
if Suppress_Restriction_Message (N) then
null;
elsif not Restrictions.Set (R) then
null;
elsif R in All_Boolean_Restrictions
or else (Restrictions.Unknown (R)
and then Restrictions.Value (R) = 0)
or else Restrictions.Count (R) > Restrictions.Value (R)
then
Error_Msg_Sloc := Restrictions_Loc (R);
if Error_Msg_Sloc > No_Location
or else Error_Msg_Sloc = System_Location
then
if Restriction_Warnings (R) then
Restriction_Msg ("|violation of restriction %#?", Rimage, N);
else
Restriction_Msg ("|violation of restriction %#", Rimage, N);
end if;
else
Restriction_Msg
("|violation of implicit restriction %", Rimage, N);
end if;
end if;
end Check_Restriction;
procedure Check_Restriction_No_Dependence (U : Node_Id; Err : Node_Id) is
DU : Node_Id;
begin
for J in No_Dependence.First .. No_Dependence.Last loop
DU := No_Dependence.Table (J).Unit;
if Same_Unit (U, DU) then
Error_Msg_Sloc := Sloc (DU);
Error_Msg_Node_1 := DU;
if No_Dependence.Table (J).Warn then
Error_Msg
("?violation of restriction `No_Dependence '='> &`#",
Sloc (Err));
else
Error_Msg
("|violation of restriction `No_Dependence '='> &`#",
Sloc (Err));
end if;
return;
end if;
end loop;
end Check_Restriction_No_Dependence;
procedure Cunit_Boolean_Restrictions_Restore
(R : Save_Cunit_Boolean_Restrictions)
is
begin
for J in Cunit_Boolean_Restrictions loop
Restrictions.Set (J) := R (J);
end loop;
end Cunit_Boolean_Restrictions_Restore;
function Cunit_Boolean_Restrictions_Save
return Save_Cunit_Boolean_Restrictions
is
R : Save_Cunit_Boolean_Restrictions;
begin
for J in Cunit_Boolean_Restrictions loop
R (J) := Restrictions.Set (J);
Restrictions.Set (J) := False;
end loop;
return R;
end Cunit_Boolean_Restrictions_Save;
function Get_Restriction_Id
(N : Name_Id) return Restriction_Id
is
begin
Get_Name_String (N);
Set_Casing (All_Upper_Case);
for J in All_Restrictions loop
declare
S : constant String := Restriction_Id'Image (J);
begin
if S = Name_Buffer (1 .. Name_Len) then
return J;
end if;
end;
end loop;
return Not_A_Restriction_Id;
end Get_Restriction_Id;
function No_Exception_Handlers_Set return Boolean is
begin
return Restrictions.Set (No_Exception_Handlers);
end No_Exception_Handlers_Set;
function Process_Restriction_Synonyms (N : Node_Id) return Name_Id
is
Old_Name : constant Name_Id := Chars (N);
New_Name : Name_Id;
begin
case Old_Name is
when Name_Boolean_Entry_Barriers =>
New_Name := Name_Simple_Barriers;
when Name_Max_Entry_Queue_Depth =>
New_Name := Name_Max_Entry_Queue_Length;
when Name_No_Dynamic_Interrupts =>
New_Name := Name_No_Dynamic_Attachment;
when Name_No_Requeue =>
New_Name := Name_No_Requeue_Statements;
when Name_No_Task_Attributes =>
New_Name := Name_No_Task_Attributes_Package;
when others =>
return Old_Name;
end case;
if Warn_On_Obsolescent_Feature then
Error_Msg_Name_1 := Old_Name;
Error_Msg_N ("restriction identifier % is obsolescent?", N);
Error_Msg_Name_1 := New_Name;
Error_Msg_N ("|use restriction identifier % instead", N);
end if;
return New_Name;
end Process_Restriction_Synonyms;
function Restricted_Profile return Boolean is
begin
if Restricted_Profile_Cached then
return Restricted_Profile_Result;
else
Restricted_Profile_Result := True;
Restricted_Profile_Cached := True;
declare
R : Restriction_Flags renames Profile_Info (Restricted).Set;
V : Restriction_Values renames Profile_Info (Restricted).Value;
begin
for J in R'Range loop
if R (J)
and then (Restrictions.Set (J) = False
or else Restriction_Warnings (J)
or else
(J in All_Parameter_Restrictions
and then Restrictions.Value (J) > V (J)))
then
Restricted_Profile_Result := False;
exit;
end if;
end loop;
return Restricted_Profile_Result;
end;
end if;
end Restricted_Profile;
function Restriction_Active (R : All_Restrictions) return Boolean is
begin
return Restrictions.Set (R);
end Restriction_Active;
procedure Restriction_Msg (Msg : String; R : String; N : Node_Id) is
B : String (1 .. Msg'Length + 2 * R'Length + 1);
P : Natural := 1;
begin
Name_Buffer (1 .. R'Last) := R;
Name_Len := R'Length;
Set_Casing (Identifier_Casing (Get_Source_File_Index (Sloc (N))));
P := 0;
for J in Msg'Range loop
if Msg (J) = '%' then
P := P + 1;
B (P) := '`';
for J in 1 .. Name_Len loop
if Name_Buffer (J) in 'A' .. 'Z' then
P := P + 1;
B (P) := ''';
end if;
P := P + 1;
B (P) := Name_Buffer (J);
end loop;
P := P + 1;
B (P) := '`';
else
P := P + 1;
B (P) := Msg (J);
end if;
end loop;
Error_Msg_N (B (1 .. P), N);
end Restriction_Msg;
function Same_Unit (U1, U2 : Node_Id) return Boolean is
begin
if Nkind (U1) = N_Identifier then
return Nkind (U2) = N_Identifier and then Chars (U1) = Chars (U2);
elsif Nkind (U2) = N_Identifier then
return False;
elsif (Nkind (U1) = N_Selected_Component
or else Nkind (U1) = N_Expanded_Name)
and then
(Nkind (U2) = N_Selected_Component
or else Nkind (U2) = N_Expanded_Name)
then
return Same_Unit (Prefix (U1), Prefix (U2))
and then Same_Unit (Selector_Name (U1), Selector_Name (U2));
else
return False;
end if;
end Same_Unit;
procedure Set_Profile_Restrictions
(P : Profile_Name;
N : Node_Id;
Warn : Boolean)
is
R : Restriction_Flags renames Profile_Info (P).Set;
V : Restriction_Values renames Profile_Info (P).Value;
begin
for J in R'Range loop
if R (J) then
if J in All_Boolean_Restrictions then
Set_Restriction (J, N);
else
Set_Restriction (J, N, V (J));
end if;
Restriction_Warnings (J) := Warn;
end if;
end loop;
end Set_Profile_Restrictions;
procedure Set_Restriction
(R : All_Boolean_Restrictions;
N : Node_Id)
is
begin
Restrictions.Set (R) := True;
if Restricted_Profile_Cached and Restricted_Profile_Result then
null;
else
Restricted_Profile_Cached := False;
end if;
if Restrictions_Loc (R) /= System_Location then
Restrictions_Loc (R) := Sloc (N);
end if;
if Current_Sem_Unit = Main_Unit
or else In_Extended_Main_Source_Unit (N)
then
if not Restriction_Warnings (R) then
Main_Restrictions.Set (R) := True;
end if;
end if;
end Set_Restriction;
procedure Set_Restriction
(R : All_Parameter_Restrictions;
N : Node_Id;
V : Integer)
is
begin
if Restricted_Profile_Cached and Restricted_Profile_Result then
null;
else
Restricted_Profile_Cached := False;
end if;
if Restrictions.Set (R) then
if V < Restrictions.Value (R) then
Restrictions.Value (R) := V;
Restrictions_Loc (R) := Sloc (N);
end if;
else
Restrictions.Set (R) := True;
Restrictions.Value (R) := V;
Restrictions_Loc (R) := Sloc (N);
end if;
if Current_Sem_Unit = Main_Unit
or else In_Extended_Main_Source_Unit (N)
then
if Main_Restrictions.Set (R) then
if V < Main_Restrictions.Value (R) then
Main_Restrictions.Value (R) := V;
end if;
elsif not Restriction_Warnings (R) then
Main_Restrictions.Set (R) := True;
Main_Restrictions.Value (R) := V;
end if;
end if;
end Set_Restriction;
procedure Set_Restriction_No_Dependence
(Unit : Node_Id;
Warn : Boolean)
is
begin
for J in No_Dependence.First .. No_Dependence.Last loop
if Same_Unit (Unit, No_Dependence.Table (J).Unit) then
if not Warn then
No_Dependence.Table (J).Warn := False;
end if;
return;
end if;
end loop;
No_Dependence.Append ((Unit, Warn));
end Set_Restriction_No_Dependence;
function Suppress_Restriction_Message (N : Node_Id) return Boolean is
begin
if In_Extended_Main_Source_Unit (N) then
return False;
elsif Sloc (N) <= No_Location then
return True;
else
return Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)));
end if;
end Suppress_Restriction_Message;
function Tasking_Allowed return Boolean is
begin
return not Restrictions.Set (No_Tasking)
and then (not Restrictions.Set (Max_Tasks)
or else Restrictions.Value (Max_Tasks) > 0);
end Tasking_Allowed;
end Restrict;