1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             E X P _ U T I L                              --
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--  Package containing utility procedures used throughout the expander
27
28with Exp_Tss; use Exp_Tss;
29with Namet;   use Namet;
30with Rtsfind; use Rtsfind;
31with Sinfo;   use Sinfo;
32with Types;   use Types;
33with Uintp;   use Uintp;
34
35package Exp_Util is
36
37   -----------------------------------------------
38   -- Handling of Actions Associated with Nodes --
39   -----------------------------------------------
40
41   --  The evaluation of certain expression nodes involves the elaboration
42   --  of associated types and other declarations, and the execution of
43   --  statement sequences. Expansion routines generating such actions must
44   --  find an appropriate place in the tree to hang the actions so that
45   --  they will be evaluated at the appropriate point.
46
47   --  Some cases are simple:
48
49   --    For an expression occurring in a simple statement that is in a list
50   --    of statements, the actions are simply inserted into the list before
51   --    the associated statement.
52
53   --    For an expression occurring in a declaration (declarations always
54   --    appear in lists), the actions are similarly inserted into the list
55   --    just before the associated declaration.
56
57   --  The following special cases arise:
58
59   --    For actions associated with the right operand of a short circuit
60   --    form, the actions are first stored in the short circuit form node
61   --    in the Actions field. The expansion of these forms subsequently
62   --    expands the short circuit forms into if statements which can then
63   --    be moved as described above.
64
65   --    For actions appearing in the Condition expression of a while loop,
66   --    or an elsif clause, the actions are similarly temporarily stored in
67   --    in the node (N_Elsif_Part or N_Iteration_Scheme) associated with
68   --    the expression using the Condition_Actions field. Subsequently, the
69   --    expansion of these nodes rewrites the control structures involved to
70   --    reposition the actions in normal statement sequence.
71
72   --    For actions appearing in the then or else expression of a conditional
73   --    expression, these actions are similarly placed in the node, using the
74   --    Then_Actions or Else_Actions field as appropriate. Once again the
75   --    expansion of the N_If_Expression node rewrites the node so that the
76   --    actions can be positioned normally.
77
78   --    For actions coming from expansion of the expression in an expression
79   --    with actions node, the action is appended to the list of actions.
80
81   --  Basically what we do is to climb up to the tree looking for the
82   --  proper insertion point, as described by one of the above cases,
83   --  and then insert the appropriate action or actions.
84
85   --  Note if more than one insert call is made specifying the same
86   --  Assoc_Node, then the actions are elaborated in the order of the
87   --  calls, and this guarantee is preserved for the special cases above.
88
89   procedure Insert_Action
90     (Assoc_Node : Node_Id;
91      Ins_Action : Node_Id);
92   --  Insert the action Ins_Action at the appropriate point as described
93   --  above. The action is analyzed using the default checks after it is
94   --  inserted. Assoc_Node is the node with which the action is associated.
95
96   procedure Insert_Action
97     (Assoc_Node : Node_Id;
98      Ins_Action : Node_Id;
99      Suppress   : Check_Id);
100   --  Insert the action Ins_Action at the appropriate point as described
101   --  above. The action is analyzed using the default checks as modified
102   --  by the given Suppress argument after it is inserted. Assoc_Node is
103   --  the node with which the action is associated.
104
105   procedure Insert_Actions
106     (Assoc_Node  : Node_Id;
107      Ins_Actions : List_Id);
108   --  Insert the list of action Ins_Actions at the appropriate point as
109   --  described above. The actions are analyzed using the default checks
110   --  after they are inserted. Assoc_Node is the node with which the actions
111   --  are associated. Ins_Actions may be No_List, in which case the call has
112   --  no effect.
113
114   procedure Insert_Actions
115     (Assoc_Node  : Node_Id;
116      Ins_Actions : List_Id;
117      Suppress    : Check_Id);
118   --  Insert the list of action Ins_Actions at the appropriate point as
119   --  described above. The actions are analyzed using the default checks
120   --  as modified by the given Suppress argument after they are inserted.
121   --  Assoc_Node is the node with which the actions are associated.
122   --  Ins_Actions may be No_List, in which case the call has no effect.
123
124   procedure Insert_Action_After
125     (Assoc_Node : Node_Id;
126      Ins_Action : Node_Id);
127   --  Assoc_Node must be a node in a list. Same as Insert_Action but the
128   --  action will be inserted after N in a manner that is compatible with
129   --  the transient scope mechanism.
130
131   procedure Insert_Actions_After
132     (Assoc_Node  : Node_Id;
133      Ins_Actions : List_Id);
134   --  Assoc_Node must be a node in a list. Same as Insert_Actions but
135   --  actions will be inserted after N in a manner that is compatible with
136   --  the transient scope mechanism. This procedure must be used instead
137   --  of Insert_List_After if Assoc_Node may be in a transient scope.
138   --
139   --  Implementation limitation: Assoc_Node must be a statement. We can
140   --  generalize to expressions if there is a need but this is tricky to
141   --  implement because of short-circuits (among other things).???
142
143   procedure Insert_Declaration (N : Node_Id; Decl : Node_Id);
144   --  N must be a subexpression (Nkind in N_Subexpr). This is similar to
145   --  Insert_Action (N, Decl), but inserts Decl outside the expression in
146   --  which N appears. This is called Insert_Declaration because the intended
147   --  use is for declarations that have no associated code. We can't go
148   --  moving other kinds of things out of the current expression, since they
149   --  could be executed conditionally (e.g. right operand of short circuit,
150   --  or THEN/ELSE of if expression). This is currently used only in
151   --  Modify_Tree_For_C mode, where it is needed because in C we have no
152   --  way of having declarations within an expression (a really annoying
153   --  limitation).
154
155   procedure Insert_Library_Level_Action (N : Node_Id);
156   --  This procedure inserts and analyzes the node N as an action at the
157   --  library level for the current unit (i.e. it is attached to the
158   --  Actions field of the N_Compilation_Aux node for the main unit).
159
160   procedure Insert_Library_Level_Actions (L : List_Id);
161   --  Similar, but inserts a list of actions
162
163   -----------------------
164   -- Other Subprograms --
165   -----------------------
166
167   procedure Activate_Atomic_Synchronization (N : Node_Id);
168   --  N is a node for which atomic synchronization may be required (it is
169   --  either an identifier, expanded name, or selected/indexed component or
170   --  an explicit dereference). The caller has checked the basic conditions
171   --  (atomic variable appearing and Atomic_Sync not disabled). This function
172   --  checks if atomic synchronization is required and if so sets the flag
173   --  and if appropriate generates a warning (in -gnatw.n mode).
174
175   procedure Adjust_Condition (N : Node_Id);
176   --  The node N is an expression whose root-type is Boolean, and which
177   --  represents a boolean value used as a condition (i.e. a True/False
178   --  value). This routine handles the case of C and Fortran convention
179   --  boolean types, which have zero/non-zero semantics rather than the normal
180   --  0/1 semantics, and also the case of an enumeration rep clause that
181   --  specifies a non-standard representation. On return, node N always has
182   --  the type Standard.Boolean, with a value that is a standard Boolean
183   --  values of 0/1 for False/True. This procedure is used in two situations.
184   --  First, the processing for a condition field always calls
185   --  Adjust_Condition, so that the boolean value presented to the backend is
186   --  a standard value. Second, for the code for boolean operations such as
187   --  AND, Adjust_Condition is called on both operands, and then the operation
188   --  is done in the domain of Standard_Boolean, then Adjust_Result_Type is
189   --  called on the result to possibly reset the original type. This procedure
190   --  also takes care of validity checking if Validity_Checks = Tests.
191
192   procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id);
193   --  The processing of boolean operations like AND uses the procedure
194   --  Adjust_Condition so that it can operate on Standard.Boolean, which is
195   --  the only boolean type on which the backend needs to be able to implement
196   --  such operators. This means that the result is also of type
197   --  Standard.Boolean. In general the type must be reset back to the original
198   --  type to get proper semantics, and that is the purpose of this procedure.
199   --  N is the node (of type Standard.Boolean), and T is the desired type. As
200   --  an optimization, this procedure leaves the type as Standard.Boolean in
201   --  contexts where this is permissible (in particular for Condition fields,
202   --  and for operands of other logical operations higher up the tree). The
203   --  call to this procedure is completely ignored if the argument N is not of
204   --  type Boolean.
205
206   procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id);
207   --  Add a new freeze action for the given type. The freeze action is
208   --  attached to the freeze node for the type. Actions will be elaborated in
209   --  the order in which they are added. Note that the added node is not
210   --  analyzed. The analyze call is found in Exp_Ch13.Expand_N_Freeze_Entity.
211
212   procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id);
213   --  Adds the given list of freeze actions (declarations or statements) for
214   --  the given type. The freeze actions are attached to the freeze node for
215   --  the type. Actions will be elaborated in the order in which they are
216   --  added, and the actions within the list will be elaborated in list order.
217   --  Note that the added nodes are not analyzed. The analyze call is found in
218   --  Exp_Ch13.Expand_N_Freeze_Entity.
219
220   procedure Build_Allocate_Deallocate_Proc
221     (N           : Node_Id;
222      Is_Allocate : Boolean);
223   --  Create a custom Allocate/Deallocate to be associated with an allocation
224   --  or deallocation:
225   --
226   --    1) controlled objects
227   --    2) class-wide objects
228   --    3) any kind of object on a subpool
229   --
230   --  N must be an allocator or the declaration of a temporary variable which
231   --  represents the expression of the original allocator node, otherwise N
232   --  must be a free statement. If flag Is_Allocate is set, the generated
233   --  routine is allocate, deallocate otherwise.
234
235   function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id;
236   --  Build an N_Procedure_Call_Statement calling the given runtime entity.
237   --  The call has no parameters. The first argument provides the location
238   --  information for the tree and for error messages. The call node is not
239   --  analyzed on return, the caller is responsible for analyzing it.
240
241   function Build_Task_Image_Decls
242     (Loc          : Source_Ptr;
243      Id_Ref       : Node_Id;
244      A_Type       : Entity_Id;
245      In_Init_Proc : Boolean := False) return List_Id;
246   --  Build declaration for a variable that holds an identifying string to be
247   --  used as a task name. Id_Ref is an identifier if the task is a variable,
248   --  and a selected or indexed component if the task is component of an
249   --  object. If it is an indexed component, A_Type is the corresponding array
250   --  type. Its index types are used to build the string as an image of the
251   --  index values. For composite types, the result includes two declarations:
252   --  one for a generated function that computes the image without using
253   --  concatenation, and one for the variable that holds the result.
254   --
255   --  If In_Init_Proc is true, the call is part of the initialization of
256   --  a component of a composite type, and the enclosing initialization
257   --  procedure must be flagged as using the secondary stack. If In_Init_Proc
258   --  is false, the call is for a stand-alone object, and the generated
259   --  function itself must do its own cleanups.
260
261   function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean;
262   --  This function is in charge of detecting record components that may
263   --  cause trouble in the back end if an attempt is made to assign the
264   --  component. The back end can handle such assignments with no problem if
265   --  the components involved are small (64-bits or less) records or scalar
266   --  items (including bit-packed arrays represented with modular types) or
267   --  are both aligned on a byte boundary (starting on a byte boundary, and
268   --  occupying an integral number of bytes).
269   --
270   --  However, problems arise for records larger than 64 bits, or for arrays
271   --  (other than bit-packed arrays represented with a modular type) if the
272   --  component starts on a non-byte boundary, or does not occupy an integral
273   --  number of bytes (i.e. there are some bits possibly shared with fields
274   --  at the start or beginning of the component). The back end cannot handle
275   --  loading and storing such components in a single operation.
276   --
277   --  This function is used to detect the troublesome situation. it is
278   --  conservative in the sense that it produces True unless it knows for
279   --  sure that the component is safe (as outlined in the first paragraph
280   --  above). The code generation for record and array assignment checks for
281   --  trouble using this function, and if so the assignment is generated
282   --  component-wise, which the back end is required to handle correctly.
283   --
284   --  Note that in GNAT 3, the back end will reject such components anyway,
285   --  so the hard work in checking for this case is wasted in GNAT 3, but
286   --  it is harmless, so it is easier to do it in all cases, rather than
287   --  conditionalize it in GNAT 5 or beyond.
288
289   procedure Convert_To_Actual_Subtype (Exp : Node_Id);
290   --  The Etype of an expression is the nominal type of the expression,
291   --  not the actual subtype. Often these are the same, but not always.
292   --  For example, a reference to a formal of unconstrained type has the
293   --  unconstrained type as its Etype, but the actual subtype is obtained by
294   --  applying the actual bounds. This routine is given an expression, Exp,
295   --  and (if necessary), replaces it using Rewrite, with a conversion to
296   --  the actual subtype, building the actual subtype if necessary. If the
297   --  expression is already of the requested type, then it is unchanged.
298
299   function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id;
300   --  Return the id of the runtime package that will provide support for
301   --  concurrent type Typ. Currently only protected types are supported,
302   --  and the returned value is one of the following:
303   --    System_Tasking_Protected_Objects
304   --    System_Tasking_Protected_Objects_Entries
305   --    System_Tasking_Protected_Objects_Single_Entry
306
307   function Current_Sem_Unit_Declarations return List_Id;
308   --  Return the place where it is fine to insert declarations for the
309   --  current semantic unit. If the unit is a package body, return the
310   --  visible declarations of the corresponding spec. For RCI stubs, this
311   --  is necessary because the point at which they are generated may not
312   --  be the earliest point at which they are used.
313
314   function Duplicate_Subexpr
315     (Exp      : Node_Id;
316      Name_Req : Boolean := False) return Node_Id;
317   --  Given the node for a subexpression, this function makes a logical copy
318   --  of the subexpression, and returns it. This is intended for use when the
319   --  expansion of an expression needs to repeat part of it. For example,
320   --  replacing a**2 by a*a requires two references to a which may be a
321   --  complex subexpression. Duplicate_Subexpr guarantees not to duplicate
322   --  side effects. If necessary, it generates actions to save the expression
323   --  value in a temporary, inserting these actions into the tree using
324   --  Insert_Actions with Exp as the insertion location. The original
325   --  expression and the returned result then become references to this saved
326   --  value. Exp must be analyzed on entry. On return, Exp is analyzed, but
327   --  the caller is responsible for analyzing the returned copy after it is
328   --  attached to the tree. The Name_Req flag is set to ensure that the result
329   --  is suitable for use in a context requiring name (e.g. the prefix of an
330   --  attribute reference).
331   --
332   --  Note that if there are any run time checks in Exp, these same checks
333   --  will be duplicated in the returned duplicated expression. The two
334   --  following functions allow this behavior to be modified.
335
336   function Duplicate_Subexpr_No_Checks
337     (Exp      : Node_Id;
338      Name_Req : Boolean := False) return Node_Id;
339   --  Identical in effect to Duplicate_Subexpr, except that Remove_Checks
340   --  is called on the result, so that the duplicated expression does not
341   --  include checks. This is appropriate for use when Exp, the original
342   --  expression is unconditionally elaborated before the duplicated
343   --  expression, so that there is no need to repeat any checks.
344
345   function Duplicate_Subexpr_Move_Checks
346     (Exp      : Node_Id;
347      Name_Req : Boolean := False) return Node_Id;
348   --  Identical in effect to Duplicate_Subexpr, except that Remove_Checks is
349   --  called on Exp after the duplication is complete, so that the original
350   --  expression does not include checks. In this case the result returned
351   --  (the duplicated expression) will retain the original checks. This is
352   --  appropriate for use when the duplicated expression is sure to be
353   --  elaborated before the original expression Exp, so that there is no need
354   --  to repeat the checks.
355
356   procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id);
357   --  This procedure ensures that type referenced by Typ is defined. For the
358   --  case of a type other than an Itype, nothing needs to be done, since
359   --  all such types have declaration nodes. For Itypes, an N_Itype_Reference
360   --  node is generated and inserted as an action on node N. This is typically
361   --  used to ensure that an Itype is properly defined outside a conditional
362   --  construct when it is referenced in more than one branch.
363
364   function Entry_Names_OK return Boolean;
365   --  Determine whether it is appropriate to dynamically allocate strings
366   --  which represent entry [family member] names. These strings are created
367   --  by the compiler and used by GDB.
368
369   procedure Evaluate_Name (Nam : Node_Id);
370   --  Remove all side effects from a name which appears as part of an object
371   --  renaming declaration. More comments are needed here that explain how
372   --  this differs from Force_Evaluation and Remove_Side_Effects ???
373
374   procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id);
375   --  Rewrites Cond with the expression: Cond and then Cond1. If Cond is
376   --  Empty, then simply returns Cond1 (this allows the use of Empty to
377   --  initialize a series of checks evolved by this routine, with a final
378   --  result of Empty indicating that no checks were required). The Sloc field
379   --  of the constructed N_And_Then node is copied from Cond1.
380
381   procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id);
382   --  Rewrites Cond with the expression: Cond or else Cond1. If Cond is Empty,
383   --  then simply returns Cond1 (this allows the use of Empty to initialize a
384   --  series of checks evolved by this routine, with a final result of Empty
385   --  indicating that no checks were required). The Sloc field of the
386   --  constructed N_Or_Else node is copied from Cond1.
387
388   procedure Expand_Static_Predicates_In_Choices (N : Node_Id);
389   --  N is either a case alternative or a variant. The Discrete_Choices field
390   --  of N points to a list of choices. If any of these choices is the name
391   --  of a (statically) predicated subtype, then it is rewritten as the series
392   --  of choices that correspond to the values allowed for the subtype.
393
394   procedure Expand_Subtype_From_Expr
395     (N             : Node_Id;
396      Unc_Type      : Entity_Id;
397      Subtype_Indic : Node_Id;
398      Exp           : Node_Id);
399   --  Build a constrained subtype from the initial value in object
400   --  declarations and/or allocations when the type is indefinite (including
401   --  class-wide).
402
403   function Find_Interface_ADT
404     (T     : Entity_Id;
405      Iface : Entity_Id) return Elmt_Id;
406   --  Ada 2005 (AI-251): Given a type T implementing the interface Iface,
407   --  return the element of Access_Disp_Table containing the tag of the
408   --  interface.
409
410   function Find_Interface_Tag
411     (T     : Entity_Id;
412      Iface : Entity_Id) return Entity_Id;
413   --  Ada 2005 (AI-251): Given a type T implementing the interface Iface,
414   --  return the record component containing the tag of Iface.
415
416   function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id;
417   --  Find the first primitive operation of type T whose name is 'Name'.
418   --  This function allows the use of a primitive operation which is not
419   --  directly visible. If T is a class wide type, then the reference is
420   --  to an operation of the corresponding root type. Raises Program_Error
421   --  exception if no primitive operation is found. This is normally an
422   --  internal error, but in some cases is an expected consequence of
423   --  illegalities elsewhere.
424
425   function Find_Prim_Op
426     (T    : Entity_Id;
427      Name : TSS_Name_Type) return Entity_Id;
428   --  Find the first primitive operation of type T whose name has the form
429   --  indicated by the name parameter (i.e. is a type support subprogram
430   --  with the indicated suffix). This function allows use of a primitive
431   --  operation which is not directly visible. If T is a class wide type,
432   --  then the reference is to an operation of the corresponding root type.
433   --  Raises Program_Error exception if no primitive operation is found.
434   --  This is normally an internal error, but in some cases is an expected
435   --  consequence of illegalities elsewhere.
436
437   function Find_Protection_Object (Scop : Entity_Id) return Entity_Id;
438   --  Traverse the scope stack starting from Scop and look for an entry,
439   --  entry family, or a subprogram that has a Protection_Object and return
440   --  it. Raises Program_Error if no such entity is found since the context
441   --  in which this routine is invoked should always have a protection
442   --  object.
443
444   function Find_Protection_Type (Conc_Typ : Entity_Id) return Entity_Id;
445   --  Given a protected type or its corresponding record, find the type of
446   --  field _object.
447
448   procedure Force_Evaluation
449     (Exp      : Node_Id;
450      Name_Req : Boolean := False);
451   --  Force the evaluation of the expression right away. Similar behavior
452   --  to Remove_Side_Effects when Variable_Ref is set to TRUE. That is to
453   --  say, it removes the side-effects and captures the values of the
454   --  variables. Remove_Side_Effects guarantees that multiple evaluations
455   --  of the same expression won't generate multiple side effects, whereas
456   --  Force_Evaluation further guarantees that all evaluations will yield
457   --  the same result.
458
459   function Fully_Qualified_Name_String
460     (E          : Entity_Id;
461      Append_NUL : Boolean := True) return String_Id;
462   --  Generates the string literal corresponding to the fully qualified name
463   --  of entity E, in all upper case, with an ASCII.NUL appended at the end
464   --  of the name if Append_NUL is True.
465
466   procedure Generate_Poll_Call (N : Node_Id);
467   --  If polling is active, then a call to the Poll routine is built,
468   --  and then inserted before the given node N and analyzed.
469
470   procedure Get_Current_Value_Condition
471     (Var : Node_Id;
472      Op  : out Node_Kind;
473      Val : out Node_Id);
474   --  This routine processes the Current_Value field of the variable Var. If
475   --  the Current_Value field is null or if it represents a known value, then
476   --  on return Cond is set to N_Empty, and Val is set to Empty.
477   --
478   --  The other case is when Current_Value points to an N_If_Statement or an
479   --  N_Elsif_Part or a N_Iteration_Scheme node (see description in Einfo for
480   --  exact details). In this case, Get_Current_Condition digs out the
481   --  condition, and then checks if the condition is known false, known true,
482   --  or not known at all. In the first two cases, Get_Current_Condition will
483   --  return with Op set to the appropriate conditional operator (inverted if
484   --  the condition is known false), and Val set to the constant value. If the
485   --  condition is not known, then Op and Val are set for the empty case
486   --  (N_Empty and Empty).
487   --
488   --  The check for whether the condition is true/false unknown depends
489   --  on the case:
490   --
491   --     For an IF, the condition is known true in the THEN part, known false
492   --     in any ELSIF or ELSE part, and not known outside the IF statement in
493   --     question.
494   --
495   --     For an ELSIF, the condition is known true in the ELSIF part, known
496   --     FALSE in any subsequent ELSIF, or ELSE part, and not known before the
497   --     ELSIF, or after the end of the IF statement.
498   --
499   --  The caller can use this result to determine the value (for the case of
500   --  N_Op_Eq), or to determine the result of some other test in other cases
501   --  (e.g. no access check required if N_Op_Ne Null).
502
503   function Get_Stream_Size (E : Entity_Id) return Uint;
504   --  Return the stream size value of the subtype E
505
506   function Has_Access_Constraint (E : Entity_Id) return Boolean;
507   --  Given object or type E, determine if a discriminant is of an access type
508
509   function Has_Following_Address_Clause (D : Node_Id) return Boolean;
510   --  D is the node for an object declaration. This function searches the
511   --  current declarative part to look for an address clause for the object
512   --  being declared, and returns True if one is found.
513
514   function Homonym_Number (Subp : Entity_Id) return Nat;
515   --  Here subp is the entity for a subprogram. This routine returns the
516   --  homonym number used to disambiguate overloaded subprograms in the same
517   --  scope (the number is used as part of constructed names to make sure that
518   --  they are unique). The number is the ordinal position on the Homonym
519   --  chain, counting only entries in the current scope. If an entity is not
520   --  overloaded, the returned number will be one.
521
522   function Inside_Init_Proc return Boolean;
523   --  Returns True if current scope is within an init proc
524
525   function In_Library_Level_Package_Body (Id : Entity_Id) return Boolean;
526   --  Given an arbitrary entity, determine whether it appears at the library
527   --  level of a package body.
528
529   function In_Unconditional_Context (Node : Node_Id) return Boolean;
530   --  Node is the node for a statement or a component of a statement. This
531   --  function determines if the statement appears in a context that is
532   --  unconditionally executed, i.e. it is not within a loop or a conditional
533   --  or a case statement etc.
534
535   function Is_All_Null_Statements (L : List_Id) return Boolean;
536   --  Return True if all the items of the list are N_Null_Statement nodes.
537   --  False otherwise. True for an empty list. It is an error to call this
538   --  routine with No_List as the argument.
539
540   function Is_Displacement_Of_Object_Or_Function_Result
541     (Obj_Id : Entity_Id) return Boolean;
542   --  Determine whether Obj_Id is a source entity that has been initialized by
543   --  either a controlled function call or the assignment of another source
544   --  object. In both cases the initialization expression is rewritten as a
545   --  class-wide conversion of Ada.Tags.Displace.
546
547   function Is_Finalizable_Transient
548     (Decl     : Node_Id;
549      Rel_Node : Node_Id) return Boolean;
550   --  Determine whether declaration Decl denotes a controlled transient which
551   --  should be finalized. Rel_Node is the related context. Even though some
552   --  transient are controlled, they may act as renamings of other objects or
553   --  function calls.
554
555   function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean;
556   --  Tests given type T, and returns True if T is a non-discriminated tagged
557   --  type which has a record representation clause that specifies the layout
558   --  of all the components, including recursively components in all parent
559   --  types. We exclude discriminated types for convenience, it is extremely
560   --  unlikely that the special processing associated with the use of this
561   --  routine is useful for the case of a discriminated type, and testing for
562   --  component overlap would be a pain.
563
564   function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean;
565   --  Return True if Typ is a library level tagged type. Currently we use
566   --  this information to build statically allocated dispatch tables.
567
568   function Is_Non_BIP_Func_Call (Expr : Node_Id) return Boolean;
569   --  Determine whether node Expr denotes a non build-in-place function call
570
571   function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean;
572   --  Node N is an object reference. This function returns True if it is
573   --  possible that the object may not be aligned according to the normal
574   --  default alignment requirement for its type (e.g. if it appears in a
575   --  packed record, or as part of a component that has a component clause.)
576
577   function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean;
578   --  Determine whether the node P is a slice of an array where the slice
579   --  result may cause alignment problems because it has an alignment that
580   --  is not compatible with the type. Return True if so.
581
582   function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean;
583   --  Determine whether the node P is a reference to a bit packed array, i.e.
584   --  whether the designated object is a component of a bit packed array, or a
585   --  subcomponent of such a component. If so, then all subscripts in P are
586   --  evaluated with a call to Force_Evaluation, and True is returned.
587   --  Otherwise False is returned, and P is not affected.
588
589   function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean;
590   --  Determine whether the node P is a reference to a bit packed slice, i.e.
591   --  whether the designated object is bit packed slice or a component of a
592   --  bit packed slice. Return True if so.
593
594   function Is_Related_To_Func_Return (Id : Entity_Id) return Boolean;
595   --  Determine whether object Id is related to an expanded return statement.
596   --  The case concerned is "return Id.all;".
597
598   function Is_Renamed_Object (N : Node_Id) return Boolean;
599   --  Returns True if the node N is a renamed object. An expression is
600   --  considered to be a renamed object if either it is the Name of an object
601   --  renaming declaration, or is the prefix of a name which is a renamed
602   --  object. For example, in:
603   --
604   --     x : r renames a (1 .. 2) (1);
605   --
606   --  We consider that a (1 .. 2) is a renamed object since it is the prefix
607   --  of the name in the renaming declaration.
608
609   function Is_Secondary_Stack_BIP_Func_Call (Expr : Node_Id) return Boolean;
610   --  Determine whether Expr denotes a build-in-place function which returns
611   --  its result on the secondary stack.
612
613   function Is_Tag_To_Class_Wide_Conversion
614     (Obj_Id : Entity_Id) return Boolean;
615   --  Determine whether object Obj_Id is the result of a tag-to-class-wide
616   --  type conversion.
617
618   function Is_Untagged_Derivation (T : Entity_Id) return Boolean;
619   --  Returns true if type T is not tagged and is a derived type,
620   --  or is a private type whose completion is such a type.
621
622   function Is_Volatile_Reference (N : Node_Id) return Boolean;
623   --  Checks if the node N represents a volatile reference, which can be
624   --  either a direct reference to a variable treated as volatile, or an
625   --  indexed/selected component where the prefix is treated as volatile,
626   --  or has Volatile_Components set. A slice of a volatile variable is
627   --  also volatile.
628
629   function Is_VM_By_Copy_Actual (N : Node_Id) return Boolean;
630   --  Returns True if we are compiling on VM targets and N is a node that
631   --  requires pass-by-copy in these targets.
632
633   procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False);
634   --  N represents a node for a section of code that is known to be dead. Any
635   --  exception handler references and warning messages relating to this code
636   --  are removed. If Warn is True, a warning will be output at the start of N
637   --  indicating the deletion of the code. Note that the tree for the deleted
638   --  code is left intact so that e.g. cross-reference data is still valid.
639
640   procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False);
641   --  Like the above procedure, but applies to every element in the given
642   --  list. If Warn is True, a warning will be output at the start of N
643   --  indicating the deletion of the code.
644
645   function Known_Non_Negative (Opnd : Node_Id) return Boolean;
646   --  Given a node for a subexpression, determines if it represents a value
647   --  that cannot possibly be negative, and if so returns True. A value of
648   --  False means that it is not known if the value is positive or negative.
649
650   function Known_Non_Null (N : Node_Id) return Boolean;
651   --  Given a node N for a subexpression of an access type, determines if
652   --  this subexpression yields a value that is known at compile time to
653   --  be non-null and returns True if so. Returns False otherwise. It is
654   --  an error to call this function if N is not of an access type.
655
656   function Known_Null (N : Node_Id) return Boolean;
657   --  Given a node N for a subexpression of an access type, determines if this
658   --  subexpression yields a value that is known at compile time to be null
659   --  and returns True if so. Returns False otherwise. It is an error to call
660   --  this function if N is not of an access type.
661
662   function Make_Invariant_Call (Expr : Node_Id) return Node_Id;
663   --  Expr is an object of a type which Has_Invariants set (and which thus
664   --  also has an Invariant_Procedure set). If invariants are enabled, this
665   --  function returns a call to the Invariant procedure passing Expr as the
666   --  argument, and returns it unanalyzed. If invariants are not enabled,
667   --  returns a null statement.
668
669   function Make_Predicate_Call
670     (Typ  : Entity_Id;
671      Expr : Node_Id;
672      Mem  : Boolean := False) return Node_Id;
673   --  Typ is a type with Predicate_Function set. This routine builds a call to
674   --  this function passing Expr as the argument, and returns it unanalyzed.
675   --  If Mem is set True, this is the special call for the membership case,
676   --  and the function called is the Predicate_Function_M if present.
677
678   function Make_Predicate_Check
679     (Typ  : Entity_Id;
680      Expr : Node_Id) return Node_Id;
681   --  Typ is a type with Predicate_Function set. This routine builds a Check
682   --  pragma whose first argument is Predicate, and the second argument is
683   --  a call to the predicate function of Typ with Expr as the argument. If
684   --  Predicate_Check is suppressed then a null statement is returned instead.
685
686   function Make_Subtype_From_Expr
687     (E       : Node_Id;
688      Unc_Typ : Entity_Id) return Node_Id;
689   --  Returns a subtype indication corresponding to the actual type of an
690   --  expression E. Unc_Typ is an unconstrained array or record, or
691   --  a classwide type.
692
693   function Matching_Standard_Type (Typ : Entity_Id) return Entity_Id;
694   --  Given a scalar subtype Typ, returns a matching type in standard that
695   --  has the same object size value. For example, a 16 bit signed type will
696   --  typically return Standard_Short_Integer. For fixed-point types, this
697   --  will return integer types of the corresponding size.
698
699   function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean;
700   --  Determines if the given type, Typ, may require a large temporary of the
701   --  kind that causes back-end trouble if stack checking is enabled. The
702   --  result is True only the size of the type is known at compile time and
703   --  large, where large is defined heuristically by the body of this routine.
704   --  The purpose of this routine is to help avoid generating troublesome
705   --  temporaries that interfere with stack checking mechanism. Note that the
706   --  caller has to check whether stack checking is actually enabled in order
707   --  to guide the expansion (typically of a function call).
708
709   function Needs_Constant_Address
710     (Decl : Node_Id;
711      Typ  : Entity_Id) return Boolean;
712   --  Check whether the expression in an address clause is restricted to
713   --  consist of constants, when the object has a non-trivial initialization
714   --  or is controlled.
715
716   function Needs_Finalization (T : Entity_Id) return Boolean;
717   --  True if type T is controlled, or has controlled subcomponents. Also
718   --  True if T is a class-wide type, because some type extension might add
719   --  controlled subcomponents, except that if pragma Restrictions
720   --  (No_Finalization) applies, this is False for class-wide types.
721
722   function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id;
723   --  An anonymous access type may designate a limited view. Check whether
724   --  non-limited view is available during expansion, to examine components
725   --  or other characteristics of the full type.
726
727   function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean;
728   --  This function is used when testing whether or not to replace a reference
729   --  to entity E by a known constant value. Such replacement must be done
730   --  only in a scope known to be safe for such replacements. In particular,
731   --  if we are within a subprogram and the entity E is declared outside the
732   --  subprogram then we cannot do the replacement, since we do not attempt to
733   --  trace subprogram call flow. It is also unsafe to replace statically
734   --  allocated values (since they can be modified outside the scope), and we
735   --  also inhibit replacement of Volatile or aliased objects since their
736   --  address might be captured in a way we do not detect. A value of True is
737   --  returned only if the replacement is safe.
738
739   function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean;
740   --  This function is used during processing the assignment of a record or
741   --  indexed component. The argument N is either the left hand or right hand
742   --  side of an assignment, and this function determines if there is a record
743   --  component reference where the record may be bit aligned in a manner that
744   --  causes trouble for the back end (see Component_May_Be_Bit_Aligned for
745   --  further details).
746
747   function Power_Of_Two (N : Node_Id) return Nat;
748   --  Determines if N is a known at compile time value which  is of the form
749   --  2**K, where K is in the range 1 .. M, where the Esize of N is 2**(M+1).
750   --  If so, returns the value K, otherwise returns zero. The caller checks
751   --  that N is of an integer type.
752
753   procedure Process_Statements_For_Controlled_Objects (N : Node_Id);
754   --  N is a node which contains a non-handled statement list. Inspect the
755   --  statements looking for declarations of controlled objects. If at least
756   --  one such object is found, wrap the statement list in a block.
757
758   function Remove_Init_Call
759     (Var        : Entity_Id;
760      Rep_Clause : Node_Id) return Node_Id;
761   --  Look for init_proc call or aggregate initialization statements for
762   --  variable Var, either among declarations between that of Var and a
763   --  subsequent Rep_Clause applying to Var, or in the list of freeze actions
764   --  associated with Var, and if found, remove and return that call node.
765
766   procedure Remove_Side_Effects
767     (Exp          : Node_Id;
768      Name_Req     : Boolean := False;
769      Variable_Ref : Boolean := False);
770   --  Given the node for a subexpression, this function replaces the node if
771   --  necessary by an equivalent subexpression that is guaranteed to be side
772   --  effect free. This is done by extracting any actions that could cause
773   --  side effects, and inserting them using Insert_Actions into the tree
774   --  to which Exp is attached. Exp must be analyzed and resolved before the
775   --  call and is analyzed and resolved on return. Name_Req may only be set to
776   --  True if Exp has the form of a name, and the effect is to guarantee that
777   --  any replacement maintains the form of name. If Variable_Ref is set to
778   --  TRUE, a variable is considered as side effect (used in implementing
779   --  Force_Evaluation). Note: after call to Remove_Side_Effects, it is
780   --  safe to call New_Copy_Tree to obtain a copy of the resulting expression.
781
782   function Represented_As_Scalar (T : Entity_Id) return Boolean;
783   --  Returns True iff the implementation of this type in code generation
784   --  terms is scalar. This is true for scalars in the Ada sense, and for
785   --  packed arrays which are represented by a scalar (modular) type.
786
787   function Requires_Cleanup_Actions
788     (N         : Node_Id;
789      Lib_Level : Boolean) return Boolean;
790   --  Given a node N, determine whether its declarative and/or statement list
791   --  contains one of the following:
792   --
793   --    1) controlled objects
794   --    2) library-level tagged types
795   --
796   --  These cases require special actions on scope exit. The flag Lib_Level
797   --  is set True if the construct is at library level, and False otherwise.
798
799   function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean;
800   --  Given the node for an N_Unchecked_Type_Conversion, return True if this
801   --  is an unchecked conversion that Gigi can handle directly. Otherwise
802   --  return False if it is one for which the front end must provide a
803   --  temporary. Note that the node need not be analyzed, and thus the Etype
804   --  field may not be set, but in that case it must be the case that the
805   --  Subtype_Mark field of the node is set/analyzed.
806
807   procedure Set_Current_Value_Condition (Cnode : Node_Id);
808   --  Cnode is N_If_Statement, N_Elsif_Part, or N_Iteration_Scheme (the latter
809   --  when a WHILE condition is present). This call checks whether Condition
810   --  (Cnode) has embedded expressions of a form that should result in setting
811   --  the Current_Value field of one or more entities, and if so sets these
812   --  fields to point to Cnode.
813
814   procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id);
815   --  N is the node for a subprogram or generic body, and Spec_Id is the
816   --  entity for the corresponding spec. If an elaboration entity is defined,
817   --  then this procedure generates an assignment statement to set it True,
818   --  immediately after the body is elaborated. However, no assignment is
819   --  generated in the case of library level procedures, since the setting of
820   --  the flag in this case is generated in the binder. We do that so that we
821   --  can detect cases where this is the only elaboration action that is
822   --  required.
823
824   procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id);
825   --  N is an node which is an entity name that represents the name of a
826   --  renamed subprogram. The node is rewritten to be an identifier that
827   --  refers directly to the renamed subprogram, given by entity E.
828
829   function Side_Effect_Free
830     (N            : Node_Id;
831      Name_Req     : Boolean := False;
832      Variable_Ref : Boolean := False) return Boolean;
833   --  Determines if the tree N represents an expression that is known not
834   --  to have side effects. If this function returns True, then for example
835   --  a call to Remove_Side_Effects has no effect.
836   --
837   --  Name_Req controls the handling of volatile variable references. If
838   --  Name_Req is False (the normal case), then volatile references are
839   --  considered to be side effects. If Name_Req is True, then volatility
840   --  of variables is ignored.
841   --
842   --  If Variable_Ref is True, then all variable references are considered to
843   --  be side effects (regardless of volatility or the setting of Name_Req).
844
845   function Side_Effect_Free
846     (L            : List_Id;
847      Name_Req     : Boolean := False;
848      Variable_Ref : Boolean := False) return Boolean;
849   --  Determines if all elements of the list L are side effect free. Name_Req
850   --  and Variable_Ref are as described above.
851
852   procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id);
853   --  N is the node for a boolean array NOT operation, and T is the type of
854   --  the array. This routine deals with the silly case where the subtype of
855   --  the boolean array is False..False or True..True, where it is required
856   --  that a Constraint_Error exception be raised (RM 4.5.6(6)).
857
858   procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id);
859   --  N is the node for a boolean array XOR operation, and T is the type of
860   --  the array. This routine deals with the silly case where the subtype of
861   --  the boolean array is True..True, where a raise of a Constraint_Error
862   --  exception is required (RM 4.5.6(6)).
863
864   function Target_Has_Fixed_Ops
865     (Left_Typ   : Entity_Id;
866      Right_Typ  : Entity_Id;
867      Result_Typ : Entity_Id) return Boolean;
868   --  Returns True if and only if the target machine has direct support
869   --  for fixed-by-fixed multiplications and divisions for the given
870   --  operand and result types. This is called in package Exp_Fixd to
871   --  determine whether to expand such operations.
872
873   function Type_May_Have_Bit_Aligned_Components
874     (Typ : Entity_Id) return Boolean;
875   --  Determines if Typ is a composite type that has within it (looking down
876   --  recursively at any subcomponents), a record type which has component
877   --  that may be bit aligned (see Possible_Bit_Aligned_Component). The result
878   --  is conservative, in that a result of False is decisive. A result of True
879   --  means that such a component may or may not be present.
880
881   function Within_Case_Or_If_Expression (N : Node_Id) return Boolean;
882   --  Determine whether arbitrary node N is within a case or an if expression
883
884   function Within_Internal_Subprogram return Boolean;
885   --  Indicates that some expansion is taking place within the body of a
886   --  predefined primitive operation. Some expansion activity (e.g. predicate
887   --  checks) is disabled in such.
888
889   procedure Wrap_Cleanup_Procedure (N : Node_Id);
890   --  Given an N_Subprogram_Body node, this procedure adds an Abort_Defer call
891   --  at the start of the statement sequence, and an Abort_Undefer call at the
892   --  end of the statement sequence. All cleanup routines (i.e. those that are
893   --  called from "at end" handlers) must defer abort on entry and undefer
894   --  abort on exit. Note that it is assumed that the code for the procedure
895   --  does not contain any return statements which would allow the flow of
896   --  control to escape doing the undefer call.
897
898private
899   pragma Inline (Duplicate_Subexpr);
900   pragma Inline (Force_Evaluation);
901   pragma Inline (Is_Library_Level_Tagged_Type);
902end Exp_Util;
903