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