* ru.po: Update.
[official-gcc.git] / gcc / ada / sinput.adb
blob0800f3196a68b6d0d1f2938d74bdff8821011de0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S I N P U T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, 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. --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 pragma Style_Checks (All_Checks);
33 -- Subprograms not all in alpha order
35 with Atree; use Atree;
36 with Debug; use Debug;
37 with Opt; use Opt;
38 with Output; use Output;
39 with Scans; use Scans;
40 with Tree_IO; use Tree_IO;
41 with Widechar; use Widechar;
43 with GNAT.Byte_Order_Mark; use GNAT.Byte_Order_Mark;
45 with System; use System;
46 with System.Memory;
47 with System.WCh_Con; use System.WCh_Con;
49 with Unchecked_Conversion;
50 with Unchecked_Deallocation;
52 package body Sinput is
54 use ASCII;
55 -- Make control characters visible
57 First_Time_Around : Boolean := True;
58 -- This needs a comment ???
60 -- Routines to support conversion between types Lines_Table_Ptr,
61 -- Logical_Lines_Table_Ptr and System.Address.
63 pragma Warnings (Off);
64 -- These unchecked conversions are aliasing safe, since they are never
65 -- used to construct improperly aliased pointer values.
67 function To_Address is
68 new Unchecked_Conversion (Lines_Table_Ptr, Address);
70 function To_Address is
71 new Unchecked_Conversion (Logical_Lines_Table_Ptr, Address);
73 function To_Pointer is
74 new Unchecked_Conversion (Address, Lines_Table_Ptr);
76 function To_Pointer is
77 new Unchecked_Conversion (Address, Logical_Lines_Table_Ptr);
79 pragma Warnings (On);
81 ---------------------------
82 -- Add_Line_Tables_Entry --
83 ---------------------------
85 procedure Add_Line_Tables_Entry
86 (S : in out Source_File_Record;
87 P : Source_Ptr)
89 LL : Physical_Line_Number;
91 begin
92 -- Reallocate the lines tables if necessary
94 -- Note: the reason we do not use the normal Table package
95 -- mechanism is that we have several of these tables. We could
96 -- use the new GNAT.Dynamic_Tables package and that would probably
97 -- be a good idea ???
99 if S.Last_Source_Line = S.Lines_Table_Max then
100 Alloc_Line_Tables
102 Int (S.Last_Source_Line) *
103 ((100 + Alloc.Lines_Increment) / 100));
105 if Debug_Flag_D then
106 Write_Str ("--> Reallocating lines table, size = ");
107 Write_Int (Int (S.Lines_Table_Max));
108 Write_Eol;
109 end if;
110 end if;
112 S.Last_Source_Line := S.Last_Source_Line + 1;
113 LL := S.Last_Source_Line;
115 S.Lines_Table (LL) := P;
117 -- Deal with setting new entry in logical lines table if one is
118 -- present. Note that there is always space (because the call to
119 -- Alloc_Line_Tables makes sure both tables are the same length),
121 if S.Logical_Lines_Table /= null then
123 -- We can always set the entry from the previous one, because
124 -- the processing for a Source_Reference pragma ensures that
125 -- at least one entry following the pragma is set up correctly.
127 S.Logical_Lines_Table (LL) := S.Logical_Lines_Table (LL - 1) + 1;
128 end if;
129 end Add_Line_Tables_Entry;
131 -----------------------
132 -- Alloc_Line_Tables --
133 -----------------------
135 procedure Alloc_Line_Tables
136 (S : in out Source_File_Record;
137 New_Max : Nat)
139 subtype size_t is Memory.size_t;
141 New_Table : Lines_Table_Ptr;
143 New_Logical_Table : Logical_Lines_Table_Ptr;
145 New_Size : constant size_t :=
146 size_t (New_Max * Lines_Table_Type'Component_Size /
147 Storage_Unit);
149 begin
150 if S.Lines_Table = null then
151 New_Table := To_Pointer (Memory.Alloc (New_Size));
153 else
154 New_Table :=
155 To_Pointer (Memory.Realloc (To_Address (S.Lines_Table), New_Size));
156 end if;
158 if New_Table = null then
159 raise Storage_Error;
160 else
161 S.Lines_Table := New_Table;
162 S.Lines_Table_Max := Physical_Line_Number (New_Max);
163 end if;
165 if S.Num_SRef_Pragmas /= 0 then
166 if S.Logical_Lines_Table = null then
167 New_Logical_Table := To_Pointer (Memory.Alloc (New_Size));
168 else
169 New_Logical_Table := To_Pointer
170 (Memory.Realloc (To_Address (S.Logical_Lines_Table), New_Size));
171 end if;
173 if New_Logical_Table = null then
174 raise Storage_Error;
175 else
176 S.Logical_Lines_Table := New_Logical_Table;
177 end if;
178 end if;
179 end Alloc_Line_Tables;
181 -----------------
182 -- Backup_Line --
183 -----------------
185 procedure Backup_Line (P : in out Source_Ptr) is
186 Sindex : constant Source_File_Index := Get_Source_File_Index (P);
187 Src : constant Source_Buffer_Ptr :=
188 Source_File.Table (Sindex).Source_Text;
189 Sfirst : constant Source_Ptr :=
190 Source_File.Table (Sindex).Source_First;
192 begin
193 P := P - 1;
195 if P = Sfirst then
196 return;
197 end if;
199 if Src (P) = CR then
200 if Src (P - 1) = LF then
201 P := P - 1;
202 end if;
204 else -- Src (P) = LF
205 if Src (P - 1) = CR then
206 P := P - 1;
207 end if;
208 end if;
210 -- Now find first character of the previous line
212 while P > Sfirst
213 and then Src (P - 1) /= LF
214 and then Src (P - 1) /= CR
215 loop
216 P := P - 1;
217 end loop;
218 end Backup_Line;
220 ---------------------------
221 -- Build_Location_String --
222 ---------------------------
224 procedure Build_Location_String
225 (Buf : in out Bounded_String;
226 Loc : Source_Ptr)
228 Ptr : Source_Ptr := Loc;
230 begin
231 -- Loop through instantiations
233 loop
234 Append (Buf, Reference_Name (Get_Source_File_Index (Ptr)));
235 Append (Buf, ':');
236 Append (Buf, Nat (Get_Logical_Line_Number (Ptr)));
238 Ptr := Instantiation_Location (Ptr);
239 exit when Ptr = No_Location;
240 Append (Buf, " instantiated at ");
241 end loop;
242 end Build_Location_String;
244 function Build_Location_String (Loc : Source_Ptr) return String is
245 Buf : Bounded_String;
246 begin
247 Build_Location_String (Buf, Loc);
248 return +Buf;
249 end Build_Location_String;
251 -------------------
252 -- Check_For_BOM --
253 -------------------
255 procedure Check_For_BOM is
256 BOM : BOM_Kind;
257 Len : Natural;
258 Tst : String (1 .. 5);
259 C : Character;
261 begin
262 for J in 1 .. 5 loop
263 C := Source (Scan_Ptr + Source_Ptr (J) - 1);
265 -- Definitely no BOM if EOF character marks either end of file, or
266 -- an illegal non-BOM character if not at the end of file.
268 if C = EOF then
269 return;
270 end if;
272 Tst (J) := C;
273 end loop;
275 Read_BOM (Tst, Len, BOM, False);
277 case BOM is
278 when UTF8_All =>
279 Scan_Ptr := Scan_Ptr + Source_Ptr (Len);
280 Wide_Character_Encoding_Method := WCEM_UTF8;
281 Upper_Half_Encoding := True;
283 when UTF16_LE | UTF16_BE =>
284 Set_Standard_Error;
285 Write_Line ("UTF-16 encoding format not recognized");
286 Set_Standard_Output;
287 raise Unrecoverable_Error;
289 when UTF32_LE | UTF32_BE =>
290 Set_Standard_Error;
291 Write_Line ("UTF-32 encoding format not recognized");
292 Set_Standard_Output;
293 raise Unrecoverable_Error;
295 when Unknown =>
296 null;
298 when others =>
299 raise Program_Error;
300 end case;
301 end Check_For_BOM;
303 -----------------------------
304 -- Comes_From_Inlined_Body --
305 -----------------------------
307 function Comes_From_Inlined_Body (S : Source_Ptr) return Boolean is
308 SIE : Source_File_Record renames
309 Source_File.Table (Get_Source_File_Index (S));
310 begin
311 return SIE.Inlined_Body;
312 end Comes_From_Inlined_Body;
314 -----------------------
315 -- Get_Column_Number --
316 -----------------------
318 function Get_Column_Number (P : Source_Ptr) return Column_Number is
319 S : Source_Ptr;
320 C : Column_Number;
321 Sindex : Source_File_Index;
322 Src : Source_Buffer_Ptr;
324 begin
325 -- If the input source pointer is not a meaningful value then return
326 -- at once with column number 1. This can happen for a file not found
327 -- condition for a file loaded indirectly by RTE, and also perhaps on
328 -- some unknown internal error conditions. In either case we certainly
329 -- don't want to blow up.
331 if P < 1 then
332 return 1;
334 else
335 Sindex := Get_Source_File_Index (P);
336 Src := Source_File.Table (Sindex).Source_Text;
337 S := Line_Start (P);
338 C := 1;
340 while S < P loop
341 if Src (S) = HT then
342 C := (C - 1) / 8 * 8 + (8 + 1);
343 S := S + 1;
345 -- Deal with wide character case, but don't include brackets
346 -- notation in this circuit, since we know that this will
347 -- display unencoded (no one encodes brackets notation).
349 elsif Src (S) /= '[' and then Is_Start_Of_Wide_Char (Src, S) then
350 C := C + 1;
351 Skip_Wide (Src, S);
353 -- Normal (non-wide) character case or brackets sequence
355 else
356 C := C + 1;
357 S := S + 1;
358 end if;
359 end loop;
361 return C;
362 end if;
363 end Get_Column_Number;
365 -----------------------------
366 -- Get_Logical_Line_Number --
367 -----------------------------
369 function Get_Logical_Line_Number
370 (P : Source_Ptr) return Logical_Line_Number
372 SFR : Source_File_Record
373 renames Source_File.Table (Get_Source_File_Index (P));
375 L : constant Physical_Line_Number := Get_Physical_Line_Number (P);
377 begin
378 if SFR.Num_SRef_Pragmas = 0 then
379 return Logical_Line_Number (L);
380 else
381 return SFR.Logical_Lines_Table (L);
382 end if;
383 end Get_Logical_Line_Number;
385 ---------------------------------
386 -- Get_Logical_Line_Number_Img --
387 ---------------------------------
389 function Get_Logical_Line_Number_Img
390 (P : Source_Ptr) return String
392 begin
393 Name_Len := 0;
394 Add_Nat_To_Name_Buffer (Nat (Get_Logical_Line_Number (P)));
395 return Name_Buffer (1 .. Name_Len);
396 end Get_Logical_Line_Number_Img;
398 ------------------------------
399 -- Get_Physical_Line_Number --
400 ------------------------------
402 function Get_Physical_Line_Number
403 (P : Source_Ptr) return Physical_Line_Number
405 Sfile : Source_File_Index;
406 Table : Lines_Table_Ptr;
407 Lo : Physical_Line_Number;
408 Hi : Physical_Line_Number;
409 Mid : Physical_Line_Number;
410 Loc : Source_Ptr;
412 begin
413 -- If the input source pointer is not a meaningful value then return
414 -- at once with line number 1. This can happen for a file not found
415 -- condition for a file loaded indirectly by RTE, and also perhaps on
416 -- some unknown internal error conditions. In either case we certainly
417 -- don't want to blow up.
419 if P < 1 then
420 return 1;
422 -- Otherwise we can do the binary search
424 else
425 Sfile := Get_Source_File_Index (P);
426 Loc := P + Source_File.Table (Sfile).Sloc_Adjust;
427 Table := Source_File.Table (Sfile).Lines_Table;
428 Lo := 1;
429 Hi := Source_File.Table (Sfile).Last_Source_Line;
431 loop
432 Mid := (Lo + Hi) / 2;
434 if Loc < Table (Mid) then
435 Hi := Mid - 1;
437 else -- Loc >= Table (Mid)
439 if Mid = Hi or else
440 Loc < Table (Mid + 1)
441 then
442 return Mid;
443 else
444 Lo := Mid + 1;
445 end if;
447 end if;
449 end loop;
450 end if;
451 end Get_Physical_Line_Number;
453 ---------------------------
454 -- Get_Source_File_Index --
455 ---------------------------
457 function Get_Source_File_Index (S : Source_Ptr) return Source_File_Index is
458 begin
459 return Source_File_Index_Table (Int (S) / Source_Align);
460 end Get_Source_File_Index;
462 ----------------
463 -- Initialize --
464 ----------------
466 procedure Initialize is
467 begin
468 Source_gnat_adc := No_Source_File;
469 First_Time_Around := True;
471 Source_File.Init;
473 Instances.Init;
474 Instances.Append (No_Location);
475 pragma Assert (Instances.Last = No_Instance_Id);
476 end Initialize;
478 -------------------
479 -- Instantiation --
480 -------------------
482 function Instantiation (S : SFI) return Source_Ptr is
483 SIE : Source_File_Record renames Source_File.Table (S);
484 begin
485 if SIE.Inlined_Body then
486 return SIE.Inlined_Call;
487 else
488 return Instances.Table (SIE.Instance);
489 end if;
490 end Instantiation;
492 -------------------------
493 -- Instantiation_Depth --
494 -------------------------
496 function Instantiation_Depth (S : Source_Ptr) return Nat is
497 Sind : Source_File_Index;
498 Sval : Source_Ptr;
499 Depth : Nat;
501 begin
502 Sval := S;
503 Depth := 0;
505 loop
506 Sind := Get_Source_File_Index (Sval);
507 Sval := Instantiation (Sind);
508 exit when Sval = No_Location;
509 Depth := Depth + 1;
510 end loop;
512 return Depth;
513 end Instantiation_Depth;
515 ----------------------------
516 -- Instantiation_Location --
517 ----------------------------
519 function Instantiation_Location (S : Source_Ptr) return Source_Ptr is
520 begin
521 return Instantiation (Get_Source_File_Index (S));
522 end Instantiation_Location;
524 --------------------------
525 -- Iterate_On_Instances --
526 --------------------------
528 procedure Iterate_On_Instances is
529 begin
530 for J in 1 .. Instances.Last loop
531 Process (J, Instances.Table (J));
532 end loop;
533 end Iterate_On_Instances;
535 ----------------------
536 -- Last_Source_File --
537 ----------------------
539 function Last_Source_File return Source_File_Index is
540 begin
541 return Source_File.Last;
542 end Last_Source_File;
544 ----------------
545 -- Line_Start --
546 ----------------
548 function Line_Start (P : Source_Ptr) return Source_Ptr is
549 Sindex : constant Source_File_Index := Get_Source_File_Index (P);
550 Src : constant Source_Buffer_Ptr :=
551 Source_File.Table (Sindex).Source_Text;
552 Sfirst : constant Source_Ptr :=
553 Source_File.Table (Sindex).Source_First;
554 S : Source_Ptr;
556 begin
557 S := P;
558 while S > Sfirst
559 and then Src (S - 1) /= CR
560 and then Src (S - 1) /= LF
561 loop
562 S := S - 1;
563 end loop;
565 return S;
566 end Line_Start;
568 function Line_Start
569 (L : Physical_Line_Number;
570 S : Source_File_Index) return Source_Ptr
572 begin
573 return Source_File.Table (S).Lines_Table (L);
574 end Line_Start;
576 ----------
577 -- Lock --
578 ----------
580 procedure Lock is
581 begin
582 Source_File.Locked := True;
583 Source_File.Release;
584 end Lock;
586 ----------------------
587 -- Num_Source_Files --
588 ----------------------
590 function Num_Source_Files return Nat is
591 begin
592 return Int (Source_File.Last) - Int (Source_File.First) + 1;
593 end Num_Source_Files;
595 ----------------------
596 -- Num_Source_Lines --
597 ----------------------
599 function Num_Source_Lines (S : Source_File_Index) return Nat is
600 begin
601 return Nat (Source_File.Table (S).Last_Source_Line);
602 end Num_Source_Lines;
604 -----------------------
605 -- Original_Location --
606 -----------------------
608 function Original_Location (S : Source_Ptr) return Source_Ptr is
609 Sindex : Source_File_Index;
610 Tindex : Source_File_Index;
612 begin
613 if S <= No_Location then
614 return S;
616 else
617 Sindex := Get_Source_File_Index (S);
619 if Instantiation (Sindex) = No_Location then
620 return S;
622 else
623 Tindex := Template (Sindex);
624 while Instantiation (Tindex) /= No_Location loop
625 Tindex := Template (Tindex);
626 end loop;
628 return S - Source_First (Sindex) + Source_First (Tindex);
629 end if;
630 end if;
631 end Original_Location;
633 -------------------------
634 -- Physical_To_Logical --
635 -------------------------
637 function Physical_To_Logical
638 (Line : Physical_Line_Number;
639 S : Source_File_Index) return Logical_Line_Number
641 SFR : Source_File_Record renames Source_File.Table (S);
643 begin
644 if SFR.Num_SRef_Pragmas = 0 then
645 return Logical_Line_Number (Line);
646 else
647 return SFR.Logical_Lines_Table (Line);
648 end if;
649 end Physical_To_Logical;
651 --------------------------------
652 -- Register_Source_Ref_Pragma --
653 --------------------------------
655 procedure Register_Source_Ref_Pragma
656 (File_Name : File_Name_Type;
657 Stripped_File_Name : File_Name_Type;
658 Mapped_Line : Nat;
659 Line_After_Pragma : Physical_Line_Number)
661 subtype size_t is Memory.size_t;
663 SFR : Source_File_Record renames Source_File.Table (Current_Source_File);
665 ML : Logical_Line_Number;
667 begin
668 if File_Name /= No_File then
669 SFR.Reference_Name := Stripped_File_Name;
670 SFR.Full_Ref_Name := File_Name;
672 if not Debug_Generated_Code then
673 SFR.Debug_Source_Name := Stripped_File_Name;
674 SFR.Full_Debug_Name := File_Name;
675 end if;
677 SFR.Num_SRef_Pragmas := SFR.Num_SRef_Pragmas + 1;
678 end if;
680 if SFR.Num_SRef_Pragmas = 1 then
681 SFR.First_Mapped_Line := Logical_Line_Number (Mapped_Line);
682 end if;
684 if SFR.Logical_Lines_Table = null then
685 SFR.Logical_Lines_Table := To_Pointer
686 (Memory.Alloc
687 (size_t (SFR.Lines_Table_Max *
688 Logical_Lines_Table_Type'Component_Size /
689 Storage_Unit)));
690 end if;
692 SFR.Logical_Lines_Table (Line_After_Pragma - 1) := No_Line_Number;
694 ML := Logical_Line_Number (Mapped_Line);
695 for J in Line_After_Pragma .. SFR.Last_Source_Line loop
696 SFR.Logical_Lines_Table (J) := ML;
697 ML := ML + 1;
698 end loop;
699 end Register_Source_Ref_Pragma;
701 ---------------------------------
702 -- Set_Source_File_Index_Table --
703 ---------------------------------
705 procedure Set_Source_File_Index_Table (Xnew : Source_File_Index) is
706 Ind : Int;
707 SP : Source_Ptr;
708 SL : constant Source_Ptr := Source_File.Table (Xnew).Source_Last;
709 begin
710 SP := Source_File.Table (Xnew).Source_First;
711 pragma Assert (SP mod Source_Align = 0);
712 Ind := Int (SP) / Source_Align;
713 while SP <= SL loop
714 Source_File_Index_Table (Ind) := Xnew;
715 SP := SP + Source_Align;
716 Ind := Ind + 1;
717 end loop;
718 end Set_Source_File_Index_Table;
720 ---------------------------
721 -- Skip_Line_Terminators --
722 ---------------------------
724 procedure Skip_Line_Terminators
725 (P : in out Source_Ptr;
726 Physical : out Boolean)
728 Chr : constant Character := Source (P);
730 begin
731 if Chr = CR then
732 if Source (P + 1) = LF then
733 P := P + 2;
734 else
735 P := P + 1;
736 end if;
738 elsif Chr = LF then
739 P := P + 1;
741 elsif Chr = FF or else Chr = VT then
742 P := P + 1;
743 Physical := False;
744 return;
746 -- Otherwise we have a wide character
748 else
749 Skip_Wide (Source, P);
750 end if;
752 -- Fall through in the physical line terminator case. First deal with
753 -- making a possible entry into the lines table if one is needed.
755 -- Note: we are dealing with a real source file here, this cannot be
756 -- the instantiation case, so we need not worry about Sloc adjustment.
758 declare
759 S : Source_File_Record
760 renames Source_File.Table (Current_Source_File);
762 begin
763 Physical := True;
765 -- Make entry in lines table if not already made (in some scan backup
766 -- cases, we will be rescanning previously scanned source, so the
767 -- entry may have already been made on the previous forward scan).
769 if Source (P) /= EOF
770 and then P > S.Lines_Table (S.Last_Source_Line)
771 then
772 Add_Line_Tables_Entry (S, P);
773 end if;
774 end;
775 end Skip_Line_Terminators;
777 ----------------
778 -- Sloc_Range --
779 ----------------
781 procedure Sloc_Range (N : Node_Id; Min, Max : out Source_Ptr) is
783 function Process (N : Node_Id) return Traverse_Result;
784 -- Process function for traversing the node tree
786 procedure Traverse is new Traverse_Proc (Process);
788 -------------
789 -- Process --
790 -------------
792 function Process (N : Node_Id) return Traverse_Result is
793 Orig : constant Node_Id := Original_Node (N);
795 begin
796 if Sloc (Orig) < Min then
797 if Sloc (Orig) > No_Location then
798 Min := Sloc (Orig);
799 end if;
801 elsif Sloc (Orig) > Max then
802 if Sloc (Orig) > No_Location then
803 Max := Sloc (Orig);
804 end if;
805 end if;
807 return OK_Orig;
808 end Process;
810 -- Start of processing for Sloc_Range
812 begin
813 Min := Sloc (N);
814 Max := Sloc (N);
815 Traverse (N);
816 end Sloc_Range;
818 -------------------
819 -- Source_Offset --
820 -------------------
822 function Source_Offset (S : Source_Ptr) return Nat is
823 Sindex : constant Source_File_Index := Get_Source_File_Index (S);
824 Sfirst : constant Source_Ptr :=
825 Source_File.Table (Sindex).Source_First;
826 begin
827 return Nat (S - Sfirst);
828 end Source_Offset;
830 ------------------------
831 -- Top_Level_Location --
832 ------------------------
834 function Top_Level_Location (S : Source_Ptr) return Source_Ptr is
835 Oldloc : Source_Ptr;
836 Newloc : Source_Ptr;
838 begin
839 Newloc := S;
840 loop
841 Oldloc := Newloc;
842 Newloc := Instantiation_Location (Oldloc);
843 exit when Newloc = No_Location;
844 end loop;
846 return Oldloc;
847 end Top_Level_Location;
849 ---------------
850 -- Tree_Read --
851 ---------------
853 procedure Tree_Read is
854 begin
855 -- First we must free any old source buffer pointers
857 if not First_Time_Around then
858 for J in Source_File.First .. Source_File.Last loop
859 declare
860 S : Source_File_Record renames Source_File.Table (J);
862 procedure Free_Ptr is new Unchecked_Deallocation
863 (Big_Source_Buffer, Source_Buffer_Ptr);
865 pragma Warnings (Off);
866 -- This unchecked conversion is aliasing safe, since it is not
867 -- used to create improperly aliased pointer values.
869 function To_Source_Buffer_Ptr is new
870 Unchecked_Conversion (Address, Source_Buffer_Ptr);
872 pragma Warnings (On);
874 Tmp1 : Source_Buffer_Ptr;
876 begin
877 if S.Instance /= No_Instance_Id then
878 null;
880 else
881 -- Free the buffer, we use Free here, because we used malloc
882 -- or realloc directly to allocate the tables. That is
883 -- because we were playing the big array trick.
885 -- We have to recreate a proper pointer to the actual array
886 -- from the zero origin pointer stored in the source table.
888 Tmp1 :=
889 To_Source_Buffer_Ptr
890 (S.Source_Text (S.Source_First)'Address);
891 Free_Ptr (Tmp1);
893 if S.Lines_Table /= null then
894 Memory.Free (To_Address (S.Lines_Table));
895 S.Lines_Table := null;
896 end if;
898 if S.Logical_Lines_Table /= null then
899 Memory.Free (To_Address (S.Logical_Lines_Table));
900 S.Logical_Lines_Table := null;
901 end if;
902 end if;
903 end;
904 end loop;
905 end if;
907 -- Read in source file table and instance table
909 Source_File.Tree_Read;
910 Instances.Tree_Read;
912 -- The pointers we read in there for the source buffer and lines table
913 -- pointers are junk. We now read in the actual data that is referenced
914 -- by these two fields.
916 for J in Source_File.First .. Source_File.Last loop
917 declare
918 S : Source_File_Record renames Source_File.Table (J);
920 begin
921 -- For the instantiation case, we do not read in any data. Instead
922 -- we share the data for the generic template entry. Since the
923 -- template always occurs first, we can safely refer to its data.
925 if S.Instance /= No_Instance_Id then
926 declare
927 ST : Source_File_Record renames
928 Source_File.Table (S.Template);
930 begin
931 -- The lines tables are copied from the template entry
933 S.Lines_Table :=
934 Source_File.Table (S.Template).Lines_Table;
935 S.Logical_Lines_Table :=
936 Source_File.Table (S.Template).Logical_Lines_Table;
938 -- In the case of the source table pointer, we share the
939 -- same data as the generic template, but the virtual origin
940 -- is adjusted. For example, if the first subscript of the
941 -- template is 100, and that of the instantiation is 200,
942 -- then the instantiation pointer is obtained by subtracting
943 -- 100 from the template pointer.
945 declare
946 pragma Suppress (All_Checks);
948 pragma Warnings (Off);
949 -- This unchecked conversion is aliasing safe since it
950 -- not used to create improperly aliased pointer values.
952 function To_Source_Buffer_Ptr is new
953 Unchecked_Conversion (Address, Source_Buffer_Ptr);
955 pragma Warnings (On);
957 begin
958 S.Source_Text :=
959 To_Source_Buffer_Ptr
960 (ST.Source_Text
961 (ST.Source_First - S.Source_First)'Address);
962 end;
963 end;
965 -- Normal case (non-instantiation)
967 else
968 First_Time_Around := False;
969 S.Lines_Table := null;
970 S.Logical_Lines_Table := null;
971 Alloc_Line_Tables (S, Int (S.Last_Source_Line));
973 for J in 1 .. S.Last_Source_Line loop
974 Tree_Read_Int (Int (S.Lines_Table (J)));
975 end loop;
977 if S.Num_SRef_Pragmas /= 0 then
978 for J in 1 .. S.Last_Source_Line loop
979 Tree_Read_Int (Int (S.Logical_Lines_Table (J)));
980 end loop;
981 end if;
983 -- Allocate source buffer and read in the data and then set the
984 -- virtual origin to point to the logical zero'th element. This
985 -- address must be computed with subscript checks turned off.
987 declare
988 subtype B is Text_Buffer (S.Source_First .. S.Source_Last);
989 type Text_Buffer_Ptr is access B;
990 T : Text_Buffer_Ptr;
992 pragma Suppress (All_Checks);
994 pragma Warnings (Off);
995 -- This unchecked conversion is aliasing safe, since it is
996 -- never used to create improperly aliased pointer values.
998 function To_Source_Buffer_Ptr is new
999 Unchecked_Conversion (Address, Source_Buffer_Ptr);
1001 pragma Warnings (On);
1003 begin
1004 T := new B;
1006 Tree_Read_Data (T (S.Source_First)'Address,
1007 Int (S.Source_Last) - Int (S.Source_First) + 1);
1009 S.Source_Text := To_Source_Buffer_Ptr (T (0)'Address);
1010 end;
1011 end if;
1012 end;
1014 Set_Source_File_Index_Table (J);
1015 end loop;
1016 end Tree_Read;
1018 ----------------
1019 -- Tree_Write --
1020 ----------------
1022 procedure Tree_Write is
1023 begin
1024 Source_File.Tree_Write;
1025 Instances.Tree_Write;
1027 -- The pointers we wrote out there for the source buffer and lines
1028 -- table pointers are junk, we now write out the actual data that
1029 -- is referenced by these two fields.
1031 for J in Source_File.First .. Source_File.Last loop
1032 declare
1033 S : Source_File_Record renames Source_File.Table (J);
1035 begin
1036 -- For instantiations, there is nothing to do, since the data is
1037 -- shared with the generic template. When the tree is read, the
1038 -- pointers must be set, but no extra data needs to be written.
1040 if S.Instance /= No_Instance_Id then
1041 null;
1043 -- For the normal case, write out the data of the tables
1045 else
1046 -- Lines table
1048 for J in 1 .. S.Last_Source_Line loop
1049 Tree_Write_Int (Int (S.Lines_Table (J)));
1050 end loop;
1052 -- Logical lines table if present
1054 if S.Num_SRef_Pragmas /= 0 then
1055 for J in 1 .. S.Last_Source_Line loop
1056 Tree_Write_Int (Int (S.Logical_Lines_Table (J)));
1057 end loop;
1058 end if;
1060 -- Source buffer
1062 Tree_Write_Data
1063 (S.Source_Text (S.Source_First)'Address,
1064 Int (S.Source_Last) - Int (S.Source_First) + 1);
1065 end if;
1066 end;
1067 end loop;
1068 end Tree_Write;
1070 --------------------
1071 -- Write_Location --
1072 --------------------
1074 procedure Write_Location (P : Source_Ptr) is
1075 begin
1076 if P = No_Location then
1077 Write_Str ("<no location>");
1079 elsif P <= Standard_Location then
1080 Write_Str ("<standard location>");
1082 else
1083 declare
1084 SI : constant Source_File_Index := Get_Source_File_Index (P);
1086 begin
1087 Write_Name (Debug_Source_Name (SI));
1088 Write_Char (':');
1089 Write_Int (Int (Get_Logical_Line_Number (P)));
1090 Write_Char (':');
1091 Write_Int (Int (Get_Column_Number (P)));
1093 if Instantiation (SI) /= No_Location then
1094 Write_Str (" [");
1095 Write_Location (Instantiation (SI));
1096 Write_Char (']');
1097 end if;
1098 end;
1099 end if;
1100 end Write_Location;
1102 ----------------------
1103 -- Write_Time_Stamp --
1104 ----------------------
1106 procedure Write_Time_Stamp (S : Source_File_Index) is
1107 T : constant Time_Stamp_Type := Time_Stamp (S);
1108 P : Natural;
1110 begin
1111 if T (1) = '9' then
1112 Write_Str ("19");
1113 P := 0;
1114 else
1115 Write_Char (T (1));
1116 Write_Char (T (2));
1117 P := 2;
1118 end if;
1120 Write_Char (T (P + 1));
1121 Write_Char (T (P + 2));
1122 Write_Char ('-');
1124 Write_Char (T (P + 3));
1125 Write_Char (T (P + 4));
1126 Write_Char ('-');
1128 Write_Char (T (P + 5));
1129 Write_Char (T (P + 6));
1130 Write_Char (' ');
1132 Write_Char (T (P + 7));
1133 Write_Char (T (P + 8));
1134 Write_Char (':');
1136 Write_Char (T (P + 9));
1137 Write_Char (T (P + 10));
1138 Write_Char (':');
1140 Write_Char (T (P + 11));
1141 Write_Char (T (P + 12));
1142 end Write_Time_Stamp;
1144 ----------------------------------------------
1145 -- Access Subprograms for Source File Table --
1146 ----------------------------------------------
1148 function Debug_Source_Name (S : SFI) return File_Name_Type is
1149 begin
1150 return Source_File.Table (S).Debug_Source_Name;
1151 end Debug_Source_Name;
1153 function Instance (S : SFI) return Instance_Id is
1154 begin
1155 return Source_File.Table (S).Instance;
1156 end Instance;
1158 function File_Name (S : SFI) return File_Name_Type is
1159 begin
1160 return Source_File.Table (S).File_Name;
1161 end File_Name;
1163 function File_Type (S : SFI) return Type_Of_File is
1164 begin
1165 return Source_File.Table (S).File_Type;
1166 end File_Type;
1168 function First_Mapped_Line (S : SFI) return Logical_Line_Number is
1169 begin
1170 return Source_File.Table (S).First_Mapped_Line;
1171 end First_Mapped_Line;
1173 function Full_Debug_Name (S : SFI) return File_Name_Type is
1174 begin
1175 return Source_File.Table (S).Full_Debug_Name;
1176 end Full_Debug_Name;
1178 function Full_File_Name (S : SFI) return File_Name_Type is
1179 begin
1180 return Source_File.Table (S).Full_File_Name;
1181 end Full_File_Name;
1183 function Full_Ref_Name (S : SFI) return File_Name_Type is
1184 begin
1185 return Source_File.Table (S).Full_Ref_Name;
1186 end Full_Ref_Name;
1188 function Identifier_Casing (S : SFI) return Casing_Type is
1189 begin
1190 return Source_File.Table (S).Identifier_Casing;
1191 end Identifier_Casing;
1193 function Inlined_Body (S : SFI) return Boolean is
1194 begin
1195 return Source_File.Table (S).Inlined_Body;
1196 end Inlined_Body;
1198 function Inlined_Call (S : SFI) return Source_Ptr is
1199 begin
1200 return Source_File.Table (S).Inlined_Call;
1201 end Inlined_Call;
1203 function Keyword_Casing (S : SFI) return Casing_Type is
1204 begin
1205 return Source_File.Table (S).Keyword_Casing;
1206 end Keyword_Casing;
1208 function Last_Source_Line (S : SFI) return Physical_Line_Number is
1209 begin
1210 return Source_File.Table (S).Last_Source_Line;
1211 end Last_Source_Line;
1213 function License (S : SFI) return License_Type is
1214 begin
1215 return Source_File.Table (S).License;
1216 end License;
1218 function Num_SRef_Pragmas (S : SFI) return Nat is
1219 begin
1220 return Source_File.Table (S).Num_SRef_Pragmas;
1221 end Num_SRef_Pragmas;
1223 function Reference_Name (S : SFI) return File_Name_Type is
1224 begin
1225 return Source_File.Table (S).Reference_Name;
1226 end Reference_Name;
1228 function Source_Checksum (S : SFI) return Word is
1229 begin
1230 return Source_File.Table (S).Source_Checksum;
1231 end Source_Checksum;
1233 function Source_First (S : SFI) return Source_Ptr is
1234 begin
1235 if S = Internal_Source_File then
1236 return Internal_Source'First;
1237 else
1238 return Source_File.Table (S).Source_First;
1239 end if;
1240 end Source_First;
1242 function Source_Last (S : SFI) return Source_Ptr is
1243 begin
1244 if S = Internal_Source_File then
1245 return Internal_Source'Last;
1246 else
1247 return Source_File.Table (S).Source_Last;
1248 end if;
1249 end Source_Last;
1251 function Source_Text (S : SFI) return Source_Buffer_Ptr is
1252 begin
1253 if S = Internal_Source_File then
1254 return Internal_Source_Ptr;
1255 else
1256 return Source_File.Table (S).Source_Text;
1257 end if;
1258 end Source_Text;
1260 function Template (S : SFI) return SFI is
1261 begin
1262 return Source_File.Table (S).Template;
1263 end Template;
1265 function Time_Stamp (S : SFI) return Time_Stamp_Type is
1266 begin
1267 return Source_File.Table (S).Time_Stamp;
1268 end Time_Stamp;
1270 function Unit (S : SFI) return Unit_Number_Type is
1271 begin
1272 return Source_File.Table (S).Unit;
1273 end Unit;
1275 ------------------------------------------
1276 -- Set Procedures for Source File Table --
1277 ------------------------------------------
1279 procedure Set_Identifier_Casing (S : SFI; C : Casing_Type) is
1280 begin
1281 Source_File.Table (S).Identifier_Casing := C;
1282 end Set_Identifier_Casing;
1284 procedure Set_Keyword_Casing (S : SFI; C : Casing_Type) is
1285 begin
1286 Source_File.Table (S).Keyword_Casing := C;
1287 end Set_Keyword_Casing;
1289 procedure Set_License (S : SFI; L : License_Type) is
1290 begin
1291 Source_File.Table (S).License := L;
1292 end Set_License;
1294 procedure Set_Unit (S : SFI; U : Unit_Number_Type) is
1295 begin
1296 Source_File.Table (S).Unit := U;
1297 end Set_Unit;
1299 ----------------------
1300 -- Trim_Lines_Table --
1301 ----------------------
1303 procedure Trim_Lines_Table (S : Source_File_Index) is
1304 Max : constant Nat := Nat (Source_File.Table (S).Last_Source_Line);
1306 begin
1307 -- Release allocated storage that is no longer needed
1309 Source_File.Table (S).Lines_Table := To_Pointer
1310 (Memory.Realloc
1311 (To_Address (Source_File.Table (S).Lines_Table),
1312 Memory.size_t
1313 (Max * (Lines_Table_Type'Component_Size / System.Storage_Unit))));
1314 Source_File.Table (S).Lines_Table_Max := Physical_Line_Number (Max);
1315 end Trim_Lines_Table;
1317 ------------
1318 -- Unlock --
1319 ------------
1321 procedure Unlock is
1322 begin
1323 Source_File.Locked := False;
1324 Source_File.Release;
1325 end Unlock;
1327 --------
1328 -- wl --
1329 --------
1331 procedure wl (P : Source_Ptr) is
1332 begin
1333 Write_Location (P);
1334 Write_Eol;
1335 end wl;
1337 end Sinput;