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-2013, 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 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.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26--  This package deals with the implementation of the Restrictions pragma
27
28with Namet;  use Namet;
29with Rident; use Rident;
30with Table;
31with Types;  use Types;
32with Uintp;  use Uintp;
33
34package Restrict is
35
36   Restrictions : Restrictions_Info := No_Restrictions;
37   --  This variable records restrictions found in any units in the main
38   --  extended unit, and in the case of restrictions checked for partition
39   --  consistency, restrictions found in any with'ed units, parent specs
40   --  etc., since we may as well check as much as we can at compile time.
41   --  These variables should not be referenced directly by clients. Instead
42   --  use Check_Restriction to record a violation of a restriction, and
43   --  Restriction_Active to test if a given restriction is active.
44
45   Restrictions_Loc : array (All_Restrictions) of Source_Ptr :=
46                       (others => No_Location);
47   --  Locations of Restrictions pragmas for error message purposes.
48   --  Valid only if corresponding entry in Restrictions is set. A value
49   --  of No_Location is used for implicit restrictions set by another
50   --  pragma, and a value of System_Location is used for restrictions
51   --  set from package Standard by the processing in Targparm.
52
53   Restriction_Profile_Name : array (All_Restrictions) of Profile_Name;
54   --  Entries in this array are valid only if the corresponding restriction
55   --  in Restrictions set. The value is the corresponding profile name if the
56   --  restriction was set by a Profile or Profile_Warnings pragma. The value
57   --  is No_Profile in all other cases.
58
59   Main_Restrictions : Restrictions_Info := No_Restrictions;
60   --  This variable records only restrictions found in any units of the
61   --  main extended unit. These are the variables used for ali file output,
62   --  since we want the binder to be able to accurately diagnose inter-unit
63   --  restriction violations.
64
65   Restriction_Warnings : Rident.Restriction_Flags := (others => False);
66   --  If one of these flags is set, then it means that violation of the
67   --  corresponding restriction results only in a warning message, not
68   --  in an error message, and the restriction is not otherwise enforced.
69   --  Note that the flags in Restrictions are set to indicate that the
70   --  restriction is set in this case, but Main_Restrictions is never
71   --  set if Restriction_Warnings is set, so this does not look like a
72   --  restriction to the binder.
73
74   --  The following declarations establish a mapping between restriction
75   --  identifiers, and the names of corresponding restriction library units.
76
77   type Unit_Entry is record
78      Res_Id : Restriction_Id;
79      Filenm : String (1 .. 8);
80   end record;
81
82   Unit_Array : constant array (Positive range <>) of Unit_Entry := (
83     (No_Asynchronous_Control,     "a-astaco"),
84     (No_Calendar,                 "a-calend"),
85     (No_Calendar,                 "calendar"),
86     (No_Delay,                    "a-calend"),
87     (No_Delay,                    "calendar"),
88     (No_Dynamic_Priorities,       "a-dynpri"),
89     (No_Finalization,             "a-finali"),
90     (No_IO,                       "a-direio"),
91     (No_IO,                       "directio"),
92     (No_IO,                       "a-sequio"),
93     (No_IO,                       "sequenio"),
94     (No_IO,                       "a-ststio"),
95     (No_IO,                       "a-textio"),
96     (No_IO,                       "text_io "),
97     (No_IO,                       "a-witeio"),
98     (No_Task_Attributes_Package,  "a-tasatt"),
99     (No_Unchecked_Conversion,     "a-unccon"),
100     (No_Unchecked_Conversion,     "unchconv"),
101     (No_Unchecked_Deallocation,   "a-uncdea"),
102     (No_Unchecked_Deallocation,   "unchdeal"));
103
104   --  The following map has True for all GNAT-defined Restrictions. It is used
105   --  to implement pragma Restrictions (No_Implementation_Restrictions) (which
106   --  is why this restriction itself is excluded from the list).
107
108   Implementation_Restriction : array (All_Restrictions) of Boolean :=
109     (Simple_Barriers                    => True,
110      No_Calendar                        => True,
111      No_Default_Initialization          => True,
112      No_Direct_Boolean_Operators        => True,
113      No_Dispatching_Calls               => True,
114      No_Dynamic_Attachment              => True,
115      No_Elaboration_Code                => True,
116      No_Enumeration_Maps                => True,
117      No_Entry_Calls_In_Elaboration_Code => True,
118      No_Entry_Queue                     => True,
119      No_Exception_Handlers              => True,
120      No_Exception_Propagation           => True,
121      No_Exception_Registration          => True,
122      No_Finalization                    => True,
123      No_Implementation_Attributes       => True,
124      No_Implementation_Pragmas          => True,
125      No_Implicit_Conditionals           => True,
126      No_Implicit_Aliasing               => True,
127      No_Implicit_Dynamic_Code           => True,
128      No_Implicit_Loops                  => True,
129      No_Initialize_Scalars              => True,
130      No_Local_Protected_Objects         => True,
131      No_Protected_Type_Allocators       => True,
132      No_Relative_Delay                  => True,
133      No_Requeue_Statements              => True,
134      No_Secondary_Stack                 => True,
135      No_Select_Statements               => True,
136      No_Standard_Storage_Pools          => True,
137      No_Stream_Optimizations            => True,
138      No_Streams                         => True,
139      No_Task_Attributes_Package         => True,
140      No_Task_Termination                => True,
141      No_Tasking                         => True,
142      No_Wide_Characters                 => True,
143      Static_Priorities                  => True,
144      Static_Storage_Size                => True,
145      SPARK_05                           => True,
146      others                             => False);
147
148   --  The following table records entries made by Restrictions pragmas
149   --  that specify a parameter for No_Dependence. Each such pragma makes
150   --  an entry in this table.
151
152   --  Note: we have chosen to implement this restriction in the "syntactic"
153   --  form, where we do not check that the named package is a language defined
154   --  package, but instead we allow arbitrary package names. The discussion of
155   --  this issue is not complete in the ARG, but the sense seems to be leaning
156   --  in this direction, which makes more sense to us, since it is much more
157   --  useful, and much easier to implement.
158
159   type ND_Entry is record
160      Unit : Node_Id;
161      --  The unit parameter from the No_Dependence pragma
162
163      Warn : Boolean;
164      --  True if from Restriction_Warnings, False if from Restrictions
165
166      Profile : Profile_Name;
167      --  Set to name of profile from which No_Dependence entry came, or to
168      --  No_Profile if a pragma Restriction set the No_Dependence entry.
169   end record;
170
171   package No_Dependences is new Table.Table (
172     Table_Component_Type => ND_Entry,
173     Table_Index_Type     => Int,
174     Table_Low_Bound      => 0,
175     Table_Initial        => 200,
176     Table_Increment      => 200,
177     Table_Name           => "Name_No_Dependences");
178
179   -----------------
180   -- Subprograms --
181   -----------------
182
183   --  Note: several of these subprograms can generate error messages (e.g.
184   --  Check_Restriction). These routines should be called in the analyzer
185   --  rather than the expander, so that the associated error messages are
186   --  correctly generated in semantics only (-gnatc) mode.
187
188   function Abort_Allowed return Boolean;
189   pragma Inline (Abort_Allowed);
190   --  Tests to see if abort is allowed by the current restrictions settings.
191   --  For abort to be allowed, either No_Abort_Statements must be False,
192   --  or Max_Asynchronous_Select_Nesting must be non-zero.
193
194   procedure Check_Compiler_Unit (N : Node_Id);
195   --  If unit N is in a unit that has a pragma Compiler_Unit, then a message
196   --  is posted on node N noting use of a construct that is not permitted in
197   --  the compiler.
198
199   procedure Check_Restricted_Unit (U : Unit_Name_Type; N : Node_Id);
200   --  Checks if loading of unit U is prohibited by the setting of some
201   --  restriction (e.g. No_IO restricts the loading of unit Ada.Text_IO).
202   --  If a restriction exists post error message at the given node.
203
204   procedure Check_Restriction
205     (Msg_Issued : out Boolean;
206      R          : Restriction_Id;
207      N          : Node_Id;
208      V          : Uint := Uint_Minus_1);
209   --  Checks that the given restriction is not set, and if it is set, an
210   --  appropriate message is posted on the given node, in which case
211   --  Msg_Issued is set to True (and False otherwise). Also records the
212   --  violation in the appropriate internal arrays. Note that it is mandatory
213   --  to always use this routine to check if a restriction is violated. Such
214   --  checks must never be done directly by the caller, since otherwise
215   --  violations in the absence of restrictions are not properly recorded. The
216   --  value of V is relevant only for parameter restrictions, and in this case
217   --  indicates the exact count for the violation. If the exact count is not
218   --  known, V is left at its default of -1 which indicates an unknown count.
219
220   procedure Check_Restriction
221     (R : Restriction_Id;
222      N : Node_Id;
223      V : Uint := Uint_Minus_1);
224   --  Wrapper on Check_Restriction with Msg_Issued, with the out-parameter
225   --  being ignored here.
226
227   procedure Check_Restriction_No_Use_Of_Attribute (N : Node_Id);
228   --  N is the node of an attribute definition clause. An error message
229   --  (warning) will be issued if a restriction (warning) was previously set
230   --  for this attribute using Set_No_Use_Of_Attribute.
231
232   procedure Check_Restriction_No_Use_Of_Pragma (N : Node_Id);
233   --  N is the node of a pragma. An error message (warning) will be issued
234   --  if a restriction (warning) was previously set for this pragma using
235   --  Set_No_Use_Of_Pragma.
236
237   procedure Check_Restriction_No_Dependence (U : Node_Id; Err : Node_Id);
238   --  Called when a dependence on a unit is created (either implicitly, or by
239   --  an explicit WITH clause). U is a node for the unit involved, and Err is
240   --  the node to which an error will be attached if necessary.
241
242   procedure Check_Restriction_No_Specification_Of_Aspect (N : Node_Id);
243   --  N is the node id for an N_Aspect_Specification. An error message
244   --  (warning) will be issued if a restriction (warning) was previous set
245   --  for this aspect using Set_No_Specification_Of_Aspect.
246
247   procedure Check_Elaboration_Code_Allowed (N : Node_Id);
248   --  Tests to see if elaboration code is allowed by the current restrictions
249   --  settings. This function is called by Gigi when it needs to define an
250   --  elaboration routine. If elaboration code is not allowed, an error
251   --  message is posted on the node given as argument.
252
253   procedure Check_SPARK_Restriction
254     (Msg   : String;
255      N     : Node_Id;
256      Force : Boolean := False);
257   --  Node N represents a construct not allowed in SPARK_05 mode. If this is
258   --  a source node, or if the restriction is forced (Force = True), and
259   --  the SPARK_05 restriction is set, then an error is issued on N. Msg
260   --  is appended to the restriction failure message.
261
262   procedure Check_SPARK_Restriction (Msg1, Msg2 : String; N : Node_Id);
263   --  Same as Check_SPARK_Restriction except there is a continuation message
264   --  Msg2 following the initial message Msg1.
265
266   procedure Check_No_Implicit_Aliasing (Obj : Node_Id);
267   --  Obj is a node for which Is_Aliased_View is True, which is being used in
268   --  a context (e.g. 'Access) where no implicit aliasing is allowed if the
269   --  restriction No_Implicit_Aliasing is set. This procedure checks for the
270   --  case where the restriction is active and Obj does not meet the required
271   --  rules for avoiding implicit aliases, and issues a restriction message.
272
273   procedure Check_Implicit_Dynamic_Code_Allowed (N : Node_Id);
274   --  Tests to see if dynamic code generation (dynamically generated
275   --  trampolines, in particular) is allowed by the current restrictions
276   --  settings. This function is called by Gigi when it needs to generate code
277   --  that generates a trampoline. If not allowed, an error message is posted
278   --  on the node given as argument.
279
280   procedure Check_No_Implicit_Heap_Alloc (N : Node_Id);
281   --  Equivalent to Check_Restriction (No_Implicit_Heap_Allocations, N).
282   --  Provided for easy use by back end, which has to check this restriction.
283
284   procedure Check_Obsolescent_2005_Entity (E : Entity_Id; N : Node_Id);
285   --  This routine checks if the entity E is one of the obsolescent entries
286   --  in Ada.Characters.Handling in Ada 2005 and No_Obsolescent_Features
287   --  restriction is active. If so an appropriate message is given. N is
288   --  the node on which the message is to be placed. It's a bit kludgy to
289   --  have this highly specialized routine rather than some wonderful general
290   --  mechanism (e.g. a special pragma) to handle this case, but there are
291   --  only six cases, and it is not worth the effort to do something general.
292
293   procedure Check_Wide_Character_Restriction (E : Entity_Id; N : Node_Id);
294   --  This procedure checks if the No_Wide_Character restriction is active,
295   --  and if so, if N Comes_From_Source, and the root type of E is one of
296   --  [Wide_]Wide_Character or [Wide_]Wide_String, then the restriction
297   --  violation is recorded, and an appropriate message given.
298
299   function Get_Restriction_Id
300     (N : Name_Id) return Restriction_Id;
301   --  Given an identifier name, determines if it is a valid restriction
302   --  identifier, and if so returns the corresponding Restriction_Id value,
303   --  otherwise returns Not_A_Restriction_Id.
304
305   function OK_No_Dependence_Unit_Name (N : Node_Id) return Boolean;
306   --  Used in checking No_Dependence argument of pragma Restrictions or
307   --  pragma Restrictions_Warning, or attribute Restriction_Set. Returns
308   --  True if N has the proper form for a unit name, False otherwise.
309
310   function Is_In_Hidden_Part_In_SPARK (Loc : Source_Ptr) return Boolean;
311   --  Determine if given location is covered by a hidden region range in the
312   --  SPARK hides table.
313
314   function No_Exception_Handlers_Set return Boolean;
315   --  Test to see if current restrictions settings specify that no exception
316   --  handlers are present. This function is called by Gigi when it needs to
317   --  expand an AT END clean up identifier with no exception handler. True
318   --  will be returned if the configurable run-time is activated, and either
319   --  of the restrictions No_Exception_Handlers or No_Exception_Propagation is
320   --  set. In the latter case, the source may contain handlers but they either
321   --  get converted using the local goto transformation or deleted.
322
323   function No_Exception_Propagation_Active return Boolean;
324   --  Test to see if current restrictions settings specify that no
325   --  exception propagation is activated.
326
327   function Process_Restriction_Synonyms (N : Node_Id) return Name_Id;
328   --  Id is a node whose Chars field contains the name of a restriction.
329   --  If it is one of synonyms that we allow for historical purposes (for
330   --  list see System.Rident), then the proper official name is returned.
331   --  Otherwise the Chars field of the argument is returned unchanged.
332
333   function Restriction_Active (R : All_Restrictions) return Boolean;
334   pragma Inline (Restriction_Active);
335   --  Determines if a given restriction is active. This call should only be
336   --  used where the compiled code depends on whether the restriction is
337   --  active. Always use Check_Restriction to record a violation. Note that
338   --  this returns False if we only have a Restriction_Warnings set, since
339   --  restriction warnings should never affect generated code. If you want
340   --  to know if a call to Check_Restriction is needed then use the function
341   --  Restriction_Check_Required instead.
342
343   function Restriction_Check_Required (R : All_Restrictions) return Boolean;
344   pragma Inline (Restriction_Check_Required);
345   --  Determines if either a Restriction_Warnings or Restrictions pragma has
346   --  been given for the specified restriction. If true, then a subsequent
347   --  call to Check_Restriction is required if the restriction is violated.
348   --  This must not be used to guard code generation that depends on whether
349   --  a restriction is active (see Restriction_Active above). Typically it
350   --  is used to avoid complex code to determine if a restriction is violated,
351   --  executing this code only if needed.
352
353   function Restricted_Profile return Boolean;
354   --  Tests if set of restrictions corresponding to Profile (Restricted) is
355   --  currently in effect (set by pragma Profile, or by an appropriate set of
356   --  individual Restrictions pragmas). Returns True only if all the required
357   --  restrictions are set.
358
359   procedure Set_Hidden_Part_In_SPARK (Loc1, Loc2 : Source_Ptr);
360   --  Insert a new hidden region range in the SPARK hides table. The effect
361   --  is to hide any SPARK violation messages which are in the range Loc1 to
362   --  Loc2-1 (i.e. Loc2 is the first location for reenabling checks).
363
364   procedure Set_Profile_Restrictions
365     (P    : Profile_Name;
366      N    : Node_Id;
367      Warn : Boolean);
368   --  Sets the set of restrictions associated with the given profile name. N
369   --  is the node of the construct to which error messages are to be attached
370   --  as required. Warn is set True for the case of Profile_Warnings where the
371   --  restrictions are set as warnings rather than legality requirements, and
372   --  is also True for Profile if the Treat_Restrictions_As_Warnings flag is
373   --  set. It is false for Profile if this flag is not set.
374
375   procedure Set_Restriction
376     (R : All_Boolean_Restrictions;
377      N : Node_Id);
378   --  N is a node (typically a pragma node) that has the effect of setting
379   --  Boolean restriction R. The restriction is set in Restrictions, and
380   --  also in Main_Restrictions if this is the main unit.
381
382   procedure Set_Restriction
383     (R : All_Parameter_Restrictions;
384      N : Node_Id;
385      V : Integer);
386   --  Similar to the above, except that this is used for the case of a
387   --  parameter restriction, and the corresponding value V is given.
388
389   procedure Set_Restriction_No_Dependence
390     (Unit    : Node_Id;
391      Warn    : Boolean;
392      Profile : Profile_Name := No_Profile);
393   --  Sets given No_Dependence restriction in table if not there already. Warn
394   --  is True if from Restriction_Warnings, or for Restrictions if the flag
395   --  Treat_Restrictions_As_Warnings is set. False if from Restrictions and
396   --  this flag is not set. Profile is set to a non-default value if the
397   --  No_Dependence restriction comes from a Profile pragma.
398
399   procedure Set_Restriction_No_Specification_Of_Aspect
400     (N       : Node_Id;
401      Warning : Boolean);
402   --  N is the node id for an identifier from a pragma Restrictions for the
403   --  No_Specification_Of_Aspect pragma. An error message will be issued if
404   --  the identifier is not a valid aspect name. Warning is set True for the
405   --  case of a Restriction_Warnings pragma specifying this restriction and
406   --  False for a Restrictions pragma specifying this restriction.
407
408   procedure Set_Restriction_No_Use_Of_Attribute
409     (N       : Node_Id;
410      Warning : Boolean);
411   --  N is the node id for the identifier in a pragma Restrictions for
412   --  No_Use_Of_Attribute. Caller has verified that this is a valid attribute
413   --  designator.
414
415   procedure Set_Restriction_No_Use_Of_Pragma
416     (N       : Node_Id;
417      Warning : Boolean);
418   --  N is the node id for the identifier in a pragma Restrictions for
419   --  No_Use_Of_Pragma. Caller has verified that this is a valid pragma id.
420
421   function Tasking_Allowed return Boolean;
422   pragma Inline (Tasking_Allowed);
423   --  Tests if tasking operations are allowed by the current restrictions
424   --  settings. For tasking to be allowed Max_Tasks must be non-zero.
425
426   ----------------------------------------------
427   -- Handling of Boolean Compilation Switches --
428   ----------------------------------------------
429
430   --  The following declarations are used for proper saving and restoring of
431   --  restrictions for separate compilation units. There are two cases:
432
433   --    For partition-wide restrictions, we just let the restrictions pragmas
434   --    pile up, and we never reset them. We might as well detect what we can
435   --    at compile time. If e.g. a with'ed unit has a restriction for one of
436   --    the partition-wide restrictions, then the binder will enforce it on
437   --    all units in the partition, including the unit with the WITH. Although
438   --    it would not be wrong to leave this till bind time, we might as well
439   --    flag it earlier at compile time.
440
441   --    For non-partition-wide restrictions, we have quite a different state
442   --    of affairs. Here it would be quite wrong to carry a restriction from
443   --    a with'ed unit to another with'ed unit, or from a package spec to the
444   --    package body. This means that we have to reset these non-partition
445   --    wide restrictions at the start of each separate compilation unit. For
446   --    units in the extended main program, we need to reset them all to the
447   --    values set by the configuration pragma file(s). For units not in the
448   --    extended main program, e.g. with'ed units, we might as well reset all
449   --    of these restrictions to off (False). The actual initial values will
450   --    be taken from the config files active when those units are compiled
451   --    as main units.
452
453   type Save_Cunit_Boolean_Restrictions is private;
454   --  Type used for saving and restoring compilation unit restrictions.
455
456   function Cunit_Boolean_Restrictions_Save
457     return Save_Cunit_Boolean_Restrictions;
458   --  This function saves the compilation unit restriction settings, leaving
459   --  then unchanged. This is used e.g. at the start of processing a context
460   --  clause, so that the main unit restrictions can be restored after all
461   --  the with'ed units have been processed.
462
463   procedure Cunit_Boolean_Restrictions_Restore
464     (R : Save_Cunit_Boolean_Restrictions);
465   --  This is the corresponding restore procedure to restore restrictions
466   --  previously saved by Cunit_Boolean_Restrictions_Save. However it does
467   --  not reset No_Elaboration_Code, this stays set if it was set before
468   --  the call, and also if it is set before the call, then the Config
469   --  setting is also updated to include this restriction. This is what
470   --  implements the special handling of No_Elaboration_Code.
471
472   procedure Save_Config_Cunit_Boolean_Restrictions;
473   --  This saves the current compilation unit restrictions in an internal
474   --  variable, and leaves them unchanged. This is called immediately after
475   --  processing the configuration file pragmas, to record the restrictions
476   --  set by these configuration file pragmas.
477
478   procedure Restore_Config_Cunit_Boolean_Restrictions;
479   --  This restores the value saved by the previous call to save config values
480   --  saved by Save_Config_Cunit_Boolean_Restrictions. It is called at the
481   --  start of processing a new unit that is part of the main sources (e.g.
482   --  a package spec when the main unit is a package body).
483
484   procedure Reset_Cunit_Boolean_Restrictions;
485   --  Turns off all non-partition-wide boolean restrictions
486
487   procedure Add_To_Config_Boolean_Restrictions (R : Restriction_Id);
488   --  Add specified restriction to stored configuration boolean restrictions.
489   --  This is used for handling the special case of No_Elaboration_Code.
490
491private
492   type Save_Cunit_Boolean_Restrictions is
493     array (Cunit_Boolean_Restrictions) of Boolean;
494   --  Type used for saving and restoring compilation unit restrictions.
495   --  See Compilation_Unit_Restrictions_[Save|Restore] subprograms.
496
497end Restrict;
498