| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- B I N D O . U N I T S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2019-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 Bindo.Writers; |
| 27 |
use Bindo.Writers; |
| 28 |
use Bindo.Writers.Phase_Writers; |
| 29 |
|
| 30 |
package body Bindo.Units is |
| 31 |
|
| 32 |
------------------- |
| 33 |
-- Signature set -- |
| 34 |
------------------- |
| 35 |
|
| 36 |
package Signature_Sets is new Membership_Sets |
| 37 |
(Element_Type => Invocation_Signature_Id, |
| 38 |
"=" => "=", |
| 39 |
Hash => Hash_Invocation_Signature); |
| 40 |
|
| 41 |
----------------- |
| 42 |
-- Global data -- |
| 43 |
----------------- |
| 44 |
|
| 45 |
-- The following set stores all invocation signatures that appear in |
| 46 |
-- elaborable units. |
| 47 |
|
| 48 |
Elaborable_Constructs : Signature_Sets.Membership_Set := Signature_Sets.Nil; |
| 49 |
|
| 50 |
-- The following set stores all units the need to be elaborated |
| 51 |
|
| 52 |
Elaborable_Units : Unit_Sets.Membership_Set := Unit_Sets.Nil; |
| 53 |
|
| 54 |
----------------------- |
| 55 |
-- Local subprograms -- |
| 56 |
----------------------- |
| 57 |
|
| 58 |
function Corresponding_Unit (Nam : Name_Id) return Unit_Id; |
| 59 |
pragma Inline (Corresponding_Unit); |
| 60 |
-- Obtain the unit which corresponds to name Nam |
| 61 |
|
| 62 |
function Is_Stand_Alone_Library_Unit (U_Id : Unit_Id) return Boolean; |
| 63 |
pragma Inline (Is_Stand_Alone_Library_Unit); |
| 64 |
-- Determine whether unit U_Id is part of a stand-alone library |
| 65 |
|
| 66 |
procedure Process_Invocation_Construct (IC_Id : Invocation_Construct_Id); |
| 67 |
pragma Inline (Process_Invocation_Construct); |
| 68 |
-- Process invocation construct IC_Id by adding its signature to set |
| 69 |
-- Elaborable_Constructs_Set. |
| 70 |
|
| 71 |
procedure Process_Invocation_Constructs (U_Id : Unit_Id); |
| 72 |
pragma Inline (Process_Invocation_Constructs); |
| 73 |
-- Process all invocation constructs of unit U_Id for classification |
| 74 |
-- purposes. |
| 75 |
|
| 76 |
procedure Process_Unit (U_Id : Unit_Id); |
| 77 |
pragma Inline (Process_Unit); |
| 78 |
-- Process unit U_Id for unit classification purposes |
| 79 |
|
| 80 |
------------------------------ |
| 81 |
-- Collect_Elaborable_Units -- |
| 82 |
------------------------------ |
| 83 |
|
| 84 |
procedure Collect_Elaborable_Units is |
| 85 |
begin |
| 86 |
Start_Phase (Unit_Collection); |
| 87 |
|
| 88 |
for U_Id in ALI.Units.First .. ALI.Units.Last loop |
| 89 |
Process_Unit (U_Id); |
| 90 |
end loop; |
| 91 |
|
| 92 |
End_Phase (Unit_Collection); |
| 93 |
end Collect_Elaborable_Units; |
| 94 |
|
| 95 |
------------------------ |
| 96 |
-- Corresponding_Body -- |
| 97 |
------------------------ |
| 98 |
|
| 99 |
function Corresponding_Body (U_Id : Unit_Id) return Unit_Id is |
| 100 |
pragma Assert (Present (U_Id)); |
| 101 |
|
| 102 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 103 |
|
| 104 |
begin |
| 105 |
pragma Assert (U_Rec.Utype = Is_Spec); |
| 106 |
return Result : constant Unit_Id := U_Id - 1 do |
| 107 |
pragma Assert (ALI.Units.Table (Result).Utype = Is_Body); |
| 108 |
end return; |
| 109 |
end Corresponding_Body; |
| 110 |
|
| 111 |
------------------------ |
| 112 |
-- Corresponding_Spec -- |
| 113 |
------------------------ |
| 114 |
|
| 115 |
function Corresponding_Spec (U_Id : Unit_Id) return Unit_Id is |
| 116 |
pragma Assert (Present (U_Id)); |
| 117 |
|
| 118 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 119 |
|
| 120 |
begin |
| 121 |
pragma Assert (U_Rec.Utype = Is_Body); |
| 122 |
return Result : constant Unit_Id := U_Id + 1 do |
| 123 |
pragma Assert (ALI.Units.Table (Result).Utype = Is_Spec); |
| 124 |
end return; |
| 125 |
end Corresponding_Spec; |
| 126 |
|
| 127 |
------------------------ |
| 128 |
-- Corresponding_Unit -- |
| 129 |
------------------------ |
| 130 |
|
| 131 |
function Corresponding_Unit (FNam : File_Name_Type) return Unit_Id is |
| 132 |
begin |
| 133 |
return Corresponding_Unit (Name_Id (FNam)); |
| 134 |
end Corresponding_Unit; |
| 135 |
|
| 136 |
------------------------ |
| 137 |
-- Corresponding_Unit -- |
| 138 |
------------------------ |
| 139 |
|
| 140 |
function Corresponding_Unit (Nam : Name_Id) return Unit_Id is |
| 141 |
begin |
| 142 |
return Unit_Id (Get_Name_Table_Int (Nam)); |
| 143 |
end Corresponding_Unit; |
| 144 |
|
| 145 |
------------------------ |
| 146 |
-- Corresponding_Unit -- |
| 147 |
------------------------ |
| 148 |
|
| 149 |
function Corresponding_Unit (UNam : Unit_Name_Type) return Unit_Id is |
| 150 |
begin |
| 151 |
return Corresponding_Unit (Name_Id (UNam)); |
| 152 |
end Corresponding_Unit; |
| 153 |
|
| 154 |
--------------- |
| 155 |
-- File_Name -- |
| 156 |
--------------- |
| 157 |
|
| 158 |
function File_Name (U_Id : Unit_Id) return File_Name_Type is |
| 159 |
pragma Assert (Present (U_Id)); |
| 160 |
|
| 161 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 162 |
|
| 163 |
begin |
| 164 |
return U_Rec.Sfile; |
| 165 |
end File_Name; |
| 166 |
|
| 167 |
-------------------- |
| 168 |
-- Finalize_Units -- |
| 169 |
-------------------- |
| 170 |
|
| 171 |
procedure Finalize_Units is |
| 172 |
begin |
| 173 |
Signature_Sets.Destroy (Elaborable_Constructs); |
| 174 |
Unit_Sets.Destroy (Elaborable_Units); |
| 175 |
end Finalize_Units; |
| 176 |
|
| 177 |
------------------------------ |
| 178 |
-- For_Each_Elaborable_Unit -- |
| 179 |
------------------------------ |
| 180 |
|
| 181 |
procedure For_Each_Elaborable_Unit (Processor : Unit_Processor_Ptr) is |
| 182 |
Iter : Elaborable_Units_Iterator; |
| 183 |
U_Id : Unit_Id; |
| 184 |
|
| 185 |
begin |
| 186 |
Iter := Iterate_Elaborable_Units; |
| 187 |
while Has_Next (Iter) loop |
| 188 |
Next (Iter, U_Id); |
| 189 |
|
| 190 |
Processor.all (U_Id); |
| 191 |
end loop; |
| 192 |
end For_Each_Elaborable_Unit; |
| 193 |
|
| 194 |
------------------- |
| 195 |
-- For_Each_Unit -- |
| 196 |
------------------- |
| 197 |
|
| 198 |
procedure For_Each_Unit (Processor : Unit_Processor_Ptr) is |
| 199 |
begin |
| 200 |
for U_Id in ALI.Units.First .. ALI.Units.Last loop |
| 201 |
Processor.all (U_Id); |
| 202 |
end loop; |
| 203 |
end For_Each_Unit; |
| 204 |
|
| 205 |
-------------- |
| 206 |
-- Has_Next -- |
| 207 |
-------------- |
| 208 |
|
| 209 |
function Has_Next (Iter : Elaborable_Units_Iterator) return Boolean is |
| 210 |
begin |
| 211 |
return Unit_Sets.Has_Next (Unit_Sets.Iterator (Iter)); |
| 212 |
end Has_Next; |
| 213 |
|
| 214 |
----------------------------- |
| 215 |
-- Has_No_Elaboration_Code -- |
| 216 |
----------------------------- |
| 217 |
|
| 218 |
function Has_No_Elaboration_Code (U_Id : Unit_Id) return Boolean is |
| 219 |
pragma Assert (Present (U_Id)); |
| 220 |
|
| 221 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 222 |
|
| 223 |
begin |
| 224 |
return U_Rec.No_Elab; |
| 225 |
end Has_No_Elaboration_Code; |
| 226 |
|
| 227 |
------------------------------- |
| 228 |
-- Hash_Invocation_Signature -- |
| 229 |
------------------------------- |
| 230 |
|
| 231 |
function Hash_Invocation_Signature |
| 232 |
(IS_Id : Invocation_Signature_Id) return Bucket_Range_Type |
| 233 |
is |
| 234 |
begin |
| 235 |
pragma Assert (Present (IS_Id)); |
| 236 |
|
| 237 |
return Bucket_Range_Type (IS_Id); |
| 238 |
end Hash_Invocation_Signature; |
| 239 |
|
| 240 |
--------------- |
| 241 |
-- Hash_Unit -- |
| 242 |
--------------- |
| 243 |
|
| 244 |
function Hash_Unit (U_Id : Unit_Id) return Bucket_Range_Type is |
| 245 |
begin |
| 246 |
pragma Assert (Present (U_Id)); |
| 247 |
|
| 248 |
return Bucket_Range_Type (U_Id); |
| 249 |
end Hash_Unit; |
| 250 |
|
| 251 |
---------------------- |
| 252 |
-- Initialize_Units -- |
| 253 |
---------------------- |
| 254 |
|
| 255 |
procedure Initialize_Units is |
| 256 |
begin |
| 257 |
Elaborable_Constructs := Signature_Sets.Create (Number_Of_Units); |
| 258 |
Elaborable_Units := Unit_Sets.Create (Number_Of_Units); |
| 259 |
end Initialize_Units; |
| 260 |
|
| 261 |
------------------------------- |
| 262 |
-- Invocation_Graph_Encoding -- |
| 263 |
------------------------------- |
| 264 |
|
| 265 |
function Invocation_Graph_Encoding |
| 266 |
(U_Id : Unit_Id) return Invocation_Graph_Encoding_Kind |
| 267 |
is |
| 268 |
pragma Assert (Present (U_Id)); |
| 269 |
|
| 270 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 271 |
U_ALI : ALIs_Record renames ALI.ALIs.Table (U_Rec.My_ALI); |
| 272 |
|
| 273 |
begin |
| 274 |
return U_ALI.Invocation_Graph_Encoding; |
| 275 |
end Invocation_Graph_Encoding; |
| 276 |
|
| 277 |
------------------------------- |
| 278 |
-- Is_Dynamically_Elaborated -- |
| 279 |
------------------------------- |
| 280 |
|
| 281 |
function Is_Dynamically_Elaborated (U_Id : Unit_Id) return Boolean is |
| 282 |
pragma Assert (Present (U_Id)); |
| 283 |
|
| 284 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 285 |
|
| 286 |
begin |
| 287 |
return U_Rec.Dynamic_Elab; |
| 288 |
end Is_Dynamically_Elaborated; |
| 289 |
|
| 290 |
---------------------- |
| 291 |
-- Is_Internal_Unit -- |
| 292 |
---------------------- |
| 293 |
|
| 294 |
function Is_Internal_Unit (U_Id : Unit_Id) return Boolean is |
| 295 |
pragma Assert (Present (U_Id)); |
| 296 |
|
| 297 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 298 |
|
| 299 |
begin |
| 300 |
return U_Rec.Internal; |
| 301 |
end Is_Internal_Unit; |
| 302 |
|
| 303 |
------------------------ |
| 304 |
-- Is_Predefined_Unit -- |
| 305 |
------------------------ |
| 306 |
|
| 307 |
function Is_Predefined_Unit (U_Id : Unit_Id) return Boolean is |
| 308 |
pragma Assert (Present (U_Id)); |
| 309 |
|
| 310 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 311 |
|
| 312 |
begin |
| 313 |
return U_Rec.Predefined; |
| 314 |
end Is_Predefined_Unit; |
| 315 |
|
| 316 |
--------------------------------- |
| 317 |
-- Is_Stand_Alone_Library_Unit -- |
| 318 |
--------------------------------- |
| 319 |
|
| 320 |
function Is_Stand_Alone_Library_Unit (U_Id : Unit_Id) return Boolean is |
| 321 |
pragma Assert (Present (U_Id)); |
| 322 |
|
| 323 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 324 |
|
| 325 |
begin |
| 326 |
return U_Rec.SAL_Interface; |
| 327 |
end Is_Stand_Alone_Library_Unit; |
| 328 |
|
| 329 |
------------------------------ |
| 330 |
-- Iterate_Elaborable_Units -- |
| 331 |
------------------------------ |
| 332 |
|
| 333 |
function Iterate_Elaborable_Units return Elaborable_Units_Iterator is |
| 334 |
begin |
| 335 |
return Elaborable_Units_Iterator (Unit_Sets.Iterate (Elaborable_Units)); |
| 336 |
end Iterate_Elaborable_Units; |
| 337 |
|
| 338 |
---------- |
| 339 |
-- Name -- |
| 340 |
---------- |
| 341 |
|
| 342 |
function Name (U_Id : Unit_Id) return Unit_Name_Type is |
| 343 |
pragma Assert (Present (U_Id)); |
| 344 |
|
| 345 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 346 |
|
| 347 |
begin |
| 348 |
return U_Rec.Uname; |
| 349 |
end Name; |
| 350 |
|
| 351 |
----------------------- |
| 352 |
-- Needs_Elaboration -- |
| 353 |
----------------------- |
| 354 |
|
| 355 |
function Needs_Elaboration |
| 356 |
(IS_Id : Invocation_Signature_Id) return Boolean |
| 357 |
is |
| 358 |
begin |
| 359 |
pragma Assert (Present (IS_Id)); |
| 360 |
|
| 361 |
return Signature_Sets.Contains (Elaborable_Constructs, IS_Id); |
| 362 |
end Needs_Elaboration; |
| 363 |
|
| 364 |
----------------------- |
| 365 |
-- Needs_Elaboration -- |
| 366 |
----------------------- |
| 367 |
|
| 368 |
function Needs_Elaboration (U_Id : Unit_Id) return Boolean is |
| 369 |
begin |
| 370 |
pragma Assert (Present (U_Id)); |
| 371 |
|
| 372 |
return Unit_Sets.Contains (Elaborable_Units, U_Id); |
| 373 |
end Needs_Elaboration; |
| 374 |
|
| 375 |
---------- |
| 376 |
-- Next -- |
| 377 |
---------- |
| 378 |
|
| 379 |
procedure Next |
| 380 |
(Iter : in out Elaborable_Units_Iterator; |
| 381 |
U_Id : out Unit_Id) |
| 382 |
is |
| 383 |
begin |
| 384 |
Unit_Sets.Next (Unit_Sets.Iterator (Iter), U_Id); |
| 385 |
end Next; |
| 386 |
|
| 387 |
-------------------------------- |
| 388 |
-- Number_Of_Elaborable_Units -- |
| 389 |
-------------------------------- |
| 390 |
|
| 391 |
function Number_Of_Elaborable_Units return Natural is |
| 392 |
begin |
| 393 |
return Unit_Sets.Size (Elaborable_Units); |
| 394 |
end Number_Of_Elaborable_Units; |
| 395 |
|
| 396 |
--------------------- |
| 397 |
-- Number_Of_Units -- |
| 398 |
--------------------- |
| 399 |
|
| 400 |
function Number_Of_Units return Natural is |
| 401 |
begin |
| 402 |
return Natural (ALI.Units.Last) - Natural (ALI.Units.First) + 1; |
| 403 |
end Number_Of_Units; |
| 404 |
|
| 405 |
---------------------------------- |
| 406 |
-- Process_Invocation_Construct -- |
| 407 |
---------------------------------- |
| 408 |
|
| 409 |
procedure Process_Invocation_Construct (IC_Id : Invocation_Construct_Id) is |
| 410 |
pragma Assert (Present (IC_Id)); |
| 411 |
|
| 412 |
IS_Id : constant Invocation_Signature_Id := Signature (IC_Id); |
| 413 |
|
| 414 |
pragma Assert (Present (IS_Id)); |
| 415 |
|
| 416 |
begin |
| 417 |
Signature_Sets.Insert (Elaborable_Constructs, IS_Id); |
| 418 |
end Process_Invocation_Construct; |
| 419 |
|
| 420 |
----------------------------------- |
| 421 |
-- Process_Invocation_Constructs -- |
| 422 |
----------------------------------- |
| 423 |
|
| 424 |
procedure Process_Invocation_Constructs (U_Id : Unit_Id) is |
| 425 |
pragma Assert (Present (U_Id)); |
| 426 |
|
| 427 |
U_Rec : Unit_Record renames ALI.Units.Table (U_Id); |
| 428 |
|
| 429 |
begin |
| 430 |
for IC_Id in U_Rec.First_Invocation_Construct .. |
| 431 |
U_Rec.Last_Invocation_Construct |
| 432 |
loop |
| 433 |
Process_Invocation_Construct (IC_Id); |
| 434 |
end loop; |
| 435 |
end Process_Invocation_Constructs; |
| 436 |
|
| 437 |
------------------ |
| 438 |
-- Process_Unit -- |
| 439 |
------------------ |
| 440 |
|
| 441 |
procedure Process_Unit (U_Id : Unit_Id) is |
| 442 |
begin |
| 443 |
pragma Assert (Present (U_Id)); |
| 444 |
|
| 445 |
-- A stand-alone library unit must not be elaborated as part of the |
| 446 |
-- current compilation because the library already carries its own |
| 447 |
-- elaboration code. |
| 448 |
|
| 449 |
if Is_Stand_Alone_Library_Unit (U_Id) then |
| 450 |
null; |
| 451 |
|
| 452 |
-- Otherwise the unit needs to be elaborated. Add it to the set |
| 453 |
-- of units that require elaboration, as well as all invocation |
| 454 |
-- signatures of constructs it declares. |
| 455 |
|
| 456 |
else |
| 457 |
Unit_Sets.Insert (Elaborable_Units, U_Id); |
| 458 |
Process_Invocation_Constructs (U_Id); |
| 459 |
end if; |
| 460 |
end Process_Unit; |
| 461 |
|
| 462 |
end Bindo.Units; |