Daily bump.
[official-gcc.git] / gcc / ada / exp_util.ads
blobf200b4bfb2fbdc2af00cc466572f1912b53c0069
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ U T I L --
6 -- --
7 -- S p e c --
8 -- --
9 -- $Revision: 1.3 $
10 -- --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- Extensive contributions were provided by Ada Core Technologies Inc. --
26 -- --
27 ------------------------------------------------------------------------------
29 -- Package containing utility procedures used throughout the expander
31 with Snames; use Snames;
32 with Rtsfind; use Rtsfind;
33 with Types; use Types;
35 package Exp_Util is
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_Conditional_Expression node rewrites the node so
76 -- that the actions can be normally positioned.
78 -- Basically what we do is to climb up to the tree looking for the
79 -- proper insertion point, as described by one of the above cases,
80 -- and then insert the appropriate action or actions.
82 -- Note if more than one insert call is made specifying the same
83 -- Assoc_Node, then the actions are elaborated in the order of the
84 -- calls, and this guarantee is preserved for the special cases above.
86 procedure Insert_Action
87 (Assoc_Node : Node_Id;
88 Ins_Action : Node_Id);
89 -- Insert the action Ins_Action at the appropriate point as described
90 -- above. The action is analyzed using the default checks after it is
91 -- inserted. Assoc_Node is the node with which the action is associated.
93 procedure Insert_Action
94 (Assoc_Node : Node_Id;
95 Ins_Action : Node_Id;
96 Suppress : Check_Id);
97 -- Insert the action Ins_Action at the appropriate point as described
98 -- above. The action is analyzed using the default checks as modified
99 -- by the given Suppress argument after it is inserted. Assoc_Node is
100 -- the node with which the action is associated.
102 procedure Insert_Actions
103 (Assoc_Node : Node_Id;
104 Ins_Actions : List_Id);
105 -- Insert the list of action Ins_Actions at the appropriate point as
106 -- described above. The actions are analyzed using the default checks
107 -- after they are inserted. Assoc_Node is the node with which the actions
108 -- are associated. Ins_Actions may be No_List, in which case the call has
109 -- no effect.
111 procedure Insert_Actions
112 (Assoc_Node : Node_Id;
113 Ins_Actions : List_Id;
114 Suppress : Check_Id);
115 -- Insert the list of action Ins_Actions at the appropriate point as
116 -- described above. The actions are analyzed using the default checks
117 -- as modified by the given Suppress argument after they are inserted.
118 -- Assoc_Node is the node with which the actions are associated.
119 -- Ins_Actions may be No_List, in which case the call has no effect.
121 procedure Insert_Actions_After
122 (Assoc_Node : Node_Id;
123 Ins_Actions : List_Id);
124 -- Assoc_Node must be a node in a list. Same as Insert_Actions but
125 -- actions will be inserted after N in a manner that is compatible with
126 -- the transient scope mechanism. This procedure must be used instead
127 -- of Insert_List_After if Assoc_Node may be in a transient scope.
129 -- Implementation limitation: Assoc_Node must be a statement. We can
130 -- generalize to expressions if there is a need but this is tricky to
131 -- implement because of short-ciruits (among other things).???
133 procedure Insert_Library_Level_Action (N : Node_Id);
134 -- This procedure inserts and analyzes the node N as an action at the
135 -- library level for the current unit (i.e. it is attached to the
136 -- Actions field of the N_Compilation_Aux node for the main unit).
138 procedure Insert_Library_Level_Actions (L : List_Id);
139 -- Similar, but inserts a list of actions.
141 -----------------------
142 -- Other Subprograms --
143 -----------------------
145 procedure Adjust_Condition (N : Node_Id);
146 -- The node N is an expression whose root-type is Boolean, and which
147 -- represents a boolean value used as a condition (i.e. a True/False
148 -- value). This routine handles the case of C and Fortran convention
149 -- boolean types, which have zero/non-zero semantics rather than the
150 -- normal 0/1 semantics, and also the case of an enumeration rep
151 -- clause that specifies a non-standard representation. On return,
152 -- node N always has the type Standard.Boolean, with a value that
153 -- is a standard Boolean values of 0/1 for False/True. This procedure
154 -- is used in two situations. First, the processing for a condition
155 -- field always calls Adjust_Condition, so that the boolean value
156 -- presented to the backend is a standard value. Second, for the
157 -- code for boolean operations such as AND, Adjust_Condition is
158 -- called on both operands, and then the operation is done in the
159 -- domain of Standard_Boolean, then Adjust_Result_Type is called
160 -- on the result to possibly reset the original type. This procedure
161 -- also takes care of validity checking if Validity_Checks = Tests.
163 procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id);
164 -- The processing of boolean operations like AND uses the procedure
165 -- Adjust_Condition so that it can operate on Standard.Boolean, which
166 -- is the only boolean type on which the backend needs to be able to
167 -- implement such operators. This means that the result is also of
168 -- type Standard.Boolean. In general the type must be reset back to
169 -- the original type to get proper semantics, and that is the purpose
170 -- of this procedure. N is the node (of type Standard.Boolean), and
171 -- T is the desired type. As an optimization, this procedure leaves
172 -- the type as Standard.Boolean in contexts where this is permissible
173 -- (in particular for Condition fields, and for operands of other
174 -- logical operations higher up the tree). The call to this procedure
175 -- is completely ignored if the argument N is not of type Boolean.
177 procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id);
178 -- Add a new freeze action for the given type. The freeze action is
179 -- attached to the freeze node for the type. Actions will be elaborated
180 -- in the order in which they are added. Note that the added node is not
181 -- analyzed. The analyze call is found in Sem_Ch13.Expand_N_Freeze_Entity.
183 procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id);
184 -- Adds the given list of freeze actions (declarations or statements)
185 -- for the given type. The freeze actions are attached to the freeze
186 -- node for the type. Actions will be elaborated in the order in which
187 -- they are added, and the actions within the list will be elaborated in
188 -- list order. Note that the added nodes are not analyzed. The analyze
189 -- call is found in Sem_Ch13.Expand_N_Freeze_Entity.
191 function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id;
192 -- Build an N_Procedure_Call_Statement calling the given runtime entity.
193 -- The call has no parameters. The first argument provides the location
194 -- information for the tree and for error messages. The call node is not
195 -- analyzed on return, the caller is responsible for analyzing it.
197 function Build_Task_Image_Decls
198 (Loc : Source_Ptr;
199 Id_Ref : Node_Id;
200 A_Type : Entity_Id)
201 return List_Id;
202 -- Build declaration for a variable that holds an identifying string
203 -- to be used as a task name. Id_Ref is an identifier if the task is
204 -- a variable, and a selected or indexed component if the task is a
205 -- component of an object. If it is an indexed component, A_Type is
206 -- the corresponding array type. Its index types are used to build the
207 -- string as an image of the index values. For composite types, the
208 -- result includes two declarations: one for a generated function that
209 -- computes the image without using concatenation, and one for the
210 -- variable that holds the result.
212 procedure Convert_To_Actual_Subtype (Exp : Node_Id);
213 -- The Etype of an expression is the nominal type of the expression,
214 -- not the actual subtype. Often these are the same, but not always.
215 -- For example, a reference to a formal of unconstrained type has the
216 -- unconstrained type as its Etype, but the actual subtype is obtained
217 -- by applying the actual bounds. This routine is given an expression,
218 -- Exp, and (if necessary), replaces it using Rewrite, with a conversion
219 -- to the actual subtype, building the actual subtype if necessary. If
220 -- the expression is already of the requested type, then it is unchanged.
222 function Current_Sem_Unit_Declarations return List_Id;
223 -- Return the a place where it is fine to insert declarations for the
224 -- current semantic unit. If the unit is a package body, return the
225 -- visible declarations of the corresponding spec. For RCI stubs, this
226 -- is necessary because the point at which they are generated may not
227 -- be the earliest point at which they are used.
229 function Duplicate_Subexpr
230 (Exp : Node_Id;
231 Name_Req : Boolean := False)
232 return Node_Id;
233 -- Given the node for a subexpression, this function makes a logical
234 -- copy of the subexpression, and returns it. This is intended for use
235 -- when the expansion of an expression needs to repeat part of it. For
236 -- example, replacing a**2 by a*a requires two references to a which
237 -- may be a complex subexpression. Duplicate_Subexpression guarantees
238 -- not to duplicate side effects. If necessary, it generates actions
239 -- to save the expression value in a temporary, inserting these actions
240 -- into the tree using Insert_Actions with Exp as the insertion location.
241 -- The original expression and the returned result then become references
242 -- to this saved value. Exp must be analyzed on entry. On return, Exp
243 -- is analyzed, but the caller is responsible for analyzing the returned
244 -- copy after it is attached to the tree. The Name_Req flag is set to
245 -- ensure that the result is suitable for use in a context requiring a
246 -- name (e.g. the prefix of an attribute reference).
248 procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id);
249 -- This procedure ensures that type referenced by Typ is defined. For the
250 -- case of a type other than an Itype, nothing needs to be done, since
251 -- all such types have declaration nodes. For Itypes, an N_Itype_Reference
252 -- node is generated and inserted at the given node N. This is typically
253 -- used to ensure that an Itype is properly defined outside a conditional
254 -- construct when it is referenced in more than one branch.
256 procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id);
257 -- Rewrites Cond with the expression: Cond and then Cond1. If Cond is
258 -- Empty, then simply returns Cond1 (this allows the use of Empty to
259 -- initialize a series of checks evolved by this routine, with a final
260 -- result of Empty indicating that no checks were required). The Sloc
261 -- field of the constructed N_And_Then node is copied from Cond1.
263 procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id);
264 -- Rewrites Cond with the expression: Cond or else Cond1. If Cond is
265 -- Empty, then simply returns Cond1 (this allows the use of Empty to
266 -- initialize a series of checks evolved by this routine, with a final
267 -- result of Empty indicating that no checks were required). The Sloc
268 -- field of the constructed N_And_Then node is copied from Cond1.
270 procedure Expand_Subtype_From_Expr
271 (N : Node_Id;
272 Unc_Type : Entity_Id;
273 Subtype_Indic : Node_Id;
274 Exp : Node_Id);
275 -- Build a constrained subtype from the initial value in object
276 -- declarations and/or allocations when the type is indefinite (including
277 -- class-wide).
279 function Find_Prim_Op (T : Entity_Id; Name : Name_Id) return Entity_Id;
280 -- Find the first primitive operation of type T whose name is 'Name'.
281 -- this function allows the use of a primitive operation which is not
282 -- directly visible
284 procedure Force_Evaluation
285 (Exp : Node_Id;
286 Name_Req : Boolean := False);
287 -- Force the evaluation of the expression right away. Similar behavior
288 -- to Remove_Side_Effects when Variable_Ref is set to TRUE. That is to
289 -- say, it removes the side-effects and capture the values of the
290 -- variables. Remove_Side_effects guarantees that multiple evaluations
291 -- of the same expression won't generate multiple side effects, whereas
292 -- Force_Evaluation further guarantees that all evaluations will yield
293 -- the same result.
295 procedure Generate_Poll_Call (N : Node_Id);
296 -- If polling is active, then a call to the Poll routine is built,
297 -- and then inserted before the given node N and analyzed.
299 function Homonym_Number (Subp : Entity_Id) return Nat;
300 -- Here subp is the entity for a subprogram. This routine returns the
301 -- homonym number used to disambiguate overloaded subprograms in the
302 -- same scope (the number is used as part of constructed names to make
303 -- sure that they are unique). The number is the ordinal position on
304 -- the Homonym chain, counting only entries in the curren scope. If
305 -- an entity is not overloaded, the returned number will be one.
307 function Inside_Init_Proc return Boolean;
308 -- Returns True if current scope is within an Init_Proc
310 function In_Unconditional_Context (Node : Node_Id) return Boolean;
311 -- Node is the node for a statement or a component of a statement.
312 -- This function deteermines if the statement appears in a context
313 -- that is unconditionally executed, i.e. it is not within a loop
314 -- or a conditional or a case statement etc.
316 function Is_Ref_To_Bit_Packed_Array (P : Node_Id) return Boolean;
317 -- Determine whether the node P is a reference to a bit packed
318 -- array, i.e. whether the designated object is a component of
319 -- a bit packed array, or a subcomponent of such a component.
320 -- If so, then all subscripts in P are evaluated with a call
321 -- to Force_Evaluation, and True is returned. Otherwise False
322 -- is returned, and P is not affected.
324 function Is_Ref_To_Bit_Packed_Slice (P : Node_Id) return Boolean;
325 -- Determine whether the node P is a reference to a bit packed
326 -- slice, i.e. whether the designated object is bit packed slice
327 -- or a component of a bit packed slice. Return True if so.
329 function Is_Renamed_Object (N : Node_Id) return Boolean;
330 -- Returns True if the node N is a renamed object. An expression
331 -- is considered to be a renamed object if either it is the Name
332 -- of an object renaming declaration, or is the prefix of a name
333 -- which is a renamed object. For example, in:
335 -- x : r renames a (1 .. 2) (1);
337 -- We consider that a (1 .. 2) is a renamed object since it is the
338 -- prefix of the name in the renaming declaration.
340 function Is_Untagged_Derivation (T : Entity_Id) return Boolean;
341 -- Returns true if type T is not tagged and is a derived type,
342 -- or is a private type whose completion is such a type.
344 procedure Kill_Dead_Code (N : Node_Id);
345 -- N represents a node for a section of code that is known to be
346 -- dead. The node is deleted, and any exception handler references
347 -- and warning messages relating to this code are removed.
349 procedure Kill_Dead_Code (L : List_Id);
350 -- Like the above procedure, but applies to every element in the given
351 -- list. Each of the entries is removed from the list before killing it.
353 function Known_Non_Negative (Opnd : Node_Id) return Boolean;
354 -- Given a node for a subexpression, determines if it represents a value
355 -- that cannot possibly be negative, and if so returns True. A value of
356 -- False means that it is not known if the value is positive or negative.
358 function Make_Subtype_From_Expr
359 (E : Node_Id;
360 Unc_Typ : Entity_Id)
361 return Node_Id;
362 -- Returns a subtype indication corresponding to the actual type of an
363 -- expression E. Unc_Typ is an unconstrained array or record, or
364 -- a classwide type.
366 function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean;
367 -- Determines if the given type, Typ, may require a large temporary
368 -- of the type that causes trouble if stack checking is enabled. The
369 -- result is True only if stack checking is enabled and the size of
370 -- the type is known at compile time and large, where large is defined
371 -- hueristically by the body of this routine. The purpose of this
372 -- routine is to help avoid generating troublesome temporaries that
373 -- intefere with the stack checking mechanism.
375 procedure Remove_Side_Effects
376 (Exp : Node_Id;
377 Name_Req : Boolean := False;
378 Variable_Ref : Boolean := False);
379 -- Given the node for a subexpression, this function replaces the node
380 -- if necessary by an equivalent subexpression that is guaranteed to be
381 -- side effect free. This is done by extracting any actions that could
382 -- cause side effects, and inserting them using Insert_Actions into the
383 -- tree to which Exp is attached. Exp must be analayzed and resolved
384 -- before the call and is analyzed and resolved on return. The Name_Req
385 -- may only be set to True if Exp has the form of a name, and the
386 -- effect is to guarantee that any replacement maintains the form of a
387 -- name. If Variable_Ref is set to TRUE, a variable is considered as a
388 -- side effect (used in implementing Force_Evaluation). Note: after a
389 -- call to Remove_Side_Effects, it is safe to use a call to
390 -- New_Copy_Tree to obtain a copy of the resulting expression.
392 function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean;
393 -- Given the node for an N_Unchecked_Type_Conversion, return True
394 -- if this is an unchecked conversion that Gigi can handle directly.
395 -- Otherwise return False if it is one for which the front end must
396 -- provide a temporary. Note that the node need not be analyzed, and
397 -- thus the Etype field may not be set, but in that case it must be
398 -- the case that the Subtype_Mark field of the node is set/analyzed.
400 procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id);
401 -- N is the node for a subprogram or generic body, and Spec_Id
402 -- is the entity for the corresponding spec. If an elaboration
403 -- entity is defined, then this procedure generates an assignment
404 -- statement to set it True, immediately after the body is elaborated.
405 -- However, no assignment is generated in the case of library level
406 -- procedures, since the setting of the flag in this case is generated
407 -- in the binder. We do that so that we can detect cases where this is
408 -- the only elaboration action that is required.
410 procedure Wrap_Cleanup_Procedure (N : Node_Id);
411 -- Given an N_Subprogram_Body node, this procedure adds an Abort_Defer
412 -- call at the start of the statement sequence, and an Abort_Undefer call
413 -- at the end of the statement sequence. All cleanup routines (i.e. those
414 -- that are called from "at end" handlers) must defer abort on entry and
415 -- undefer abort on exit. Note that it is assumed that the code for the
416 -- procedure does not contain any return statements which would allow the
417 -- flow of control to escape doing the undefer call.
419 private
420 pragma Inline (Force_Evaluation);
421 pragma Inline (Duplicate_Subexpr);
423 end Exp_Util;