1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Errout
; use Errout
;
31 with Fname
; use Fname
;
34 with Osint
; use Osint
;
35 with Output
; use Output
;
37 with Prepcomp
; use Prepcomp
;
38 with Scans
; use Scans
;
40 with Sem_Aux
; use Sem_Aux
;
41 with Sem_Util
; use Sem_Util
;
42 with Sinfo
; use Sinfo
;
43 with Snames
; use Snames
;
44 with System
; use System
;
46 with System
.OS_Lib
; use System
.OS_Lib
;
48 with Unchecked_Conversion
;
50 package body Sinput
.L
is
52 Prep_Buffer
: Text_Buffer_Ptr
:= null;
53 -- A buffer to temporarily stored the result of preprocessing a source.
54 -- It is only allocated if there is at least one source to preprocess.
56 Prep_Buffer_Last
: Text_Ptr
:= 0;
57 -- Index of the last significant character in Prep_Buffer
59 Initial_Size_Of_Prep_Buffer
: constant := 10_000
;
60 -- Size of Prep_Buffer when it is first allocated
62 -- When a file is to be preprocessed and the options to list symbols
63 -- has been selected (switch -s), Prep.List_Symbols is called with a
64 -- "foreword", a single line indicating what source the symbols apply to.
65 -- The following two constant String are the start and the end of this
68 Foreword_Start
: constant String :=
69 "Preprocessing Symbols for source """;
71 Foreword_End
: constant String := """";
77 procedure Put_Char_In_Prep_Buffer
(C
: Character);
78 -- Add one character in Prep_Buffer, extending Prep_Buffer if need be.
79 -- Used to initialize the preprocessor.
81 procedure New_EOL_In_Prep_Buffer
;
82 -- Add an LF to Prep_Buffer (used to initialize the preprocessor)
86 T
: Osint
.File_Type
) return Source_File_Index
;
87 -- Load a source file, a configuration pragmas file or a definition file
88 -- Coding also allows preprocessing file, but not a library file ???
90 -------------------------------
91 -- Adjust_Instantiation_Sloc --
92 -------------------------------
94 procedure Adjust_Instantiation_Sloc
96 Factor
: Sloc_Adjustment
)
98 Loc
: constant Source_Ptr
:= Sloc
(N
);
101 -- We only do the adjustment if the value is between the appropriate low
102 -- and high values. It is not clear that this should ever not be the
103 -- case, but in practice there seem to be some nodes that get copied
104 -- twice, and this is a defence against that happening.
106 if Factor
.Lo
<= Loc
and then Loc
<= Factor
.Hi
then
107 Set_Sloc
(N
, Loc
+ Factor
.Adjust
);
109 end Adjust_Instantiation_Sloc
;
111 --------------------------------
112 -- Complete_Source_File_Entry --
113 --------------------------------
115 procedure Complete_Source_File_Entry
is
116 CSF
: constant Source_File_Index
:= Current_Source_File
;
118 Trim_Lines_Table
(CSF
);
119 Source_File
.Table
(CSF
).Source_Checksum
:= Checksum
;
120 end Complete_Source_File_Entry
;
122 ---------------------------------
123 -- Create_Instantiation_Source --
124 ---------------------------------
126 procedure Create_Instantiation_Source
127 (Inst_Node
: Entity_Id
;
128 Template_Id
: Entity_Id
;
129 Factor
: out Sloc_Adjustment
;
130 Inlined_Body
: Boolean := False;
131 Inherited_Pragma
: Boolean := False)
133 Dnod
: constant Node_Id
:= Declaration_Node
(Template_Id
);
134 Xold
: Source_File_Index
;
135 Xnew
: Source_File_Index
;
138 Xold
:= Get_Source_File_Index
(Sloc
(Template_Id
));
139 Factor
.Lo
:= Source_File
.Table
(Xold
).Source_First
;
140 Factor
.Hi
:= Source_File
.Table
(Xold
).Source_Last
;
142 Source_File
.Append
(Source_File
.Table
(Xold
));
143 Xnew
:= Source_File
.Last
;
146 Write_Str
("Create_Instantiation_Source: created source ");
147 Write_Int
(Int
(Xnew
));
152 Sold
: Source_File_Record
renames Source_File
.Table
(Xold
);
153 Snew
: Source_File_Record
renames Source_File
.Table
(Xnew
);
159 Snew
.Inlined_Body
:= Inlined_Body
;
160 Snew
.Inherited_Pragma
:= Inherited_Pragma
;
161 Snew
.Template
:= Xold
;
163 -- For a genuine generic instantiation, assign new instance id. For
164 -- inlined bodies or inherited pragmas, we retain that of the
165 -- template, but we save the call location.
167 if Inlined_Body
or Inherited_Pragma
then
168 Snew
.Inlined_Call
:= Sloc
(Inst_Node
);
171 -- If the spec has been instantiated already, and we are now
172 -- creating the instance source for the corresponding body now,
173 -- retrieve the instance id that was assigned to the spec, which
174 -- corresponds to the same instantiation sloc.
176 Inst_Spec
:= Instance_Spec
(Inst_Node
);
177 if Present
(Inst_Spec
) then
179 Inst_Spec_Ent
: Entity_Id
;
180 -- Instance spec entity
182 Inst_Spec_Sloc
: Source_Ptr
;
183 -- Virtual sloc of the spec instance source
185 Inst_Spec_Inst_Id
: Instance_Id
;
186 -- Instance id assigned to the instance spec
189 Inst_Spec_Ent
:= Defining_Entity
(Inst_Spec
);
191 -- For a subprogram instantiation, we want the subprogram
192 -- instance, not the wrapper package.
194 if Present
(Related_Instance
(Inst_Spec_Ent
)) then
195 Inst_Spec_Ent
:= Related_Instance
(Inst_Spec_Ent
);
198 -- The specification of the instance entity has a virtual
199 -- sloc within the instance sloc range.
201 -- ??? But the Unit_Declaration_Node has the sloc of the
202 -- instantiation, which is somewhat of an oddity.
206 (Specification
(Unit_Declaration_Node
(Inst_Spec_Ent
)));
209 (Get_Source_File_Index
(Inst_Spec_Sloc
)).Instance
;
212 (Sloc
(Inst_Node
) = Instances
.Table
(Inst_Spec_Inst_Id
));
213 Snew
.Instance
:= Inst_Spec_Inst_Id
;
217 Instances
.Append
(Sloc
(Inst_Node
));
218 Snew
.Instance
:= Instances
.Last
;
222 -- Now compute the new values of Source_First and Source_Last and
223 -- adjust the source file pointer to have the correct bounds for the
224 -- new range of values.
226 -- Source_First must be greater than the last Source_Last value and
227 -- also must be a multiple of Source_Align.
230 ((Source_File
.Table
(Xnew
- 1).Source_Last
+ Source_Align
) /
231 Source_Align
) * Source_Align
;
232 Factor
.Adjust
:= Snew
.Source_First
- Factor
.Lo
;
233 Snew
.Source_Last
:= Factor
.Hi
+ Factor
.Adjust
;
235 Set_Source_File_Index_Table
(Xnew
);
237 Snew
.Sloc_Adjust
:= Sold
.Sloc_Adjust
- Factor
.Adjust
;
239 -- Modify the Dope of the instance Source_Text to use the
240 -- above-computed bounds.
243 Dope
: constant Dope_Ptr
:=
244 new Dope_Rec
'(Snew.Source_First, Snew.Source_Last);
246 Snew.Source_Text := Sold.Source_Text;
247 Set_Dope (Snew.Source_Text'Address, Dope);
248 pragma Assert (Snew.Source_Text'First = Snew.Source_First);
249 pragma Assert (Snew.Source_Text'Last = Snew.Source_Last);
254 Write_Str ("*** Create instantiation source for ");
256 if Nkind (Dnod) in N_Proper_Body
257 and then Was_Originally_Stub (Dnod)
259 Write_Str ("subunit ");
261 elsif Ekind (Template_Id) = E_Generic_Package then
262 if Nkind (Dnod) = N_Package_Body then
263 Write_Str ("body of package ");
265 Write_Str ("spec of package ");
268 elsif Ekind (Template_Id) = E_Function then
269 Write_Str ("body of function ");
271 elsif Ekind (Template_Id) = E_Procedure then
272 Write_Str ("body of procedure ");
274 elsif Ekind (Template_Id) = E_Generic_Function then
275 Write_Str ("spec of function ");
277 elsif Ekind (Template_Id) = E_Generic_Procedure then
278 Write_Str ("spec of procedure ");
280 elsif Ekind (Template_Id) = E_Package_Body then
281 Write_Str ("body of package ");
283 else pragma Assert (Ekind (Template_Id) = E_Subprogram_Body);
284 if Nkind (Dnod) = N_Procedure_Specification then
285 Write_Str ("body of procedure ");
287 Write_Str ("body of function ");
291 Write_Name (Chars (Template_Id));
294 Write_Str (" new source index = ");
295 Write_Int (Int (Xnew));
298 Write_Str (" copying from file name = ");
299 Write_Name (File_Name (Xold));
302 Write_Str (" old source index = ");
303 Write_Int (Int (Xold));
306 Write_Str (" old lo = ");
307 Write_Int (Int (Factor.Lo));
310 Write_Str (" old hi = ");
311 Write_Int (Int (Factor.Hi));
314 Write_Str (" new lo = ");
315 Write_Int (Int (Snew.Source_First));
318 Write_Str (" new hi = ");
319 Write_Int (Int (Snew.Source_Last));
322 Write_Str (" adjustment factor = ");
323 Write_Int (Int (Factor.Adjust));
326 Write_Str (" instantiation location: ");
327 Write_Location (Sloc (Inst_Node));
331 end Create_Instantiation_Source;
333 ----------------------
334 -- Load_Config_File --
335 ----------------------
337 function Load_Config_File
338 (N : File_Name_Type) return Source_File_Index
341 return Load_File (N, Osint.Config);
342 end Load_Config_File;
344 --------------------------
345 -- Load_Definition_File --
346 --------------------------
348 function Load_Definition_File
349 (N : File_Name_Type) return Source_File_Index
352 return Load_File (N, Osint.Definition);
353 end Load_Definition_File;
361 T : Osint.File_Type) return Source_File_Index
363 Src : Source_Buffer_Ptr;
364 X : Source_File_Index;
368 Preprocessing_Needed : Boolean := False;
371 -- If already there, don't need to reload file. An exception occurs
372 -- in multiple unit per file mode. It would be nice in this case to
373 -- share the same source file for each unit, but this leads to many
374 -- difficulties with assumptions (e.g. in the body of lib), that a
375 -- unit can be found by locating its source file index. Since we do
376 -- not expect much use of this mode, it's no big deal to waste a bit
377 -- of space and time by reading and storing the source multiple times.
379 if Multiple_Unit_Index = 0 then
380 for J in 1 .. Source_File.Last loop
381 if Source_File.Table (J).File_Name = N then
387 -- Here we must build a new entry in the file table
389 -- But first, we must check if a source needs to be preprocessed,
390 -- because we may have to load and parse a definition file, and we want
391 -- to do that before we load the source, so that the buffer of the
392 -- source will be the last created, and we will be able to replace it
393 -- and modify Hi without stepping on another buffer.
395 if T = Osint.Source and then not Is_Internal_File_Name (N) then
396 Prepare_To_Preprocess
397 (Source => N, Preprocessing_Needed => Preprocessing_Needed);
400 Source_File.Increment_Last;
401 X := Source_File.Last;
404 Write_Str ("Sinput.L.Load_File: created source ");
407 Write_Str (Get_Name_String (N));
411 -- Compute starting index, respecting alignment requirement
413 if X = Source_File.First then
414 Lo := First_Source_Ptr;
416 Lo := ((Source_File.Table (X - 1).Source_Last + Source_Align) /
417 Source_Align) * Source_Align;
420 Osint.Read_Source_File (N, Lo, Hi, Src, T);
422 if Null_Source_Buffer_Ptr (Src) then
423 Source_File.Decrement_Last;
424 return No_Source_File;
429 Write_Str ("*** Build source file table entry, Index = ");
431 Write_Str (", file name = ");
434 Write_Str (" lo = ");
435 Write_Int (Int (Lo));
437 Write_Str (" hi = ");
438 Write_Int (Int (Hi));
441 Write_Str (" first 10 chars -->");
444 procedure Wchar (C : Character);
445 -- Writes character or ? for control character
451 procedure Wchar (C : Character) is
454 or else C in ASCII.DEL .. Character'Val (16#9F#)
463 for J in Lo .. Lo + 9 loop
470 Write_Str (" last 10 chars -->");
472 for J in Hi - 10 .. Hi - 1 loop
479 if Src (Hi) /= EOF then
480 Write_Str (" error: no EOF at end");
487 S : Source_File_Record renames Source_File.Table (X);
488 File_Type : Type_Of_File;
493 File_Type := Sinput.Src;
495 when Osint.Library =>
499 File_Type := Sinput.Config;
501 when Osint.Definition =>
504 when Osint.Preprocessing_Data =>
505 File_Type := Preproc;
508 S := (Debug_Source_Name => N,
510 File_Type => File_Type,
511 First_Mapped_Line => No_Line_Number,
512 Full_Debug_Name => Osint.Full_Source_Name,
513 Full_File_Name => Osint.Full_Source_Name,
514 Full_Ref_Name => Osint.Full_Source_Name,
515 Instance => No_Instance_Id,
516 Identifier_Casing => Unknown,
517 Inlined_Call => No_Location,
518 Inlined_Body => False,
519 Inherited_Pragma => False,
520 Keyword_Casing => Unknown,
521 Last_Source_Line => 1,
524 Lines_Table_Max => 1,
525 Logical_Lines_Table => null,
526 Num_SRef_Pragmas => 0,
529 Source_Checksum => 0,
533 Template => No_Source_File,
535 Time_Stamp => Osint.Current_Source_File_Stamp,
538 Alloc_Line_Tables (S, Opt.Table_Factor * Alloc.Lines_Initial);
539 S.Lines_Table (1) := Lo;
542 -- Preprocess the source if it needs to be preprocessed
544 if Preprocessing_Needed then
546 -- Temporarily set the Source_File_Index_Table entries for the
547 -- source, to avoid crash when reporting an error.
549 Set_Source_File_Index_Table (X);
551 if Opt.List_Preprocessing_Symbols then
555 Foreword : String (1 .. Foreword_Start'Length +
556 Name_Len + Foreword_End'Length);
559 Foreword (1 .. Foreword_Start'Length) := Foreword_Start;
560 Foreword (Foreword_Start'Length + 1 ..
561 Foreword_Start'Length + Name_Len) :=
562 Name_Buffer (1 .. Name_Len);
563 Foreword (Foreword'Last - Foreword_End'Length + 1 ..
564 Foreword'Last) := Foreword_End;
565 Prep.List_Symbols (Foreword);
570 T : constant Nat := Total_Errors_Detected;
571 -- Used to check if there were errors during preprocessing
573 Save_Style_Check : Boolean;
574 -- Saved state of the Style_Check flag (which needs to be
575 -- temporarily set to False during preprocessing, see below).
580 -- If this is the first time we preprocess a source, allocate
581 -- the preprocessing buffer.
583 if Prep_Buffer = null then
585 new Text_Buffer (1 .. Initial_Size_Of_Prep_Buffer);
588 -- Make sure the preprocessing buffer is empty
590 Prep_Buffer_Last := 0;
592 -- Initialize the preprocessor hooks
595 (Error_Msg => Errout.Error_Msg'Access,
596 Scan => Scn.Scanner.Scan'Access,
597 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
598 Put_Char => Put_Char_In_Prep_Buffer'Access,
599 New_EOL => New_EOL_In_Prep_Buffer'Access);
601 -- Initialize scanner and set its behavior for preprocessing,
602 -- then preprocess. Also disable style checks, since some of
603 -- them are done in the scanner (specifically, those dealing
604 -- with line length and line termination), and cannot be done
605 -- during preprocessing (because the source file index table
606 -- has not been set yet).
608 Scn.Scanner.Initialize_Scanner (X);
610 Scn.Scanner.Set_Special_Character ('#
');
611 Scn.Scanner.Set_Special_Character ('$
');
612 Scn.Scanner.Set_End_Of_Line_As_Token (True);
613 Save_Style_Check := Opt.Style_Check;
614 Opt.Style_Check := False;
616 -- The actual preprocessing step
618 Preprocess (Modified);
620 -- Reset the scanner to its standard behavior, and restore the
621 -- Style_Checks flag.
623 Scn.Scanner.Reset_Special_Characters;
624 Scn.Scanner.Set_End_Of_Line_As_Token (False);
625 Opt.Style_Check := Save_Style_Check;
627 -- If there were errors during preprocessing, record an error
628 -- at the start of the file, and do not change the source
631 if T /= Total_Errors_Detected then
633 ("file could not be successfully preprocessed", Lo);
634 return No_Source_File;
637 -- Output the result of the preprocessing, if requested and
638 -- the source has been modified by the preprocessing. Only
639 -- do that for the main unit (spec, body and subunits).
641 if Generate_Processed_File
644 ((Compiler_State = Parsing
645 and then Parsing_Main_Extended_Source)
647 (Compiler_State = Analyzing
648 and then Analysing_Subunit_Of_Main))
651 FD : File_Descriptor;
657 Add_Str_To_Name_Buffer (Prep_Suffix);
659 Delete_File (Name_Buffer (1 .. Name_Len), Status);
662 Create_New_File (Name_Buffer (1 .. Name_Len), Text);
664 Status := FD /= Invalid_FD;
670 Prep_Buffer (1)'Address,
671 Integer (Prep_Buffer_Last));
672 Status := NB = Integer (Prep_Buffer_Last);
681 ("??could not write processed file """ &
682 Name_Buffer (1 .. Name_Len) & '"',
688 -- Set the new value of Hi
690 Hi := Lo + Source_Ptr (Prep_Buffer_Last);
692 -- Create the new source buffer
695 Var_Ptr : constant Source_Buffer_Ptr_Var :=
696 new Source_Buffer (Lo .. Hi);
697 -- Allocate source buffer, allowing extra character at
701 Var_Ptr (Lo .. Hi - 1) :=
702 Prep_Buffer (1 .. Prep_Buffer_Last);
704 Src := Var_Ptr.all'Access;
707 -- Record in the table the new source buffer and the
710 Source_File.Table (X).Source_Text := Src;
711 Source_File.Table (X).Source_Last := Hi;
713 -- Reset Last_Line to 1, because the lines do not
714 -- have necessarily the same starts and lengths.
716 Source_File.Table (X).Last_Source_Line := 1;
721 Set_Source_File_Index_Table (X);
726 ----------------------------------
727 -- Load_Preprocessing_Data_File --
728 ----------------------------------
730 function Load_Preprocessing_Data_File
731 (N : File_Name_Type) return Source_File_Index
734 return Load_File (N, Osint.Preprocessing_Data);
735 end Load_Preprocessing_Data_File;
737 ----------------------
738 -- Load_Source_File --
739 ----------------------
741 function Load_Source_File
742 (N : File_Name_Type) return Source_File_Index
745 return Load_File (N, Osint.Source);
746 end Load_Source_File;
748 ----------------------------
749 -- New_EOL_In_Prep_Buffer --
750 ----------------------------
752 procedure New_EOL_In_Prep_Buffer is
754 Put_Char_In_Prep_Buffer (ASCII.LF);
755 end New_EOL_In_Prep_Buffer;
757 -----------------------------
758 -- Put_Char_In_Prep_Buffer --
759 -----------------------------
761 procedure Put_Char_In_Prep_Buffer (C : Character) is
763 -- If preprocessing buffer is not large enough, double it
765 if Prep_Buffer_Last = Prep_Buffer'Last then
767 New_Prep_Buffer : constant Text_Buffer_Ptr :=
768 new Text_Buffer (1 .. 2 * Prep_Buffer_Last);
771 New_Prep_Buffer (Prep_Buffer'Range) := Prep_Buffer.all;
773 Prep_Buffer := New_Prep_Buffer;
777 Prep_Buffer_Last := Prep_Buffer_Last + 1;
778 Prep_Buffer (Prep_Buffer_Last) := C;
779 end Put_Char_In_Prep_Buffer;
781 -------------------------
782 -- Source_File_Is_Body --
783 -------------------------
785 function Source_File_Is_Body (X : Source_File_Index) return Boolean is
789 Initialize_Scanner (No_Unit, X);
791 -- Loop to look for subprogram or package body
796 -- PRAGMA, WITH, USE (which can appear before a body)
802 -- We just want to skip any of these, do it by skipping to a
803 -- semicolon, but check for EOF, in case we have bad syntax.
806 if Token = Tok_Semicolon then
809 elsif Token = Tok_EOF then
819 Scan; -- Past PACKAGE
821 -- We have a body if and only if BODY follows
823 return Token = Tok_Body;
825 -- FUNCTION or PROCEDURE
832 -- Loop through tokens following PROCEDURE or FUNCTION
839 -- For parens, count paren level (note that paren level
840 -- can get greater than 1 if we have default parameters).
842 when Tok_Left_Paren =>
843 Pcount := Pcount + 1;
845 when Tok_Right_Paren =>
846 Pcount := Pcount - 1;
848 -- EOF means something weird, probably no body
853 -- BEGIN or IS or END definitely means body is present
861 -- Semicolon means no body present if at outside any
862 -- parens. If within parens, ignore, since it could be
863 -- a parameter separator.
865 when Tok_Semicolon =>
870 -- Skip anything else
877 -- Anything else in main scan means we don't have a body
883 end Source_File_Is_Body;
885 ----------------------------
886 -- Source_File_Is_No_Body --
887 ----------------------------
889 function Source_File_Is_No_Body (X : Source_File_Index) return Boolean is
891 Initialize_Scanner (No_Unit, X);
893 if Token /= Tok_Pragma then
899 if Token /= Tok_Identifier
900 or else Chars (Token_Node) /= Name_No_Body
905 Scan; -- past No_Body
907 if Token /= Tok_Semicolon then
911 Scan; -- past semicolon
913 return Token = Tok_EOF;
914 end Source_File_Is_No_Body;