| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S I N F O . U T I L S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2020-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 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 27 |
|
| 28 |
package Sinfo.Utils is |
| 29 |
|
| 30 |
-- We would like to get rid of the Library_Unit field, and replace it with |
| 31 |
-- Other_Comp_Unit (on N_Compilation_Unit), Withed_Lib_Unit (on |
| 32 |
-- N_With_Clause), and Subunit (on N_Body_Stub). Or we could split |
| 33 |
-- Other_Comp_Unit into Spec_Lib_Unit, Body_Lib_Unit, Subunit_Parent. |
| 34 |
-- However, gnat-llvm, codepeer, and spark are still using Library_Unit. |
| 35 |
-- Therefore, we use the wrappers below. |
| 36 |
-- |
| 37 |
-- The call site should always know whether it has an N_Compilation_Unit, |
| 38 |
-- N_Body_Stub, or N_With_Clause. In the N_Compilation_Unit case, it should |
| 39 |
-- also know whether it's looking for the spec of a body, the body of a |
| 40 |
-- spec, or the parent of a subunit. Spec_Or_Body_Lib_Unit and |
| 41 |
-- Other_Comp_Unit should be avoided when possible; these are for the |
| 42 |
-- N_Compilation_Unit cases where the call site does NOT know what it's |
| 43 |
-- looking for. |
| 44 |
|
| 45 |
function Spec_Lib_Unit |
| 46 |
(N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id; |
| 47 |
procedure Set_Spec_Lib_Unit (N, Val : N_Compilation_Unit_Id); |
| 48 |
-- The spec compilation unit of a body compilation unit. |
| 49 |
-- It can be an acts-as-spec subprogram body; in that case |
| 50 |
-- Spec_Lib_Unit points to itself. |
| 51 |
|
| 52 |
function Body_Lib_Unit |
| 53 |
(N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id; |
| 54 |
procedure Set_Body_Lib_Unit (N, Val : N_Compilation_Unit_Id); |
| 55 |
-- The body compilation unit of a spec compilation unit. |
| 56 |
-- Empty if not present. |
| 57 |
|
| 58 |
function Spec_Or_Body_Lib_Unit |
| 59 |
(N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id; |
| 60 |
-- Same as Spec_Lib_Unit or Body_Lib_Unit, depending on whether |
| 61 |
-- N is a body or spec. Used when we know N is a library unit |
| 62 |
-- (not a subunit), but we don't know whether it's the spec |
| 63 |
-- or the body. |
| 64 |
|
| 65 |
function Subunit_Parent |
| 66 |
(N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id; |
| 67 |
procedure Set_Subunit_Parent (N, Val : N_Compilation_Unit_Id); |
| 68 |
-- The parent body of a subunit |
| 69 |
|
| 70 |
function Other_Comp_Unit |
| 71 |
(N : N_Compilation_Unit_Id) return Opt_N_Compilation_Unit_Id; |
| 72 |
-- Same as Spec_Lib_Unit, Body_Lib_Unit, or Subunit_Parent, |
| 73 |
-- as appropriate. Used when we don't know whether N is a |
| 74 |
-- a library unit spec, library unit body, or subunit. |
| 75 |
|
| 76 |
function Stub_Subunit (N : N_Body_Stub_Id) return Opt_N_Compilation_Unit_Id; |
| 77 |
procedure Set_Stub_Subunit |
| 78 |
(N : N_Body_Stub_Id; Val : N_Compilation_Unit_Id); |
| 79 |
-- Subunit corresponding to a stub |
| 80 |
|
| 81 |
function Withed_Lib_Unit |
| 82 |
(N : N_With_Clause_Id) return Opt_N_Compilation_Unit_Id; |
| 83 |
procedure Set_Withed_Lib_Unit |
| 84 |
(N : N_With_Clause_Id; Val : N_Compilation_Unit_Id); |
| 85 |
-- The compilation unit that a with clause refers to. |
| 86 |
-- Note that the Sem_Elab creates with clauses that point to bodies |
| 87 |
-- (including non-Acts_As_Spec bodies). |
| 88 |
|
| 89 |
------------------------------- |
| 90 |
-- Parent-related operations -- |
| 91 |
------------------------------- |
| 92 |
|
| 93 |
procedure Copy_Parent (To, From : Node_Or_Entity_Id); |
| 94 |
-- Does Set_Parent (To, Parent (From)), except that if To or From are |
| 95 |
-- empty, does nothing. If From is empty but To is not, then Parent (To) |
| 96 |
-- should already be Empty. |
| 97 |
|
| 98 |
function Parent_Kind (N : Node_Id) return Node_Kind; |
| 99 |
-- Same as Nkind (Parent (N)), except if N is Empty, return N_Empty |
| 100 |
|
| 101 |
------------------------- |
| 102 |
-- Iterator Procedures -- |
| 103 |
------------------------- |
| 104 |
|
| 105 |
-- The call to Next_xxx (N) is equivalent to N := Next_xxx (N) |
| 106 |
|
| 107 |
procedure Next_Entity (N : in out Node_Id); |
| 108 |
procedure Next_Named_Actual (N : in out Node_Id); |
| 109 |
procedure Next_Rep_Item (N : in out Node_Id); |
| 110 |
procedure Next_Use_Clause (N : in out Node_Id); |
| 111 |
|
| 112 |
------------------------------------------- |
| 113 |
-- Miscellaneous Tree Access Subprograms -- |
| 114 |
------------------------------------------- |
| 115 |
|
| 116 |
function First_Real_Statement -- ??? |
| 117 |
(Ignored : N_Handled_Sequence_Of_Statements_Id) return Node_Id is (Empty); |
| 118 |
-- The First_Real_Statement field has been removed, but it is referenced in |
| 119 |
-- codepeer and gnat-llvm. This is a temporary version, always returning |
| 120 |
-- Empty, to ease the transition. |
| 121 |
|
| 122 |
function End_Location (N : Node_Id) return Source_Ptr; |
| 123 |
-- N is an N_If_Statement or N_Case_Statement node, and this function |
| 124 |
-- returns the location of the IF token in the END IF sequence by |
| 125 |
-- translating the value of the End_Span field. |
| 126 |
|
| 127 |
-- WARNING: There is a matching C declaration of this subprogram in fe.h |
| 128 |
|
| 129 |
procedure Set_End_Location (N : Node_Id; S : Source_Ptr); |
| 130 |
-- N is an N_If_Statement or N_Case_Statement node. This procedure sets |
| 131 |
-- the End_Span field to correspond to the given value S. In other words, |
| 132 |
-- End_Span is set to the difference between S and Sloc (N), the starting |
| 133 |
-- location. |
| 134 |
|
| 135 |
function Get_Pragma_Arg (Arg : Node_Id) return Node_Id; |
| 136 |
-- Given an argument to a pragma Arg, this function returns the expression |
| 137 |
-- for the argument. This is Arg itself, or, in the case where Arg is a |
| 138 |
-- pragma argument association node, the expression from this node. |
| 139 |
|
| 140 |
function Lowest_Common_Ancestor (N1, N2 : Node_Id) return Union_Id; |
| 141 |
-- Returns the list or node that is the lowest common ancestor of N1 and |
| 142 |
-- N2 in the syntax tree. |
| 143 |
|
| 144 |
----------------------- |
| 145 |
-- Utility Functions -- |
| 146 |
----------------------- |
| 147 |
|
| 148 |
procedure Map_Pragma_Name (From, To : Name_Id); |
| 149 |
-- Used in the implementation of pragma Rename_Pragma. Maps pragma name |
| 150 |
-- From to pragma name To, so From can be used as a synonym for To. |
| 151 |
|
| 152 |
Too_Many_Pragma_Mappings : exception; |
| 153 |
-- Raised if Map_Pragma_Name is called too many times. We expect that few |
| 154 |
-- programs will use it at all, and those that do will use it approximately |
| 155 |
-- once or twice. |
| 156 |
|
| 157 |
function Pragma_Name (N : Node_Id) return Name_Id; |
| 158 |
-- Obtain the name of pragma N from the Chars field of its identifier. If |
| 159 |
-- the pragma has been renamed using Rename_Pragma, this routine returns |
| 160 |
-- the name of the renaming. |
| 161 |
|
| 162 |
function Pragma_Name_Unmapped (N : Node_Id) return Name_Id; |
| 163 |
-- Obtain the name of pragma N from the Chars field of its identifier. This |
| 164 |
-- form of name extraction does not take into account renamings performed |
| 165 |
-- by Rename_Pragma. |
| 166 |
|
| 167 |
generic |
| 168 |
with procedure Action (U : Union_Id); |
| 169 |
procedure Walk_Sinfo_Fields (N : Node_Id); |
| 170 |
-- Walk the Sinfo fields of N, for all field types that Union_Id includes, |
| 171 |
-- and call Action on each one. However, skip the Link field, which is the |
| 172 |
-- Parent, and would cause us to wander off into the weeds. |
| 173 |
|
| 174 |
generic |
| 175 |
with function Transform (U : Union_Id) return Union_Id; |
| 176 |
procedure Walk_Sinfo_Fields_Pairwise (N1, N2 : Node_Id); |
| 177 |
-- Walks the Sinfo fields of N1 and N2 pairwise, calls Tranform on each N2 |
| 178 |
-- field, copying the resut into the corresponding field of N1. The Nkinds |
| 179 |
-- must match. Link is skipped. |
| 180 |
|
| 181 |
------------------------------------------- |
| 182 |
-- Aliases for Entity_Or_Associated_Node -- |
| 183 |
------------------------------------------- |
| 184 |
|
| 185 |
-- Historically, the Entity, Associated_Node, and Entity_Or_Associated_Node |
| 186 |
-- fields shared the same slot. A further complication is that there is an |
| 187 |
-- N_Has_Entity that does not include all node types that have the Entity |
| 188 |
-- field. N_Inclusive_Has_Entity are the node types that have the Entity |
| 189 |
-- field. |
| 190 |
|
| 191 |
subtype N_Inclusive_Has_Entity is Node_Id with Predicate => |
| 192 |
N_Inclusive_Has_Entity in |
| 193 |
N_Has_Entity_Id |
| 194 |
| N_Attribute_Definition_Clause_Id |
| 195 |
| N_Aspect_Specification_Id |
| 196 |
| N_Freeze_Entity_Id |
| 197 |
| N_Freeze_Generic_Entity_Id; |
| 198 |
|
| 199 |
subtype N_Has_Associated_Node is Node_Id with Predicate => |
| 200 |
N_Has_Associated_Node in |
| 201 |
N_Has_Entity_Id |
| 202 |
| N_Aggregate_Id |
| 203 |
| N_Extension_Aggregate_Id |
| 204 |
| N_Selected_Component_Id |
| 205 |
| N_Use_Package_Clause_Id; |
| 206 |
|
| 207 |
function Associated_Node |
| 208 |
(N : N_Has_Associated_Node) return Node_Id |
| 209 |
renames Entity_Or_Associated_Node; |
| 210 |
|
| 211 |
function Entity |
| 212 |
(N : N_Inclusive_Has_Entity) return Node_Id |
| 213 |
renames Entity_Or_Associated_Node; |
| 214 |
|
| 215 |
procedure Set_Associated_Node |
| 216 |
(N : N_Has_Associated_Node; Val : Node_Id) |
| 217 |
renames Set_Entity_Or_Associated_Node; |
| 218 |
|
| 219 |
procedure Set_Entity |
| 220 |
(N : N_Inclusive_Has_Entity; Val : Node_Id) |
| 221 |
renames Set_Entity_Or_Associated_Node; |
| 222 |
|
| 223 |
--------------------------------------------------- |
| 224 |
-- Aliases for Aggregate_Bounds_Or_Ancestor_Type -- |
| 225 |
--------------------------------------------------- |
| 226 |
|
| 227 |
function Aggregate_Bounds (N : Node_Id) return Node_Id |
| 228 |
renames Aggregate_Bounds_Or_Ancestor_Type; |
| 229 |
|
| 230 |
function Ancestor_Type (N : Node_Id) return Node_Id |
| 231 |
renames Aggregate_Bounds_Or_Ancestor_Type; |
| 232 |
|
| 233 |
procedure Set_Aggregate_Bounds (N : Node_Id; Val : Node_Id) |
| 234 |
renames Set_Aggregate_Bounds_Or_Ancestor_Type; |
| 235 |
|
| 236 |
procedure Set_Ancestor_Type (N : Node_Id; Val : Node_Id) |
| 237 |
renames Set_Aggregate_Bounds_Or_Ancestor_Type; |
| 238 |
|
| 239 |
--------------- |
| 240 |
-- Debugging -- |
| 241 |
--------------- |
| 242 |
|
| 243 |
procedure New_Node_Debugging_Output (N : Node_Id); |
| 244 |
pragma Inline (New_Node_Debugging_Output); |
| 245 |
-- See package body for documentation |
| 246 |
|
| 247 |
end Sinfo.Utils; |