| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- T B U I L D -- |
| 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 contains various utility procedures to assist in building |
| 27 |
-- specific types of tree nodes. |
| 28 |
|
| 29 |
with Namet; use Namet; |
| 30 |
with Sinfo; use Sinfo; |
| 31 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 32 |
with Types; use Types; |
| 33 |
with Uintp; use Uintp; |
| 34 |
|
| 35 |
package Tbuild is |
| 36 |
|
| 37 |
function Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id; |
| 38 |
-- Returns an expression that is a type conversion of expression Expr to |
| 39 |
-- type Typ. If the type of Expr is Typ, then no conversion is required. |
| 40 |
-- Otherwise an N_Type_Conversion node is constructed to convert the |
| 41 |
-- expression. Relocate_Node is applied to Expr, so that it is safe to |
| 42 |
-- replace a node by a Convert_To of itself to some other type. |
| 43 |
|
| 44 |
procedure Convert_To_And_Rewrite (Typ : Entity_Id; Expr : Node_Id); |
| 45 |
pragma Inline (Convert_To_And_Rewrite); |
| 46 |
-- Like the function, except that there is an extra step of calling |
| 47 |
-- Rewrite on the Expr node and replacing it with the converted result. |
| 48 |
|
| 49 |
procedure Discard_Node (N : Node_Or_Entity_Id); |
| 50 |
pragma Inline (Discard_Node); |
| 51 |
-- This is a dummy procedure that simply returns and does nothing. It is |
| 52 |
-- used when a function returning a Node_Id value is called for its side |
| 53 |
-- effect (e.g. a call to Make to construct a node) but the Node_Id value |
| 54 |
-- is not required. |
| 55 |
|
| 56 |
procedure Discard_List (L : List_Id); |
| 57 |
pragma Inline (Discard_List); |
| 58 |
-- This is a dummy procedure that simply returns and does nothing. It is |
| 59 |
-- used when a function returning a Node_Id value is called for its side |
| 60 |
-- effect (e.g. a call to the parser to parse a list of compilation |
| 61 |
-- units), but the List_Id value is not required. |
| 62 |
|
| 63 |
function Make_Byte_Aligned_Attribute_Reference |
| 64 |
(Sloc : Source_Ptr; |
| 65 |
Prefix : Node_Id; |
| 66 |
Attribute_Name : Name_Id) return Node_Id; |
| 67 |
pragma Inline (Make_Byte_Aligned_Attribute_Reference); |
| 68 |
-- Like the standard Make_Attribute_Reference but the special flag |
| 69 |
-- Must_Be_Byte_Aligned is set in the attribute reference node. The |
| 70 |
-- Attribute_Name must be Name_Address or Name_Unrestricted_Access. |
| 71 |
|
| 72 |
function Make_Float_Literal |
| 73 |
(Loc : Source_Ptr; |
| 74 |
Radix : Uint; |
| 75 |
Significand : Uint; |
| 76 |
Exponent : Uint) return Node_Id; |
| 77 |
-- Create a real literal for the floating point expression value |
| 78 |
-- Significand * Radix ** Exponent. Radix must be greater than 1. |
| 79 |
|
| 80 |
function Make_Implicit_Exception_Handler |
| 81 |
(Sloc : Source_Ptr; |
| 82 |
Choice_Parameter : Node_Id := Empty; |
| 83 |
Exception_Choices : List_Id; |
| 84 |
Statements : List_Id) return Node_Id; |
| 85 |
pragma Inline (Make_Implicit_Exception_Handler); |
| 86 |
-- This is just like Make_Exception_Handler, except that it also sets the |
| 87 |
-- Local_Raise_Statements field to No_Elist, ensuring that it is properly |
| 88 |
-- initialized. This should always be used when creating implicit exception |
| 89 |
-- handlers during expansion (i.e. handlers that do not correspond to user |
| 90 |
-- source program exception handlers). |
| 91 |
|
| 92 |
function Make_Implicit_If_Statement |
| 93 |
(Node : Node_Id; |
| 94 |
Condition : Node_Id; |
| 95 |
Then_Statements : List_Id; |
| 96 |
Elsif_Parts : List_Id := No_List; |
| 97 |
Else_Statements : List_Id := No_List) return Node_Id; |
| 98 |
pragma Inline (Make_Implicit_If_Statement); |
| 99 |
-- This function makes an N_If_Statement node whose fields are filled |
| 100 |
-- in with the indicated values (see Sinfo), and whose Sloc field is |
| 101 |
-- is set to Sloc (Node). The effect is identical to calling function |
| 102 |
-- Nmake.Make_If_Statement except that there is a check for restriction |
| 103 |
-- No_Implicit_Conditionals, and if this restriction is being violated, |
| 104 |
-- an error message is posted on Node. |
| 105 |
|
| 106 |
function Make_Implicit_Label_Declaration |
| 107 |
(Loc : Source_Ptr; |
| 108 |
Defining_Identifier : Node_Id; |
| 109 |
Label_Construct : Node_Id) return Node_Id; |
| 110 |
-- Used to construct an implicit label declaration node, including setting |
| 111 |
-- the proper Label_Construct field (since Label_Construct is a semantic |
| 112 |
-- field, the normal call to Make_Implicit_Label_Declaration does not |
| 113 |
-- set this field). |
| 114 |
|
| 115 |
function Make_Implicit_Loop_Statement |
| 116 |
(Node : Node_Id; |
| 117 |
Statements : List_Id; |
| 118 |
Identifier : Node_Id := Empty; |
| 119 |
Iteration_Scheme : Node_Id := Empty; |
| 120 |
Has_Created_Identifier : Boolean := False; |
| 121 |
End_Label : Node_Id := Empty) return Node_Id; |
| 122 |
-- This function makes an N_Loop_Statement node whose fields are filled |
| 123 |
-- in with the indicated values (see Sinfo), and whose Sloc field is |
| 124 |
-- is set to Sloc (Node). The effect is identical to calling function |
| 125 |
-- Nmake.Make_Loop_Statement except that there is a check for restrictions |
| 126 |
-- No_Implicit_Loops and No_Implicit_Conditionals (the first applying in |
| 127 |
-- all cases, and the second only for while loops), and if one of these |
| 128 |
-- restrictions is being violated, an error message is posted on Node. |
| 129 |
|
| 130 |
function Make_Increment |
| 131 |
(Loc : Source_Ptr; Index : Entity_Id; Typ : Entity_Id) return Node_Id; |
| 132 |
-- Return an assignment statement of the form "Index := Typ'Succ (Index);" |
| 133 |
|
| 134 |
function Make_Integer_Literal |
| 135 |
(Loc : Source_Ptr; |
| 136 |
Intval : Int) return Node_Id; |
| 137 |
pragma Inline (Make_Integer_Literal); |
| 138 |
-- A convenient form of Make_Integer_Literal taking Int instead of Uint |
| 139 |
|
| 140 |
function Make_Linker_Section_Pragma |
| 141 |
(Ent : Entity_Id; |
| 142 |
Loc : Source_Ptr; |
| 143 |
Sec : String) return Node_Id; |
| 144 |
-- Construct a Linker_Section pragma for entity Ent, using string Sec as |
| 145 |
-- the section name. Loc is the Sloc value to use in building the pragma. |
| 146 |
|
| 147 |
function Make_Pragma |
| 148 |
(Sloc : Source_Ptr; |
| 149 |
Chars : Name_Id; |
| 150 |
Pragma_Argument_Associations : List_Id := No_List) return Node_Id; |
| 151 |
-- A convenient form of Make_Pragma not requiring a Pragma_Identifier |
| 152 |
-- argument (this argument is built from the value given for Chars). |
| 153 |
|
| 154 |
function Make_Raise_Constraint_Error |
| 155 |
(Sloc : Source_Ptr; |
| 156 |
Condition : Node_Id := Empty; |
| 157 |
Reason : RT_Exception_Code) return Node_Id; |
| 158 |
pragma Inline (Make_Raise_Constraint_Error); |
| 159 |
-- A convenient form of Make_Raise_Constraint_Error where the Reason |
| 160 |
-- is given simply as an enumeration value, rather than a Uint code. |
| 161 |
|
| 162 |
function Make_Raise_Program_Error |
| 163 |
(Sloc : Source_Ptr; |
| 164 |
Condition : Node_Id := Empty; |
| 165 |
Reason : RT_Exception_Code) return Node_Id; |
| 166 |
pragma Inline (Make_Raise_Program_Error); |
| 167 |
-- A convenient form of Make_Raise_Program_Error where the Reason |
| 168 |
-- is given simply as an enumeration value, rather than a Uint code. |
| 169 |
|
| 170 |
function Make_Raise_Storage_Error |
| 171 |
(Sloc : Source_Ptr; |
| 172 |
Condition : Node_Id := Empty; |
| 173 |
Reason : RT_Exception_Code) return Node_Id; |
| 174 |
pragma Inline (Make_Raise_Storage_Error); |
| 175 |
-- A convenient form of Make_Raise_Storage_Error where the Reason is given |
| 176 |
-- simply as an enumeration value, rather than a Uint code. |
| 177 |
|
| 178 |
function Make_String_Literal |
| 179 |
(Sloc : Source_Ptr; |
| 180 |
Strval : String) return Node_Id; |
| 181 |
-- A convenient form of Make_String_Literal, where the string value is |
| 182 |
-- given as a normal string instead of a String_Id value. |
| 183 |
|
| 184 |
function Make_Suppress_Block |
| 185 |
(Loc : Source_Ptr; |
| 186 |
Check : Name_Id; |
| 187 |
Stmts : List_Id) return Node_Id; |
| 188 |
-- Build a block with a pragma Suppress on Check. Stmts is the statements |
| 189 |
-- list that needs protection against the check activation. |
| 190 |
|
| 191 |
function Make_Temporary |
| 192 |
(Loc : Source_Ptr; |
| 193 |
Id : Character; |
| 194 |
Related_Node : Node_Id := Empty) return Entity_Id; |
| 195 |
-- This function should be used for all cases where a defining identifier |
| 196 |
-- is to be built with a name to be obtained by New_Internal_Name (here Id |
| 197 |
-- is the character passed as the argument to New_Internal_Name). Loc is |
| 198 |
-- the location for the Sloc value of the resulting Entity. Note that this |
| 199 |
-- can be used for all kinds of temporary defining identifiers used in |
| 200 |
-- expansion (objects, subtypes, functions etc). |
| 201 |
-- |
| 202 |
-- Related_Node is used when the defining identifier is for an object that |
| 203 |
-- captures the value of an expression (e.g. an aggregate). It should be |
| 204 |
-- set whenever possible to point to the expression that is being captured. |
| 205 |
-- This is provided to get better error messages, e.g. from CodePeer. |
| 206 |
|
| 207 |
function Make_Unsuppress_Block |
| 208 |
(Loc : Source_Ptr; |
| 209 |
Check : Name_Id; |
| 210 |
Stmts : List_Id) return Node_Id; |
| 211 |
-- Build a block with a pragma Unsuppress on Check. Stmts is the statements |
| 212 |
-- list that needs protection against the check suppression. |
| 213 |
|
| 214 |
function New_Constraint_Error (Loc : Source_Ptr) return Node_Id; |
| 215 |
-- This function builds a tree corresponding to the Ada statement |
| 216 |
-- "raise Constraint_Error" and returns the root of this tree, |
| 217 |
-- the N_Raise_Statement node. |
| 218 |
|
| 219 |
function New_Op_Node |
| 220 |
(New_Node_Kind : Node_Kind; |
| 221 |
New_Sloc : Source_Ptr) return Node_Id; |
| 222 |
-- Create node using New_Node and, if its kind is in N_Op, set its Chars |
| 223 |
-- field accordingly. |
| 224 |
|
| 225 |
function New_External_Name |
| 226 |
(Related_Id : Name_Id; |
| 227 |
Suffix : Character := ' '; |
| 228 |
Suffix_Index : Int := 0; |
| 229 |
Prefix : Character := ' ') return Name_Id; |
| 230 |
function New_External_Name |
| 231 |
(Related_Id : Name_Id; |
| 232 |
Suffix : String; |
| 233 |
Suffix_Index : Int := 0; |
| 234 |
Prefix : Character := ' ') return Name_Id; |
| 235 |
-- Builds a new entry in the names table of the form: |
| 236 |
-- |
| 237 |
-- [Prefix &] Related_Id [& Suffix] [& Suffix_Index] |
| 238 |
-- |
| 239 |
-- Prefix is prepended only if Prefix is non-blank (in which case it |
| 240 |
-- must be an upper case letter other than O,Q,U,W (which are used for |
| 241 |
-- identifier encoding, see Namet), or an underscore, and T is reserved for |
| 242 |
-- use by implicit types, and X is reserved for use by debug type encoding |
| 243 |
-- (see package Exp_Dbug). Note: the reason that Prefix is last is that it |
| 244 |
-- is almost always omitted. The notable case of Prefix being non-null is |
| 245 |
-- when it is 'T' for an implicit type. |
| 246 |
|
| 247 |
-- Suffix_Index'Image is appended only if the value of Suffix_Index is |
| 248 |
-- positive, or if Suffix_Index is negative 1, then a unique serialized |
| 249 |
-- suffix is added. If Suffix_Index is zero, then no index is appended. |
| 250 |
|
| 251 |
-- Suffix is also a single upper case letter other than O,Q,U,W,X (T is |
| 252 |
-- allowed in this context), or a string of such upper case letters. In |
| 253 |
-- the case of a string, an initial underscore may be given. |
| 254 |
-- |
| 255 |
-- The constructed name is stored using Name_Find so that it can be located |
| 256 |
-- using a subsequent Name_Find operation (i.e. it is properly hashed into |
| 257 |
-- the names table). The upper case letter given as the Suffix argument |
| 258 |
-- ensures that the name does not clash with any Ada identifier name. These |
| 259 |
-- generated names are permitted, but not required, to be made public by |
| 260 |
-- setting the flag Is_Public in the associated entity. |
| 261 |
-- |
| 262 |
-- Note: it is dubious to make them public if they have serial numbers, |
| 263 |
-- since we are counting on the serial numbers being the same for the |
| 264 |
-- clients with'ing a package and the actual compilation of the package |
| 265 |
-- with full expansion. This is a dubious assumption ??? |
| 266 |
|
| 267 |
function New_External_Name |
| 268 |
(Suffix : Character; |
| 269 |
Suffix_Index : Nat) return Name_Id; |
| 270 |
-- Builds a new entry in the names table of the form |
| 271 |
-- Suffix & Suffix_Index'Image |
| 272 |
-- where Suffix is a single upper case letter other than O,Q,U,W,X and is |
| 273 |
-- a required parameter (T is permitted). The constructed name is stored |
| 274 |
-- using Name_Find so that it can be located using a subsequent Name_Find |
| 275 |
-- operation (i.e. it is properly hashed into the names table). The upper |
| 276 |
-- case letter given as the Suffix argument ensures that the name does |
| 277 |
-- not clash with any Ada identifier name. These generated names are |
| 278 |
-- permitted, but not required, to be made public by setting the flag |
| 279 |
-- Is_Public in the associated entity. |
| 280 |
-- |
| 281 |
-- Note: it is dubious to make these public since they have serial numbers, |
| 282 |
-- which means we are counting on the serial numbers being the same for the |
| 283 |
-- clients with'ing a package and the actual compilation of the package |
| 284 |
-- with full expansion. This is a dubious assumption ??? |
| 285 |
|
| 286 |
function New_Internal_Name (Id_Char : Character) return Name_Id; |
| 287 |
-- Id_Char is an upper case letter other than O,Q,U,W (which are reserved |
| 288 |
-- for identifier encoding (see Namet package for details) and X which is |
| 289 |
-- used for debug encoding (see Exp_Dbug). The letter T is permitted, but |
| 290 |
-- is reserved by convention for the case of internally generated types. |
| 291 |
-- The result of the call is a new generated unique name of the form XyyyU |
| 292 |
-- where X is Id_Char, yyy is a unique serial number, and U is either a |
| 293 |
-- lower case s or b indicating if the current unit is a spec or a body. |
| 294 |
-- |
| 295 |
-- The name is entered into the names table using Name_Enter rather than |
| 296 |
-- Name_Find, because there can never be a need to locate the entry using |
| 297 |
-- the Name_Find procedure later on. Names created by New_Internal_Name |
| 298 |
-- are guaranteed to be consistent from one compilation to another (i.e. |
| 299 |
-- if the identical unit is compiled with a semantically consistent set |
| 300 |
-- of sources, the numbers will be consistent). This means that it is fine |
| 301 |
-- to use these as public symbols. |
| 302 |
-- |
| 303 |
-- Note: Nearly all uses of this function are via calls to Make_Temporary, |
| 304 |
-- but there are just a few cases where it is called directly. |
| 305 |
-- |
| 306 |
-- Note: despite the guarantee of consistency stated above, it is dubious |
| 307 |
-- to make these public since they have serial numbers, which means we are |
| 308 |
-- counting on the serial numbers being the same for the clients with'ing |
| 309 |
-- a package and the actual compilation of the package with full expansion. |
| 310 |
-- This is a dubious assumption ??? |
| 311 |
|
| 312 |
function New_Occurrence_Of |
| 313 |
(Def_Id : Entity_Id; |
| 314 |
Loc : Source_Ptr) return Node_Id; |
| 315 |
-- New_Occurrence_Of creates an N_Identifier node that is an occurrence of |
| 316 |
-- the defining identifier Def_Id. The Entity of the result is Def_Id. The |
| 317 |
-- Etype of the result is Def_Id for types, and Etype (Def_Id) otherwise. |
| 318 |
-- Is_Static_Expression is set if this call creates an occurrence of an |
| 319 |
-- enumeration literal. |
| 320 |
|
| 321 |
function New_Suffixed_Name |
| 322 |
(Related_Id : Name_Id; |
| 323 |
Suffix : String) return Name_Id; |
| 324 |
-- This function is used to create special suffixed names used by the |
| 325 |
-- debugger. Suffix is a string of upper case letters, used to construct |
| 326 |
-- the required name. For instance, the special type used to record the |
| 327 |
-- fixed-point small is called typ_SMALL where typ is the name of the |
| 328 |
-- fixed-point type (as passed in Related_Id), and Suffix is "SMALL". |
| 329 |
|
| 330 |
function Sel_Comp (Pre, Sel : String; Loc : Source_Ptr) return Node_Id; |
| 331 |
function Sel_Comp (Pre : Node_Id; Sel : String) return Node_Id; |
| 332 |
-- Create a selected component of the form Pre.Sel; that is, Pre is the |
| 333 |
-- prefix, and Sel is the selector name. |
| 334 |
|
| 335 |
function OK_Convert_To (Typ : Entity_Id; Expr : Node_Id) return Node_Id; |
| 336 |
-- Like Convert_To, except that a conversion node is always generated, and |
| 337 |
-- the Conversion_OK flag is set on this conversion node. |
| 338 |
|
| 339 |
function Unchecked_Convert_To |
| 340 |
(Typ : Entity_Id; |
| 341 |
Expr : Node_Id) return Node_Id; |
| 342 |
-- Like Convert_To, but if a conversion is actually needed, constructs an |
| 343 |
-- N_Unchecked_Type_Conversion node to do the required conversion. Unlike |
| 344 |
-- Convert_To, a new node is not required if Expr is already of the correct |
| 345 |
-- BASE type, and if a new node is created, the Parent of Expr is copied to |
| 346 |
-- it. |
| 347 |
|
| 348 |
------------------------------------- |
| 349 |
-- Subprograms for Use by Gnat1drv -- |
| 350 |
------------------------------------- |
| 351 |
|
| 352 |
function Make_Id (Str : Text_Buffer) return Node_Id; |
| 353 |
function Make_SC (Pre, Sel : Node_Id) return Node_Id; |
| 354 |
procedure Set_NOD (Unit : Node_Id); |
| 355 |
procedure Set_NSA (Asp : Name_Id; OK : out Boolean); |
| 356 |
procedure Set_NUA (Attr : Name_Id; OK : out Boolean); |
| 357 |
procedure Set_NUP (Prag : Name_Id; OK : out Boolean); |
| 358 |
-- Subprograms for call to Get_Target_Parameters in Gnat1drv, see spec |
| 359 |
-- of package Targparm for full description of these four subprograms. |
| 360 |
-- These have to be declared at the top level of a package (accessibility |
| 361 |
-- issues), and Gnat1drv is a procedure, so they can't go there. |
| 362 |
|
| 363 |
end Tbuild; |