]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/lib.adb
lib.ads, [...] (Is_Compiler_Unit): Removed.
[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-- --
c86cf714 9-- Copyright (C) 1992-2014, 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
56e94186
AC
36with Atree; use Atree;
37with Csets; use Csets;
38with Einfo; use Einfo;
39with Fname; use Fname;
2a290fec 40with Nlists; use Nlists;
56e94186
AC
41with Output; use Output;
42with Sinfo; use Sinfo;
43with Sinput; use Sinput;
44with Stand; use Stand;
45with Stringt; use Stringt;
46with Tree_IO; use Tree_IO;
47with Uname; use Uname;
48with Widechar; use Widechar;
38cbfe40
RK
49
50package body Lib is
51
0da2c8ac 52 Switch_Storing_Enabled : Boolean := True;
7fee6a39 53 -- Controlled by Enable_Switch_Storing/Disable_Switch_Storing
0da2c8ac 54
38cbfe40
RK
55 -----------------------
56 -- Local Subprograms --
57 -----------------------
58
59 type SEU_Result is (
60 Yes_Before, -- S1 is in same extended unit as S2 and appears before it
61 Yes_Same, -- S1 is in same extended unit as S2, Slocs are the same
62 Yes_After, -- S1 is in same extended unit as S2, and appears after it
63 No); -- S2 is not in same extended unit as S2
64
65 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result;
66 -- Used by In_Same_Extended_Unit and Earlier_In_Extended_Unit. Returns
67 -- value as described above.
68
68e2ea27
TQ
69 function Get_Code_Or_Source_Unit
70 (S : Source_Ptr;
71 Unwind_Instances : Boolean) return Unit_Number_Type;
72 -- Common code for Get_Code_Unit (get unit of instantiation for location)
73 -- and Get_Source_Unit (get unit of template for location).
74
38cbfe40
RK
75 --------------------------------------------
76 -- Access Functions for Unit Table Fields --
77 --------------------------------------------
78
79 function Cunit (U : Unit_Number_Type) return Node_Id is
80 begin
81 return Units.Table (U).Cunit;
82 end Cunit;
83
84 function Cunit_Entity (U : Unit_Number_Type) return Entity_Id is
85 begin
86 return Units.Table (U).Cunit_Entity;
87 end Cunit_Entity;
88
89 function Dependency_Num (U : Unit_Number_Type) return Nat is
90 begin
91 return Units.Table (U).Dependency_Num;
92 end Dependency_Num;
93
38cbfe40
RK
94 function Dynamic_Elab (U : Unit_Number_Type) return Boolean is
95 begin
96 return Units.Table (U).Dynamic_Elab;
97 end Dynamic_Elab;
98
99 function Error_Location (U : Unit_Number_Type) return Source_Ptr is
100 begin
101 return Units.Table (U).Error_Location;
102 end Error_Location;
103
104 function Expected_Unit (U : Unit_Number_Type) return Unit_Name_Type is
105 begin
106 return Units.Table (U).Expected_Unit;
107 end Expected_Unit;
108
109 function Fatal_Error (U : Unit_Number_Type) return Boolean is
110 begin
111 return Units.Table (U).Fatal_Error;
112 end Fatal_Error;
113
114 function Generate_Code (U : Unit_Number_Type) return Boolean is
115 begin
116 return Units.Table (U).Generate_Code;
117 end Generate_Code;
118
87003b28
RD
119 function Has_Allocator (U : Unit_Number_Type) return Boolean is
120 begin
121 return Units.Table (U).Has_Allocator;
122 end Has_Allocator;
123
38cbfe40
RK
124 function Has_RACW (U : Unit_Number_Type) return Boolean is
125 begin
126 return Units.Table (U).Has_RACW;
127 end Has_RACW;
128
129 function Ident_String (U : Unit_Number_Type) return Node_Id is
130 begin
131 return Units.Table (U).Ident_String;
132 end Ident_String;
133
134 function Loading (U : Unit_Number_Type) return Boolean is
135 begin
136 return Units.Table (U).Loading;
137 end Loading;
138
8918fe18
AC
139 function Main_CPU (U : Unit_Number_Type) return Int is
140 begin
141 return Units.Table (U).Main_CPU;
142 end Main_CPU;
143
38cbfe40
RK
144 function Main_Priority (U : Unit_Number_Type) return Int is
145 begin
146 return Units.Table (U).Main_Priority;
147 end Main_Priority;
148
2820d220
AC
149 function Munit_Index (U : Unit_Number_Type) return Nat is
150 begin
151 return Units.Table (U).Munit_Index;
152 end Munit_Index;
153
ce4a6e84
RD
154 function OA_Setting (U : Unit_Number_Type) return Character is
155 begin
156 return Units.Table (U).OA_Setting;
157 end OA_Setting;
158
38cbfe40
RK
159 function Source_Index (U : Unit_Number_Type) return Source_File_Index is
160 begin
161 return Units.Table (U).Source_Index;
162 end Source_Index;
163
164 function Unit_File_Name (U : Unit_Number_Type) return File_Name_Type is
165 begin
166 return Units.Table (U).Unit_File_Name;
167 end Unit_File_Name;
168
169 function Unit_Name (U : Unit_Number_Type) return Unit_Name_Type is
170 begin
171 return Units.Table (U).Unit_Name;
172 end Unit_Name;
173
174 ------------------------------------------
175 -- Subprograms to Set Unit Table Fields --
176 ------------------------------------------
177
178 procedure Set_Cunit (U : Unit_Number_Type; N : Node_Id) is
179 begin
180 Units.Table (U).Cunit := N;
181 end Set_Cunit;
182
183 procedure Set_Cunit_Entity (U : Unit_Number_Type; E : Entity_Id) is
184 begin
185 Units.Table (U).Cunit_Entity := E;
186 Set_Is_Compilation_Unit (E);
187 end Set_Cunit_Entity;
188
189 procedure Set_Dynamic_Elab (U : Unit_Number_Type; B : Boolean := True) is
190 begin
191 Units.Table (U).Dynamic_Elab := B;
192 end Set_Dynamic_Elab;
193
194 procedure Set_Error_Location (U : Unit_Number_Type; W : Source_Ptr) is
195 begin
196 Units.Table (U).Error_Location := W;
197 end Set_Error_Location;
198
199 procedure Set_Fatal_Error (U : Unit_Number_Type; B : Boolean := True) is
200 begin
07fc65c4 201 Units.Table (U).Fatal_Error := B;
38cbfe40
RK
202 end Set_Fatal_Error;
203
204 procedure Set_Generate_Code (U : Unit_Number_Type; B : Boolean := True) is
205 begin
206 Units.Table (U).Generate_Code := B;
207 end Set_Generate_Code;
208
87003b28
RD
209 procedure Set_Has_Allocator (U : Unit_Number_Type; B : Boolean := True) is
210 begin
211 Units.Table (U).Has_Allocator := B;
212 end Set_Has_Allocator;
213
38cbfe40
RK
214 procedure Set_Has_RACW (U : Unit_Number_Type; B : Boolean := True) is
215 begin
216 Units.Table (U).Has_RACW := B;
217 end Set_Has_RACW;
218
219 procedure Set_Ident_String (U : Unit_Number_Type; N : Node_Id) is
220 begin
221 Units.Table (U).Ident_String := N;
222 end Set_Ident_String;
223
224 procedure Set_Loading (U : Unit_Number_Type; B : Boolean := True) is
225 begin
226 Units.Table (U).Loading := B;
227 end Set_Loading;
228
8918fe18
AC
229 procedure Set_Main_CPU (U : Unit_Number_Type; P : Int) is
230 begin
231 Units.Table (U).Main_CPU := P;
232 end Set_Main_CPU;
233
38cbfe40
RK
234 procedure Set_Main_Priority (U : Unit_Number_Type; P : Int) is
235 begin
236 Units.Table (U).Main_Priority := P;
237 end Set_Main_Priority;
238
ce4a6e84
RD
239 procedure Set_OA_Setting (U : Unit_Number_Type; C : Character) is
240 begin
241 Units.Table (U).OA_Setting := C;
242 end Set_OA_Setting;
243
38cbfe40
RK
244 procedure Set_Unit_Name (U : Unit_Number_Type; N : Unit_Name_Type) is
245 begin
246 Units.Table (U).Unit_Name := N;
247 end Set_Unit_Name;
248
249 ------------------------------
250 -- Check_Same_Extended_Unit --
251 ------------------------------
252
253 function Check_Same_Extended_Unit (S1, S2 : Source_Ptr) return SEU_Result is
254 Sloc1 : Source_Ptr;
255 Sloc2 : Source_Ptr;
256 Sind1 : Source_File_Index;
257 Sind2 : Source_File_Index;
258 Inst1 : Source_Ptr;
259 Inst2 : Source_Ptr;
260 Unum1 : Unit_Number_Type;
261 Unum2 : Unit_Number_Type;
262 Unit1 : Node_Id;
263 Unit2 : Node_Id;
264 Depth1 : Nat;
265 Depth2 : Nat;
266
267 begin
268 if S1 = No_Location or else S2 = No_Location then
269 return No;
270
271 elsif S1 = Standard_Location then
272 if S2 = Standard_Location then
273 return Yes_Same;
274 else
275 return No;
276 end if;
277
278 elsif S2 = Standard_Location then
279 return No;
280 end if;
281
282 Sloc1 := S1;
283 Sloc2 := S2;
66dc8075
AC
284
285 Unum1 := Get_Source_Unit (Sloc1);
286 Unum2 := Get_Source_Unit (Sloc2);
38cbfe40
RK
287
288 loop
66dc8075
AC
289 -- Step 1: Check whether the two locations are in the same source
290 -- file.
291
38cbfe40
RK
292 Sind1 := Get_Source_File_Index (Sloc1);
293 Sind2 := Get_Source_File_Index (Sloc2);
294
295 if Sind1 = Sind2 then
296 if Sloc1 < Sloc2 then
297 return Yes_Before;
298 elsif Sloc1 > Sloc2 then
299 return Yes_After;
300 else
301 return Yes_Same;
302 end if;
303 end if;
304
66dc8075
AC
305 -- Step 2: Check subunits. If a subunit is instantiated, follow the
306 -- instantiation chain rather than the stub chain.
38cbfe40
RK
307
308 Unit1 := Unit (Cunit (Unum1));
309 Unit2 := Unit (Cunit (Unum2));
66dc8075
AC
310 Inst1 := Instantiation (Sind1);
311 Inst2 := Instantiation (Sind2);
38cbfe40
RK
312
313 if Nkind (Unit1) = N_Subunit
314 and then Present (Corresponding_Stub (Unit1))
66dc8075 315 and then Inst1 = No_Location
38cbfe40 316 then
38cbfe40
RK
317 if Nkind (Unit2) = N_Subunit
318 and then Present (Corresponding_Stub (Unit2))
66dc8075 319 and then Inst2 = No_Location
38cbfe40 320 then
66dc8075
AC
321 -- Both locations refer to subunits which may have a common
322 -- ancestor. If they do, the deeper subunit must have a longer
323 -- unit name. Replace the deeper one with its corresponding
324 -- stub in order to find the nearest ancestor.
325
38cbfe40
RK
326 if Length_Of_Name (Unit_Name (Unum1)) <
327 Length_Of_Name (Unit_Name (Unum2))
328 then
329 Sloc2 := Sloc (Corresponding_Stub (Unit2));
330 Unum2 := Get_Source_Unit (Sloc2);
331 goto Continue;
332
333 else
334 Sloc1 := Sloc (Corresponding_Stub (Unit1));
335 Unum1 := Get_Source_Unit (Sloc1);
336 goto Continue;
337 end if;
338
66dc8075 339 -- Sloc1 in subunit, Sloc2 not
38cbfe40
RK
340
341 else
342 Sloc1 := Sloc (Corresponding_Stub (Unit1));
343 Unum1 := Get_Source_Unit (Sloc1);
344 goto Continue;
345 end if;
346
66dc8075 347 -- Sloc2 in subunit, Sloc1 not
38cbfe40
RK
348
349 elsif Nkind (Unit2) = N_Subunit
350 and then Present (Corresponding_Stub (Unit2))
66dc8075 351 and then Inst2 = No_Location
38cbfe40
RK
352 then
353 Sloc2 := Sloc (Corresponding_Stub (Unit2));
354 Unum2 := Get_Source_Unit (Sloc2);
355 goto Continue;
356 end if;
357
66dc8075
AC
358 -- Step 3: Check instances. The two locations may yield a common
359 -- ancestor.
38cbfe40
RK
360
361 if Inst1 /= No_Location then
38cbfe40
RK
362 if Inst2 /= No_Location then
363
66dc8075
AC
364 -- Both locations denote instantiations
365
38cbfe40
RK
366 Depth1 := Instantiation_Depth (Sloc1);
367 Depth2 := Instantiation_Depth (Sloc2);
368
369 if Depth1 < Depth2 then
370 Sloc2 := Inst2;
371 Unum2 := Get_Source_Unit (Sloc2);
372 goto Continue;
373
374 elsif Depth1 > Depth2 then
375 Sloc1 := Inst1;
376 Unum1 := Get_Source_Unit (Sloc1);
377 goto Continue;
378
379 else
380 Sloc1 := Inst1;
381 Sloc2 := Inst2;
382 Unum1 := Get_Source_Unit (Sloc1);
383 Unum2 := Get_Source_Unit (Sloc2);
384 goto Continue;
385 end if;
386
66dc8075 387 -- Sloc1 is an instantiation
38cbfe40
RK
388
389 else
390 Sloc1 := Inst1;
391 Unum1 := Get_Source_Unit (Sloc1);
392 goto Continue;
393 end if;
394
66dc8075 395 -- Sloc2 is an instantiation
38cbfe40
RK
396
397 elsif Inst2 /= No_Location then
398 Sloc2 := Inst2;
399 Unum2 := Get_Source_Unit (Sloc2);
400 goto Continue;
401 end if;
402
66dc8075
AC
403 -- Step 4: One location in the spec, the other in the corresponding
404 -- body of the same unit. The location in the spec is considered
405 -- earlier.
38cbfe40
RK
406
407 if Nkind (Unit1) = N_Subprogram_Body
408 or else
409 Nkind (Unit1) = N_Package_Body
410 then
411 if Library_Unit (Cunit (Unum1)) = Cunit (Unum2) then
412 return Yes_After;
413 end if;
414
415 elsif Nkind (Unit2) = N_Subprogram_Body
416 or else
417 Nkind (Unit2) = N_Package_Body
418 then
419 if Library_Unit (Cunit (Unum2)) = Cunit (Unum1) then
420 return Yes_Before;
421 end if;
422 end if;
423
66dc8075
AC
424 -- At this point it is certain that the two locations denote two
425 -- entirely separate units.
38cbfe40
RK
426
427 return No;
428
429 <<Continue>>
430 null;
431 end loop;
38cbfe40
RK
432 end Check_Same_Extended_Unit;
433
07fc65c4
GB
434 -------------------------------
435 -- Compilation_Switches_Last --
436 -------------------------------
437
438 function Compilation_Switches_Last return Nat is
439 begin
440 return Compilation_Switches.Last;
441 end Compilation_Switches_Last;
442
7fee6a39
BZ
443 ---------------------------
444 -- Enable_Switch_Storing --
445 ---------------------------
446
447 procedure Enable_Switch_Storing is
448 begin
449 Switch_Storing_Enabled := True;
450 end Enable_Switch_Storing;
451
452 ----------------------------
453 -- Disable_Switch_Storing --
454 ----------------------------
455
0da2c8ac
AC
456 procedure Disable_Switch_Storing is
457 begin
458 Switch_Storing_Enabled := False;
459 end Disable_Switch_Storing;
460
38cbfe40
RK
461 ------------------------------
462 -- Earlier_In_Extended_Unit --
463 ------------------------------
464
465 function Earlier_In_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
466 begin
467 return Check_Same_Extended_Unit (S1, S2) = Yes_Before;
468 end Earlier_In_Extended_Unit;
469
56e94186
AC
470 -----------------------
471 -- Exact_Source_Name --
472 -----------------------
473
474 function Exact_Source_Name (Loc : Source_Ptr) return String is
475 U : constant Unit_Number_Type := Get_Source_Unit (Loc);
476 Buf : constant Source_Buffer_Ptr := Source_Text (Source_Index (U));
477 Orig : constant Source_Ptr := Original_Location (Loc);
478 P : Source_Ptr;
479
480 WC : Char_Code;
481 Err : Boolean;
482 pragma Warnings (Off, WC);
483 pragma Warnings (Off, Err);
484
485 begin
486 -- Entity is character literal
487
488 if Buf (Orig) = ''' then
489 return String (Buf (Orig .. Orig + 2));
490
491 -- Entity is operator symbol
492
493 elsif Buf (Orig) = '"' or else Buf (Orig) = '%' then
494 P := Orig;
495
496 loop
497 P := P + 1;
498 exit when Buf (P) = Buf (Orig);
499 end loop;
500
501 return String (Buf (Orig .. P));
502
503 -- Entity is identifier
504
505 else
506 P := Orig;
507
508 loop
509 if Is_Start_Of_Wide_Char (Buf, P) then
510 Scan_Wide (Buf, P, WC, Err);
511 elsif not Identifier_Char (Buf (P)) then
512 exit;
513 else
514 P := P + 1;
515 end if;
516 end loop;
517
518 -- Write out the identifier by copying the exact source characters
519 -- used in its declaration. Note that this means wide characters will
520 -- be in their original encoded form.
521
522 return String (Buf (Orig .. P - 1));
523 end if;
524 end Exact_Source_Name;
525
38cbfe40
RK
526 ----------------------------
527 -- Entity_Is_In_Main_Unit --
528 ----------------------------
529
530 function Entity_Is_In_Main_Unit (E : Entity_Id) return Boolean is
531 S : Entity_Id;
532
533 begin
534 S := Scope (E);
535
536 while S /= Standard_Standard loop
537 if S = Main_Unit_Entity then
538 return True;
539 elsif Ekind (S) = E_Package and then Is_Child_Unit (S) then
540 return False;
541 else
542 S := Scope (S);
543 end if;
544 end loop;
545
546 return False;
547 end Entity_Is_In_Main_Unit;
548
9410151a
TQ
549 --------------------------
550 -- Generic_May_Lack_ALI --
551 --------------------------
552
553 function Generic_May_Lack_ALI (Sfile : File_Name_Type) return Boolean is
554 begin
555 -- We allow internal generic units to be used without having a
556 -- corresponding ALI files to help bootstrapping with older compilers
557 -- that did not support generating ALIs for such generics. It is safe
558 -- to do so because the only thing the generated code would contain
559 -- is the elaboration boolean, and we are careful to elaborate all
560 -- predefined units first anyway.
561
562 return Is_Internal_File_Name
563 (Fname => Sfile,
564 Renamings_Included => True);
565 end Generic_May_Lack_ALI;
fbf5a39b 566
68e2ea27
TQ
567 -----------------------------
568 -- Get_Code_Or_Source_Unit --
569 -----------------------------
38cbfe40 570
68e2ea27
TQ
571 function Get_Code_Or_Source_Unit
572 (S : Source_Ptr;
573 Unwind_Instances : Boolean) return Unit_Number_Type
574 is
38cbfe40 575 begin
fbf5a39b
AC
576 -- Search table unless we have No_Location, which can happen if the
577 -- relevant location has not been set yet. Happens for example when
578 -- we obtain Sloc (Cunit (Main_Unit)) before it is set.
38cbfe40 579
fbf5a39b
AC
580 if S /= No_Location then
581 declare
68e2ea27
TQ
582 Source_File : Source_File_Index;
583 Source_Unit : Unit_Number_Type;
fbf5a39b
AC
584
585 begin
68e2ea27
TQ
586 Source_File := Get_Source_File_Index (S);
587
588 if Unwind_Instances then
589 while Template (Source_File) /= No_Source_File loop
590 Source_File := Template (Source_File);
591 end loop;
592 end if;
593
594 Source_Unit := Unit (Source_File);
595
596 if Source_Unit /= No_Unit then
597 return Source_Unit;
598 end if;
fbf5a39b
AC
599 end;
600 end if;
601
68e2ea27
TQ
602 -- If S was No_Location, or was not in the table, we must be in the main
603 -- source unit (and the value has not been placed in the table yet),
604 -- or in one of the configuration pragma files.
38cbfe40
RK
605
606 return Main_Unit;
68e2ea27
TQ
607 end Get_Code_Or_Source_Unit;
608
609 -------------------
610 -- Get_Code_Unit --
611 -------------------
612
613 function Get_Code_Unit (S : Source_Ptr) return Unit_Number_Type is
614 begin
615 return Get_Code_Or_Source_Unit (Top_Level_Location (S),
616 Unwind_Instances => False);
38cbfe40
RK
617 end Get_Code_Unit;
618
07fc65c4 619 function Get_Code_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
38cbfe40
RK
620 begin
621 return Get_Code_Unit (Sloc (N));
622 end Get_Code_Unit;
623
624 ----------------------------
625 -- Get_Compilation_Switch --
626 ----------------------------
627
628 function Get_Compilation_Switch (N : Pos) return String_Ptr is
629 begin
07fc65c4 630 if N <= Compilation_Switches.Last then
38cbfe40
RK
631 return Compilation_Switches.Table (N);
632
633 else
634 return null;
635 end if;
636 end Get_Compilation_Switch;
637
638 ----------------------------------
639 -- Get_Cunit_Entity_Unit_Number --
640 ----------------------------------
641
642 function Get_Cunit_Entity_Unit_Number
9596236a 643 (E : Entity_Id) return Unit_Number_Type
38cbfe40
RK
644 is
645 begin
646 for U in Units.First .. Units.Last loop
647 if Cunit_Entity (U) = E then
648 return U;
649 end if;
650 end loop;
651
652 -- If not in the table, must be the main source unit, and we just
653 -- have not got it put into the table yet.
654
655 return Main_Unit;
656 end Get_Cunit_Entity_Unit_Number;
657
658 ---------------------------
659 -- Get_Cunit_Unit_Number --
660 ---------------------------
661
662 function Get_Cunit_Unit_Number (N : Node_Id) return Unit_Number_Type is
663 begin
664 for U in Units.First .. Units.Last loop
665 if Cunit (U) = N then
666 return U;
667 end if;
668 end loop;
669
f3a67cfc
ES
670 -- If not in the table, must be a spec created for a main unit that is a
671 -- child subprogram body which we have not inserted into the table yet.
38cbfe40 672
2c011ce1
RD
673 if N = Library_Unit (Cunit (Main_Unit)) then
674 return Main_Unit;
675
676 -- If it is anything else, something is seriously wrong, and we really
677 -- don't want to proceed, even if assertions are off, so we explicitly
678 -- raise an exception in this case to terminate compilation.
959dd7d8 679
f3a67cfc 680 else
2c011ce1 681 raise Program_Error;
f3a67cfc 682 end if;
38cbfe40
RK
683 end Get_Cunit_Unit_Number;
684
685 ---------------------
686 -- Get_Source_Unit --
687 ---------------------
688
689 function Get_Source_Unit (S : Source_Ptr) return Unit_Number_Type is
38cbfe40 690 begin
68e2ea27 691 return Get_Code_Or_Source_Unit (S, Unwind_Instances => True);
38cbfe40
RK
692 end Get_Source_Unit;
693
07fc65c4 694 function Get_Source_Unit (N : Node_Or_Entity_Id) return Unit_Number_Type is
38cbfe40
RK
695 begin
696 return Get_Source_Unit (Sloc (N));
697 end Get_Source_Unit;
698
699 --------------------------------
700 -- In_Extended_Main_Code_Unit --
701 --------------------------------
702
07fc65c4 703 function In_Extended_Main_Code_Unit
9596236a 704 (N : Node_Or_Entity_Id) return Boolean
07fc65c4 705 is
38cbfe40
RK
706 begin
707 if Sloc (N) = Standard_Location then
3b8056a5 708 return False;
38cbfe40
RK
709
710 elsif Sloc (N) = No_Location then
711 return False;
712
713 -- Special case Itypes to test the Sloc of the associated node. The
714 -- reason we do this is for possible calls from gigi after -gnatD
715 -- processing is complete in sprint. This processing updates the
716 -- sloc fields of all nodes in the tree, but itypes are not in the
717 -- tree so their slocs do not get updated.
718
719 elsif Nkind (N) = N_Defining_Identifier
720 and then Is_Itype (N)
721 then
722 return In_Extended_Main_Code_Unit (Associated_Node_For_Itype (N));
723
fbf5a39b
AC
724 -- Otherwise see if we are in the main unit
725
38cbfe40
RK
726 elsif Get_Code_Unit (Sloc (N)) = Get_Code_Unit (Cunit (Main_Unit)) then
727 return True;
728
fbf5a39b
AC
729 -- Node may be in spec (or subunit etc) of main unit
730
731 else
38cbfe40 732 return
cc335f43 733 In_Same_Extended_Unit (N, Cunit (Main_Unit));
38cbfe40
RK
734 end if;
735 end In_Extended_Main_Code_Unit;
736
9596236a 737 function In_Extended_Main_Code_Unit (Loc : Source_Ptr) return Boolean is
fbf5a39b
AC
738 begin
739 if Loc = Standard_Location then
3b8056a5 740 return False;
fbf5a39b
AC
741
742 elsif Loc = No_Location then
743 return False;
744
745 -- Otherwise see if we are in the main unit
746
747 elsif Get_Code_Unit (Loc) = Get_Code_Unit (Cunit (Main_Unit)) then
748 return True;
749
750 -- Location may be in spec (or subunit etc) of main unit
751
752 else
753 return
754 In_Same_Extended_Unit (Loc, Sloc (Cunit (Main_Unit)));
755 end if;
756 end In_Extended_Main_Code_Unit;
757
38cbfe40
RK
758 ----------------------------------
759 -- In_Extended_Main_Source_Unit --
760 ----------------------------------
761
07fc65c4 762 function In_Extended_Main_Source_Unit
9596236a 763 (N : Node_Or_Entity_Id) return Boolean
07fc65c4 764 is
fbf5a39b
AC
765 Nloc : constant Source_Ptr := Sloc (N);
766 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
767
38cbfe40 768 begin
e1d9659d 769 -- If parsing, then use the global flag to indicate result
fbf5a39b 770
e1d9659d
AC
771 if Compiler_State = Parsing then
772 return Parsing_Main_Extended_Source;
38cbfe40 773
fbf5a39b
AC
774 -- Special value cases
775
776 elsif Nloc = Standard_Location then
3b8056a5 777 return False;
fbf5a39b
AC
778
779 elsif Nloc = No_Location then
38cbfe40
RK
780 return False;
781
782 -- Special case Itypes to test the Sloc of the associated node. The
783 -- reason we do this is for possible calls from gigi after -gnatD
784 -- processing is complete in sprint. This processing updates the
785 -- sloc fields of all nodes in the tree, but itypes are not in the
786 -- tree so their slocs do not get updated.
787
788 elsif Nkind (N) = N_Defining_Identifier
789 and then Is_Itype (N)
790 then
791 return In_Extended_Main_Source_Unit (Associated_Node_For_Itype (N));
792
fbf5a39b
AC
793 -- Otherwise compare original locations to see if in same unit
794
38cbfe40
RK
795 else
796 return
797 In_Same_Extended_Unit
fbf5a39b
AC
798 (Original_Location (Nloc), Original_Location (Mloc));
799 end if;
800 end In_Extended_Main_Source_Unit;
801
802 function In_Extended_Main_Source_Unit
9596236a 803 (Loc : Source_Ptr) return Boolean
fbf5a39b
AC
804 is
805 Mloc : constant Source_Ptr := Sloc (Cunit (Main_Unit));
806
807 begin
e1d9659d 808 -- If parsing, then use the global flag to indicate result
fbf5a39b 809
e1d9659d
AC
810 if Compiler_State = Parsing then
811 return Parsing_Main_Extended_Source;
fbf5a39b
AC
812
813 -- Special value cases
814
815 elsif Loc = Standard_Location then
3b8056a5 816 return False;
fbf5a39b
AC
817
818 elsif Loc = No_Location then
819 return False;
820
821 -- Otherwise compare original locations to see if in same unit
822
823 else
824 return
825 In_Same_Extended_Unit
826 (Original_Location (Loc), Original_Location (Mloc));
38cbfe40
RK
827 end if;
828 end In_Extended_Main_Source_Unit;
829
9b832db5
RD
830 ------------------------
831 -- In_Predefined_Unit --
832 ------------------------
833
834 function In_Predefined_Unit (N : Node_Or_Entity_Id) return Boolean is
835 begin
836 return In_Predefined_Unit (Sloc (N));
837 end In_Predefined_Unit;
838
839 function In_Predefined_Unit (S : Source_Ptr) return Boolean is
840 Unit : constant Unit_Number_Type := Get_Source_Unit (S);
841 File : constant File_Name_Type := Unit_File_Name (Unit);
842 begin
843 return Is_Predefined_File_Name (File);
844 end In_Predefined_Unit;
845
38cbfe40
RK
846 -----------------------
847 -- In_Same_Code_Unit --
848 -----------------------
849
850 function In_Same_Code_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
851 S1 : constant Source_Ptr := Sloc (N1);
852 S2 : constant Source_Ptr := Sloc (N2);
853
854 begin
855 if S1 = No_Location or else S2 = No_Location then
856 return False;
857
858 elsif S1 = Standard_Location then
859 return S2 = Standard_Location;
860
861 elsif S2 = Standard_Location then
862 return False;
863 end if;
864
865 return Get_Code_Unit (N1) = Get_Code_Unit (N2);
866 end In_Same_Code_Unit;
867
868 ---------------------------
869 -- In_Same_Extended_Unit --
870 ---------------------------
871
cc335f43
AC
872 function In_Same_Extended_Unit
873 (N1, N2 : Node_Or_Entity_Id) return Boolean
874 is
875 begin
876 return Check_Same_Extended_Unit (Sloc (N1), Sloc (N2)) /= No;
877 end In_Same_Extended_Unit;
878
38cbfe40
RK
879 function In_Same_Extended_Unit (S1, S2 : Source_Ptr) return Boolean is
880 begin
881 return Check_Same_Extended_Unit (S1, S2) /= No;
882 end In_Same_Extended_Unit;
883
884 -------------------------
885 -- In_Same_Source_Unit --
886 -------------------------
887
888 function In_Same_Source_Unit (N1, N2 : Node_Or_Entity_Id) return Boolean is
889 S1 : constant Source_Ptr := Sloc (N1);
890 S2 : constant Source_Ptr := Sloc (N2);
891
892 begin
893 if S1 = No_Location or else S2 = No_Location then
894 return False;
895
896 elsif S1 = Standard_Location then
897 return S2 = Standard_Location;
898
899 elsif S2 = Standard_Location then
900 return False;
901 end if;
902
903 return Get_Source_Unit (N1) = Get_Source_Unit (N2);
904 end In_Same_Source_Unit;
905
906 -----------------------------
907 -- Increment_Serial_Number --
908 -----------------------------
909
910 function Increment_Serial_Number return Nat is
911 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
38cbfe40
RK
912 begin
913 TSN := TSN + 1;
914 return TSN;
915 end Increment_Serial_Number;
916
917 ----------------
918 -- Initialize --
919 ----------------
920
921 procedure Initialize is
922 begin
923 Linker_Option_Lines.Init;
7eaa7cdf 924 Notes.Init;
38cbfe40
RK
925 Load_Stack.Init;
926 Units.Init;
38cbfe40
RK
927 Compilation_Switches.Init;
928 end Initialize;
929
930 ---------------
931 -- Is_Loaded --
932 ---------------
933
934 function Is_Loaded (Uname : Unit_Name_Type) return Boolean is
935 begin
936 for Unum in Units.First .. Units.Last loop
937 if Uname = Unit_Name (Unum) then
938 return True;
939 end if;
940 end loop;
941
942 return False;
943 end Is_Loaded;
944
945 ---------------
946 -- Last_Unit --
947 ---------------
948
949 function Last_Unit return Unit_Number_Type is
950 begin
951 return Units.Last;
952 end Last_Unit;
953
954 ----------
955 -- List --
956 ----------
957
958 procedure List (File_Names_Only : Boolean := False) is separate;
959
960 ----------
961 -- Lock --
962 ----------
963
964 procedure Lock is
965 begin
966 Linker_Option_Lines.Locked := True;
967 Load_Stack.Locked := True;
968 Units.Locked := True;
969 Linker_Option_Lines.Release;
970 Load_Stack.Release;
971 Units.Release;
972 end Lock;
973
974 ---------------
975 -- Num_Units --
976 ---------------
977
978 function Num_Units return Nat is
979 begin
980 return Int (Units.Last) - Int (Main_Unit) + 1;
981 end Num_Units;
982
6c1e24d3
AC
983 -----------------
984 -- Remove_Unit --
985 -----------------
986
987 procedure Remove_Unit (U : Unit_Number_Type) is
988 begin
989 if U = Units.Last then
990 Units.Decrement_Last;
991 end if;
992 end Remove_Unit;
993
38cbfe40
RK
994 ----------------------------------
995 -- Replace_Linker_Option_String --
996 ----------------------------------
997
998 procedure Replace_Linker_Option_String
999 (S : String_Id; Match_String : String)
1000 is
1001 begin
1002 if Match_String'Length > 0 then
1003 for J in 1 .. Linker_Option_Lines.Last loop
07fc65c4 1004 String_To_Name_Buffer (Linker_Option_Lines.Table (J).Option);
38cbfe40
RK
1005
1006 if Match_String = Name_Buffer (1 .. Match_String'Length) then
07fc65c4 1007 Linker_Option_Lines.Table (J).Option := S;
38cbfe40
RK
1008 return;
1009 end if;
1010 end loop;
1011 end if;
1012
1013 Store_Linker_Option_String (S);
1014 end Replace_Linker_Option_String;
1015
1016 ----------
1017 -- Sort --
1018 ----------
1019
1020 procedure Sort (Tbl : in out Unit_Ref_Table) is separate;
1021
1022 ------------------------------
1023 -- Store_Compilation_Switch --
1024 ------------------------------
1025
1026 procedure Store_Compilation_Switch (Switch : String) is
1027 begin
0da2c8ac
AC
1028 if Switch_Storing_Enabled then
1029 Compilation_Switches.Increment_Last;
1030 Compilation_Switches.Table (Compilation_Switches.Last) :=
1031 new String'(Switch);
fbf5a39b 1032
0da2c8ac
AC
1033 -- Fix up --RTS flag which has been transformed by the gcc driver
1034 -- into -fRTS
fbf5a39b 1035
0da2c8ac
AC
1036 if Switch'Last >= Switch'First + 4
1037 and then Switch (Switch'First .. Switch'First + 4) = "-fRTS"
1038 then
1039 Compilation_Switches.Table
1040 (Compilation_Switches.Last) (Switch'First + 1) := '-';
1041 end if;
fbf5a39b 1042 end if;
38cbfe40
RK
1043 end Store_Compilation_Switch;
1044
1045 --------------------------------
1046 -- Store_Linker_Option_String --
1047 --------------------------------
1048
1049 procedure Store_Linker_Option_String (S : String_Id) is
1050 begin
7eaa7cdf 1051 Linker_Option_Lines.Append ((Option => S, Unit => Current_Sem_Unit));
38cbfe40
RK
1052 end Store_Linker_Option_String;
1053
7eaa7cdf
RD
1054 ----------------
1055 -- Store_Note --
1056 ----------------
1057
1058 procedure Store_Note (N : Node_Id) is
1059 begin
1060 Notes.Append ((Pragma_Node => N, Unit => Current_Sem_Unit));
1061 end Store_Note;
1062
7324bf49
AC
1063 -------------------------------
1064 -- Synchronize_Serial_Number --
1065 -------------------------------
1066
1067 procedure Synchronize_Serial_Number is
1068 TSN : Int renames Units.Table (Current_Sem_Unit).Serial_Number;
1069 begin
1070 TSN := TSN + 1;
1071 end Synchronize_Serial_Number;
1072
38cbfe40
RK
1073 ---------------
1074 -- Tree_Read --
1075 ---------------
1076
1077 procedure Tree_Read is
1078 N : Nat;
1079 S : String_Ptr;
1080
1081 begin
1082 Units.Tree_Read;
1083
2491bb7d
SR
1084 -- Read Compilation_Switches table. First release the memory occupied
1085 -- by the previously loaded switches.
1086
1087 for J in Compilation_Switches.First .. Compilation_Switches.Last loop
1088 Free (Compilation_Switches.Table (J));
1089 end loop;
38cbfe40
RK
1090
1091 Tree_Read_Int (N);
1092 Compilation_Switches.Set_Last (N);
1093
1094 for J in 1 .. N loop
1095 Tree_Read_Str (S);
1096 Compilation_Switches.Table (J) := S;
1097 end loop;
1098 end Tree_Read;
1099
1100 ----------------
1101 -- Tree_Write --
1102 ----------------
1103
1104 procedure Tree_Write is
1105 begin
1106 Units.Tree_Write;
1107
1108 -- Write Compilation_Switches table
1109
1110 Tree_Write_Int (Compilation_Switches.Last);
1111
1112 for J in 1 .. Compilation_Switches.Last loop
1113 Tree_Write_Str (Compilation_Switches.Table (J));
1114 end loop;
1115 end Tree_Write;
1116
1c28fe3a
RD
1117 ------------
1118 -- Unlock --
1119 ------------
1120
1121 procedure Unlock is
1122 begin
1123 Linker_Option_Lines.Locked := False;
1124 Load_Stack.Locked := False;
1125 Units.Locked := False;
1126 end Unlock;
1127
38cbfe40
RK
1128 -----------------
1129 -- Version_Get --
1130 -----------------
1131
1132 function Version_Get (U : Unit_Number_Type) return Word_Hex_String is
1133 begin
1134 return Get_Hex_String (Units.Table (U).Version);
1135 end Version_Get;
1136
1137 ------------------------
1138 -- Version_Referenced --
1139 ------------------------
1140
1141 procedure Version_Referenced (S : String_Id) is
1142 begin
1143 Version_Ref.Append (S);
1144 end Version_Referenced;
1145
2a290fec
AC
1146 ---------------------
1147 -- Write_Unit_Info --
1148 ---------------------
1149
1150 procedure Write_Unit_Info
1151 (Unit_Num : Unit_Number_Type;
1152 Item : Node_Id;
1153 Prefix : String := "";
1154 Withs : Boolean := False)
1155 is
1156 begin
1157 Write_Str (Prefix);
1158 Write_Unit_Name (Unit_Name (Unit_Num));
1159 Write_Str (", unit ");
1160 Write_Int (Int (Unit_Num));
1161 Write_Str (", ");
1162 Write_Int (Int (Item));
1163 Write_Str ("=");
1164 Write_Str (Node_Kind'Image (Nkind (Item)));
1165
1166 if Item /= Original_Node (Item) then
1167 Write_Str (", orig = ");
1168 Write_Int (Int (Original_Node (Item)));
1169 Write_Str ("=");
1170 Write_Str (Node_Kind'Image (Nkind (Original_Node (Item))));
1171 end if;
1172
1173 Write_Eol;
1174
1175 -- Skip the rest if we're not supposed to print the withs
1176
1177 if not Withs then
1178 return;
1179 end if;
1180
1181 declare
1182 Context_Item : Node_Id;
1183
1184 begin
1185 Context_Item := First (Context_Items (Cunit (Unit_Num)));
1186 while Present (Context_Item)
1187 and then (Nkind (Context_Item) /= N_With_Clause
1188 or else Limited_Present (Context_Item))
1189 loop
1190 Context_Item := Next (Context_Item);
1191 end loop;
1192
1193 if Present (Context_Item) then
1194 Indent;
1195 Write_Line ("withs:");
1196 Indent;
1197
1198 while Present (Context_Item) loop
1199 if Nkind (Context_Item) = N_With_Clause
1200 and then not Limited_Present (Context_Item)
1201 then
1202 pragma Assert (Present (Library_Unit (Context_Item)));
1203 Write_Unit_Name
1204 (Unit_Name
1205 (Get_Cunit_Unit_Number (Library_Unit (Context_Item))));
1206
1207 if Implicit_With (Context_Item) then
1208 Write_Str (" -- implicit");
1209 end if;
1210
1211 Write_Eol;
1212 end if;
1213
1214 Context_Item := Next (Context_Item);
1215 end loop;
1216
1217 Outdent;
1218 Write_Line ("end withs");
1219 Outdent;
1220 end if;
1221 end;
1222 end Write_Unit_Info;
1223
38cbfe40 1224end Lib;