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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Casing
; use Casing
;
28 with Errout
; use Errout
;
29 with Fname
; use Fname
;
30 with Fname
.UF
; use Fname
.UF
;
33 with Sinfo
; use Sinfo
;
34 with Sinput
; use Sinput
;
35 with Snames
; use Snames
;
36 with Uname
; use Uname
;
38 package body Restrict
is
40 Restricted_Profile_Result
: Boolean := False;
41 -- This switch memoizes the result of Restricted_Profile function
42 -- calls for improved efficiency. Its setting is valid only if
43 -- Restricted_Profile_Cached is True. Note that if this switch
44 -- is ever set True, it need never be turned off again.
46 Restricted_Profile_Cached
: Boolean := False;
47 -- This flag is set to True if the Restricted_Profile_Result
48 -- contains the correct cached result of Restricted_Profile calls.
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 procedure Restriction_Msg
(Msg
: String; R
: String; N
: Node_Id
);
55 -- Output error message at node N with given text, replacing the
56 -- '%' in the message with the name of the restriction given as R,
57 -- cased according to the current identifier casing. We do not use
58 -- the normal insertion mechanism, since this requires an entry
59 -- in the Names table, and this table will be locked if we are
60 -- generating a message from gigi.
62 function Same_Unit
(U1
, U2
: Node_Id
) return Boolean;
63 -- Returns True iff U1 and U2 represent the same library unit. Used for
64 -- handling of No_Dependence => Unit restriction case.
66 function Suppress_Restriction_Message
(N
: Node_Id
) return Boolean;
67 -- N is the node for a possible restriction violation message, but
68 -- the message is to be suppressed if this is an internal file and
69 -- this file is not the main unit.
75 function Abort_Allowed
return Boolean is
77 if Restrictions
.Set
(No_Abort_Statements
)
78 and then Restrictions
.Set
(Max_Asynchronous_Select_Nesting
)
79 and then Restrictions
.Value
(Max_Asynchronous_Select_Nesting
) = 0
87 -------------------------
88 -- Check_Compiler_Unit --
89 -------------------------
91 procedure Check_Compiler_Unit
(N
: Node_Id
) is
93 if Is_Compiler_Unit
(Get_Source_Unit
(N
)) then
94 Error_Msg_N
("use of construct not allowed in compiler", N
);
96 end Check_Compiler_Unit
;
98 ------------------------------------
99 -- Check_Elaboration_Code_Allowed --
100 ------------------------------------
102 procedure Check_Elaboration_Code_Allowed
(N
: Node_Id
) is
104 Check_Restriction
(No_Elaboration_Code
, N
);
105 end Check_Elaboration_Code_Allowed
;
107 -----------------------------------------
108 -- Check_Implicit_Dynamic_Code_Allowed --
109 -----------------------------------------
111 procedure Check_Implicit_Dynamic_Code_Allowed
(N
: Node_Id
) is
113 Check_Restriction
(No_Implicit_Dynamic_Code
, N
);
114 end Check_Implicit_Dynamic_Code_Allowed
;
116 ----------------------------------
117 -- Check_No_Implicit_Heap_Alloc --
118 ----------------------------------
120 procedure Check_No_Implicit_Heap_Alloc
(N
: Node_Id
) is
122 Check_Restriction
(No_Implicit_Heap_Allocations
, N
);
123 end Check_No_Implicit_Heap_Alloc
;
125 ---------------------------
126 -- Check_Restricted_Unit --
127 ---------------------------
129 procedure Check_Restricted_Unit
(U
: Unit_Name_Type
; N
: Node_Id
) is
131 if Suppress_Restriction_Message
(N
) then
134 elsif Is_Spec_Name
(U
) then
136 Fnam
: constant File_Name_Type
:=
137 Get_File_Name
(U
, Subunit
=> False);
142 Get_Name_String
(Fnam
);
144 -- Nothing to do if name not at least 5 characters long ending
145 -- in .ads or .adb extension, which we strip.
148 or else (Name_Buffer
(Name_Len
- 3 .. Name_Len
) /= ".ads"
150 Name_Buffer
(Name_Len
- 4 .. Name_Len
) /= ".adb")
155 -- Strip extension and pad to eight characters
157 Name_Len
:= Name_Len
- 4;
158 while Name_Len
< 8 loop
159 Name_Len
:= Name_Len
+ 1;
160 Name_Buffer
(Name_Len
) := ' ';
163 -- If predefined unit, check the list of restricted units
165 if Is_Predefined_File_Name
(Fnam
) then
166 for J
in Unit_Array
'Range loop
168 and then Name_Buffer
(1 .. 8) = Unit_Array
(J
).Filenm
170 Check_Restriction
(Unit_Array
(J
).Res_Id
, N
);
174 -- If not predefied unit, then one special check still remains.
175 -- GNAT.Current_Exception is not allowed if we have restriction
176 -- No_Exception_Propagation active.
179 if Name_Buffer
(1 .. 8) = "g-curexc" then
180 Check_Restriction
(No_Exception_Propagation
, N
);
185 end Check_Restricted_Unit
;
187 -----------------------
188 -- Check_Restriction --
189 -----------------------
191 procedure Check_Restriction
194 V
: Uint
:= Uint_Minus_1
)
196 Rimage
: constant String := Restriction_Id
'Image (R
);
199 -- V converted to integer form. If V is greater than Integer'Last,
200 -- it is reset to minus 1 (unknown value).
202 procedure Update_Restrictions
(Info
: in out Restrictions_Info
);
203 -- Update violation information in Info.Violated and Info.Count
205 -------------------------
206 -- Update_Restrictions --
207 -------------------------
209 procedure Update_Restrictions
(Info
: in out Restrictions_Info
) is
211 -- If not violated, set as violated now
213 if not Info
.Violated
(R
) then
214 Info
.Violated
(R
) := True;
216 if R
in All_Parameter_Restrictions
then
218 Info
.Unknown
(R
) := True;
221 Info
.Count
(R
) := VV
;
225 -- Otherwise if violated already and a parameter restriction,
226 -- update count by maximizing or summing depending on restriction.
228 elsif R
in All_Parameter_Restrictions
then
230 -- If new value is unknown, result is unknown
233 Info
.Unknown
(R
) := True;
235 -- If checked by maximization, do maximization
237 elsif R
in Checked_Max_Parameter_Restrictions
then
238 Info
.Count
(R
) := Integer'Max (Info
.Count
(R
), VV
);
240 -- If checked by adding, do add, checking for overflow
242 elsif R
in Checked_Add_Parameter_Restrictions
then
244 pragma Unsuppress
(Overflow_Check
);
246 Info
.Count
(R
) := Info
.Count
(R
) + VV
;
248 when Constraint_Error
=>
249 Info
.Count
(R
) := Integer'Last;
250 Info
.Unknown
(R
) := True;
253 -- Should not be able to come here, known counts should only
254 -- occur for restrictions that are Checked_max or Checked_Sum.
260 end Update_Restrictions
;
262 -- Start of processing for Check_Restriction
265 if UI_Is_In_Int_Range
(V
) then
266 VV
:= Integer (UI_To_Int
(V
));
271 -- Count can only be specified in the checked val parameter case
273 pragma Assert
(VV
< 0 or else R
in Checked_Val_Parameter_Restrictions
);
275 -- Nothing to do if value of zero specified for parameter restriction
281 -- Update current restrictions
283 Update_Restrictions
(Restrictions
);
285 -- If in main extended unit, update main restrictions as well
287 if Current_Sem_Unit
= Main_Unit
288 or else In_Extended_Main_Source_Unit
(N
)
290 Update_Restrictions
(Main_Restrictions
);
293 -- Nothing to do if restriction message suppressed
295 if Suppress_Restriction_Message
(N
) then
298 -- If restriction not set, nothing to do
300 elsif not Restrictions
.Set
(R
) then
303 -- Here if restriction set, check for violation (either this is a
304 -- Boolean restriction, or a parameter restriction with a value of
305 -- zero and an unknown count, or a parameter restriction with a
306 -- known value that exceeds the restriction count).
308 elsif R
in All_Boolean_Restrictions
309 or else (Restrictions
.Unknown
(R
)
310 and then Restrictions
.Value
(R
) = 0)
311 or else Restrictions
.Count
(R
) > Restrictions
.Value
(R
)
313 Error_Msg_Sloc
:= Restrictions_Loc
(R
);
315 -- If we have a location for the Restrictions pragma, output it
317 if Error_Msg_Sloc
> No_Location
318 or else Error_Msg_Sloc
= System_Location
320 if Restriction_Warnings
(R
) then
321 Restriction_Msg
("|violation of restriction %#?", Rimage
, N
);
323 Restriction_Msg
("|violation of restriction %#", Rimage
, N
);
326 -- Otherwise we have the case of an implicit restriction
327 -- (e.g. a restriction implicitly set by another pragma)
331 ("|violation of implicit restriction %", Rimage
, N
);
334 end Check_Restriction
;
336 -------------------------------------
337 -- Check_Restriction_No_Dependence --
338 -------------------------------------
340 procedure Check_Restriction_No_Dependence
(U
: Node_Id
; Err
: Node_Id
) is
344 for J
in No_Dependence
.First
.. No_Dependence
.Last
loop
345 DU
:= No_Dependence
.Table
(J
).Unit
;
347 if Same_Unit
(U
, DU
) then
348 Error_Msg_Sloc
:= Sloc
(DU
);
349 Error_Msg_Node_1
:= DU
;
351 if No_Dependence
.Table
(J
).Warn
then
353 ("?violation of restriction `No_Dependence '='> &`#",
357 ("|violation of restriction `No_Dependence '='> &`#",
364 end Check_Restriction_No_Dependence
;
366 ----------------------------------------
367 -- Cunit_Boolean_Restrictions_Restore --
368 ----------------------------------------
370 procedure Cunit_Boolean_Restrictions_Restore
371 (R
: Save_Cunit_Boolean_Restrictions
)
374 for J
in Cunit_Boolean_Restrictions
loop
375 Restrictions
.Set
(J
) := R
(J
);
377 end Cunit_Boolean_Restrictions_Restore
;
379 -------------------------------------
380 -- Cunit_Boolean_Restrictions_Save --
381 -------------------------------------
383 function Cunit_Boolean_Restrictions_Save
384 return Save_Cunit_Boolean_Restrictions
386 R
: Save_Cunit_Boolean_Restrictions
;
389 for J
in Cunit_Boolean_Restrictions
loop
390 R
(J
) := Restrictions
.Set
(J
);
391 Restrictions
.Set
(J
) := False;
395 end Cunit_Boolean_Restrictions_Save
;
397 ------------------------
398 -- Get_Restriction_Id --
399 ------------------------
401 function Get_Restriction_Id
402 (N
: Name_Id
) return Restriction_Id
406 Set_Casing
(All_Upper_Case
);
408 for J
in All_Restrictions
loop
410 S
: constant String := Restriction_Id
'Image (J
);
412 if S
= Name_Buffer
(1 .. Name_Len
) then
418 return Not_A_Restriction_Id
;
419 end Get_Restriction_Id
;
421 -------------------------------
422 -- No_Exception_Handlers_Set --
423 -------------------------------
425 function No_Exception_Handlers_Set
return Boolean is
427 return (No_Run_Time_Mode
or else Configurable_Run_Time_Mode
)
428 and then (Restrictions
.Set
(No_Exception_Handlers
)
430 Restrictions
.Set
(No_Exception_Propagation
));
431 end No_Exception_Handlers_Set
;
433 ----------------------------------
434 -- Process_Restriction_Synonyms --
435 ----------------------------------
437 -- Note: body of this function must be coordinated with list of
438 -- renaming declarations in System.Rident.
440 function Process_Restriction_Synonyms
(N
: Node_Id
) return Name_Id
442 Old_Name
: constant Name_Id
:= Chars
(N
);
447 when Name_Boolean_Entry_Barriers
=>
448 New_Name
:= Name_Simple_Barriers
;
450 when Name_Max_Entry_Queue_Depth
=>
451 New_Name
:= Name_Max_Entry_Queue_Length
;
453 when Name_No_Dynamic_Interrupts
=>
454 New_Name
:= Name_No_Dynamic_Attachment
;
456 when Name_No_Requeue
=>
457 New_Name
:= Name_No_Requeue_Statements
;
459 when Name_No_Task_Attributes
=>
460 New_Name
:= Name_No_Task_Attributes_Package
;
466 if Warn_On_Obsolescent_Feature
then
467 Error_Msg_Name_1
:= Old_Name
;
468 Error_Msg_N
("restriction identifier % is obsolescent?", N
);
469 Error_Msg_Name_1
:= New_Name
;
470 Error_Msg_N
("|use restriction identifier % instead", N
);
474 end Process_Restriction_Synonyms
;
476 ------------------------
477 -- Restricted_Profile --
478 ------------------------
480 function Restricted_Profile
return Boolean is
482 if Restricted_Profile_Cached
then
483 return Restricted_Profile_Result
;
486 Restricted_Profile_Result
:= True;
487 Restricted_Profile_Cached
:= True;
490 R
: Restriction_Flags
renames Profile_Info
(Restricted
).Set
;
491 V
: Restriction_Values
renames Profile_Info
(Restricted
).Value
;
493 for J
in R
'Range loop
495 and then (Restrictions
.Set
(J
) = False
496 or else Restriction_Warnings
(J
)
498 (J
in All_Parameter_Restrictions
499 and then Restrictions
.Value
(J
) > V
(J
)))
501 Restricted_Profile_Result
:= False;
506 return Restricted_Profile_Result
;
509 end Restricted_Profile
;
511 ------------------------
512 -- Restriction_Active --
513 ------------------------
515 function Restriction_Active
(R
: All_Restrictions
) return Boolean is
517 return Restrictions
.Set
(R
) and then not Restriction_Warnings
(R
);
518 end Restriction_Active
;
520 ---------------------
521 -- Restriction_Msg --
522 ---------------------
524 procedure Restriction_Msg
(Msg
: String; R
: String; N
: Node_Id
) is
525 B
: String (1 .. Msg
'Length + 2 * R
'Length + 1);
529 Name_Buffer
(1 .. R
'Last) := R
;
530 Name_Len
:= R
'Length;
531 Set_Casing
(Identifier_Casing
(Get_Source_File_Index
(Sloc
(N
))));
534 for J
in Msg
'Range loop
535 if Msg
(J
) = '%' then
539 -- Put characters of image in message, quoting upper case letters
541 for J
in 1 .. Name_Len
loop
542 if Name_Buffer
(J
) in 'A' .. 'Z' then
548 B
(P
) := Name_Buffer
(J
);
560 Error_Msg_N
(B
(1 .. P
), N
);
567 function Same_Unit
(U1
, U2
: Node_Id
) return Boolean is
569 if Nkind
(U1
) = N_Identifier
then
570 return Nkind
(U2
) = N_Identifier
and then Chars
(U1
) = Chars
(U2
);
572 elsif Nkind
(U2
) = N_Identifier
then
575 elsif (Nkind
(U1
) = N_Selected_Component
576 or else Nkind
(U1
) = N_Expanded_Name
)
578 (Nkind
(U2
) = N_Selected_Component
579 or else Nkind
(U2
) = N_Expanded_Name
)
581 return Same_Unit
(Prefix
(U1
), Prefix
(U2
))
582 and then Same_Unit
(Selector_Name
(U1
), Selector_Name
(U2
));
588 ------------------------------
589 -- Set_Profile_Restrictions --
590 ------------------------------
592 procedure Set_Profile_Restrictions
597 R
: Restriction_Flags
renames Profile_Info
(P
).Set
;
598 V
: Restriction_Values
renames Profile_Info
(P
).Value
;
601 for J
in R
'Range loop
604 Already_Restricted
: constant Boolean := Restriction_Active
(J
);
607 -- Set the restriction
609 if J
in All_Boolean_Restrictions
then
610 Set_Restriction
(J
, N
);
612 Set_Restriction
(J
, N
, V
(J
));
615 -- Set warning flag, except that we do not set the warning
616 -- flag if the restriction was already active and this is
617 -- the warning case. That avoids a warning overriding a real
618 -- restriction, which should never happen.
620 if not (Warn
and Already_Restricted
) then
621 Restriction_Warnings
(J
) := Warn
;
626 end Set_Profile_Restrictions
;
628 ---------------------
629 -- Set_Restriction --
630 ---------------------
632 -- Case of Boolean restriction
634 procedure Set_Restriction
635 (R
: All_Boolean_Restrictions
;
639 -- Restriction No_Elaboration_Code must be enforced on a unit by unit
640 -- basis. Hence, we avoid setting the restriction when processing an
641 -- unit which is not the main one being compiled (or its corresponding
642 -- spec). It can happen, for example, when processing an inlined body
643 -- (the package containing the inlined subprogram is analyzed,
644 -- including its pragma Restrictions).
646 -- This seems like a very nasty kludge??? This is not the only per unit
647 -- restriction why is this treated specially ???
649 if R
= No_Elaboration_Code
650 and then Current_Sem_Unit
/= Main_Unit
651 and then Cunit
(Current_Sem_Unit
) /= Library_Unit
(Cunit
(Main_Unit
))
656 Restrictions
.Set
(R
) := True;
658 if Restricted_Profile_Cached
and Restricted_Profile_Result
then
661 Restricted_Profile_Cached
:= False;
664 -- Set location, but preserve location of system
665 -- restriction for nice error msg with run time name
667 if Restrictions_Loc
(R
) /= System_Location
then
668 Restrictions_Loc
(R
) := Sloc
(N
);
671 -- Record the restriction if we are in the main unit, or in the extended
672 -- main unit. The reason that we test separately for Main_Unit is that
673 -- gnat.adc is processed with Current_Sem_Unit = Main_Unit, but nodes in
674 -- gnat.adc do not appear to be in the extended main source unit (they
675 -- probably should do ???)
677 if Current_Sem_Unit
= Main_Unit
678 or else In_Extended_Main_Source_Unit
(N
)
680 if not Restriction_Warnings
(R
) then
681 Main_Restrictions
.Set
(R
) := True;
686 -- Case of parameter restriction
688 procedure Set_Restriction
689 (R
: All_Parameter_Restrictions
;
694 if Restricted_Profile_Cached
and Restricted_Profile_Result
then
697 Restricted_Profile_Cached
:= False;
700 if Restrictions
.Set
(R
) then
701 if V
< Restrictions
.Value
(R
) then
702 Restrictions
.Value
(R
) := V
;
703 Restrictions_Loc
(R
) := Sloc
(N
);
707 Restrictions
.Set
(R
) := True;
708 Restrictions
.Value
(R
) := V
;
709 Restrictions_Loc
(R
) := Sloc
(N
);
712 -- Record the restriction if we are in the main unit,
713 -- or in the extended main unit. The reason that we
714 -- test separately for Main_Unit is that gnat.adc is
715 -- processed with Current_Sem_Unit = Main_Unit, but
716 -- nodes in gnat.adc do not appear to be the extended
717 -- main source unit (they probably should do ???)
719 if Current_Sem_Unit
= Main_Unit
720 or else In_Extended_Main_Source_Unit
(N
)
722 if Main_Restrictions
.Set
(R
) then
723 if V
< Main_Restrictions
.Value
(R
) then
724 Main_Restrictions
.Value
(R
) := V
;
727 elsif not Restriction_Warnings
(R
) then
728 Main_Restrictions
.Set
(R
) := True;
729 Main_Restrictions
.Value
(R
) := V
;
734 -----------------------------------
735 -- Set_Restriction_No_Dependence --
736 -----------------------------------
738 procedure Set_Restriction_No_Dependence
743 -- Loop to check for duplicate entry
745 for J
in No_Dependence
.First
.. No_Dependence
.Last
loop
747 -- Case of entry already in table
749 if Same_Unit
(Unit
, No_Dependence
.Table
(J
).Unit
) then
751 -- Error has precedence over warning
754 No_Dependence
.Table
(J
).Warn
:= False;
761 -- Entry is not currently in table
763 No_Dependence
.Append
((Unit
, Warn
));
764 end Set_Restriction_No_Dependence
;
766 ----------------------------------
767 -- Suppress_Restriction_Message --
768 ----------------------------------
770 function Suppress_Restriction_Message
(N
: Node_Id
) return Boolean is
772 -- We only output messages for the extended main source unit
774 if In_Extended_Main_Source_Unit
(N
) then
777 -- If loaded by rtsfind, then suppress message
779 elsif Sloc
(N
) <= No_Location
then
782 -- Otherwise suppress message if internal file
785 return Is_Internal_File_Name
(Unit_File_Name
(Get_Source_Unit
(N
)));
787 end Suppress_Restriction_Message
;
789 ---------------------
790 -- Tasking_Allowed --
791 ---------------------
793 function Tasking_Allowed
return Boolean is
795 return not Restrictions
.Set
(No_Tasking
)
796 and then (not Restrictions
.Set
(Max_Tasks
)
797 or else Restrictions
.Value
(Max_Tasks
) > 0);