1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             R E S T R I C T                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2003 Free Software Foundation, Inc.          --
10--                                                                          --
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,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, USA.                                                      --
21--                                                                          --
22-- GNAT was originally developed  by the GNAT team at  New York University. --
23-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24--                                                                          --
25------------------------------------------------------------------------------
26
27--  This package deals with the implementation of the Restrictions pragma
28
29with Rident;
30with Types;  use Types;
31with Uintp;  use Uintp;
32
33package Restrict is
34
35   type Restriction_Id is new Rident.Restriction_Id;
36   --  The type Restriction_Id defines the set of restriction identifiers,
37   --  which take no parameter (i.e. they are either present or not present).
38   --  The actual definition is in the separate package Rident, so that
39   --  it can easily be accessed by the binder without dragging in lots
40   --  of stuff.
41
42   subtype All_Restrictions is
43     Restriction_Id range
44       Restriction_Id (Rident.All_Restrictions'First) ..
45       Restriction_Id (Rident.All_Restrictions'Last);
46   --  All restriction identifiers
47
48   subtype Partition_Restrictions is
49     Restriction_Id range
50       Restriction_Id (Rident.Partition_Restrictions'First) ..
51       Restriction_Id (Rident.Partition_Restrictions'Last);
52   --  Range of restriction identifiers that are checked by the binder
53
54   subtype Compilation_Unit_Restrictions is
55     Restriction_Id range
56       Restriction_Id (Rident.Compilation_Unit_Restrictions'First) ..
57       Restriction_Id (Rident.Compilation_Unit_Restrictions'Last);
58   --  Range of restriction identifiers not checked by binder
59
60   type Restriction_Parameter_Id is new Rident.Restriction_Parameter_Id;
61   --  The type Restriction_Parameter_Id records cases where a parameter is
62   --  present in the corresponding pragma. The actual definition is in the
63   --  separate package Rident for consistency.
64
65   type Restrictions_Flags is array (Restriction_Id) of Boolean;
66   --  Type used for arrays indexed by Restriction_Id.
67
68   Restrictions : Restrictions_Flags := (others => False);
69   --  Corresponding entry is False if restriction is not active, and
70   --  True if the restriction is active, i.e. if a pragma Restrictions
71   --  has been seen anywhere. Note that we are happy to pick up any
72   --  restrictions pragmas in with'ed units, since we are required to
73   --  be consistent at link time, and we might as well find the error
74   --  at compile time. Clients must NOT use this array for checking to
75   --  see if a restriction is violated, instead it is required that the
76   --  Check_Restriction subprograms be used for this purpose. The only
77   --  legitimate direct use of this array is when the code is modified
78   --  as a result of the restriction in some way.
79
80   Restrictions_Loc : array (Restriction_Id) of Source_Ptr :=
81                       (others => No_Location);
82   --  Locations of Restrictions pragmas for error message purposes.
83   --  Valid only if corresponding entry in Restrictions is set. A value
84   --  of No_Location is used for implicit restrictions set by another
85   --  pragma, and a value of System_Location is used for restrictions
86   --  set from package Standard by the processing in Targparm.
87
88   Main_Restrictions : Restrictions_Flags := (others => False);
89   --  This variable saves the cumulative restrictions in effect compiling
90   --  any unit that is part of the extended main unit (i.e. the compiled
91   --  unit, its spec if any, and its subunits if any). The reason we keep
92   --  track of this is for the information that goes to the binder about
93   --  restrictions that are set. The binder will identify a unit that has
94   --  a restrictions pragma for error message purposes, and we do not want
95   --  to pick up a restrictions pragma in a with'ed unit for this purpose.
96
97   Violations : Restrictions_Flags := (others => False);
98   --  Corresponding entry is False if the restriction has not been
99   --  violated in the current main unit, and True if it has been violated.
100
101   Restriction_Warnings : Restrictions_Flags := (others => False);
102   --  If one of these flags is set, then it means that violation of the
103   --  corresponding restriction results only in a warning message, not
104   --  in an error message, and the restriction is not otherwise enforced.
105
106   Restriction_Parameters :
107     array (Restriction_Parameter_Id) of Uint := (others => No_Uint);
108   --  This array indicates the setting of restriction parameter identifier
109   --  values. All values are initially set to No_Uint indicating that the
110   --  parameter is not set, and are set to the appropriate non-negative
111   --  value if a Restrictions pragma specifies the corresponding
112   --  restriction parameter identifier with an appropriate value.
113
114   Restriction_Parameters_Loc :
115     array (Restriction_Parameter_Id) of Source_Ptr;
116   --  Locations of Restrictions pragmas for error message purposes.
117   --  Valid only if corresponding entry in Restriction_Parameters is
118   --  set to a value other than No_Uint.
119
120   type Unit_Entry is record
121      Res_Id : Restriction_Id;
122      Filenm : String (1 .. 8);
123   end record;
124
125   type Unit_Array_Type is array (Positive range <>) of Unit_Entry;
126
127   Unit_Array : constant Unit_Array_Type := (
128     (No_Asynchronous_Control,    "a-astaco"),
129     (No_Calendar,                "a-calend"),
130     (No_Calendar,                "calendar"),
131     (No_Delay,                   "a-calend"),
132     (No_Delay,                   "calendar"),
133     (No_Dynamic_Priorities,      "a-dynpri"),
134     (No_Finalization,            "a-finali"),
135     (No_IO,                      "a-direio"),
136     (No_IO,                      "directio"),
137     (No_IO,                      "a-sequio"),
138     (No_IO,                      "sequenio"),
139     (No_IO,                      "a-ststio"),
140     (No_IO,                      "a-textio"),
141     (No_IO,                      "text_io "),
142     (No_IO,                      "a-witeio"),
143     (No_Task_Attributes,         "a-tasatt"),
144     (No_Streams,                 "a-stream"),
145     (No_Unchecked_Conversion,    "a-unccon"),
146     (No_Unchecked_Conversion,    "unchconv"),
147     (No_Unchecked_Deallocation,  "a-uncdea"),
148     (No_Unchecked_Deallocation,  "unchdeal"));
149   --  This array defines the mapping between restriction identifiers and
150   --  predefined language files containing units for which the identifier
151   --  forbids semantic dependence.
152
153   type Save_Compilation_Unit_Restrictions is private;
154   --  Type used for saving and restoring compilation unit restrictions.
155   --  See Compilation_Unit_Restrictions_[Save|Restore] subprograms.
156
157   --  The following map has True for all GNAT pragmas. It is used to
158   --  implement pragma Restrictions (No_Implementation_Restrictions)
159   --  (which is why this restriction itself is excluded from the list).
160
161   Implementation_Restriction : Restrictions_Flags :=
162     (Boolean_Entry_Barriers             => True,
163      No_Calendar                        => True,
164      No_Dynamic_Interrupts              => True,
165      No_Enumeration_Maps                => True,
166      No_Entry_Calls_In_Elaboration_Code => True,
167      No_Entry_Queue                     => True,
168      No_Exception_Handlers              => True,
169      No_Exception_Registration          => True,
170      No_Implicit_Conditionals           => True,
171      No_Implicit_Dynamic_Code           => True,
172      No_Implicit_Loops                  => True,
173      No_Local_Protected_Objects         => True,
174      No_Protected_Type_Allocators       => True,
175      No_Relative_Delay                  => True,
176      No_Requeue                         => True,
177      No_Secondary_Stack                 => True,
178      No_Select_Statements               => True,
179      No_Standard_Storage_Pools          => True,
180      No_Streams                         => True,
181      No_Task_Attributes                 => True,
182      No_Task_Termination                => True,
183      No_Wide_Characters                 => True,
184      Static_Priorities                  => True,
185      Static_Storage_Size                => True,
186      No_Implementation_Attributes       => True,
187      No_Implementation_Pragmas          => True,
188      No_Elaboration_Code                => True,
189      others                             => False);
190
191   -----------------
192   -- Subprograms --
193   -----------------
194
195   function Abort_Allowed return Boolean;
196   pragma Inline (Abort_Allowed);
197   --  Tests to see if abort is allowed by the current restrictions settings.
198   --  For abort to be allowed, either No_Abort_Statements must be False,
199   --  or Max_Asynchronous_Select_Nesting must be non-zero.
200
201   procedure Check_Restricted_Unit (U : Unit_Name_Type; N : Node_Id);
202   --  Checks if loading of unit U is prohibited by the setting of some
203   --  restriction (e.g. No_IO restricts the loading of unit Ada.Text_IO).
204   --  If a restriction exists post error message at the given node.
205
206   procedure Check_Restriction (R : Restriction_Id; N : Node_Id);
207   --  Checks that the given restriction is not set, and if it is set, an
208   --  appropriate message is posted on the given node. Also records the
209   --  violation in the violations array. Note that it is mandatory to
210   --  always use this routine to check if a restriction is violated. Such
211   --  checks must never be done directly by the caller, since otherwise
212   --  they are not properly recorded in the violations array.
213
214   procedure Check_Restriction
215     (R : Restriction_Parameter_Id;
216      V : Uint;
217      N : Node_Id);
218   --  Checks that the count in V does not exceed the maximum value of the
219   --  restriction parameter value corresponding to the given restriction
220   --  parameter identifier (if it has been set). If the count in V exceeds
221   --  the maximum, then post an error message on node N. We use this call
222   --  when we can tell the maximum usage at compile time. In other words,
223   --  we guarantee that if a call is made to this routine, then the front
224   --  end will make all necessary calls for the restriction parameter R
225   --  to ensure that we really know the maximum value used anywhere.
226
227   procedure Check_Restriction (R : Restriction_Parameter_Id; N : Node_Id);
228   --  Check that the maximum value of the restriction parameter corresponding
229   --  to the given restriction parameter identifier is not set to zero. If
230   --  it has been set to zero, post an error message on node N. We use this
231   --  call in cases where we can tell at compile time that the count must be
232   --  at least one, but we can't tell anything more.
233
234   procedure Check_Elaboration_Code_Allowed (N : Node_Id);
235   --  Tests to see if elaboration code is allowed by the current restrictions
236   --  settings. This function is called by Gigi when it needs to define
237   --  an elaboration routine. If elaboration code is not allowed, an error
238   --  message is posted on the node given as argument.
239
240   procedure Check_No_Implicit_Heap_Alloc (N : Node_Id);
241   --  Equivalent to Check_Restriction (No_Implicit_Heap_Allocations, N).
242   --  Provided for easy use by back end, which has to check this restriction.
243
244   function Compilation_Unit_Restrictions_Save
245     return Save_Compilation_Unit_Restrictions;
246   --  This function saves the compilation unit restriction settings, and
247   --  resets them to False. This is used e.g. when compiling a with'ed
248   --  unit to avoid incorrectly propagating restrictions. Note that it
249   --  would not be wrong to also save and reset the partition restrictions,
250   --  since the binder would catch inconsistencies, but actually it is a
251   --  good thing to acquire restrictions from with'ed units if they are
252   --  required to be partition wide, because it allows the restriction
253   --  violation message to be given at compile time instead of link time.
254
255   procedure Compilation_Unit_Restrictions_Restore
256     (R : Save_Compilation_Unit_Restrictions);
257   --  This is the corresponding restore procedure to restore restrictions
258   --  previously saved by Compilation_Unit_Restrictions_Save.
259
260   function Get_Restriction_Id
261     (N    : Name_Id)
262      return Restriction_Id;
263   --  Given an identifier name, determines if it is a valid restriction
264   --  identifier, and if so returns the corresponding Restriction_Id
265   --  value, otherwise returns Not_A_Restriction_Id.
266
267   function Get_Restriction_Parameter_Id
268     (N    : Name_Id)
269      return Restriction_Parameter_Id;
270   --  Given an identifier name, determines if it is a valid restriction
271   --  parameter identifier, and if so returns the corresponding
272   --  Restriction_Parameter_Id value, otherwise returns
273   --  Not_A_Restriction_Parameter_Id.
274
275   function No_Exception_Handlers_Set return Boolean;
276   --  Test to see if current restrictions settings specify that no exception
277   --  handlers are present. This function is called by Gigi when it needs to
278   --  expand an AT END clean up identifier with no exception handler.
279
280   function Restricted_Profile return Boolean;
281   --  Tests to see if tasking operations follow the GNAT restricted run time
282   --  profile.
283
284   procedure Set_Ravenscar (N : Node_Id);
285   --  Enables the set of restrictions for Ravenscar. N is the corresponding
286   --  pragma node, which is used for error messages on any constructs that
287   --  violate the profile.
288
289   procedure Set_Restricted_Profile (N : Node_Id);
290   --  Enables the set of restrictions for pragma Restricted_Run_Time. N is
291   --  the corresponding pragma node, which is used for error messages on
292   --  constructs that violate the profile.
293
294   function Tasking_Allowed return Boolean;
295   pragma Inline (Tasking_Allowed);
296   --  Tests to see if tasking operations are allowed by the current
297   --  restrictions settings. For tasking to be allowed Max_Tasks must
298   --  be non-zero.
299
300private
301   type Save_Compilation_Unit_Restrictions is
302     array (Compilation_Unit_Restrictions) of Boolean;
303   --  Type used for saving and restoring compilation unit restrictions.
304   --  See Compilation_Unit_Restrictions_[Save|Restore] subprograms.
305
306end Restrict;
307