Debian Codegraph @

gcc/ada/atree.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A T R E E --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 -- This package defines the low-level representation of the tree used to
27 -- represent the Ada program internally. Syntactic and semantic information
28 -- is combined in this tree. There is no separate symbol table structure.
29
30 -- WARNING: There is a C++ version of this package. Any changes to this source
31 -- file must be properly reflected in the C++ header file atree.h.
32
33 -- Package Atree defines the basic structure of the tree and its nodes and
34 -- provides the basic abstract interface for manipulating the tree. Two other
35 -- packages use this interface to define the representation of Ada programs
36 -- using this tree format. The package Sinfo defines the basic representation
37 -- of the syntactic structure of the program, as output by the parser. The
38 -- package Einfo defines the semantic information that is added to the tree
39 -- nodes that represent declared entities (i.e. the information that is
40 -- described in a separate symbol table structure in some other compilers).
41
42 -- The front end of the compiler first parses the program and generates a
43 -- tree that is simply a syntactic representation of the program in abstract
44 -- syntax tree format. Subsequent processing in the front end traverses the
45 -- tree, transforming it in various ways and adding semantic information.
46
47 with Alloc;
48 with Sinfo.Nodes; use Sinfo.Nodes;
49 with Einfo.Entities; use Einfo.Entities;
50 with Einfo.Utils; use Einfo.Utils;
51 with Types; use Types;
52 with Seinfo; use Seinfo;
53 with System; use System;
54 with Table;
55
56 package Atree is
57
58 -- Access to node fields is generally done through the getters and setters
59 -- in packages Sinfo.Nodes and Einfo.Entities, which are automatically
60 -- generated (see Gen_IL.Gen). However, in specialized circumstances
61 -- (examples are the circuit in generic instantiation to copy trees, and in
62 -- the tree dump routine), it is useful to be able to do untyped
63 -- traversals, and an internal package in Atree allows for direct untyped
64 -- accesses in such cases.
65
66 function Last_Node_Id return Node_Id;
67 -- Returns Id of last allocated node Id
68
69 function Node_Offsets_Address return System.Address;
70 function Slots_Address return System.Address;
71 -- Address of Node_Offsets.Table and Slots.Table. Used in Back_End for Gigi
72 -- call.
73
74 function Approx_Num_Nodes_And_Entities return Nat;
75 -- This is an approximation to the number of nodes and entities allocated,
76 -- used to determine sizes of hash tables.
77
78 -----------------------
79 -- Use of Empty Node --
80 -----------------------
81
82 -- The special Node_Id Empty is used to mark missing fields, similar to
83 -- "null" in Ada. Whenever the syntax has an optional component, then the
84 -- corresponding field will be set to Empty if the component is missing.
85
86 -- Note: Empty is not used to describe an empty list. Instead in this
87 -- case the node field contains a list which is empty, and these cases
88 -- should be distinguished (essentially from a type point of view, Empty
89 -- is a Node, not a list).
90
91 -- Note: Empty does in fact correspond to an allocated node. The Nkind
92 -- field of this node may be referenced. It contains N_Empty, which
93 -- uniquely identifies the empty case. This allows the Nkind field to be
94 -- dereferenced before the check for Empty which is sometimes useful. We
95 -- also access certain other fields of Empty; see comments in
96 -- Gen_IL.Gen.Gen_Nodes.
97
98 -----------------------
99 -- Use of Error Node --
100 -----------------------
101
102 -- The Error node is used during syntactic and semantic analysis to
103 -- indicate that the corresponding piece of syntactic structure or
104 -- semantic meaning cannot properly be represented in the tree because
105 -- of an illegality in the program.
106
107 -- If an Error node is encountered, then you know that a previous
108 -- illegality has been detected. The proper reaction should be to
109 -- avoid posting related cascaded error messages, and to propagate
110 -- the Error node if necessary.
111
112 ------------------------
113 -- Current_Error_Node --
114 ------------------------
115
116 -- Current_Error_Node is a global variable indicating the current node
117 -- that is being processed for the purposes of placing a compiler
118 -- abort message. This is not necessarily perfectly accurate, it is
119 -- just a reasonably accurate best guess. It is used to output the
120 -- source location in the abort message by Comperr, and also to
121 -- implement the d3 debugging flag.
122
123 -- There are two ways this gets set. During parsing, when new source
124 -- nodes are being constructed by calls to New_Node and New_Entity,
125 -- either one of these calls sets Current_Error_Node to the newly
126 -- created node. During semantic analysis, this mechanism is not
127 -- used, and instead Current_Error_Node is set by the subprograms in
128 -- Debug_A that mark the start and end of analysis/expansion of a
129 -- node in the tree.
130
131 -- Current_Error_Node is also used for other purposes. See, for example,
132 -- Rtsfind.
133
134 Current_Error_Node : Node_Id := Empty;
135 -- Node to place compiler abort messages
136
137 ------------------
138 -- Error Counts --
139 ------------------
140
141 -- The following variables denote the count of errors of various kinds
142 -- detected in the tree. Note that these might be more logically located in
143 -- Err_Vars, but we put it here to deal with licensing issues (we need this
144 -- to have the GPL exception licensing, since Check_Error_Detected can be
145 -- called from units with this licensing).
146
147 Serious_Errors_Detected : Nat := 0;
148 -- This is a count of errors that are serious enough to stop expansion,
149 -- and hence to prevent generation of an object file even if the
150 -- switch -gnatQ is set. Initialized to zero at the start of compilation.
151
152 -- WARNING: There is a matching C declaration of this variable in fe.h
153
154 Total_Errors_Detected : Nat := 0;
155 -- Number of errors detected so far. Includes count of serious errors and
156 -- non-serious errors, so this value is always greater than or equal to the
157 -- Serious_Errors_Detected value. Initialized to zero at the start of
158 -- compilation.
159
160 Warnings_Detected : Nat := 0;
161 -- Number of warnings detected. Initialized to zero at the start of
162 -- compilation. This count includes the count of style and info messages.
163
164 Info_Messages : Nat := 0;
165 -- Number of info messages generated as reports. Info messages are never
166 -- treated as errors (whether from use of the pragma, or the compiler
167 -- switch -gnatwe). Used by GNATprove under SPARK_Mode to report proved
168 -- checks.
169
170 Check_Messages : Nat := 0;
171 -- Number of check messages generated. Check messages are neither warnings
172 -- nor errors.
173
174 Warnings_Treated_As_Errors : Nat := 0;
175 -- Number of warnings changed into errors as a result of matching a pattern
176 -- given in a Warning_As_Error configuration pragma.
177
178 Configurable_Run_Time_Violations : Nat := 0;
179 -- Count of configurable run time violations so far. This is used to
180 -- suppress certain cascaded error messages when we know that we may not
181 -- have fully expanded some items, due to high integrity violations (e.g.
182 -- the use of constructs not permitted by the library in use, or improper
183 -- constructs in No_Run_Time mode).
184
185 procedure Check_Error_Detected;
186 -- When an anomaly is found in the tree, many semantic routines silently
187 -- bail out, assuming that the anomaly was caused by a previously detected
188 -- serious error (or configurable run time violation). This routine should
189 -- be called in these cases, and will raise an exception if no such error
190 -- has been detected. This ensures that the anomaly is never allowed to go
191 -- unnoticed in legal programs.
192
193 --------------------------------------------------
194 -- Node Allocation and Modification Subprograms --
195 --------------------------------------------------
196
197 -- The following subprograms are used for constructing the tree in the
198 -- first place, and then for subsequent modifications as required.
199
200 procedure Initialize;
201 -- Called at the start of compilation to make the entries for Empty and
202 -- Error.
203
204 procedure Lock;
205 -- Called before the back end is invoked to lock the nodes table.
206 -- Also called after Unlock to relock.
207
208 procedure Unlock;
209 -- Unlocks nodes table, in cases where the back end needs to modify it
210
211 procedure Lock_Nodes;
212 -- Called to lock node modifications when assertions are enabled; without
213 -- assertions calling this subprogram has no effect. The initial state of
214 -- the lock is unlocked.
215
216 procedure Unlock_Nodes;
217 -- Called to unlock node modifications when assertions are enabled; if
218 -- assertions are not enabled calling this subprogram has no effect.
219
220 function Is_Entity (N : Node_Or_Entity_Id) return Boolean;
221 pragma Inline (Is_Entity);
222 -- Returns True if N is an entity
223
224 function Is_Syntactic_Node
225 (Source : Node_Id;
226 Field : Node_Id)
227 return Boolean;
228 -- Return True when Field is a syntactic child of node Source. It is called
229 -- when creating a copy of Source to preserve the Parent link in the copy
230 -- of Field.
231
232 function New_Node
233 (New_Node_Kind : Node_Kind;
234 New_Sloc : Source_Ptr) return Node_Id;
235 -- Allocates a new node with the given node type and source location
236 -- values. Fields have defaults depending on their type:
237
238 -- Flag: False
239 -- Node_Id: Empty
240 -- List_Id: Empty
241 -- Elist_Id: No_Elist
242 -- Uint: No_Uint
243 --
244 -- Name_Id, String_Id, Valid_Uint, Unat, Upos, Nonzero_Uint, Ureal:
245 -- No default. This means it is an error to call the getter before
246 -- calling the setter.
247 --
248 -- The usual approach is to build a new node using this function and
249 -- then, using the value returned, use the Set_xxx functions to set
250 -- fields of the node as required. New_Node can only be used for
251 -- non-entity nodes.
252 --
253 -- If we are currently parsing, as indicated by a previous call to
254 -- Set_Comes_From_Source_Default (True), then this call also resets
255 -- the value of Current_Error_Node.
256
257 function New_Entity
258 (New_Node_Kind : Node_Kind;
259 New_Sloc : Source_Ptr) return Entity_Id;
260 -- Similar to New_Node, except that it is used only for entity nodes.
261
262 procedure Set_Comes_From_Source_Default (Default : Boolean);
263 -- Sets value of Comes_From_Source flag to be used in all subsequent
264 -- New_Node and New_Entity calls until another call to this procedure
265 -- changes the default. This value is set True during parsing and
266 -- False during semantic analysis. This is also used to determine
267 -- if New_Node and New_Entity should set Current_Error_Node.
268
269 function Get_Comes_From_Source_Default return Boolean;
270 pragma Inline (Get_Comes_From_Source_Default);
271 -- Gets the current value of the Comes_From_Source flag
272
273 procedure Preserve_Comes_From_Source (NewN, OldN : Node_Id);
274 pragma Inline (Preserve_Comes_From_Source);
275 -- When a node is rewritten, it is sometimes appropriate to preserve the
276 -- original comes from source indication. This is true when the rewrite
277 -- essentially corresponds to a transformation corresponding exactly to
278 -- semantics in the reference manual. This procedure copies the setting
279 -- of Comes_From_Source from OldN to NewN.
280
281 procedure Change_Node (N : Node_Id; New_Kind : Node_Kind);
282 -- This procedure replaces the given node by setting its Nkind field to the
283 -- indicated value and resetting all other fields to their default values
284 -- except for certain fields that are preserved (see body for details).
285
286 procedure Copy_Node (Source, Destination : Node_Or_Entity_Id);
287 -- Copy the entire contents of the source node to the destination node.
288 -- The contents of the source node is not affected. If the source node
289 -- has an extension, then the destination must have an extension also.
290 -- The parent pointer of the destination and its list link, if any, are
291 -- not affected by the copy. Note that parent pointers of descendants
292 -- are not adjusted, so the descendants of the destination node after
293 -- the Copy_Node is completed have dubious parent pointers. Note that
294 -- this routine does NOT copy aspect specifications, the Has_Aspects
295 -- flag in the returned node will always be False. The caller must deal
296 -- with copying aspect specifications where this is required.
297
298 function New_Copy (Source : Node_Id) return Node_Id;
299 -- This function allocates a new node, and then initializes it by copying
300 -- the contents of the source node into it. The contents of the source node
301 -- is not affected. The target node is always marked as not being in a list
302 -- (even if the source is a list member), and not overloaded. The new node
303 -- will have an extension if the source has an extension. New_Copy (Empty)
304 -- returns Empty, and New_Copy (Error) returns Error. Note that, unlike
305 -- Copy_Separate_Tree, New_Copy does not recursively copy any descendants,
306 -- so in general parent pointers are not set correctly for the descendants
307 -- of the copied node.
308
309 function Relocate_Node (Source : Node_Id) return Node_Id;
310 -- Source is a non-entity node that is to be relocated. A new node is
311 -- allocated, and the contents of Source are copied to this node, using
312 -- New_Copy. The parent pointers of descendants of the node are then
313 -- adjusted to point to the relocated copy. The original node is not
314 -- modified, but the parent pointers of its descendants are no longer
315 -- valid. The new copy is always marked as not overloaded. This routine is
316 -- used in conjunction with the tree rewrite routines (see descriptions of
317 -- Replace/Rewrite).
318 --
319 -- Note that the resulting node has the same parent as the source node, and
320 -- is thus still attached to the tree. It is valid for Source to be Empty,
321 -- in which case Relocate_Node simply returns Empty as the result.
322
323 function Copy_Separate_Tree (Source : Node_Id) return Node_Id;
324 -- Given a node that is the root of a subtree, Copy_Separate_Tree copies
325 -- the entire syntactic subtree, including recursively any descendants
326 -- whose parent field references a copied node (descendants not linked to
327 -- a copied node by the parent field are also copied.) The parent pointers
328 -- in the copy are properly set. Copy_Separate_Tree (Empty/Error) returns
329 -- Empty/Error. The new subtree does not share entities with the source,
330 -- but has new entities with the same name.
331 --
332 -- Most of the time this routine is called on an unanalyzed tree, and no
333 -- semantic information is copied. However, to ensure that no entities
334 -- are shared between the two when the source is already analyzed, and
335 -- that the result looks like an unanalyzed tree from the parser, Entity
336 -- fields and Etype fields are set to Empty, and Analyzed flags set False.
337 --
338 -- In addition, Expanded_Name nodes are converted back into the original
339 -- parser form (where they are Selected_Components), so that reanalysis
340 -- does the right thing.
341
342 function Copy_Separate_List (Source : List_Id) return List_Id;
343 -- Applies Copy_Separate_Tree to each element of the Source list, returning
344 -- a new list of the results of these copy operations.
345
346 procedure Exchange_Entities (E1 : Entity_Id; E2 : Entity_Id);
347 -- Exchange the contents of two entities. The parent pointers are switched
348 -- as well as the Defining_Identifier fields in the parents, so that the
349 -- entities point correctly to their original parents. The effect is thus
350 -- to leave the tree unchanged in structure, except that the entity ID
351 -- values of the two entities are interchanged. Neither of the two entities
352 -- may be list members. Note that entities appear on two semantic chains:
353 -- Homonym and Next_Entity: the corresponding links must be adjusted by the
354 -- caller, according to context.
355
356 procedure Extend_Node (Source : Node_Id);
357 -- This turns a node into an entity; it is only used by Sinfo.CN.
358
359 type Ignored_Ghost_Record_Proc is access procedure (N : Node_Or_Entity_Id);
360
361 procedure Set_Ignored_Ghost_Recording_Proc
362 (Proc : Ignored_Ghost_Record_Proc);
363 -- Register a procedure that is invoked when an ignored Ghost node or
364 -- entity is created.
365
366 type Report_Proc is access procedure (Target : Node_Id; Source : Node_Id);
367
368 procedure Set_Reporting_Proc (Proc : Report_Proc);
369 -- Register a procedure that is invoked when a node is allocated, replaced
370 -- or rewritten.
371
372 type Rewrite_Proc is access procedure (Target : Node_Id; Source : Node_Id);
373
374 procedure Set_Rewriting_Proc (Proc : Rewrite_Proc);
375 -- Register a procedure that is invoked when a node is rewritten
376
377 type Traverse_Result is (Abandon, OK, OK_Orig, Skip);
378 -- This is the type of the result returned by the Process function passed
379 -- to Traverse_Func and Traverse_Proc. See below for details.
380
381 subtype Traverse_Final_Result is Traverse_Result range Abandon .. OK;
382 -- This is the type of the final result returned Traverse_Func, based on
383 -- the results of Process calls. See below for details.
384
385 generic
386 with function Process (N : Node_Id) return Traverse_Result is <>;
387 function Traverse_Func (Node : Node_Id) return Traverse_Final_Result;
388 -- This is a generic function that, given the parent node for a subtree,
389 -- traverses all syntactic nodes of this tree, calling the given function
390 -- Process on each one, in pre order (i.e. top-down). The order of
391 -- traversing subtrees is arbitrary. The traversal is controlled as follows
392 -- by the result returned by Process:
393
394 -- OK The traversal continues normally with the syntactic
395 -- children of the node just processed.
396
397 -- OK_Orig The traversal continues normally with the syntactic
398 -- children of the original node of the node just processed.
399
400 -- Skip The children of the node just processed are skipped and
401 -- excluded from the traversal, but otherwise processing
402 -- continues elsewhere in the tree.
403
404 -- Abandon The entire traversal is immediately abandoned, and the
405 -- original call to Traverse returns Abandon.
406
407 -- The result returned by Traverse is Abandon if processing was terminated
408 -- by a call to Process returning Abandon, otherwise it is OK (meaning that
409 -- all calls to process returned either OK, OK_Orig, or Skip).
410
411 generic
412 with function Process
413 (Parent_Node : Node_Id;
414 Node : Node_Id) return Traverse_Result is <>;
415 function Traverse_Func_With_Parent
416 (Node : Node_Id) return Traverse_Final_Result;
417 pragma Inline (Traverse_Func_With_Parent);
418 -- Same as Traverse_Func except that the called function Process receives
419 -- also the Parent_Node of Node.
420
421 generic
422 with function Process (N : Node_Id) return Traverse_Result is <>;
423 procedure Traverse_Proc (Node : Node_Id);
424 pragma Inline (Traverse_Proc);
425 -- This is the same as Traverse_Func except that no result is returned,
426 -- i.e. Traverse_Func is called and the result is simply discarded.
427
428 generic
429 with function Process
430 (Parent_Node : Node_Id;
431 Node : Node_Id) return Traverse_Result is <>;
432 procedure Traverse_Proc_With_Parent (Node : Node_Id);
433 pragma Inline (Traverse_Proc_With_Parent);
434 -- Same as Traverse_Proc except that the called function Process receives
435 -- also the Parent_Node of Node.
436
437 ---------------------------
438 -- Node Access Functions --
439 ---------------------------
440
441 -- The following functions return the contents of the indicated field of
442 -- the node referenced by the argument, which is a Node_Id.
443
444 function No (N : Node_Id) return Boolean;
445 pragma Inline (No);
446 -- Tests given Id for equality with the Empty node. This allows notations
447 -- like "if No (Variant_Part)" as opposed to "if Variant_Part = Empty".
448
449 function Node_Parent (N : Node_Or_Entity_Id) return Node_Or_Entity_Id;
450 pragma Inline (Node_Parent);
451 function Parent (N : Node_Or_Entity_Id) return Node_Or_Entity_Id
452 renames Node_Parent;
453 pragma Inline (Parent);
454 -- Returns the parent of a node if the node is not a list member, or else
455 -- the parent of the list containing the node if the node is a list member.
456 -- Parent has the same name as the one in Nlists; Node_Parent can be used
457 -- more easily in the debugger.
458
459 function Parent_Or_List_Containing (X : Union_Id) return Union_Id;
460 -- X must be in Node_Range or in List_Range. If X is in Node_Range and is
461 -- contained in a list, returns that list, otherwise return the parent of
462 -- the list or node represented by X.
463
464 function Paren_Count (N : Node_Id) return Nat;
465 pragma Inline (Paren_Count);
466 -- Number of parentheses that surround an expression
467
468 function Present (N : Node_Id) return Boolean;
469 pragma Inline (Present);
470 -- Tests given Id for inequality with the Empty node. This allows notations
471 -- like "if Present (Statement)" as opposed to "if Statement /= Empty".
472
473 procedure Set_Original_Node (N : Node_Id; Val : Node_Id);
474 pragma Inline (Set_Original_Node);
475 -- Note that this routine is used only in very peculiar cases. In normal
476 -- cases, the Original_Node link is set by calls to Rewrite.
477
478 procedure Set_Node_Parent (N : Node_Or_Entity_Id; Val : Node_Or_Entity_Id);
479 pragma Inline (Set_Node_Parent);
480 procedure Set_Parent (N : Node_Or_Entity_Id; Val : Node_Or_Entity_Id)
481 renames Set_Node_Parent;
482 pragma Inline (Set_Parent);
483
484 procedure Set_Paren_Count (N : Node_Id; Val : Nat);
485 pragma Inline (Set_Paren_Count);
486
487 ---------------------------
488 -- Tree Rewrite Routines --
489 ---------------------------
490
491 -- During the compilation process it is necessary in a number of situations
492 -- to rewrite the tree. In some cases, such rewrites do not affect the
493 -- structure of the tree, for example, when an indexed component node is
494 -- replaced by the corresponding call node (the parser cannot distinguish
495 -- between these two cases).
496
497 -- In other situations, the rewrite does affect the structure of the
498 -- tree. Examples are the replacement of a generic instantiation by the
499 -- instantiated spec and body, and the static evaluation of expressions.
500
501 -- If such structural modifications are done by the expander, there are
502 -- no difficulties, since the form of the tree after the expander has no
503 -- special significance, except as input to the backend of the compiler.
504 -- However, if these modifications are done by the semantic phase, then
505 -- it is important that they be done in a manner which allows the original
506 -- tree to be preserved. This is because tools like pretty printers need
507 -- to have this original tree structure available.
508
509 -- The subprograms in this section allow rewriting of the tree by either
510 -- insertion of new nodes in an existing list, or complete replacement of
511 -- a subtree. The resulting tree for most purposes looks as though it has
512 -- been really changed, and there is no trace of the original. However,
513 -- special subprograms, also defined in this section, allow the original
514 -- tree to be reconstructed if necessary.
515
516 -- For tree modifications done in the expander, it is permissible to
517 -- destroy the original tree, although it is also allowable to use the
518 -- tree rewrite routines where it is convenient to do so.
519
520 procedure Mark_Rewrite_Insertion (New_Node : Node_Id);
521 pragma Inline (Mark_Rewrite_Insertion);
522 -- This procedure marks the given node as an insertion made during a tree
523 -- rewriting operation. Only the root needs to be marked. The call does
524 -- not do the actual insertion, which must be done using one of the normal
525 -- list insertion routines. The node is treated normally in all respects
526 -- except for its response to Is_Rewrite_Insertion. The function of these
527 -- calls is to be able to get an accurate original tree. This helps the
528 -- accuracy of Sprint.Sprint_Node, and in particular, when stubs are being
529 -- generated, it is essential that the original tree be accurate.
530
531 function Is_Rewrite_Insertion (Node : Node_Id) return Boolean;
532 pragma Inline (Is_Rewrite_Insertion);
533 -- Tests whether the given node was marked using Mark_Rewrite_Insertion.
534 -- This is used in reconstructing the original tree (where such nodes are
535 -- to be eliminated).
536
537 procedure Rewrite (Old_Node, New_Node : Node_Id);
538 -- This is used when a complete subtree is to be replaced. Old_Node is the
539 -- root of the old subtree to be replaced, and New_Node is the root of the
540 -- newly constructed replacement subtree. The actual mechanism is to swap
541 -- the contents of these two nodes fixing up the parent pointers of the
542 -- replaced node (we do not attempt to preserve parent pointers for the
543 -- original node).
544 -- ??? The above explanation is incorrect, instead Copy_Node is called.
545 --
546 -- Note: New_Node may not contain references to Old_Node, for example as
547 -- descendants, since the rewrite would make such references invalid. If
548 -- New_Node does need to reference Old_Node, then these references should
549 -- be to a relocated copy of Old_Node (see Relocate_Node procedure).
550 --
551 -- Note: The Original_Node function applied to Old_Node (which has now
552 -- been replaced by the contents of New_Node), can be used to obtain the
553 -- original node, i.e. the old contents of Old_Node.
554
555 procedure Replace (Old_Node, New_Node : Node_Id);
556 -- This is similar to Rewrite, except that the old value of Old_Node
557 -- is not saved. New_Node should not be used after Replace. The flag
558 -- Is_Rewrite_Substitution will be False for the resulting node, unless
559 -- it was already true on entry, and Original_Node will not return the
560 -- original contents of the Old_Node, but rather the New_Node value.
561 -- Replace also preserves the setting of Comes_From_Source.
562 --
563 -- Note that New_Node must not contain references to Old_Node, for example
564 -- as descendants, since the rewrite would make such references invalid. If
565 -- New_Node does need to reference Old_Node, then these references should
566 -- be to a relocated copy of Old_Node (see Relocate_Node procedure).
567 --
568 -- Replace is used in certain circumstances where it is desirable to
569 -- suppress any history of the rewriting operation. Notably, it is used
570 -- when the parser has mis-classified a node (e.g. a task entry call
571 -- that the parser has parsed as a procedure call).
572
573 function Is_Rewrite_Substitution (Node : Node_Id) return Boolean;
574 pragma Inline (Is_Rewrite_Substitution);
575 -- Return True iff Node has been rewritten (i.e. if Node is the root
576 -- of a subtree which was installed using Rewrite).
577
578 function Original_Node (Node : Node_Id) return Node_Id;
579 pragma Inline (Original_Node);
580 -- If Node has not been rewritten, then returns its input argument
581 -- unchanged, else returns the Node for the original subtree. See section
582 -- in sinfo.ads for requirements on original nodes returned by this
583 -- function.
584 --
585 -- Note: Parents are not preserved in original tree nodes that are
586 -- retrieved in this way (i.e. their children may have children whose
587 -- Parent pointers reference some other node).
588 --
589 -- Note: there is no direct mechanism for deleting an original node (in
590 -- a manner that can be reversed later). One possible approach is to use
591 -- Rewrite to substitute a null statement for the node to be deleted.
592
593 ----------------------
594 -- Vanishing Fields --
595 ----------------------
596
597 -- The Nkind and Ekind fields are like Ada discriminants governing a
598 -- variant part. They determine which fields are present. If the Nkind
599 -- or Ekind fields are changed, then this can change which fields are
600 -- present. If a field is present for the old kind, but not for the
601 -- new kind, the field vanishes. This requires some care when changing
602 -- kinds, as described below. Note that Ada doesn't even allow direct
603 -- modification of a discriminant.
604
605 type Node_Field_Set is array (Node_Field) of Boolean with Pack;
606
607 type Entity_Field_Set is array (Entity_Field) of Boolean with Pack;
608
609 procedure Reinit_Field_To_Zero (N : Node_Id; Field : Node_Or_Entity_Field);
610 -- When a node is created, all fields are initialized to zero, even if zero
611 -- is not a valid value of the field type. This procedure puts the field
612 -- back to its initial zero value. Note that you can't just do something
613 -- like Set_Some_Field (N, 0), if Some_Field is of (say) type Uintp,
614 -- because Uintp is a subrange that does not include 0.
615 type Entity_Kind_Set is array (Entity_Kind) of Boolean with Pack;
616 procedure Reinit_Field_To_Zero
617 (N : Node_Id; Field : Entity_Field; Old_Ekind : Entity_Kind_Set);
618 procedure Reinit_Field_To_Zero
619 (N : Node_Id; Field : Entity_Field; Old_Ekind : Entity_Kind);
620 -- Same as above, but assert that the old Ekind is as specified. We might
621 -- want to get rid of these, but it's useful documentation while working on
622 -- this.
623
624 function Field_Is_Initial_Zero
625 (N : Node_Id; Field : Node_Or_Entity_Field) return Boolean;
626 -- True if the field value is the initial zero value
627
628 procedure Mutate_Nkind (N : Node_Id; Val : Node_Kind) with Inline;
629 -- There is no Set_Nkind in Sinfo.Nodes. We use this instead. This is here,
630 -- and has a different name, because it does some extra checking. Nkind is
631 -- like a discriminant, in that it controls which fields exist, and that
632 -- set of fields can be different for the new kind. Discriminants cannot be
633 -- modified in Ada for that reason. The rule here is more flexible: Nkind
634 -- can be modified. However, when Nkind is modified, fields that exist for
635 -- the old kind, but not for the new kind will vanish. We require that all
636 -- vanishing fields be set to their initial zero value before calling
637 -- Mutate_Nkind. This is necessary, because the memory occupied by the
638 -- vanishing fields might be used for totally unrelated fields in the new
639 -- node. See Reinit_Field_To_Zero.
640 --
641 -- It is an error to mutate a node to the same kind it already has.
642
643 procedure Mutate_Ekind (N : Entity_Id; Val : Entity_Kind) with Inline;
644 -- Ekind is also like a discriminant, and is mostly treated as above (see
645 -- Mutate_Nkind).
646 --
647 -- It is not (yet?) an error to mutate an entity to the same kind it
648 -- already has. It is an error to mutate to E_Void.
649
650 function Node_To_Fetch_From
651 (N : Node_Or_Entity_Id; Field : Node_Or_Entity_Field)
652 return Node_Or_Entity_Id is
653 (case Field_Descriptors (Field).Type_Only is
654 when No_Type_Only => N,
655 when Base_Type_Only => Base_Type (N),
656 when Impl_Base_Type_Only => Implementation_Base_Type (N),
657 when Root_Type_Only => Root_Type (N));
658 -- This is analogous to the same-named function in Gen_IL.Gen. Normally,
659 -- Type_Only is No_Type_Only, and we fetch the field from the node N. But
660 -- if Type_Only = Base_Type_Only, we need to go to the Base_Type, and
661 -- similarly for the other two cases. This can return something other
662 -- than N only if N is an Entity.
663
664 -----------------------------
665 -- Private Part Subpackage --
666 -----------------------------
667
668 -- The following package contains the definition of the data structure
669 -- used by the implementation of the Atree package. Logically it really
670 -- corresponds to the private part, hence the name. The reason that it
671 -- is defined as a sub-package is to allow special access from clients
672 -- that need to see the internals of the data structures.
673
674 package Atree_Private_Part is
675
676 pragma Assert (Node_Kind'Pos (N_Unused_At_Start) = 0);
677 pragma Assert (Empty_List_Or_Node = 0);
678 pragma Assert (Entity_Kind'Pos (E_Void) = 0);
679 -- We want nodes initialized to zero bits by default
680
681 -------------------------
682 -- Tree Representation --
683 -------------------------
684
685 -- The nodes of the tree are stored in two tables (i.e. growable
686 -- arrays).
687
688 -- A Node_Id points to an element of Node_Offsets, which contains a
689 -- Field_Offset that points to an element of Slots. Each slot can
690 -- contain a single 32-bit field, or multiple smaller fields.
691 -- An n-bit field is aligned on an n-bit boundary. The size of a node is
692 -- the number of slots, which can range from 1 up to however many are
693 -- needed.
694 --
695 -- The reason for the extra level of indirection is that Copy_Node,
696 -- Exchange_Entities, and Rewrite all assume that nodes can be modified
697 -- in place.
698 --
699 -- As an optimization, we store a few slots directly in the Node_Offsets
700 -- table (see type Node_Header) rather than requiring the extra level of
701 -- indirection for accessing those slots. N_Head is the number of slots
702 -- stored in the Node_Header. N_Head can be adjusted by modifying
703 -- Gen_IL.Gen. If N_Head is (say) 3, then a node containing 7 slots will
704 -- have slots 0..2 in the header, and 3..6 stored indirect in the Slots
705 -- table. We use zero-origin addressing, so the Offset into the Slots
706 -- table will point 3 slots before slot 3.
707
708 pragma Assert (N_Head <= Min_Node_Size);
709 pragma Assert (N_Head <= Min_Entity_Size);
710
711 Slot_Size : constant := 32;
712 type Slot is mod 2**Slot_Size;
713 for Slot'Size use Slot_Size;
714
715 -- The type Slot is defined in Types as a 32-bit modular integer. It
716 -- is logically split into the appropriate numbers of components of
717 -- appropriate size, but this splitting is not explicit because packed
718 -- arrays cannot be properly interfaced in C/C++ and packed records are
719 -- way too slow.
720
721 type Node_Header_Slots is
722 array (Field_Offset range 0 .. N_Head - 1) of Slot;
723 type Node_Header is record
724 Slots : Node_Header_Slots;
725 Offset : Node_Offset'Base;
726 end record;
727 pragma Assert (Node_Header'Size = (N_Head + 1) * Slot_Size);
728 pragma Assert (Node_Header'Size = 16 * 8);
729
730 package Node_Offsets is new Table.Table
731 (Table_Component_Type => Node_Header,
732 Table_Index_Type => Node_Id'Base,
733 Table_Low_Bound => First_Node_Id,
734 Table_Initial => Alloc.Node_Offsets_Initial,
735 Table_Increment => Alloc.Node_Offsets_Increment,
736 Table_Name => "Node_Offsets");
737
738 Noff : Node_Offsets.Table_Ptr renames Node_Offsets.Table with
739 Unreferenced;
740 function Nlast return Node_Id'Base renames Node_Offsets.Last with
741 Unreferenced;
742 -- Short names for use in gdb, not used in real code. Note that gdb
743 -- can't find Node_Offsets.Table without a full expanded name.
744
745 function Shift_Left (S : Slot; V : Natural) return Slot;
746 pragma Import (Intrinsic, Shift_Left);
747
748 function Shift_Right (S : Slot; V : Natural) return Slot;
749 pragma Import (Intrinsic, Shift_Right);
750
751 -- Low-level types for fields of the various supported sizes.
752 -- All fields are a power of 2 number of bits, and are aligned
753 -- to that number of bits:
754
755 type Field_Size_1_Bit is mod 2**1;
756 type Field_Size_2_Bit is mod 2**2;
757 type Field_Size_4_Bit is mod 2**4;
758 type Field_Size_8_Bit is mod 2**8;
759 type Field_Size_32_Bit is mod 2**32;
760
761 Slots_Low_Bound : constant Field_Offset := Field_Offset'First + 1;
762
763 package Slots is new Table.Table
764 (Table_Component_Type => Slot,
765 Table_Index_Type => Node_Offset'Base,
766 Table_Low_Bound => Slots_Low_Bound,
767 Table_Initial => Alloc.Slots_Initial,
768 Table_Increment => Alloc.Slots_Increment,
769 Table_Name => "Slots");
770 -- Note that Table_Low_Bound is set such that if we try to access
771 -- Slots.Table (0), we will get Constraint_Error.
772
773 Slts : Slots.Table_Ptr renames Slots.Table with
774 Unreferenced;
775 function Slast return Node_Offset'Base renames Slots.Last with
776 Unreferenced;
777 -- Short names for use in gdb, not used in real code. Note that gdb
778 -- can't find Slots.Table without a full expanded name.
779
780 function Alloc_Node_Id return Node_Id with Inline;
781
782 function Alloc_Slots (Num_Slots : Slot_Count) return Node_Offset
783 with Inline;
784 -- Allocate the slots for a node in the Slots table
785
786 -- Each of the following Get_N_Bit_Field functions fetches the field of
787 -- the given Field_Type at the given offset. Field_Type'Size must be N.
788 -- The offset is measured in units of Field_Type'Size. Likewise for the
789 -- Set_N_Bit_Field procedures. These are instantiated in Sinfo.Nodes and
790 -- Einfo.Entities for the various possible Field_Types (Flag, Node_Id,
791 -- Uint, etc).
792
793 generic
794 type Field_Type is private;
795 function Get_1_Bit_Field
796 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
797 with Inline;
798
799 generic
800 type Field_Type is private;
801 function Get_2_Bit_Field
802 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
803 with Inline;
804
805 generic
806 type Field_Type is private;
807 function Get_4_Bit_Field
808 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
809 with Inline;
810
811 generic
812 type Field_Type is private;
813 function Get_8_Bit_Field
814 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
815 with Inline;
816
817 generic
818 type Field_Type is private;
819 function Get_32_Bit_Field
820 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
821 with Inline;
822
823 generic
824 type Field_Type is private;
825 Default_Val : Field_Type;
826 function Get_32_Bit_Field_With_Default
827 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
828 with Inline;
829 -- If the field has not yet been set, return Default_Val
830
831 generic
832 type Field_Type is private;
833 function Get_Valid_32_Bit_Field
834 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Type
835 with Inline;
836 -- Assert that the field has already been set. This is currently used
837 -- only for Uints, but could be used more generally.
838
839 generic
840 type Field_Type is private;
841 procedure Set_1_Bit_Field
842 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Type)
843 with Inline;
844
845 generic
846 type Field_Type is private;
847 procedure Set_2_Bit_Field
848 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Type)
849 with Inline;
850
851 generic
852 type Field_Type is private;
853 procedure Set_4_Bit_Field
854 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Type)
855 with Inline;
856
857 generic
858 type Field_Type is private;
859 procedure Set_8_Bit_Field
860 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Type)
861 with Inline;
862
863 generic
864 type Field_Type is private;
865 procedure Set_32_Bit_Field
866 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Type)
867 with Inline;
868
869 -- The following are similar to the above generics, but are not generic,
870 -- and work with the low-level Field_n_bit types. If generics could be
871 -- overloaded, we would use the same names.
872
873 function Get_1_Bit_Val
874 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Size_1_Bit
875 with Inline;
876
877 function Get_2_Bit_Val
878 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Size_2_Bit
879 with Inline;
880
881 function Get_4_Bit_Val
882 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Size_4_Bit
883 with Inline;
884
885 function Get_8_Bit_Val
886 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Size_8_Bit
887 with Inline;
888
889 function Get_32_Bit_Val
890 (N : Node_Or_Entity_Id; Offset : Field_Offset) return Field_Size_32_Bit
891 with Inline;
892
893 procedure Set_1_Bit_Val
894 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Size_1_Bit)
895 with Inline;
896
897 procedure Set_2_Bit_Val
898 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Size_2_Bit)
899 with Inline;
900
901 procedure Set_4_Bit_Val
902 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Size_4_Bit)
903 with Inline;
904
905 procedure Set_8_Bit_Val
906 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Size_8_Bit)
907 with Inline;
908
909 procedure Set_32_Bit_Val
910 (N : Node_Or_Entity_Id; Offset : Field_Offset; Val : Field_Size_32_Bit)
911 with Inline;
912
913 -- The following are used in "asserts on" mode to validate nodes; an
914 -- exception is raised if invalid node content is detected.
915
916 procedure Validate_Node (N : Node_Or_Entity_Id);
917 -- Validate for reading
918 procedure Validate_Node_Write (N : Node_Or_Entity_Id);
919 -- Validate for writing
920
921 function Is_Valid_Node (U : Union_Id) return Boolean;
922 -- True if U is within the range of Node_Offsets
923
924 procedure Print_Atree_Info (N : Node_Or_Entity_Id);
925 -- Called from Treepr to print out information about N that is private
926 -- to Atree.
927
928 end Atree_Private_Part;
929
930 -- Statistics:
931
932 subtype Call_Count is Nat_64;
933 Get_Count, Set_Count : array (Node_Or_Entity_Field) of Call_Count :=
934 (others => 0);
935 -- Number of calls to each getter and setter. See documentaton for
936 -- -gnatd.A.
937
938 Get_Original_Node_Count, Set_Original_Node_Count : Call_Count := 0;
939
940 procedure Print_Statistics;
941
942 end Atree;