1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Errout; use Errout;
30 with Fname; use Fname;
31 with Fname.UF; use Fname.UF;
34 with Sinfo; use Sinfo;
35 with Sinput; use Sinput;
36 with Snames; use Snames;
37 with Uname; use Uname;
39 package body Restrict is
41 Restricted_Profile_Result : Boolean := False;
42 -- This switch memoizes the result of Restricted_Profile function
43 -- calls for improved efficiency. Its setting is valid only if
44 -- Restricted_Profile_Cached is True. Note that if this switch
45 -- is ever set True, it need never be turned off again.
47 Restricted_Profile_Cached : Boolean := False;
48 -- This flag is set to True if the Restricted_Profile_Result
49 -- contains the correct cached result of Restricted_Profile calls.
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 procedure Restriction_Msg (Msg : String; R : String; N : Node_Id);
56 -- Output error message at node N with given text, replacing the
57 -- '%' in the message with the name of the restriction given as R,
58 -- cased according to the current identifier casing. We do not use
59 -- the normal insertion mechanism, since this requires an entry
60 -- in the Names table, and this table will be locked if we are
61 -- generating a message from gigi.
63 function Same_Unit (U1, U2 : Node_Id) return Boolean;
64 -- Returns True iff U1 and U2 represent the same library unit. Used for
65 -- handling of No_Dependence => Unit restriction case.
67 function Suppress_Restriction_Message (N : Node_Id) return Boolean;
68 -- N is the node for a possible restriction violation message, but
69 -- the message is to be suppressed if this is an internal file and
70 -- this file is not the main unit.
76 function Abort_Allowed return Boolean is
78 if Restrictions.Set (No_Abort_Statements)
79 and then Restrictions.Set (Max_Asynchronous_Select_Nesting)
80 and then Restrictions.Value (Max_Asynchronous_Select_Nesting) = 0
88 ------------------------------------
89 -- Check_Elaboration_Code_Allowed --
90 ------------------------------------
92 procedure Check_Elaboration_Code_Allowed (N : Node_Id) is
94 -- Avoid calling Namet.Unlock/Lock except when there is an error.
95 -- Even in the error case it is a bit dubious, either gigi needs
96 -- the table locked or it does not! ???
98 if Restrictions.Set (No_Elaboration_Code)
99 and then not Suppress_Restriction_Message (N)
102 Check_Restriction (Restriction_Id'(No_Elaboration_Code), N);
105 end Check_Elaboration_Code_Allowed;
107 ----------------------------------
108 -- Check_No_Implicit_Heap_Alloc --
109 ----------------------------------
111 procedure Check_No_Implicit_Heap_Alloc (N : Node_Id) is
113 Check_Restriction (Restriction_Id'(No_Implicit_Heap_Allocations), N);
114 end Check_No_Implicit_Heap_Alloc;
116 ---------------------------
117 -- Check_Restricted_Unit --
118 ---------------------------
120 procedure Check_Restricted_Unit (U : Unit_Name_Type; N : Node_Id) is
122 if Suppress_Restriction_Message (N) then
125 elsif Is_Spec_Name (U) then
127 Fnam : constant File_Name_Type :=
128 Get_File_Name (U, Subunit => False);
133 Get_Name_String (Fnam);
135 -- Nothing to do if name not at least 5 characters long ending
136 -- in .ads or .adb extension, which we strip.
139 or else (Name_Buffer (Name_Len - 3 .. Name_Len) /= ".ads"
141 Name_Buffer (Name_Len - 4 .. Name_Len) /= ".adb")
146 -- Strip extension and pad to eight characters
148 Name_Len := Name_Len - 4;
149 while Name_Len < 8 loop
150 Name_Len := Name_Len + 1;
151 Name_Buffer (Name_Len) := ' ';
154 -- If predefined unit, check the list of restricted units
156 if Is_Predefined_File_Name (Fnam) then
157 for J in Unit_Array'Range loop
159 and then Name_Buffer (1 .. 8) = Unit_Array (J).Filenm
161 Check_Restriction (Unit_Array (J).Res_Id, N);
165 -- If not predefied unit, then one special check still remains.
166 -- GNAT.Current_Exception is not allowed if we have restriction
167 -- No_Exception_Propagation active.
170 if Name_Buffer (1 .. 8) = "g-curexc" then
171 Check_Restriction (No_Exception_Propagation, N);
176 end Check_Restricted_Unit;
178 -----------------------
179 -- Check_Restriction --
180 -----------------------
182 procedure Check_Restriction
185 V : Uint := Uint_Minus_1)
187 Rimage : constant String := Restriction_Id'Image (R);
190 -- V converted to integer form. If V is greater than Integer'Last,
191 -- it is reset to minus 1 (unknown value).
193 procedure Update_Restrictions (Info : in out Restrictions_Info);
194 -- Update violation information in Info.Violated and Info.Count
196 -------------------------
197 -- Update_Restrictions --
198 -------------------------
200 procedure Update_Restrictions (Info : in out Restrictions_Info) is
202 -- If not violated, set as violated now
204 if not Info.Violated (R) then
205 Info.Violated (R) := True;
207 if R in All_Parameter_Restrictions then
209 Info.Unknown (R) := True;
212 Info.Count (R) := VV;
216 -- Otherwise if violated already and a parameter restriction,
217 -- update count by maximizing or summing depending on restriction.
219 elsif R in All_Parameter_Restrictions then
221 -- If new value is unknown, result is unknown
224 Info.Unknown (R) := True;
226 -- If checked by maximization, do maximization
228 elsif R in Checked_Max_Parameter_Restrictions then
229 Info.Count (R) := Integer'Max (Info.Count (R), VV);
231 -- If checked by adding, do add, checking for overflow
233 elsif R in Checked_Add_Parameter_Restrictions then
235 pragma Unsuppress (Overflow_Check);
237 Info.Count (R) := Info.Count (R) + VV;
239 when Constraint_Error =>
240 Info.Count (R) := Integer'Last;
241 Info.Unknown (R) := True;
244 -- Should not be able to come here, known counts should only
245 -- occur for restrictions that are Checked_max or Checked_Sum.
251 end Update_Restrictions;
253 -- Start of processing for Check_Restriction
256 if UI_Is_In_Int_Range (V) then
257 VV := Integer (UI_To_Int (V));
262 -- Count can only be specified in the checked val parameter case
264 pragma Assert (VV < 0 or else R in Checked_Val_Parameter_Restrictions);
266 -- Nothing to do if value of zero specified for parameter restriction
272 -- Update current restrictions
274 Update_Restrictions (Restrictions);
276 -- If in main extended unit, update main restrictions as well
278 if Current_Sem_Unit = Main_Unit
279 or else In_Extended_Main_Source_Unit (N)
281 Update_Restrictions (Main_Restrictions);
284 -- Nothing to do if restriction message suppressed
286 if Suppress_Restriction_Message (N) then
289 -- If restriction not set, nothing to do
291 elsif not Restrictions.Set (R) then
294 -- Here if restriction set, check for violation (either this is a
295 -- Boolean restriction, or a parameter restriction with a value of
296 -- zero and an unknown count, or a parameter restriction with a
297 -- known value that exceeds the restriction count).
299 elsif R in All_Boolean_Restrictions
300 or else (Restrictions.Unknown (R)
301 and then Restrictions.Value (R) = 0)
302 or else Restrictions.Count (R) > Restrictions.Value (R)
304 Error_Msg_Sloc := Restrictions_Loc (R);
306 -- If we have a location for the Restrictions pragma, output it
308 if Error_Msg_Sloc > No_Location
309 or else Error_Msg_Sloc = System_Location
311 if Restriction_Warnings (R) then
312 Restriction_Msg ("|violation of restriction %#?", Rimage, N);
314 Restriction_Msg ("|violation of restriction %#", Rimage, N);
317 -- Otherwise we have the case of an implicit restriction
318 -- (e.g. a restriction implicitly set by another pragma)
322 ("|violation of implicit restriction %", Rimage, N);
325 end Check_Restriction;
327 -------------------------------------
328 -- Check_Restriction_No_Dependence --
329 -------------------------------------
331 procedure Check_Restriction_No_Dependence (U : Node_Id; Err : Node_Id) is
335 for J in No_Dependence.First .. No_Dependence.Last loop
336 DU := No_Dependence.Table (J).Unit;
338 if Same_Unit (U, DU) then
339 Error_Msg_Sloc := Sloc (DU);
340 Error_Msg_Node_1 := DU;
342 if No_Dependence.Table (J).Warn then
344 ("?violation of restriction `No_Dependence '='> &`#",
348 ("|violation of restriction `No_Dependence '='> &`#",
355 end Check_Restriction_No_Dependence;
357 ----------------------------------------
358 -- Cunit_Boolean_Restrictions_Restore --
359 ----------------------------------------
361 procedure Cunit_Boolean_Restrictions_Restore
362 (R : Save_Cunit_Boolean_Restrictions)
365 for J in Cunit_Boolean_Restrictions loop
366 Restrictions.Set (J) := R (J);
368 end Cunit_Boolean_Restrictions_Restore;
370 -------------------------------------
371 -- Cunit_Boolean_Restrictions_Save --
372 -------------------------------------
374 function Cunit_Boolean_Restrictions_Save
375 return Save_Cunit_Boolean_Restrictions
377 R : Save_Cunit_Boolean_Restrictions;
380 for J in Cunit_Boolean_Restrictions loop
381 R (J) := Restrictions.Set (J);
382 Restrictions.Set (J) := False;
386 end Cunit_Boolean_Restrictions_Save;
388 ------------------------
389 -- Get_Restriction_Id --
390 ------------------------
392 function Get_Restriction_Id
393 (N : Name_Id) return Restriction_Id
397 Set_Casing (All_Upper_Case);
399 for J in All_Restrictions loop
401 S : constant String := Restriction_Id'Image (J);
403 if S = Name_Buffer (1 .. Name_Len) then
409 return Not_A_Restriction_Id;
410 end Get_Restriction_Id;
412 -------------------------------
413 -- No_Exception_Handlers_Set --
414 -------------------------------
416 function No_Exception_Handlers_Set return Boolean is
418 return (No_Run_Time_Mode or else Configurable_Run_Time_Mode)
419 and then (Restrictions.Set (No_Exception_Handlers)
421 Restrictions.Set (No_Exception_Propagation));
422 end No_Exception_Handlers_Set;
424 ----------------------------------
425 -- Process_Restriction_Synonyms --
426 ----------------------------------
428 -- Note: body of this function must be coordinated with list of
429 -- renaming declarations in System.Rident.
431 function Process_Restriction_Synonyms (N : Node_Id) return Name_Id
433 Old_Name : constant Name_Id := Chars (N);
438 when Name_Boolean_Entry_Barriers =>
439 New_Name := Name_Simple_Barriers;
441 when Name_Max_Entry_Queue_Depth =>
442 New_Name := Name_Max_Entry_Queue_Length;
444 when Name_No_Dynamic_Interrupts =>
445 New_Name := Name_No_Dynamic_Attachment;
447 when Name_No_Requeue =>
448 New_Name := Name_No_Requeue_Statements;
450 when Name_No_Task_Attributes =>
451 New_Name := Name_No_Task_Attributes_Package;
457 if Warn_On_Obsolescent_Feature then
458 Error_Msg_Name_1 := Old_Name;
459 Error_Msg_N ("restriction identifier % is obsolescent?", N);
460 Error_Msg_Name_1 := New_Name;
461 Error_Msg_N ("|use restriction identifier % instead", N);
465 end Process_Restriction_Synonyms;
467 ------------------------
468 -- Restricted_Profile --
469 ------------------------
471 function Restricted_Profile return Boolean is
473 if Restricted_Profile_Cached then
474 return Restricted_Profile_Result;
477 Restricted_Profile_Result := True;
478 Restricted_Profile_Cached := True;
481 R : Restriction_Flags renames Profile_Info (Restricted).Set;
482 V : Restriction_Values renames Profile_Info (Restricted).Value;
484 for J in R'Range loop
486 and then (Restrictions.Set (J) = False
487 or else Restriction_Warnings (J)
489 (J in All_Parameter_Restrictions
490 and then Restrictions.Value (J) > V (J)))
492 Restricted_Profile_Result := False;
497 return Restricted_Profile_Result;
500 end Restricted_Profile;
502 ------------------------
503 -- Restriction_Active --
504 ------------------------
506 function Restriction_Active (R : All_Restrictions) return Boolean is
508 return Restrictions.Set (R) and then not Restriction_Warnings (R);
509 end Restriction_Active;
511 ---------------------
512 -- Restriction_Msg --
513 ---------------------
515 procedure Restriction_Msg (Msg : String; R : String; N : Node_Id) is
516 B : String (1 .. Msg'Length + 2 * R'Length + 1);
520 Name_Buffer (1 .. R'Last) := R;
521 Name_Len := R'Length;
522 Set_Casing (Identifier_Casing (Get_Source_File_Index (Sloc (N))));
525 for J in Msg'Range loop
526 if Msg (J) = '%' then
530 -- Put characters of image in message, quoting upper case letters
532 for J in 1 .. Name_Len loop
533 if Name_Buffer (J) in 'A' .. 'Z' then
539 B (P) := Name_Buffer (J);
551 Error_Msg_N (B (1 .. P), N);
558 function Same_Unit (U1, U2 : Node_Id) return Boolean is
560 if Nkind (U1) = N_Identifier then
561 return Nkind (U2) = N_Identifier and then Chars (U1) = Chars (U2);
563 elsif Nkind (U2) = N_Identifier then
566 elsif (Nkind (U1) = N_Selected_Component
567 or else Nkind (U1) = N_Expanded_Name)
569 (Nkind (U2) = N_Selected_Component
570 or else Nkind (U2) = N_Expanded_Name)
572 return Same_Unit (Prefix (U1), Prefix (U2))
573 and then Same_Unit (Selector_Name (U1), Selector_Name (U2));
579 ------------------------------
580 -- Set_Profile_Restrictions --
581 ------------------------------
583 procedure Set_Profile_Restrictions
588 R : Restriction_Flags renames Profile_Info (P).Set;
589 V : Restriction_Values renames Profile_Info (P).Value;
592 for J in R'Range loop
595 Already_Restricted : constant Boolean := Restriction_Active (J);
598 -- Set the restriction
600 if J in All_Boolean_Restrictions then
601 Set_Restriction (J, N);
603 Set_Restriction (J, N, V (J));
606 -- Set warning flag, except that we do not set the warning
607 -- flag if the restriction was already active and this is
608 -- the warning case. That avoids a warning overriding a real
609 -- restriction, which should never happen.
611 if not (Warn and Already_Restricted) then
612 Restriction_Warnings (J) := Warn;
617 end Set_Profile_Restrictions;
619 ---------------------
620 -- Set_Restriction --
621 ---------------------
623 -- Case of Boolean restriction
625 procedure Set_Restriction
626 (R : All_Boolean_Restrictions;
630 -- Restriction No_Elaboration_Code must be enforced on a unit by unit
631 -- basis. Hence, we avoid setting the restriction when processing an
632 -- unit which is not the main one being compiled (or its corresponding
633 -- spec). It can happen, for example, when processing an inlined body
634 -- (the package containing the inlined subprogram is analyzed,
635 -- including its pragma Restrictions).
637 -- This seems like a very nasty kludge??? This is not the only per unit
638 -- restriction why is this treated specially ???
640 if R = No_Elaboration_Code
641 and then Current_Sem_Unit /= Main_Unit
642 and then Cunit (Current_Sem_Unit) /= Library_Unit (Cunit (Main_Unit))
647 Restrictions.Set (R) := True;
649 if Restricted_Profile_Cached and Restricted_Profile_Result then
652 Restricted_Profile_Cached := False;
655 -- Set location, but preserve location of system
656 -- restriction for nice error msg with run time name
658 if Restrictions_Loc (R) /= System_Location then
659 Restrictions_Loc (R) := Sloc (N);
662 -- Record the restriction if we are in the main unit, or in the extended
663 -- main unit. The reason that we test separately for Main_Unit is that
664 -- gnat.adc is processed with Current_Sem_Unit = Main_Unit, but nodes in
665 -- gnat.adc do not appear to be in the extended main source unit (they
666 -- probably should do ???)
668 if Current_Sem_Unit = Main_Unit
669 or else In_Extended_Main_Source_Unit (N)
671 if not Restriction_Warnings (R) then
672 Main_Restrictions.Set (R) := True;
677 -- Case of parameter restriction
679 procedure Set_Restriction
680 (R : All_Parameter_Restrictions;
685 if Restricted_Profile_Cached and Restricted_Profile_Result then
688 Restricted_Profile_Cached := False;
691 if Restrictions.Set (R) then
692 if V < Restrictions.Value (R) then
693 Restrictions.Value (R) := V;
694 Restrictions_Loc (R) := Sloc (N);
698 Restrictions.Set (R) := True;
699 Restrictions.Value (R) := V;
700 Restrictions_Loc (R) := Sloc (N);
703 -- Record the restriction if we are in the main unit,
704 -- or in the extended main unit. The reason that we
705 -- test separately for Main_Unit is that gnat.adc is
706 -- processed with Current_Sem_Unit = Main_Unit, but
707 -- nodes in gnat.adc do not appear to be the extended
708 -- main source unit (they probably should do ???)
710 if Current_Sem_Unit = Main_Unit
711 or else In_Extended_Main_Source_Unit (N)
713 if Main_Restrictions.Set (R) then
714 if V < Main_Restrictions.Value (R) then
715 Main_Restrictions.Value (R) := V;
718 elsif not Restriction_Warnings (R) then
719 Main_Restrictions.Set (R) := True;
720 Main_Restrictions.Value (R) := V;
725 -----------------------------------
726 -- Set_Restriction_No_Dependence --
727 -----------------------------------
729 procedure Set_Restriction_No_Dependence
734 -- Loop to check for duplicate entry
736 for J in No_Dependence.First .. No_Dependence.Last loop
738 -- Case of entry already in table
740 if Same_Unit (Unit, No_Dependence.Table (J).Unit) then
742 -- Error has precedence over warning
745 No_Dependence.Table (J).Warn := False;
752 -- Entry is not currently in table
754 No_Dependence.Append ((Unit, Warn));
755 end Set_Restriction_No_Dependence;
757 ----------------------------------
758 -- Suppress_Restriction_Message --
759 ----------------------------------
761 function Suppress_Restriction_Message (N : Node_Id) return Boolean is
763 -- We only output messages for the extended main source unit
765 if In_Extended_Main_Source_Unit (N) then
768 -- If loaded by rtsfind, then suppress message
770 elsif Sloc (N) <= No_Location then
773 -- Otherwise suppress message if internal file
776 return Is_Internal_File_Name (Unit_File_Name (Get_Source_Unit (N)));
778 end Suppress_Restriction_Message;
780 ---------------------
781 -- Tasking_Allowed --
782 ---------------------
784 function Tasking_Allowed return Boolean is
786 return not Restrictions.Set (No_Tasking)
787 and then (not Restrictions.Set (Max_Tasks)
788 or else Restrictions.Value (Max_Tasks) > 0);