| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- M U T A B L Y _ T A G G E D -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2024-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 Atree; use Atree; |
| 27 |
with Casing; use Casing; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Exp_Util; use Exp_Util; |
| 32 |
with Namet; use Namet; |
| 33 |
with Nlists; use Nlists; |
| 34 |
with Nmake; use Nmake; |
| 35 |
with Rtsfind; use Rtsfind; |
| 36 |
with Snames; use Snames; |
| 37 |
with Sem_Util; use Sem_Util; |
| 38 |
with Sinfo; use Sinfo; |
| 39 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 40 |
with Sinfo.Utils; use Sinfo.Utils; |
| 41 |
with Stringt; use Stringt; |
| 42 |
with Tbuild; use Tbuild; |
| 43 |
with Uintp; use Uintp; |
| 44 |
|
| 45 |
package body Mutably_Tagged is |
| 46 |
|
| 47 |
--------------------------------------- |
| 48 |
-- Corresponding_Mutably_Tagged_Type -- |
| 49 |
--------------------------------------- |
| 50 |
|
| 51 |
function Corresponding_Mutably_Tagged_Type |
| 52 |
(CW_Equiv_Typ : Entity_Id) return Entity_Id |
| 53 |
is |
| 54 |
begin |
| 55 |
return Class_Wide_Type (Parent_Subtype (CW_Equiv_Typ)); |
| 56 |
end Corresponding_Mutably_Tagged_Type; |
| 57 |
|
| 58 |
---------------------------------------- |
| 59 |
-- Depends_On_Mutably_Tagged_Ext_Comp -- |
| 60 |
---------------------------------------- |
| 61 |
|
| 62 |
function Depends_On_Mutably_Tagged_Ext_Comp (N : Node_Id) return Boolean is |
| 63 |
Typ : Entity_Id; |
| 64 |
Typ_Comp : Entity_Id; |
| 65 |
Curr : Node_Id; |
| 66 |
Prev : Node_Id; |
| 67 |
begin |
| 68 |
-- Move through each prefix until we hit a type conversion from a |
| 69 |
-- mutably tagged type then check if the referenced component exists in |
| 70 |
-- the root type or an extension. |
| 71 |
|
| 72 |
Curr := N; |
| 73 |
while Has_Prefix (Curr) loop |
| 74 |
Prev := Curr; |
| 75 |
Curr := Prefix (Curr); |
| 76 |
|
| 77 |
-- Find a prefix which is a type conversion from a mutably tagged |
| 78 |
-- type in some form - either class-wide equivalent type or |
| 79 |
-- directly a mutably tagged type. |
| 80 |
|
| 81 |
if Nkind (Curr) in N_Unchecked_Type_Conversion |
| 82 |
| N_Type_Conversion |
| 83 |
and then (Is_Mutably_Tagged_CW_Equivalent_Type |
| 84 |
(Etype (Expression (Curr))) |
| 85 |
or else Is_Mutably_Tagged_Type |
| 86 |
(Etype (Expression (Curr)))) |
| 87 |
|
| 88 |
-- Verify that the prefix references a component |
| 89 |
|
| 90 |
and then Is_Entity_Name (Selector_Name (Prev)) |
| 91 |
and then Ekind (Entity (Selector_Name (Prev))) |
| 92 |
= E_Component |
| 93 |
then |
| 94 |
-- Obtain the root type |
| 95 |
|
| 96 |
Typ := Etype (if Is_Mutably_Tagged_Type |
| 97 |
(Etype (Expression (Curr))) |
| 98 |
then |
| 99 |
Etype (Expression (Curr)) |
| 100 |
else |
| 101 |
Corresponding_Mutably_Tagged_Type |
| 102 |
(Etype (Expression (Curr)))); |
| 103 |
|
| 104 |
-- Move through the components of the root type looking for a |
| 105 |
-- match to the reference component. |
| 106 |
|
| 107 |
Typ_Comp := First_Component (Typ); |
| 108 |
while Present (Typ_Comp) loop |
| 109 |
|
| 110 |
-- When there is a match we know the component reference |
| 111 |
-- doesn't depend on a type extension. |
| 112 |
|
| 113 |
if Chars (Typ_Comp) = Chars (Entity (Selector_Name (Prev))) then |
| 114 |
return False; |
| 115 |
end if; |
| 116 |
|
| 117 |
Next_Component (Typ_Comp); |
| 118 |
end loop; |
| 119 |
|
| 120 |
-- Otherwise, the component must depend on an extension |
| 121 |
|
| 122 |
return True; |
| 123 |
end if; |
| 124 |
end loop; |
| 125 |
|
| 126 |
-- If we get here then we know we don't have any sort of relevant type |
| 127 |
-- conversion from a mutably tagged object. |
| 128 |
|
| 129 |
return False; |
| 130 |
end Depends_On_Mutably_Tagged_Ext_Comp; |
| 131 |
|
| 132 |
------------------------------------------------------ |
| 133 |
-- Get_Corresponding_Mutably_Tagged_Type_If_Present -- |
| 134 |
------------------------------------------------------ |
| 135 |
|
| 136 |
function Get_Corresponding_Mutably_Tagged_Type_If_Present |
| 137 |
(Typ : Entity_Id) return Entity_Id |
| 138 |
is |
| 139 |
begin |
| 140 |
if Is_Mutably_Tagged_CW_Equivalent_Type (Typ) then |
| 141 |
return Corresponding_Mutably_Tagged_Type (Typ); |
| 142 |
end if; |
| 143 |
|
| 144 |
return Typ; |
| 145 |
end Get_Corresponding_Mutably_Tagged_Type_If_Present; |
| 146 |
|
| 147 |
---------------------------------------------- |
| 148 |
-- Get_Corresponding_Tagged_Type_If_Present -- |
| 149 |
---------------------------------------------- |
| 150 |
|
| 151 |
function Get_Corresponding_Tagged_Type_If_Present |
| 152 |
(Typ : Entity_Id) return Entity_Id |
| 153 |
is |
| 154 |
begin |
| 155 |
-- Obtain the related tagged type for the class-wide mutably |
| 156 |
-- tagged type associated with the class-wide equivalent type. |
| 157 |
|
| 158 |
if Is_Mutably_Tagged_CW_Equivalent_Type (Typ) then |
| 159 |
return Parent_Subtype (Typ); |
| 160 |
end if; |
| 161 |
|
| 162 |
return Typ; |
| 163 |
end Get_Corresponding_Tagged_Type_If_Present; |
| 164 |
|
| 165 |
---------------------------------- |
| 166 |
-- Is_Mutably_Tagged_Conversion -- |
| 167 |
---------------------------------- |
| 168 |
|
| 169 |
function Is_Mutably_Tagged_Conversion (N : Node_Id) return Boolean is |
| 170 |
begin |
| 171 |
return Nkind (N) = N_Unchecked_Type_Conversion |
| 172 |
and then Is_Mutably_Tagged_CW_Equivalent_Type |
| 173 |
(Etype (Expression (N))); |
| 174 |
end Is_Mutably_Tagged_Conversion; |
| 175 |
|
| 176 |
------------------------------------------ |
| 177 |
-- Is_Mutably_Tagged_CW_Equivalent_Type -- |
| 178 |
------------------------------------------ |
| 179 |
|
| 180 |
function Is_Mutably_Tagged_CW_Equivalent_Type |
| 181 |
(Typ : Entity_Id) return Boolean |
| 182 |
is |
| 183 |
begin |
| 184 |
-- First assure Typ is OK to test since this function can be called in |
| 185 |
-- a context where analysis failed. |
| 186 |
|
| 187 |
return Present (Typ) |
| 188 |
and then not Error_Posted (Typ) |
| 189 |
|
| 190 |
-- Finally check Typ is a class-wide equivalent type which has an |
| 191 |
-- associated mutably tagged class-wide type (e.g. it is a class-wide |
| 192 |
-- type with a size clause). |
| 193 |
|
| 194 |
and then Is_Class_Wide_Equivalent_Type (Typ) |
| 195 |
and then Present (Parent_Subtype (Typ)) |
| 196 |
and then Present (Class_Wide_Type (Parent_Subtype (Typ))) |
| 197 |
and then Has_Size_Clause (Corresponding_Mutably_Tagged_Type (Typ)); |
| 198 |
end Is_Mutably_Tagged_CW_Equivalent_Type; |
| 199 |
|
| 200 |
-------------------------------- |
| 201 |
-- Make_CW_Size_Compile_Check -- |
| 202 |
-------------------------------- |
| 203 |
|
| 204 |
function Make_CW_Size_Compile_Check |
| 205 |
(New_Typ : Entity_Id; |
| 206 |
Mut_Tag_Typ : Entity_Id) return Node_Id |
| 207 |
is |
| 208 |
Loc : constant Source_Ptr := Sloc (New_Typ); |
| 209 |
|
| 210 |
CW_Size : constant Uint := RM_Size (Mut_Tag_Typ); |
| 211 |
|
| 212 |
function To_Mixed_Case (S : String) return String; |
| 213 |
-- convert string to mixed case |
| 214 |
|
| 215 |
------------------- |
| 216 |
-- To_Mixed_Case -- |
| 217 |
------------------- |
| 218 |
|
| 219 |
function To_Mixed_Case (S : String) return String is |
| 220 |
Buf : Bounded_String; |
| 221 |
begin |
| 222 |
Append (Buf, S); |
| 223 |
Set_Casing (Buf, Mixed_Case); |
| 224 |
return +Buf; |
| 225 |
end To_Mixed_Case; |
| 226 |
|
| 227 |
-- Start of processing for Make_CW_Size_Compile_Check |
| 228 |
|
| 229 |
begin |
| 230 |
-- Build a Compile_Time_Error pragma in order to defer the |
| 231 |
-- (compile-time) size check until after the back end has |
| 232 |
-- determined sizes. |
| 233 |
-- |
| 234 |
-- It would be nice if we could somehow include the value of |
| 235 |
-- New_Type'Size in the error message, but it is not clear how to |
| 236 |
-- accomplish that with the current FE/BE interfaces. |
| 237 |
|
| 238 |
-- Get New_Typ's name (in mixed case) into the name buffer; |
| 239 |
-- this is used immediately afterwards in the Make_Pragma call. |
| 240 |
|
| 241 |
Get_Decoded_Name_String (Chars (New_Typ)); |
| 242 |
Set_Casing (Mixed_Case); |
| 243 |
|
| 244 |
return |
| 245 |
Make_Pragma (Loc, |
| 246 |
Chars => Name_Compile_Time_Error, |
| 247 |
Pragma_Argument_Associations => New_List ( |
| 248 |
Make_Pragma_Argument_Association (Loc, |
| 249 |
Expression => ( |
| 250 |
Make_Op_Gt (Loc, |
| 251 |
Left_Opnd => |
| 252 |
Make_Attribute_Reference (Loc, |
| 253 |
Attribute_Name => Name_Size, |
| 254 |
Prefix => |
| 255 |
New_Occurrence_Of (New_Typ, Loc)), |
| 256 |
Right_Opnd => |
| 257 |
Make_Integer_Literal (Loc, CW_Size)))), |
| 258 |
Make_Pragma_Argument_Association (Loc, |
| 259 |
Expression => |
| 260 |
Make_String_Literal (Loc, |
| 261 |
To_String (String_From_Name_Buffer) |
| 262 |
& "'Size exceeds " |
| 263 |
& To_Mixed_Case ( |
| 264 |
To_String (Fully_Qualified_Name_String |
| 265 |
(Find_Specific_Type (Mut_Tag_Typ), |
| 266 |
Append_NUL => False))) |
| 267 |
& "'Size'Class limit of " |
| 268 |
& UI_Image (CW_Size))))); |
| 269 |
end Make_CW_Size_Compile_Check; |
| 270 |
|
| 271 |
------------------------------------ |
| 272 |
-- Make_Mutably_Tagged_Conversion -- |
| 273 |
------------------------------------ |
| 274 |
|
| 275 |
procedure Make_Mutably_Tagged_Conversion |
| 276 |
(N : Node_Id; |
| 277 |
Typ : Entity_Id := Empty; |
| 278 |
Force : Boolean := False) |
| 279 |
is |
| 280 |
Conv_Typ : constant Entity_Id := |
| 281 |
|
| 282 |
-- When Typ is not present, we obtain it at this point |
| 283 |
|
| 284 |
(if Present (Typ) then |
| 285 |
Typ |
| 286 |
else |
| 287 |
Corresponding_Mutably_Tagged_Type (Etype (N))); |
| 288 |
|
| 289 |
begin |
| 290 |
-- Allow "forcing" the rewrite to an unchecked conversion |
| 291 |
|
| 292 |
if Force |
| 293 |
|
| 294 |
-- Otherwise, don't make the conversion when N is on the left-hand |
| 295 |
-- side of the assignment, in cases where we need the actual type |
| 296 |
-- such as a subtype or object renaming declaration, or a generic or |
| 297 |
-- parameter specification. |
| 298 |
|
| 299 |
-- Additionally, prevent generation of the conversion if N is already |
| 300 |
-- part of an unchecked conversion or a part of a selected component. |
| 301 |
|
| 302 |
or else (not Known_To_Be_Assigned (N, Only_LHS => True) |
| 303 |
and then (No (Parent (N)) |
| 304 |
or else Nkind (Parent (N)) |
| 305 |
not in N_Selected_Component |
| 306 |
| N_Subtype_Declaration |
| 307 |
| N_Parameter_Specification |
| 308 |
| N_Generic_Association |
| 309 |
| N_Unchecked_Type_Conversion |
| 310 |
| N_Object_Renaming_Declaration)) |
| 311 |
then |
| 312 |
-- Exclude the case where we have a 'Size so that we get the proper |
| 313 |
-- size of the class-wide equivalent type. Are there other cases ??? |
| 314 |
|
| 315 |
if Present (Parent (N)) |
| 316 |
and then Nkind (Parent (N)) = N_Attribute_Reference |
| 317 |
and then Attribute_Name (Parent (N)) in Name_Size |
| 318 |
then |
| 319 |
return; |
| 320 |
end if; |
| 321 |
|
| 322 |
-- Create the conversion |
| 323 |
|
| 324 |
Rewrite (N, |
| 325 |
Unchecked_Convert_To |
| 326 |
(Conv_Typ, Relocate_Node (N))); |
| 327 |
end if; |
| 328 |
end Make_Mutably_Tagged_Conversion; |
| 329 |
|
| 330 |
---------------------------------- |
| 331 |
-- Make_Mutably_Tagged_CW_Check -- |
| 332 |
---------------------------------- |
| 333 |
|
| 334 |
function Make_Mutably_Tagged_CW_Check |
| 335 |
(N : Node_Id; |
| 336 |
Tag : Node_Id) return Node_Id |
| 337 |
is |
| 338 |
Loc : constant Source_Ptr := Sloc (N); |
| 339 |
|
| 340 |
-- Displace the pointer to the base of the objects applying 'Address, |
| 341 |
-- which is later expanded into a call to RE_Base_Address. |
| 342 |
|
| 343 |
N_Tag : constant Node_Id := |
| 344 |
Make_Explicit_Dereference (Loc, |
| 345 |
Prefix => |
| 346 |
Unchecked_Convert_To (RTE (RE_Tag_Ptr), |
| 347 |
Make_Attribute_Reference (Loc, |
| 348 |
Prefix => Duplicate_Subexpr (N), |
| 349 |
Attribute_Name => Name_Address))); |
| 350 |
begin |
| 351 |
-- Generate the runtime call to test class-wide membership |
| 352 |
|
| 353 |
return |
| 354 |
Make_Raise_Constraint_Error (Loc, |
| 355 |
Reason => CE_Tag_Check_Failed, |
| 356 |
Condition => |
| 357 |
Make_Op_Not (Loc, |
| 358 |
Make_Function_Call (Loc, |
| 359 |
Parameter_Associations => New_List (N_Tag, Tag), |
| 360 |
Name => |
| 361 |
New_Occurrence_Of (RTE (RE_CW_Membership), Loc)))); |
| 362 |
end Make_Mutably_Tagged_CW_Check; |
| 363 |
|
| 364 |
end Mutably_Tagged; |