1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Package containing utility procedures used throughout the expander
28 with Exp_Tss
; use Exp_Tss
;
29 with Namet
; use Namet
;
30 with Rtsfind
; use Rtsfind
;
31 with Sinfo
; use Sinfo
;
32 with Types
; use Types
;
33 with Uintp
; use Uintp
;
37 -----------------------------------------------
38 -- Handling of Actions Associated with Nodes --
39 -----------------------------------------------
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.
47 -- Some cases are simple:
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.
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.
57 -- The following special cases arise:
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.
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.
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.
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.
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.
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.
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.
96 procedure Insert_Action
97 (Assoc_Node
: Node_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.
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
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.
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.
131 -- Note: If several successive calls to Insert_Action_After are made for
132 -- the same node, they will each in turn be inserted just after the node.
133 -- This means they will end up being executed in reverse order. Use the
134 -- call to Insert_Actions_After to insert a list of actions to be executed
135 -- in the sequence in which they are given in the list.
137 procedure Insert_Actions_After
138 (Assoc_Node
: Node_Id
;
139 Ins_Actions
: List_Id
);
140 -- Assoc_Node must be a node in a list. Same as Insert_Actions but
141 -- actions will be inserted after N in a manner that is compatible with
142 -- the transient scope mechanism. This procedure must be used instead
143 -- of Insert_List_After if Assoc_Node may be in a transient scope.
145 -- Implementation limitation: Assoc_Node must be a statement. We can
146 -- generalize to expressions if there is a need but this is tricky to
147 -- implement because of short-circuits (among other things).???
149 procedure Insert_Declaration
(N
: Node_Id
; Decl
: Node_Id
);
150 -- N must be a subexpression (Nkind in N_Subexpr). This is similar to
151 -- Insert_Action (N, Decl), but inserts Decl outside the expression in
152 -- which N appears. This is called Insert_Declaration because the intended
153 -- use is for declarations that have no associated code. We can't go
154 -- moving other kinds of things out of the current expression, since they
155 -- could be executed conditionally (e.g. right operand of short circuit,
156 -- or THEN/ELSE of if expression). This is currently used only in
157 -- Modify_Tree_For_C mode, where it is needed because in C we have no
158 -- way of having declarations within an expression (a really annoying
161 procedure Insert_Library_Level_Action
(N
: Node_Id
);
162 -- This procedure inserts and analyzes the node N as an action at the
163 -- library level for the current unit (i.e. it is attached to the
164 -- Actions field of the N_Compilation_Aux node for the main unit).
166 procedure Insert_Library_Level_Actions
(L
: List_Id
);
167 -- Similar, but inserts a list of actions
169 -----------------------
170 -- Other Subprograms --
171 -----------------------
173 procedure Activate_Atomic_Synchronization
(N
: Node_Id
);
174 -- N is a node for which atomic synchronization may be required (it is
175 -- either an identifier, expanded name, or selected/indexed component or
176 -- an explicit dereference). The caller has checked the basic conditions
177 -- (atomic variable appearing and Atomic_Sync not disabled). This function
178 -- checks if atomic synchronization is required and if so sets the flag
179 -- and if appropriate generates a warning (in -gnatw.n mode).
181 procedure Adjust_Condition
(N
: Node_Id
);
182 -- The node N is an expression whose root-type is Boolean, and which
183 -- represents a boolean value used as a condition (i.e. a True/False
184 -- value). This routine handles the case of C and Fortran convention
185 -- boolean types, which have zero/non-zero semantics rather than the normal
186 -- 0/1 semantics, and also the case of an enumeration rep clause that
187 -- specifies a non-standard representation. On return, node N always has
188 -- the type Standard.Boolean, with a value that is a standard Boolean
189 -- values of 0/1 for False/True. This procedure is used in two situations.
190 -- First, the processing for a condition field always calls
191 -- Adjust_Condition, so that the boolean value presented to the backend is
192 -- a standard value. Second, for the code for boolean operations such as
193 -- AND, Adjust_Condition is called on both operands, and then the operation
194 -- is done in the domain of Standard_Boolean, then Adjust_Result_Type is
195 -- called on the result to possibly reset the original type. This procedure
196 -- also takes care of validity checking if Validity_Checks = Tests.
198 procedure Adjust_Result_Type
(N
: Node_Id
; T
: Entity_Id
);
199 -- The processing of boolean operations like AND uses the procedure
200 -- Adjust_Condition so that it can operate on Standard.Boolean, which is
201 -- the only boolean type on which the backend needs to be able to implement
202 -- such operators. This means that the result is also of type
203 -- Standard.Boolean. In general the type must be reset back to the original
204 -- type to get proper semantics, and that is the purpose of this procedure.
205 -- N is the node (of type Standard.Boolean), and T is the desired type. As
206 -- an optimization, this procedure leaves the type as Standard.Boolean in
207 -- contexts where this is permissible (in particular for Condition fields,
208 -- and for operands of other logical operations higher up the tree). The
209 -- call to this procedure is completely ignored if the argument N is not of
212 procedure Append_Freeze_Action
(T
: Entity_Id
; N
: Node_Id
);
213 -- Add a new freeze action for the given type. The freeze action is
214 -- attached to the freeze node for the type. Actions will be elaborated in
215 -- the order in which they are added. Note that the added node is not
216 -- analyzed. The analyze call is found in Exp_Ch13.Expand_N_Freeze_Entity.
218 procedure Append_Freeze_Actions
(T
: Entity_Id
; L
: List_Id
);
219 -- Adds the given list of freeze actions (declarations or statements) for
220 -- the given type. The freeze actions are attached to the freeze node for
221 -- the type. Actions will be elaborated in the order in which they are
222 -- added, and the actions within the list will be elaborated in list order.
223 -- Note that the added nodes are not analyzed. The analyze call is found in
224 -- Exp_Ch13.Expand_N_Freeze_Entity.
226 procedure Build_Allocate_Deallocate_Proc
228 Is_Allocate
: Boolean);
229 -- Create a custom Allocate/Deallocate to be associated with an allocation
232 -- 1) controlled objects
233 -- 2) class-wide objects
234 -- 3) any kind of object on a subpool
236 -- N must be an allocator or the declaration of a temporary variable which
237 -- represents the expression of the original allocator node, otherwise N
238 -- must be a free statement. If flag Is_Allocate is set, the generated
239 -- routine is allocate, deallocate otherwise.
241 procedure Build_Procedure_Form
(N
: Node_Id
);
242 -- Create a procedure declaration which emulates the behavior of a function
243 -- that returns an array type, for C-compatible generation.
245 function Build_Runtime_Call
(Loc
: Source_Ptr
; RE
: RE_Id
) return Node_Id
;
246 -- Build an N_Procedure_Call_Statement calling the given runtime entity.
247 -- The call has no parameters. The first argument provides the location
248 -- information for the tree and for error messages. The call node is not
249 -- analyzed on return, the caller is responsible for analyzing it.
251 function Build_SS_Mark_Call
253 Mark
: Entity_Id
) return Node_Id
;
254 -- Build a call to routine System.Secondary_Stack.Mark. Mark denotes the
255 -- entity of the secondary stack mark.
257 function Build_SS_Release_Call
259 Mark
: Entity_Id
) return Node_Id
;
260 -- Build a call to routine System.Secondary_Stack.Release. Mark denotes the
261 -- entity of the secondary stack mark.
263 function Build_Task_Image_Decls
267 In_Init_Proc
: Boolean := False) return List_Id
;
268 -- Build declaration for a variable that holds an identifying string to be
269 -- used as a task name. Id_Ref is an identifier if the task is a variable,
270 -- and a selected or indexed component if the task is component of an
271 -- object. If it is an indexed component, A_Type is the corresponding array
272 -- type. Its index types are used to build the string as an image of the
273 -- index values. For composite types, the result includes two declarations:
274 -- one for a generated function that computes the image without using
275 -- concatenation, and one for the variable that holds the result.
277 -- If In_Init_Proc is true, the call is part of the initialization of
278 -- a component of a composite type, and the enclosing initialization
279 -- procedure must be flagged as using the secondary stack. If In_Init_Proc
280 -- is false, the call is for a stand-alone object, and the generated
281 -- function itself must do its own cleanups.
283 procedure Check_Float_Op_Overflow
(N
: Node_Id
);
284 -- Called where we could have a floating-point binary operator where we
285 -- must check for infinities if we are operating in Check_Float_Overflow
286 -- mode. Note that we don't need to worry about unary operator cases,
287 -- since for floating-point, abs, unary "-", and unary "+" can never
290 function Component_May_Be_Bit_Aligned
(Comp
: Entity_Id
) return Boolean;
291 -- This function is in charge of detecting record components that may
292 -- cause trouble in the back end if an attempt is made to assign the
293 -- component. The back end can handle such assignments with no problem if
294 -- the components involved are small (64-bits or less) records or scalar
295 -- items (including bit-packed arrays represented with modular types) or
296 -- are both aligned on a byte boundary (starting on a byte boundary, and
297 -- occupying an integral number of bytes).
299 -- However, problems arise for records larger than 64 bits, or for arrays
300 -- (other than bit-packed arrays represented with a modular type) if the
301 -- component starts on a non-byte boundary, or does not occupy an integral
302 -- number of bytes (i.e. there are some bits possibly shared with fields
303 -- at the start or beginning of the component). The back end cannot handle
304 -- loading and storing such components in a single operation.
306 -- This function is used to detect the troublesome situation. it is
307 -- conservative in the sense that it produces True unless it knows for
308 -- sure that the component is safe (as outlined in the first paragraph
309 -- above). The code generation for record and array assignment checks for
310 -- trouble using this function, and if so the assignment is generated
311 -- component-wise, which the back end is required to handle correctly.
313 -- Note that in GNAT 3, the back end will reject such components anyway,
314 -- so the hard work in checking for this case is wasted in GNAT 3, but
315 -- it is harmless, so it is easier to do it in all cases, rather than
316 -- conditionalize it in GNAT 5 or beyond.
318 function Containing_Package_With_Ext_Axioms
319 (E
: Entity_Id
) return Entity_Id
;
320 -- Returns the package entity with an external axiomatization containing E,
321 -- if any, or Empty if none.
323 procedure Convert_To_Actual_Subtype
(Exp
: Node_Id
);
324 -- The Etype of an expression is the nominal type of the expression,
325 -- not the actual subtype. Often these are the same, but not always.
326 -- For example, a reference to a formal of unconstrained type has the
327 -- unconstrained type as its Etype, but the actual subtype is obtained by
328 -- applying the actual bounds. This routine is given an expression, Exp,
329 -- and (if necessary), replaces it using Rewrite, with a conversion to
330 -- the actual subtype, building the actual subtype if necessary. If the
331 -- expression is already of the requested type, then it is unchanged.
333 function Corresponding_Runtime_Package
(Typ
: Entity_Id
) return RTU_Id
;
334 -- Return the id of the runtime package that will provide support for
335 -- concurrent type Typ. Currently only protected types are supported,
336 -- and the returned value is one of the following:
337 -- System_Tasking_Protected_Objects
338 -- System_Tasking_Protected_Objects_Entries
339 -- System_Tasking_Protected_Objects_Single_Entry
341 function Current_Sem_Unit_Declarations
return List_Id
;
342 -- Return the place where it is fine to insert declarations for the
343 -- current semantic unit. If the unit is a package body, return the
344 -- visible declarations of the corresponding spec. For RCI stubs, this
345 -- is necessary because the point at which they are generated may not
346 -- be the earliest point at which they are used.
348 function Duplicate_Subexpr
350 Name_Req
: Boolean := False;
351 Renaming_Req
: Boolean := False) return Node_Id
;
352 -- Given the node for a subexpression, this function makes a logical copy
353 -- of the subexpression, and returns it. This is intended for use when the
354 -- expansion of an expression needs to repeat part of it. For example,
355 -- replacing a**2 by a*a requires two references to a which may be a
356 -- complex subexpression. Duplicate_Subexpr guarantees not to duplicate
357 -- side effects. If necessary, it generates actions to save the expression
358 -- value in a temporary, inserting these actions into the tree using
359 -- Insert_Actions with Exp as the insertion location. The original
360 -- expression and the returned result then become references to this saved
361 -- value. Exp must be analyzed on entry. On return, Exp is analyzed, but
362 -- the caller is responsible for analyzing the returned copy after it is
363 -- attached to the tree.
365 -- The Name_Req flag is set to ensure that the result is suitable for use
366 -- in a context requiring a name (for example, the prefix of an attribute
367 -- reference) (can't this just be a qualification in Ada 2012???).
369 -- The Renaming_Req flag is set to produce an object renaming declaration
370 -- rather than an object declaration. This is valid only if the expression
371 -- Exp designates a renamable object. This is used for example in the case
372 -- of an unchecked deallocation, to make sure the object gets set to null.
374 -- Note that if there are any run time checks in Exp, these same checks
375 -- will be duplicated in the returned duplicated expression. The two
376 -- following functions allow this behavior to be modified.
378 function Duplicate_Subexpr_No_Checks
380 Name_Req
: Boolean := False;
381 Renaming_Req
: Boolean := False;
382 Related_Id
: Entity_Id
:= Empty
;
383 Is_Low_Bound
: Boolean := False;
384 Is_High_Bound
: Boolean := False) return Node_Id
;
385 -- Identical in effect to Duplicate_Subexpr, except that Remove_Checks is
386 -- called on the result, so that the duplicated expression does not include
387 -- checks. This is appropriate for use when Exp, the original expression is
388 -- unconditionally elaborated before the duplicated expression, so that
389 -- there is no need to repeat any checks.
391 -- Related_Id denotes the entity of the context where Expr appears. Flags
392 -- Is_Low_Bound and Is_High_Bound specify whether the expression to check
393 -- is the low or the high bound of a range. These three optional arguments
394 -- signal Remove_Side_Effects to create an external symbol of the form
395 -- Chars (Related_Id)_FIRST/_LAST. For suggested use of these parameters
396 -- see the warning in the body of Sem_Ch3.Process_Range_Expr_In_Decl.
398 function Duplicate_Subexpr_Move_Checks
400 Name_Req
: Boolean := False;
401 Renaming_Req
: Boolean := False) return Node_Id
;
402 -- Identical in effect to Duplicate_Subexpr, except that Remove_Checks is
403 -- called on Exp after the duplication is complete, so that the original
404 -- expression does not include checks. In this case the result returned
405 -- (the duplicated expression) will retain the original checks. This is
406 -- appropriate for use when the duplicated expression is sure to be
407 -- elaborated before the original expression Exp, so that there is no need
408 -- to repeat the checks.
410 procedure Ensure_Defined
(Typ
: Entity_Id
; N
: Node_Id
);
411 -- This procedure ensures that type referenced by Typ is defined. For the
412 -- case of a type other than an Itype, nothing needs to be done, since
413 -- all such types have declaration nodes. For Itypes, an N_Itype_Reference
414 -- node is generated and inserted as an action on node N. This is typically
415 -- used to ensure that an Itype is properly defined outside a conditional
416 -- construct when it is referenced in more than one branch.
418 function Entry_Names_OK
return Boolean;
419 -- Determine whether it is appropriate to dynamically allocate strings
420 -- which represent entry [family member] names. These strings are created
421 -- by the compiler and used by GDB.
423 procedure Evaluate_Name
(Nam
: Node_Id
);
424 -- Remove all side effects from a name which appears as part of an object
425 -- renaming declaration. More comments are needed here that explain how
426 -- this differs from Force_Evaluation and Remove_Side_Effects ???
428 procedure Evolve_And_Then
(Cond
: in out Node_Id
; Cond1
: Node_Id
);
429 -- Rewrites Cond with the expression: Cond and then Cond1. If Cond is
430 -- Empty, then simply returns Cond1 (this allows the use of Empty to
431 -- initialize a series of checks evolved by this routine, with a final
432 -- result of Empty indicating that no checks were required). The Sloc field
433 -- of the constructed N_And_Then node is copied from Cond1.
435 procedure Evolve_Or_Else
(Cond
: in out Node_Id
; Cond1
: Node_Id
);
436 -- Rewrites Cond with the expression: Cond or else Cond1. If Cond is Empty,
437 -- then simply returns Cond1 (this allows the use of Empty to initialize a
438 -- series of checks evolved by this routine, with a final result of Empty
439 -- indicating that no checks were required). The Sloc field of the
440 -- constructed N_Or_Else node is copied from Cond1.
442 procedure Expand_Static_Predicates_In_Choices
(N
: Node_Id
);
443 -- N is either a case alternative or a variant. The Discrete_Choices field
444 -- of N points to a list of choices. If any of these choices is the name
445 -- of a (statically) predicated subtype, then it is rewritten as the series
446 -- of choices that correspond to the values allowed for the subtype.
448 procedure Expand_Subtype_From_Expr
450 Unc_Type
: Entity_Id
;
451 Subtype_Indic
: Node_Id
;
453 Related_Id
: Entity_Id
:= Empty
);
454 -- Build a constrained subtype from the initial value in object
455 -- declarations and/or allocations when the type is indefinite (including
456 -- class-wide). Set Related_Id to request an external name for the subtype
457 -- rather than an internal temporary.
459 function Finalize_Address
(Typ
: Entity_Id
) return Entity_Id
;
460 -- Locate TSS primitive Finalize_Address in type Typ. Return Empty if the
461 -- subprogram is not available.
463 function Find_Interface_ADT
465 Iface
: Entity_Id
) return Elmt_Id
;
466 -- Ada 2005 (AI-251): Given a type T implementing the interface Iface,
467 -- return the element of Access_Disp_Table containing the tag of the
470 function Find_Interface_Tag
472 Iface
: Entity_Id
) return Entity_Id
;
473 -- Ada 2005 (AI-251): Given a type T implementing the interface Iface,
474 -- return the record component containing the tag of Iface.
476 function Find_Primitive_Operations
478 Name
: Name_Id
) return Node_Id
;
479 -- Return a reference to a primitive operation with given name. If
480 -- operation is overloaded, the node carries the corresponding set
481 -- of overloaded interpretations.
483 function Find_Prim_Op
(T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
;
484 -- Find the first primitive operation of a tagged type T with name Name.
485 -- This function allows the use of a primitive operation which is not
486 -- directly visible. If T is a class wide type, then the reference is to an
487 -- operation of the corresponding root type. It is an error if no primitive
488 -- operation with the given name is found.
490 function Find_Prim_Op
492 Name
: TSS_Name_Type
) return Entity_Id
;
493 -- Find the first primitive operation of type T whose name has the form
494 -- indicated by the name parameter (i.e. is a type support subprogram
495 -- with the indicated suffix). This function allows use of a primitive
496 -- operation which is not directly visible. If T is a class wide type,
497 -- then the reference is to an operation of the corresponding root type.
499 function Find_Optional_Prim_Op
500 (T
: Entity_Id
; Name
: Name_Id
) return Entity_Id
;
501 function Find_Optional_Prim_Op
503 Name
: TSS_Name_Type
) return Entity_Id
;
504 -- Same as Find_Prim_Op, except returns Empty if not found
506 function Find_Protection_Object
(Scop
: Entity_Id
) return Entity_Id
;
507 -- Traverse the scope stack starting from Scop and look for an entry, entry
508 -- family, or a subprogram that has a Protection_Object and return it. Must
509 -- always return a value since the context in which this routine is invoked
510 -- should always have a protection object.
512 function Find_Protection_Type
(Conc_Typ
: Entity_Id
) return Entity_Id
;
513 -- Given a protected type or its corresponding record, find the type of
516 function Find_Hook_Context
(N
: Node_Id
) return Node_Id
;
517 -- Determine a suitable node on which to attach actions related to N that
518 -- need to be elaborated unconditionally. In general this is the topmost
519 -- expression of which N is a subexpression, which in turn may or may not
520 -- be evaluated, for example if N is the right operand of a short circuit
523 function Following_Address_Clause
(D
: Node_Id
) return Node_Id
;
524 -- D is the node for an object declaration. This function searches the
525 -- current declarative part to look for an address clause for the object
526 -- being declared, and returns the clause if one is found, returns
529 -- Note: this function can be costly and must be invoked with special care.
530 -- Possibly we could introduce a flag at parse time indicating the presence
531 -- of an address clause to speed this up???
533 -- Note: currently this function does not scan the private part, that seems
534 -- like a potential bug ???
536 procedure Force_Evaluation
538 Name_Req
: Boolean := False;
539 Related_Id
: Entity_Id
:= Empty
;
540 Is_Low_Bound
: Boolean := False;
541 Is_High_Bound
: Boolean := False);
542 -- Force the evaluation of the expression right away. Similar behavior
543 -- to Remove_Side_Effects when Variable_Ref is set to TRUE. That is to
544 -- say, it removes the side effects and captures the values of the
545 -- variables. Remove_Side_Effects guarantees that multiple evaluations
546 -- of the same expression won't generate multiple side effects, whereas
547 -- Force_Evaluation further guarantees that all evaluations will yield
550 -- Related_Id denotes the entity of the context where Expr appears. Flags
551 -- Is_Low_Bound and Is_High_Bound specify whether the expression to check
552 -- is the low or the high bound of a range. These three optional arguments
553 -- signal Remove_Side_Effects to create an external symbol of the form
554 -- Chars (Related_Id)_FIRST/_LAST. If Related_Id is set, then exactly one
555 -- of the Is_xxx_Bound flags must be set. For use of these parameters see
556 -- the warning in the body of Sem_Ch3.Process_Range_Expr_In_Decl.
558 function Fully_Qualified_Name_String
560 Append_NUL
: Boolean := True) return String_Id
;
561 -- Generates the string literal corresponding to the fully qualified name
562 -- of entity E, in all upper case, with an ASCII.NUL appended at the end
563 -- of the name if Append_NUL is True.
565 procedure Generate_Poll_Call
(N
: Node_Id
);
566 -- If polling is active, then a call to the Poll routine is built,
567 -- and then inserted before the given node N and analyzed.
569 procedure Get_Current_Value_Condition
573 -- This routine processes the Current_Value field of the variable Var. If
574 -- the Current_Value field is null or if it represents a known value, then
575 -- on return Cond is set to N_Empty, and Val is set to Empty.
577 -- The other case is when Current_Value points to an N_If_Statement or an
578 -- N_Elsif_Part or a N_Iteration_Scheme node (see description in Einfo for
579 -- exact details). In this case, Get_Current_Condition digs out the
580 -- condition, and then checks if the condition is known false, known true,
581 -- or not known at all. In the first two cases, Get_Current_Condition will
582 -- return with Op set to the appropriate conditional operator (inverted if
583 -- the condition is known false), and Val set to the constant value. If the
584 -- condition is not known, then Op and Val are set for the empty case
585 -- (N_Empty and Empty).
587 -- The check for whether the condition is true/false unknown depends
590 -- For an IF, the condition is known true in the THEN part, known false
591 -- in any ELSIF or ELSE part, and not known outside the IF statement in
594 -- For an ELSIF, the condition is known true in the ELSIF part, known
595 -- FALSE in any subsequent ELSIF, or ELSE part, and not known before the
596 -- ELSIF, or after the end of the IF statement.
598 -- The caller can use this result to determine the value (for the case of
599 -- N_Op_Eq), or to determine the result of some other test in other cases
600 -- (e.g. no access check required if N_Op_Ne Null).
602 function Get_Stream_Size
(E
: Entity_Id
) return Uint
;
603 -- Return the stream size value of the subtype E
605 function Has_Access_Constraint
(E
: Entity_Id
) return Boolean;
606 -- Given object or type E, determine if a discriminant is of an access type
608 function Has_Annotate_Pragma_For_External_Axiomatization
609 (E
: Entity_Id
) return Boolean;
610 -- Returns whether E is a package entity, for which the initial list of
611 -- pragmas at the start of the package declaration contains
612 -- pragma Annotate (GNATprove, External_Axiomatization);
614 function Homonym_Number
(Subp
: Entity_Id
) return Nat
;
615 -- Here subp is the entity for a subprogram. This routine returns the
616 -- homonym number used to disambiguate overloaded subprograms in the same
617 -- scope (the number is used as part of constructed names to make sure that
618 -- they are unique). The number is the ordinal position on the Homonym
619 -- chain, counting only entries in the current scope. If an entity is not
620 -- overloaded, the returned number will be one.
622 function Inside_Init_Proc
return Boolean;
623 -- Returns True if current scope is within an init proc
625 function In_Library_Level_Package_Body
(Id
: Entity_Id
) return Boolean;
626 -- Given an arbitrary entity, determine whether it appears at the library
627 -- level of a package body.
629 function In_Unconditional_Context
(Node
: Node_Id
) return Boolean;
630 -- Node is the node for a statement or a component of a statement. This
631 -- function determines if the statement appears in a context that is
632 -- unconditionally executed, i.e. it is not within a loop or a conditional
633 -- or a case statement etc.
635 function Is_All_Null_Statements
(L
: List_Id
) return Boolean;
636 -- Return True if all the items of the list are N_Null_Statement nodes.
637 -- False otherwise. True for an empty list. It is an error to call this
638 -- routine with No_List as the argument.
640 function Is_Displacement_Of_Object_Or_Function_Result
641 (Obj_Id
: Entity_Id
) return Boolean;
642 -- Determine whether Obj_Id is a source entity that has been initialized by
643 -- either a controlled function call or the assignment of another source
644 -- object. In both cases the initialization expression is rewritten as a
645 -- class-wide conversion of Ada.Tags.Displace.
647 function Is_Finalizable_Transient
649 Rel_Node
: Node_Id
) return Boolean;
650 -- Determine whether declaration Decl denotes a controlled transient which
651 -- should be finalized. Rel_Node is the related context. Even though some
652 -- transients are controlled, they may act as renamings of other objects or
655 function Is_Fully_Repped_Tagged_Type
(T
: Entity_Id
) return Boolean;
656 -- Tests given type T, and returns True if T is a non-discriminated tagged
657 -- type which has a record representation clause that specifies the layout
658 -- of all the components, including recursively components in all parent
659 -- types. We exclude discriminated types for convenience, it is extremely
660 -- unlikely that the special processing associated with the use of this
661 -- routine is useful for the case of a discriminated type, and testing for
662 -- component overlap would be a pain.
664 function Is_Library_Level_Tagged_Type
(Typ
: Entity_Id
) return Boolean;
665 -- Return True if Typ is a library level tagged type. Currently we use
666 -- this information to build statically allocated dispatch tables.
668 function Is_Non_BIP_Func_Call
(Expr
: Node_Id
) return Boolean;
669 -- Determine whether node Expr denotes a non build-in-place function call
671 function Is_Object_Access_BIP_Func_Call
673 Obj_Id
: Entity_Id
) return Boolean;
674 -- Determine if Expr denotes a build-in-place function which stores its
675 -- result in the BIPaccess actual parameter whose prefix must match Obj_Id.
677 function Is_Possibly_Unaligned_Object
(N
: Node_Id
) return Boolean;
678 -- Node N is an object reference. This function returns True if it is
679 -- possible that the object may not be aligned according to the normal
680 -- default alignment requirement for its type (e.g. if it appears in a
681 -- packed record, or as part of a component that has a component clause.)
683 function Is_Possibly_Unaligned_Slice
(N
: Node_Id
) return Boolean;
684 -- Determine whether the node P is a slice of an array where the slice
685 -- result may cause alignment problems because it has an alignment that
686 -- is not compatible with the type. Return True if so.
688 function Is_Ref_To_Bit_Packed_Array
(N
: Node_Id
) return Boolean;
689 -- Determine whether the node P is a reference to a bit packed array, i.e.
690 -- whether the designated object is a component of a bit packed array, or a
691 -- subcomponent of such a component. If so, then all subscripts in P are
692 -- evaluated with a call to Force_Evaluation, and True is returned.
693 -- Otherwise False is returned, and P is not affected.
695 function Is_Ref_To_Bit_Packed_Slice
(N
: Node_Id
) return Boolean;
696 -- Determine whether the node P is a reference to a bit packed slice, i.e.
697 -- whether the designated object is bit packed slice or a component of a
698 -- bit packed slice. Return True if so.
700 function Is_Related_To_Func_Return
(Id
: Entity_Id
) return Boolean;
701 -- Determine whether object Id is related to an expanded return statement.
702 -- The case concerned is "return Id.all;".
704 function Is_Renamed_Object
(N
: Node_Id
) return Boolean;
705 -- Returns True if the node N is a renamed object. An expression is
706 -- considered to be a renamed object if either it is the Name of an object
707 -- renaming declaration, or is the prefix of a name which is a renamed
708 -- object. For example, in:
710 -- x : r renames a (1 .. 2) (1);
712 -- We consider that a (1 .. 2) is a renamed object since it is the prefix
713 -- of the name in the renaming declaration.
715 function Is_Secondary_Stack_BIP_Func_Call
(Expr
: Node_Id
) return Boolean;
716 -- Determine whether Expr denotes a build-in-place function which returns
717 -- its result on the secondary stack.
719 function Is_Tag_To_Class_Wide_Conversion
720 (Obj_Id
: Entity_Id
) return Boolean;
721 -- Determine whether object Obj_Id is the result of a tag-to-class-wide
724 function Is_Untagged_Derivation
(T
: Entity_Id
) return Boolean;
725 -- Returns true if type T is not tagged and is a derived type,
726 -- or is a private type whose completion is such a type.
728 function Is_Volatile_Reference
(N
: Node_Id
) return Boolean;
729 -- Checks if the node N represents a volatile reference, which can be
730 -- either a direct reference to a variable treated as volatile, or an
731 -- indexed/selected component where the prefix is treated as volatile,
732 -- or has Volatile_Components set. A slice of a volatile variable is
735 procedure Kill_Dead_Code
(N
: Node_Id
; Warn
: Boolean := False);
736 -- N represents a node for a section of code that is known to be dead. Any
737 -- exception handler references and warning messages relating to this code
738 -- are removed. If Warn is True, a warning will be output at the start of N
739 -- indicating the deletion of the code. Note that the tree for the deleted
740 -- code is left intact so that e.g. cross-reference data is still valid.
742 procedure Kill_Dead_Code
(L
: List_Id
; Warn
: Boolean := False);
743 -- Like the above procedure, but applies to every element in the given
744 -- list. If Warn is True, a warning will be output at the start of N
745 -- indicating the deletion of the code.
747 function Known_Non_Negative
(Opnd
: Node_Id
) return Boolean;
748 -- Given a node for a subexpression, determines if it represents a value
749 -- that cannot possibly be negative, and if so returns True. A value of
750 -- False means that it is not known if the value is positive or negative.
752 function Known_Non_Null
(N
: Node_Id
) return Boolean;
753 -- Given a node N for a subexpression of an access type, determines if
754 -- this subexpression yields a value that is known at compile time to
755 -- be non-null and returns True if so. Returns False otherwise. It is
756 -- an error to call this function if N is not of an access type.
758 function Known_Null
(N
: Node_Id
) return Boolean;
759 -- Given a node N for a subexpression of an access type, determines if this
760 -- subexpression yields a value that is known at compile time to be null
761 -- and returns True if so. Returns False otherwise. It is an error to call
762 -- this function if N is not of an access type.
764 function Make_Invariant_Call
(Expr
: Node_Id
) return Node_Id
;
765 -- Expr is an object of a type which Has_Invariants set (and which thus
766 -- also has an Invariant_Procedure set). If invariants are enabled, this
767 -- function returns a call to the Invariant procedure passing Expr as the
768 -- argument, and returns it unanalyzed. If invariants are not enabled,
769 -- returns a null statement.
771 function Make_Predicate_Call
774 Mem
: Boolean := False) return Node_Id
;
775 -- Typ is a type with Predicate_Function set. This routine builds a call to
776 -- this function passing Expr as the argument, and returns it unanalyzed.
777 -- If Mem is set True, this is the special call for the membership case,
778 -- and the function called is the Predicate_Function_M if present.
780 function Make_Predicate_Check
782 Expr
: Node_Id
) return Node_Id
;
783 -- Typ is a type with Predicate_Function set. This routine builds a Check
784 -- pragma whose first argument is Predicate, and the second argument is
785 -- a call to the predicate function of Typ with Expr as the argument. If
786 -- Predicate_Check is suppressed then a null statement is returned instead.
788 function Make_Subtype_From_Expr
791 Related_Id
: Entity_Id
:= Empty
) return Node_Id
;
792 -- Returns a subtype indication corresponding to the actual type of an
793 -- expression E. Unc_Typ is an unconstrained array or record, or a class-
794 -- wide type. Set Related_Id to request an external name for the subtype
795 -- rather than an internal temporary.
797 function Matching_Standard_Type
(Typ
: Entity_Id
) return Entity_Id
;
798 -- Given a scalar subtype Typ, returns a matching type in standard that
799 -- has the same object size value. For example, a 16 bit signed type will
800 -- typically return Standard_Short_Integer. For fixed-point types, this
801 -- will return integer types of the corresponding size.
803 function May_Generate_Large_Temp
(Typ
: Entity_Id
) return Boolean;
804 -- Determines if the given type, Typ, may require a large temporary of the
805 -- kind that causes back-end trouble if stack checking is enabled. The
806 -- result is True only the size of the type is known at compile time and
807 -- large, where large is defined heuristically by the body of this routine.
808 -- The purpose of this routine is to help avoid generating troublesome
809 -- temporaries that interfere with stack checking mechanism. Note that the
810 -- caller has to check whether stack checking is actually enabled in order
811 -- to guide the expansion (typically of a function call).
813 function Needs_Constant_Address
815 Typ
: Entity_Id
) return Boolean;
816 -- Check whether the expression in an address clause is restricted to
817 -- consist of constants, when the object has a nontrivial initialization
820 function Needs_Finalization
(T
: Entity_Id
) return Boolean;
821 -- True if type T is controlled, or has controlled subcomponents. Also
822 -- True if T is a class-wide type, because some type extension might add
823 -- controlled subcomponents, except that if pragma Restrictions
824 -- (No_Finalization) applies, this is False for class-wide types.
826 function Non_Limited_Designated_Type
(T
: Entity_Id
) return Entity_Id
;
827 -- An anonymous access type may designate a limited view. Check whether
828 -- non-limited view is available during expansion, to examine components
829 -- or other characteristics of the full type.
831 function OK_To_Do_Constant_Replacement
(E
: Entity_Id
) return Boolean;
832 -- This function is used when testing whether or not to replace a reference
833 -- to entity E by a known constant value. Such replacement must be done
834 -- only in a scope known to be safe for such replacements. In particular,
835 -- if we are within a subprogram and the entity E is declared outside the
836 -- subprogram then we cannot do the replacement, since we do not attempt to
837 -- trace subprogram call flow. It is also unsafe to replace statically
838 -- allocated values (since they can be modified outside the scope), and we
839 -- also inhibit replacement of Volatile or aliased objects since their
840 -- address might be captured in a way we do not detect. A value of True is
841 -- returned only if the replacement is safe.
843 function Possible_Bit_Aligned_Component
(N
: Node_Id
) return Boolean;
844 -- This function is used during processing the assignment of a record or
845 -- indexed component. The argument N is either the left hand or right hand
846 -- side of an assignment, and this function determines if there is a record
847 -- component reference where the record may be bit aligned in a manner that
848 -- causes trouble for the back end (see Component_May_Be_Bit_Aligned for
851 function Power_Of_Two
(N
: Node_Id
) return Nat
;
852 -- Determines if N is a known at compile time value which is of the form
853 -- 2**K, where K is in the range 1 .. M, where the Esize of N is 2**(M+1).
854 -- If so, returns the value K, otherwise returns zero. The caller checks
855 -- that N is of an integer type.
857 procedure Process_Statements_For_Controlled_Objects
(N
: Node_Id
);
858 -- N is a node which contains a non-handled statement list. Inspect the
859 -- statements looking for declarations of controlled objects. If at least
860 -- one such object is found, wrap the statement list in a block.
862 function Remove_Init_Call
864 Rep_Clause
: Node_Id
) return Node_Id
;
865 -- Look for init_proc call or aggregate initialization statements for
866 -- variable Var, either among declarations between that of Var and a
867 -- subsequent Rep_Clause applying to Var, or in the list of freeze actions
868 -- associated with Var, and if found, remove and return that call node.
870 procedure Remove_Side_Effects
872 Name_Req
: Boolean := False;
873 Renaming_Req
: Boolean := False;
874 Variable_Ref
: Boolean := False;
875 Related_Id
: Entity_Id
:= Empty
;
876 Is_Low_Bound
: Boolean := False;
877 Is_High_Bound
: Boolean := False);
878 -- Given the node for a subexpression, this function replaces the node if
879 -- necessary by an equivalent subexpression that is guaranteed to be side
880 -- effect free. This is done by extracting any actions that could cause
881 -- side effects, and inserting them using Insert_Actions into the tree
882 -- to which Exp is attached. Exp must be analyzed and resolved before the
883 -- call and is analyzed and resolved on return. Name_Req may only be set to
884 -- True if Exp has the form of a name, and the effect is to guarantee that
885 -- any replacement maintains the form of name. If Renaming_Req is set to
886 -- True, the routine produces an object renaming reclaration capturing the
887 -- expression. If Variable_Ref is set to True, a variable is considered as
888 -- side effect (used in implementing Force_Evaluation). Note: after call to
889 -- Remove_Side_Effects, it is safe to call New_Copy_Tree to obtain a copy
890 -- of the resulting expression.
892 -- Related_Id denotes the entity of the context where Expr appears. Flags
893 -- Is_Low_Bound and Is_High_Bound specify whether the expression to check
894 -- is the low or the high bound of a range. These three optional arguments
895 -- signal Remove_Side_Effects to create an external symbol of the form
896 -- Chars (Related_Id)_FIRST/_LAST. If Related_Id is set, then exactly one
897 -- of the Is_xxx_Bound flags must be set. For use of these parameters see
898 -- the warning in the body of Sem_Ch3.Process_Range_Expr_In_Decl.
900 -- The side effects are captured using one of the following methods:
902 -- 1) a constant initialized with the value of the subexpression
903 -- 2) a renaming of the subexpression
904 -- 3) a reference to the subexpression
906 -- For elementary types, methods 1) and 2) are used; for composite types,
907 -- methods 2) and 3) are used. The renaming (method 2) is used only when
908 -- the subexpression denotes a name, so that it can be elaborated by gigi
909 -- without evaluating the subexpression.
911 -- Historical note: the reference (method 3) used to be the common fallback
912 -- method but it gives rise to aliasing issues if the subexpression denotes
913 -- a name that is not aliased, since it is equivalent to taking the address
914 -- in this case. The renaming (method 2) used to be applied to any objects
915 -- in the RM sense, that is to say to the cases where a renaming is legal
916 -- in Ada. But for some of these cases, most notably functions calls, the
917 -- renaming cannot be elaborated without evaluating the subexpression, so
918 -- gigi would resort to method 1) or 3) under the hood for them.
920 function Represented_As_Scalar
(T
: Entity_Id
) return Boolean;
921 -- Returns True iff the implementation of this type in code generation
922 -- terms is scalar. This is true for scalars in the Ada sense, and for
923 -- packed arrays which are represented by a scalar (modular) type.
925 function Requires_Cleanup_Actions
927 Lib_Level
: Boolean) return Boolean;
928 -- Given a node N, determine whether its declarative and/or statement list
929 -- contains one of the following:
931 -- 1) controlled objects
932 -- 2) library-level tagged types
934 -- These cases require special actions on scope exit. The flag Lib_Level
935 -- is set True if the construct is at library level, and False otherwise.
937 function Safe_Unchecked_Type_Conversion
(Exp
: Node_Id
) return Boolean;
938 -- Given the node for an N_Unchecked_Type_Conversion, return True if this
939 -- is an unchecked conversion that Gigi can handle directly. Otherwise
940 -- return False if it is one for which the front end must provide a
941 -- temporary. Note that the node need not be analyzed, and thus the Etype
942 -- field may not be set, but in that case it must be the case that the
943 -- Subtype_Mark field of the node is set/analyzed.
945 procedure Set_Current_Value_Condition
(Cnode
: Node_Id
);
946 -- Cnode is N_If_Statement, N_Elsif_Part, or N_Iteration_Scheme (the latter
947 -- when a WHILE condition is present). This call checks whether Condition
948 -- (Cnode) has embedded expressions of a form that should result in setting
949 -- the Current_Value field of one or more entities, and if so sets these
950 -- fields to point to Cnode.
952 procedure Set_Elaboration_Flag
(N
: Node_Id
; Spec_Id
: Entity_Id
);
953 -- N is the node for a subprogram or generic body, and Spec_Id is the
954 -- entity for the corresponding spec. If an elaboration entity is defined,
955 -- then this procedure generates an assignment statement to set it True,
956 -- immediately after the body is elaborated. However, no assignment is
957 -- generated in the case of library level procedures, since the setting of
958 -- the flag in this case is generated in the binder. We do that so that we
959 -- can detect cases where this is the only elaboration action that is
962 procedure Set_Renamed_Subprogram
(N
: Node_Id
; E
: Entity_Id
);
963 -- N is an node which is an entity name that represents the name of a
964 -- renamed subprogram. The node is rewritten to be an identifier that
965 -- refers directly to the renamed subprogram, given by entity E.
967 function Side_Effect_Free
969 Name_Req
: Boolean := False;
970 Variable_Ref
: Boolean := False) return Boolean;
971 -- Determines if the tree N represents an expression that is known not
972 -- to have side effects. If this function returns True, then for example
973 -- a call to Remove_Side_Effects has no effect.
975 -- Name_Req controls the handling of volatile variable references. If
976 -- Name_Req is False (the normal case), then volatile references are
977 -- considered to be side effects. If Name_Req is True, then volatility
978 -- of variables is ignored.
980 -- If Variable_Ref is True, then all variable references are considered to
981 -- be side effects (regardless of volatility or the setting of Name_Req).
983 function Side_Effect_Free
985 Name_Req
: Boolean := False;
986 Variable_Ref
: Boolean := False) return Boolean;
987 -- Determines if all elements of the list L are side effect free. Name_Req
988 -- and Variable_Ref are as described above.
990 procedure Silly_Boolean_Array_Not_Test
(N
: Node_Id
; T
: Entity_Id
);
991 -- N is the node for a boolean array NOT operation, and T is the type of
992 -- the array. This routine deals with the silly case where the subtype of
993 -- the boolean array is False..False or True..True, where it is required
994 -- that a Constraint_Error exception be raised (RM 4.5.6(6)).
996 procedure Silly_Boolean_Array_Xor_Test
(N
: Node_Id
; T
: Entity_Id
);
997 -- N is the node for a boolean array XOR operation, and T is the type of
998 -- the array. This routine deals with the silly case where the subtype of
999 -- the boolean array is True..True, where a raise of a Constraint_Error
1000 -- exception is required (RM 4.5.6(6)).
1002 function Target_Has_Fixed_Ops
1003 (Left_Typ
: Entity_Id
;
1004 Right_Typ
: Entity_Id
;
1005 Result_Typ
: Entity_Id
) return Boolean;
1006 -- Returns True if and only if the target machine has direct support
1007 -- for fixed-by-fixed multiplications and divisions for the given
1008 -- operand and result types. This is called in package Exp_Fixd to
1009 -- determine whether to expand such operations.
1011 function Type_May_Have_Bit_Aligned_Components
1012 (Typ
: Entity_Id
) return Boolean;
1013 -- Determines if Typ is a composite type that has within it (looking down
1014 -- recursively at any subcomponents), a record type which has component
1015 -- that may be bit aligned (see Possible_Bit_Aligned_Component). The result
1016 -- is conservative, in that a result of False is decisive. A result of True
1017 -- means that such a component may or may not be present.
1019 function Within_Case_Or_If_Expression
(N
: Node_Id
) return Boolean;
1020 -- Determine whether arbitrary node N is within a case or an if expression
1022 function Within_Internal_Subprogram
return Boolean;
1023 -- Indicates that some expansion is taking place within the body of a
1024 -- predefined primitive operation. Some expansion activity (e.g. predicate
1025 -- checks) is disabled in such.
1028 pragma Inline
(Duplicate_Subexpr
);
1029 pragma Inline
(Force_Evaluation
);
1030 pragma Inline
(Is_Library_Level_Tagged_Type
);