]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/lib.adb
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / gcc / ada / lib.adb
CommitLineData
38cbfe40
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- L I B --
6-- --
7-- B o d y --
8-- --
748086b7 9-- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
38cbfe40
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- --
38cbfe40
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 --
748086b7
JJ
16-- or FITNESS FOR A PARTICULAR PURPOSE. --
17-- --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception, --
20-- version 3.1, as published by the Free Software Foundation. --
21-- --
22-- You should have received a copy of the GNU General Public License and --
23-- a copy of the GCC Runtime Library Exception along with this program; --
24-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25-- <http://www.gnu.org/licenses/>. --
38cbfe40
RK
26-- --
27-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 28-- Extensive contributions were provided by Ada Core Technologies Inc. --
38cbfe40
RK
29-- --
30------------------------------------------------------------------------------
31
32pragma Style_Checks (All_Checks);
33-- Subprogram ordering not enforced in this unit
34-- (because of some logical groupings).
35
36with Atree; use Atree;
37with Einfo; use Einfo;
38with Fname; use Fname;
38cbfe40
RK
39with Output; use Output;
40with Sinfo; use Sinfo;
41with Sinput; use Sinput;
42with Stand; use Stand;
43with Stringt; use Stringt;
44with Tree_IO; use Tree_IO;
45with Uname; use Uname;
46
47package body Lib is
48
0da2c8ac 49 Switch_Storing_Enabled : Boolean := True;
7fee6a39 50 -- Controlled by Enable_Switch_Storing/Disable_Switch_Storing
0da2c8ac 51
38cbfe40
RK
52 -----------------------
53 -- Local Subprograms --
54 -----------------------
55
56 type SEU_Result is (
57 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
58 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
59 Yes_After, -- S1 is in same extended unit as S2, and appears after it
60 No); -- S2 is not in same extended unit as S2
61
62 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
63 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
64 -- value as described above.
65
68e2ea27
TQ
66 function Get_Code_Or_Source_Unit
67 (S : Source_Ptr;
68 Unwind_Instances : Boolean) return Unit_Number_Type;
69 -- Common code for Get_Code_Unit (get unit of instantiation for location)
70 -- and Get_Source_Unit (get unit of template for location).
71
38cbfe40
RK
72 --------------------------------------------
73 -- Access Functions for Unit Table Fields --
74 --------------------------------------------
75
76 function Cunit (U : Unit_Number_Type) return Node_Id is
77 begin
78 return Units.Table (U).Cunit;
79 end Cunit;
80
81 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
82 begin
83 return Units.Table (U).Cunit_Entity;
84 end Cunit_Entity;
85
86 function Dependency_Num (U : Unit_Number_Type) return Nat is
87 begin
88 return Units.Table (U).Dependency_Num;
89 end Dependency_Num;
90
38cbfe40
RK
91 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
92 begin
93 return Units.Table (U).Dynamic_Elab;
94 end Dynamic_Elab;
95
96 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
97 begin
98 return Units.Table (U).Error_Location;
99 end Error_Location;
100
101 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
102 begin
103 return Units.Table (U).Expected_Unit;
104 end Expected_Unit;
105
106 function Fatal_Error (U : Unit_Number_Type) return Boolean is
107 begin
108 return Units.Table (U).Fatal_Error;
109 end Fatal_Error;
110
111 function Generate_Code (U : Unit_Number_Type) return Boolean is
112 begin
113 return Units.Table (U).Generate_Code;
114 end Generate_Code;
115
116 function Has_RACW (U : Unit_Number_Type) return Boolean is
117 begin
118 return Units.Table (U).Has_RACW;
119 end Has_RACW;
120
2d9ea47f
RD
121 function Is_Compiler_Unit (U : Unit_Number_Type) return Boolean is
122 begin
123 return Units.Table (U).Is_Compiler_Unit;
124 end Is_Compiler_Unit;
125
38cbfe40
RK
126 function Ident_String (U : Unit_Number_Type) return Node_Id is
127 begin
128 return Units.Table (U).Ident_String;
129 end Ident_String;
130
131 function Loading (U : Unit_Number_Type) return Boolean is
132 begin
133 return Units.Table (U).Loading;
134 end Loading;
135
136 function Main_Priority (U : Unit_Number_Type) return Int is
137 begin
138 return Units.Table (U).Main_Priority;
139 end Main_Priority;
140
2820d220
AC
141 function Munit_Index (U : Unit_Number_Type) return Nat is
142 begin
143 return Units.Table (U).Munit_Index;
144 end Munit_Index;
145
ce4a6e84
RD
146 function OA_Setting (U : Unit_Number_Type) return Character is
147 begin
148 return Units.Table (U).OA_Setting;
149 end OA_Setting;
150
38cbfe40
RK
151 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
152 begin
153 return Units.Table (U).Source_Index;
154 end Source_Index;
155
156 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
157 begin
158 return Units.Table (U).Unit_File_Name;
159 end Unit_File_Name;
160
161 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
162 begin
163 return Units.Table (U).Unit_Name;
164 end Unit_Name;
165
166 ------------------------------------------
167 -- Subprograms to Set Unit Table Fields --
168 ------------------------------------------
169
170 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
171 begin
172 Units.Table (U).Cunit := N;
173 end Set_Cunit;
174
175 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
176 begin
177 Units.Table (U).Cunit_Entity := E;
178 Set_Is_Compilation_Unit (E);
179 end Set_Cunit_Entity;
180
181 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
182 begin
183 Units.Table (U).Dynamic_Elab := B;
184 end Set_Dynamic_Elab;
185
186 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
187 begin
188 Units.Table (U).Error_Location := W;
189 end Set_Error_Location;
190
191 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
192 begin
07fc65c4 193 Units.Table (U).Fatal_Error := B;
38cbfe40
RK
194 end Set_Fatal_Error;
195
196 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
197 begin
198 Units.Table (U).Generate_Code := B;
199 end Set_Generate_Code;
200
201 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
202 begin
203 Units.Table (U).Has_RACW := B;
204 end Set_Has_RACW;
205
2d9ea47f
RD
206 procedure Set_Is_Compiler_Unit
207 (U : Unit_Number_Type;
208 B : Boolean := True)
209 is
210 begin
211 Units.Table (U).Is_Compiler_Unit := B;
212 end Set_Is_Compiler_Unit;
213
38cbfe40
RK
214 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
215 begin
216 Units.Table (U).Ident_String := N;
217 end Set_Ident_String;
218
219 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
220 begin
221 Units.Table (U).Loading := B;
222 end Set_Loading;
223
224 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
225 begin
226 Units.Table (U).Main_Priority := P;
227 end Set_Main_Priority;
228
ce4a6e84
RD
229 procedure Set_OA_Setting (U : Unit_Number_Type; C : Character) is
230 begin
231 Units.Table (U).OA_Setting := C;
232 end Set_OA_Setting;
233
38cbfe40
RK
234 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
235 begin
236 Units.Table (U).Unit_Name := N;
237 end Set_Unit_Name;
238
239 ------------------------------
240 -- Check_Same_Extended_Unit --
241 ------------------------------
242
243 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
244 Sloc1 : Source_Ptr;
245 Sloc2 : Source_Ptr;
246 Sind1 : Source_File_Index;
247 Sind2 : Source_File_Index;
248 Inst1 : Source_Ptr;
249 Inst2 : Source_Ptr;
250 Unum1 : Unit_Number_Type;
251 Unum2 : Unit_Number_Type;
252 Unit1 : Node_Id;
253 Unit2 : Node_Id;
254 Depth1 : Nat;
255 Depth2 : Nat;
256
257 begin
258 if S1 = No_Location or else S2 = No_Location then
259 return No;
260
261 elsif S1 = Standard_Location then
262 if S2 = Standard_Location then
263 return Yes_Same;
264 else
265 return No;
266 end if;
267
268 elsif S2 = Standard_Location then
269 return No;
270 end if;
271
272 Sloc1 := S1;
273 Sloc2 := S2;
274 Unum1 := Get_Code_Unit (Sloc1);
275 Unum2 := Get_Code_Unit (Sloc2);
276
277 loop
278 Sind1 := Get_Source_File_Index (Sloc1);
279 Sind2 := Get_Source_File_Index (Sloc2);
280
281 if Sind1 = Sind2 then
282 if Sloc1 < Sloc2 then
283 return Yes_Before;
284 elsif Sloc1 > Sloc2 then
285 return Yes_After;
286 else
287 return Yes_Same;
288 end if;
289 end if;
290
291 -- OK, the two nodes are in separate source elements, but this is not
292 -- decisive, because of the issue of subunits and instantiations.
293
294 -- First we deal with subunits, since if the subunit is in an
295 -- instantiation, we know that the parent is in the corresponding
296 -- instantiation, since that is the only way we can have a subunit
297 -- that is part of an instantiation.
298
299 Unit1 := Unit (Cunit (Unum1));
300 Unit2 := Unit (Cunit (Unum2));
301
302 if Nkind (Unit1) = N_Subunit
303 and then Present (Corresponding_Stub (Unit1))
304 then
305 -- Both in subunits. They could have a common ancestor. If they
306 -- do, then the deeper one must have a longer unit name. Replace
307 -- the deeper one with its corresponding stub, in order to find
308 -- nearest common ancestor, if any.
309
310 if Nkind (Unit2) = N_Subunit
311 and then Present (Corresponding_Stub (Unit2))
312 then
313 if Length_Of_Name (Unit_Name (Unum1)) <
314 Length_Of_Name (Unit_Name (Unum2))
315 then
316 Sloc2 := Sloc (Corresponding_Stub (Unit2));
317 Unum2 := Get_Source_Unit (Sloc2);
318 goto Continue;
319
320 else
321 Sloc1 := Sloc (Corresponding_Stub (Unit1));
322 Unum1 := Get_Source_Unit (Sloc1);
323 goto Continue;
324 end if;
325
326 -- Nod1 in subunit, Nod2 not
327
328 else
329 Sloc1 := Sloc (Corresponding_Stub (Unit1));
330 Unum1 := Get_Source_Unit (Sloc1);
331 goto Continue;
332 end if;
333
334 -- Nod2 in subunit, Nod1 not
335
336 elsif Nkind (Unit2) = N_Subunit
337 and then Present (Corresponding_Stub (Unit2))
338 then
339 Sloc2 := Sloc (Corresponding_Stub (Unit2));
340 Unum2 := Get_Source_Unit (Sloc2);
341 goto Continue;
342 end if;
343
344 -- At this stage we know that neither is a subunit, so we deal
dec55d76 345 -- with instantiations, since we could have a common ancestor
38cbfe40
RK
346
347 Inst1 := Instantiation (Sind1);
348 Inst2 := Instantiation (Sind2);
349
350 if Inst1 /= No_Location then
351
352 -- Both are instantiations
353
354 if Inst2 /= No_Location then
355
356 Depth1 := Instantiation_Depth (Sloc1);
357 Depth2 := Instantiation_Depth (Sloc2);
358
359 if Depth1 < Depth2 then
360 Sloc2 := Inst2;
361 Unum2 := Get_Source_Unit (Sloc2);
362 goto Continue;
363
364 elsif Depth1 > Depth2 then
365 Sloc1 := Inst1;
366 Unum1 := Get_Source_Unit (Sloc1);
367 goto Continue;
368
369 else
370 Sloc1 := Inst1;
371 Sloc2 := Inst2;
372 Unum1 := Get_Source_Unit (Sloc1);
373 Unum2 := Get_Source_Unit (Sloc2);
374 goto Continue;
375 end if;
376
377 -- Only first node is in instantiation
378
379 else
380 Sloc1 := Inst1;
381 Unum1 := Get_Source_Unit (Sloc1);
382 goto Continue;
383 end if;
384
385 -- Only second node is instantiation
386
387 elsif Inst2 /= No_Location then
388 Sloc2 := Inst2;
389 Unum2 := Get_Source_Unit (Sloc2);
390 goto Continue;
391 end if;
392
393 -- No instantiations involved, so we are not in the same unit
394 -- However, there is one case still to check, namely the case
395 -- where one location is in the spec, and the other in the
396 -- corresponding body (the spec location is earlier).
397
398 if Nkind (Unit1) = N_Subprogram_Body
399 or else
400 Nkind (Unit1) = N_Package_Body
401 then
402 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
403 return Yes_After;
404 end if;
405
406 elsif Nkind (Unit2) = N_Subprogram_Body
407 or else
408 Nkind (Unit2) = N_Package_Body
409 then
410 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
411 return Yes_Before;
412 end if;
413 end if;
414
415 -- If that special case does not occur, then we are certain that
416 -- the two locations are really in separate units.
417
418 return No;
419
420 <<Continue>>
421 null;
422 end loop;
38cbfe40
RK
423 end Check_Same_Extended_Unit;
424
07fc65c4
GB
425 -------------------------------
426 -- Compilation_Switches_Last --
427 -------------------------------
428
429 function Compilation_Switches_Last return Nat is
430 begin
431 return Compilation_Switches.Last;
432 end Compilation_Switches_Last;
433
7fee6a39
BZ
434 ---------------------------
435 -- Enable_Switch_Storing --
436 ---------------------------
437
438 procedure Enable_Switch_Storing is
439 begin
440 Switch_Storing_Enabled := True;
441 end Enable_Switch_Storing;
442
443 ----------------------------
444 -- Disable_Switch_Storing --
445 ----------------------------
446
0da2c8ac
AC
447 procedure Disable_Switch_Storing is
448 begin
449 Switch_Storing_Enabled := False;
450 end Disable_Switch_Storing;
451
38cbfe40
RK
452 ------------------------------
453 -- Earlier_In_Extended_Unit --
454 ------------------------------
455
456 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
457 begin
458 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
459 end Earlier_In_Extended_Unit;
460
461 ----------------------------
462 -- Entity_Is_In_Main_Unit --
463 ----------------------------
464
465 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
466 S : Entity_Id;
467
468 begin
469 S := Scope (E);
470
471 while S /= Standard_Standard loop
472 if S = Main_Unit_Entity then
473 return True;
474 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
475 return False;
476 else
477 S := Scope (S);
478 end if;
479 end loop;
480
481 return False;
482 end Entity_Is_In_Main_Unit;
483
9410151a
TQ
484 --------------------------
485 -- Generic_May_Lack_ALI --
486 --------------------------
487
488 function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean is
489 begin
490 -- We allow internal generic units to be used without having a
491 -- corresponding ALI files to help bootstrapping with older compilers
492 -- that did not support generating ALIs for such generics. It is safe
493 -- to do so because the only thing the generated code would contain
494 -- is the elaboration boolean, and we are careful to elaborate all
495 -- predefined units first anyway.
496
497 return Is_Internal_File_Name
498 (Fname => Sfile,
499 Renamings_Included => True);
500 end Generic_May_Lack_ALI;
fbf5a39b 501
68e2ea27
TQ
502 -----------------------------
503 -- Get_Code_Or_Source_Unit --
504 -----------------------------
38cbfe40 505
68e2ea27
TQ
506 function Get_Code_Or_Source_Unit
507 (S : Source_Ptr;
508 Unwind_Instances : Boolean) return Unit_Number_Type
509 is
38cbfe40 510 begin
fbf5a39b
AC
511 -- Search table unless we have No_Location, which can happen if the
512 -- relevant location has not been set yet. Happens for example when
513 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
38cbfe40 514
fbf5a39b
AC
515 if S /= No_Location then
516 declare
68e2ea27
TQ
517 Source_File : Source_File_Index;
518 Source_Unit : Unit_Number_Type;
fbf5a39b
AC
519
520 begin
68e2ea27
TQ
521 Source_File := Get_Source_File_Index (S);
522
523 if Unwind_Instances then
524 while Template (Source_File) /= No_Source_File loop
525 Source_File := Template (Source_File);
526 end loop;
527 end if;
528
529 Source_Unit := Unit (Source_File);
530
531 if Source_Unit /= No_Unit then
532 return Source_Unit;
533 end if;
fbf5a39b
AC
534 end;
535 end if;
536
68e2ea27
TQ
537 -- If S was No_Location, or was not in the table, we must be in the main
538 -- source unit (and the value has not been placed in the table yet),
539 -- or in one of the configuration pragma files.
38cbfe40
RK
540
541 return Main_Unit;
68e2ea27
TQ
542 end Get_Code_Or_Source_Unit;
543
544 -------------------
545 -- Get_Code_Unit --
546 -------------------
547
548 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
549 begin
550 return Get_Code_Or_Source_Unit (Top_Level_Location (S),
551 Unwind_Instances => False);
38cbfe40
RK
552 end Get_Code_Unit;
553
07fc65c4 554 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
38cbfe40
RK
555 begin
556 return Get_Code_Unit (Sloc (N));
557 end Get_Code_Unit;
558
559 ----------------------------
560 -- Get_Compilation_Switch --
561 ----------------------------
562
563 function Get_Compilation_Switch (N : Pos) return String_Ptr is
564 begin
07fc65c4 565 if N <= Compilation_Switches.Last then
38cbfe40
RK
566 return Compilation_Switches.Table (N);
567
568 else
569 return null;
570 end if;
571 end Get_Compilation_Switch;
572
573 ----------------------------------
574 -- Get_Cunit_Entity_Unit_Number --
575 ----------------------------------
576
577 function Get_Cunit_Entity_Unit_Number
9596236a 578 (E : Entity_Id) return Unit_Number_Type
38cbfe40
RK
579 is
580 begin
581 for U in Units.First .. Units.Last loop
582 if Cunit_Entity (U) = E then
583 return U;
584 end if;
585 end loop;
586
587 -- If not in the table, must be the main source unit, and we just
588 -- have not got it put into the table yet.
589
590 return Main_Unit;
591 end Get_Cunit_Entity_Unit_Number;
592
593 ---------------------------
594 -- Get_Cunit_Unit_Number --
595 ---------------------------
596
597 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
598 begin
599 for U in Units.First .. Units.Last loop
600 if Cunit (U) = N then
601 return U;
602 end if;
603 end loop;
604
605 -- If not in the table, must be the main source unit, and we just
606 -- have not got it put into the table yet.
607
608 return Main_Unit;
609 end Get_Cunit_Unit_Number;
610
611 ---------------------
612 -- Get_Source_Unit --
613 ---------------------
614
615 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
38cbfe40 616 begin
68e2ea27 617 return Get_Code_Or_Source_Unit (S, Unwind_Instances => True);
38cbfe40
RK
618 end Get_Source_Unit;
619
07fc65c4 620 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
38cbfe40
RK
621 begin
622 return Get_Source_Unit (Sloc (N));
623 end Get_Source_Unit;
624
625 --------------------------------
626 -- In_Extended_Main_Code_Unit --
627 --------------------------------
628
07fc65c4 629 function In_Extended_Main_Code_Unit
9596236a 630 (N : Node_Or_Entity_Id) return Boolean
07fc65c4 631 is
38cbfe40
RK
632 begin
633 if Sloc (N) = Standard_Location then
634 return True;
635
636 elsif Sloc (N) = No_Location then
637 return False;
638
639 -- Special case Itypes to test the Sloc of the associated node. The
640 -- reason we do this is for possible calls from gigi after -gnatD
641 -- processing is complete in sprint. This processing updates the
642 -- sloc fields of all nodes in the tree, but itypes are not in the
643 -- tree so their slocs do not get updated.
644
645 elsif Nkind (N) = N_Defining_Identifier
646 and then Is_Itype (N)
647 then
648 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
649
fbf5a39b
AC
650 -- Otherwise see if we are in the main unit
651
38cbfe40
RK
652 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
653 return True;
654
fbf5a39b
AC
655 -- Node may be in spec (or subunit etc) of main unit
656
657 else
38cbfe40 658 return
cc335f43 659 In_Same_Extended_Unit (N, Cunit (Main_Unit));
38cbfe40
RK
660 end if;
661 end In_Extended_Main_Code_Unit;
662
9596236a 663 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
fbf5a39b
AC
664 begin
665 if Loc = Standard_Location then
666 return True;
667
668 elsif Loc = No_Location then
669 return False;
670
671 -- Otherwise see if we are in the main unit
672
673 elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
674 return True;
675
676 -- Location may be in spec (or subunit etc) of main unit
677
678 else
679 return
680 In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
681 end if;
682 end In_Extended_Main_Code_Unit;
683
38cbfe40
RK
684 ----------------------------------
685 -- In_Extended_Main_Source_Unit --
686 ----------------------------------
687
07fc65c4 688 function In_Extended_Main_Source_Unit
9596236a 689 (N : Node_Or_Entity_Id) return Boolean
07fc65c4 690 is
fbf5a39b
AC
691 Nloc : constant Source_Ptr := Sloc (N);
692 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
693
38cbfe40 694 begin
fbf5a39b
AC
695 -- If Mloc is not set, it means we are still parsing the main unit,
696 -- so everything so far is in the extended main source unit.
697
698 if Mloc = No_Location then
38cbfe40
RK
699 return True;
700
fbf5a39b
AC
701 -- Special value cases
702
703 elsif Nloc = Standard_Location then
704 return True;
705
706 elsif Nloc = No_Location then
38cbfe40
RK
707 return False;
708
709 -- Special case Itypes to test the Sloc of the associated node. The
710 -- reason we do this is for possible calls from gigi after -gnatD
711 -- processing is complete in sprint. This processing updates the
712 -- sloc fields of all nodes in the tree, but itypes are not in the
713 -- tree so their slocs do not get updated.
714
715 elsif Nkind (N) = N_Defining_Identifier
716 and then Is_Itype (N)
717 then
718 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
719
fbf5a39b
AC
720 -- Otherwise compare original locations to see if in same unit
721
38cbfe40
RK
722 else
723 return
724 In_Same_Extended_Unit
fbf5a39b
AC
725 (Original_Location (Nloc), Original_Location (Mloc));
726 end if;
727 end In_Extended_Main_Source_Unit;
728
729 function In_Extended_Main_Source_Unit
9596236a 730 (Loc : Source_Ptr) return Boolean
fbf5a39b
AC
731 is
732 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
733
734 begin
735 -- If Mloc is not set, it means we are still parsing the main unit,
736 -- so everything so far is in the extended main source unit.
737
738 if Mloc = No_Location then
739 return True;
740
741 -- Special value cases
742
743 elsif Loc = Standard_Location then
744 return True;
745
746 elsif Loc = No_Location then
747 return False;
748
749 -- Otherwise compare original locations to see if in same unit
750
751 else
752 return
753 In_Same_Extended_Unit
754 (Original_Location (Loc), Original_Location (Mloc));
38cbfe40
RK
755 end if;
756 end In_Extended_Main_Source_Unit;
757
9b832db5
RD
758 ------------------------
759 -- In_Predefined_Unit --
760 ------------------------
761
762 function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
763 begin
764 return In_Predefined_Unit (Sloc (N));
765 end In_Predefined_Unit;
766
767 function In_Predefined_Unit (S : Source_Ptr) return Boolean is
768 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
769 File : constant File_Name_Type := Unit_File_Name (Unit);
770 begin
771 return Is_Predefined_File_Name (File);
772 end In_Predefined_Unit;
773
38cbfe40
RK
774 -----------------------
775 -- In_Same_Code_Unit --
776 -----------------------
777
778 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
779 S1 : constant Source_Ptr := Sloc (N1);
780 S2 : constant Source_Ptr := Sloc (N2);
781
782 begin
783 if S1 = No_Location or else S2 = No_Location then
784 return False;
785
786 elsif S1 = Standard_Location then
787 return S2 = Standard_Location;
788
789 elsif S2 = Standard_Location then
790 return False;
791 end if;
792
793 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
794 end In_Same_Code_Unit;
795
796 ---------------------------
797 -- In_Same_Extended_Unit --
798 ---------------------------
799
cc335f43
AC
800 function In_Same_Extended_Unit
801 (N1, N2 : Node_Or_Entity_Id) return Boolean
802 is
803 begin
804 return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
805 end In_Same_Extended_Unit;
806
38cbfe40
RK
807 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
808 begin
809 return Check_Same_Extended_Unit (S1, S2) /= No;
810 end In_Same_Extended_Unit;
811
812 -------------------------
813 -- In_Same_Source_Unit --
814 -------------------------
815
816 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
817 S1 : constant Source_Ptr := Sloc (N1);
818 S2 : constant Source_Ptr := Sloc (N2);
819
820 begin
821 if S1 = No_Location or else S2 = No_Location then
822 return False;
823
824 elsif S1 = Standard_Location then
825 return S2 = Standard_Location;
826
827 elsif S2 = Standard_Location then
828 return False;
829 end if;
830
831 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
832 end In_Same_Source_Unit;
833
834 -----------------------------
835 -- Increment_Serial_Number --
836 -----------------------------
837
838 function Increment_Serial_Number return Nat is
839 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
38cbfe40
RK
840 begin
841 TSN := TSN + 1;
842 return TSN;
843 end Increment_Serial_Number;
844
845 ----------------
846 -- Initialize --
847 ----------------
848
849 procedure Initialize is
850 begin
851 Linker_Option_Lines.Init;
852 Load_Stack.Init;
853 Units.Init;
38cbfe40
RK
854 Compilation_Switches.Init;
855 end Initialize;
856
857 ---------------
858 -- Is_Loaded --
859 ---------------
860
861 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
862 begin
863 for Unum in Units.First .. Units.Last loop
864 if Uname = Unit_Name (Unum) then
865 return True;
866 end if;
867 end loop;
868
869 return False;
870 end Is_Loaded;
871
872 ---------------
873 -- Last_Unit --
874 ---------------
875
876 function Last_Unit return Unit_Number_Type is
877 begin
878 return Units.Last;
879 end Last_Unit;
880
881 ----------
882 -- List --
883 ----------
884
885 procedure List (File_Names_Only : Boolean := False) is separate;
886
887 ----------
888 -- Lock --
889 ----------
890
891 procedure Lock is
892 begin
893 Linker_Option_Lines.Locked := True;
894 Load_Stack.Locked := True;
895 Units.Locked := True;
896 Linker_Option_Lines.Release;
897 Load_Stack.Release;
898 Units.Release;
899 end Lock;
900
901 ---------------
902 -- Num_Units --
903 ---------------
904
905 function Num_Units return Nat is
906 begin
907 return Int (Units.Last) - Int (Main_Unit) + 1;
908 end Num_Units;
909
6c1e24d3
AC
910 -----------------
911 -- Remove_Unit --
912 -----------------
913
914 procedure Remove_Unit (U : Unit_Number_Type) is
915 begin
916 if U = Units.Last then
917 Units.Decrement_Last;
918 end if;
919 end Remove_Unit;
920
38cbfe40
RK
921 ----------------------------------
922 -- Replace_Linker_Option_String --
923 ----------------------------------
924
925 procedure Replace_Linker_Option_String
926 (S : String_Id; Match_String : String)
927 is
928 begin
929 if Match_String'Length > 0 then
930 for J in 1 .. Linker_Option_Lines.Last loop
07fc65c4 931 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
38cbfe40
RK
932
933 if Match_String = Name_Buffer (1 .. Match_String'Length) then
07fc65c4 934 Linker_Option_Lines.Table (J).Option := S;
38cbfe40
RK
935 return;
936 end if;
937 end loop;
938 end if;
939
940 Store_Linker_Option_String (S);
941 end Replace_Linker_Option_String;
942
943 ----------
944 -- Sort --
945 ----------
946
947 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
948
949 ------------------------------
950 -- Store_Compilation_Switch --
951 ------------------------------
952
953 procedure Store_Compilation_Switch (Switch : String) is
954 begin
0da2c8ac
AC
955 if Switch_Storing_Enabled then
956 Compilation_Switches.Increment_Last;
957 Compilation_Switches.Table (Compilation_Switches.Last) :=
958 new String'(Switch);
fbf5a39b 959
0da2c8ac
AC
960 -- Fix up --RTS flag which has been transformed by the gcc driver
961 -- into -fRTS
fbf5a39b 962
0da2c8ac
AC
963 if Switch'Last >= Switch'First + 4
964 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
965 then
966 Compilation_Switches.Table
967 (Compilation_Switches.Last) (Switch'First + 1) := '-';
968 end if;
fbf5a39b 969 end if;
38cbfe40
RK
970 end Store_Compilation_Switch;
971
972 --------------------------------
973 -- Store_Linker_Option_String --
974 --------------------------------
975
976 procedure Store_Linker_Option_String (S : String_Id) is
977 begin
978 Linker_Option_Lines.Increment_Last;
07fc65c4
GB
979 Linker_Option_Lines.Table (Linker_Option_Lines.Last) :=
980 (Option => S, Unit => Current_Sem_Unit);
38cbfe40
RK
981 end Store_Linker_Option_String;
982
7324bf49
AC
983 -------------------------------
984 -- Synchronize_Serial_Number --
985 -------------------------------
986
987 procedure Synchronize_Serial_Number is
988 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
989 begin
990 TSN := TSN + 1;
991 end Synchronize_Serial_Number;
992
38cbfe40
RK
993 ---------------
994 -- Tree_Read --
995 ---------------
996
997 procedure Tree_Read is
998 N : Nat;
999 S : String_Ptr;
1000
1001 begin
1002 Units.Tree_Read;
1003
2491bb7d
SR
1004 -- Read Compilation_Switches table. First release the memory occupied
1005 -- by the previously loaded switches.
1006
1007 for J in Compilation_Switches.First .. Compilation_Switches.Last loop
1008 Free (Compilation_Switches.Table (J));
1009 end loop;
38cbfe40
RK
1010
1011 Tree_Read_Int (N);
1012 Compilation_Switches.Set_Last (N);
1013
1014 for J in 1 .. N loop
1015 Tree_Read_Str (S);
1016 Compilation_Switches.Table (J) := S;
1017 end loop;
1018 end Tree_Read;
1019
1020 ----------------
1021 -- Tree_Write --
1022 ----------------
1023
1024 procedure Tree_Write is
1025 begin
1026 Units.Tree_Write;
1027
1028 -- Write Compilation_Switches table
1029
1030 Tree_Write_Int (Compilation_Switches.Last);
1031
1032 for J in 1 .. Compilation_Switches.Last loop
1033 Tree_Write_Str (Compilation_Switches.Table (J));
1034 end loop;
1035 end Tree_Write;
1036
1c28fe3a
RD
1037 ------------
1038 -- Unlock --
1039 ------------
1040
1041 procedure Unlock is
1042 begin
1043 Linker_Option_Lines.Locked := False;
1044 Load_Stack.Locked := False;
1045 Units.Locked := False;
1046 end Unlock;
1047
38cbfe40
RK
1048 -----------------
1049 -- Version_Get --
1050 -----------------
1051
1052 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1053 begin
1054 return Get_Hex_String (Units.Table (U).Version);
1055 end Version_Get;
1056
1057 ------------------------
1058 -- Version_Referenced --
1059 ------------------------
1060
1061 procedure Version_Referenced (S : String_Id) is
1062 begin
1063 Version_Ref.Append (S);
1064 end Version_Referenced;
1065
1066end Lib;