1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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 ------------------------------------------------------------------------------
26 with System
.Strings
; use System
.Strings
;
28 with Atree
; use Atree
;
31 with Debug
; use Debug
;
37 with Ghost
; use Ghost
;
38 with Inline
; use Inline
;
40 with Lib
.Load
; use Lib
.Load
;
43 with Namet
; use Namet
;
44 with Nlists
; use Nlists
;
50 with Restrict
; use Restrict
;
51 with Rident
; use Rident
;
53 with Snames
; use Snames
;
60 with Sem_Elab
; use Sem_Elab
;
61 with Sem_Prag
; use Sem_Prag
;
63 with Sinfo
; use Sinfo
;
64 with Sinput
; use Sinput
;
65 with Sinput
.L
; use Sinput
.L
;
67 with Tbuild
; use Tbuild
;
68 with Types
; use Types
;
73 -- Carry out package initializations. These are initializations which might
74 -- logically be performed at elaboration time, were it not for the fact
75 -- that we may be doing things more than once in the big loop over files.
76 -- Like elaboration, the order in which these calls are made is in some
77 -- cases important. For example, Lib cannot be initialized before Namet,
78 -- since it uses names table entries.
97 -- Create package Standard
99 CStand
.Create_Standard
;
101 -- Check possible symbol definitions specified by -gnateD switches
103 Prepcomp
.Process_Command_Line_Symbol_Definitions
;
105 -- If -gnatep= was specified, parse the preprocessing data file
107 if Preprocessing_Data_File
/= null then
108 Name_Len
:= Preprocessing_Data_File
'Length;
109 Name_Buffer
(1 .. Name_Len
) := Preprocessing_Data_File
.all;
110 Prepcomp
.Parse_Preprocessing_Data_File
(Name_Find
);
112 -- Otherwise, check if there were preprocessing symbols on the command
113 -- line and set preprocessing if there are.
116 Prepcomp
.Check_Symbols
;
119 -- We set Parsing_Main_Extended_Source true here to cover processing of all
120 -- the configuration pragma files, as well as the main source unit itself.
122 Parsing_Main_Extended_Source
:= True;
124 -- Now that the preprocessing situation is established, we are able to
125 -- load the main source (this is no longer done by Lib.Load.Initialize).
127 Lib
.Load
.Load_Main_Source
;
129 -- Return immediately if the main source could not be found
131 if Sinput
.Main_Source_File
<= No_Source_File
then
135 -- Read and process configuration pragma files if present
138 Dot_Gnat_Adc
: constant File_Name_Type
:= Name_Find
("./gnat.adc");
139 Gnat_Adc
: constant File_Name_Type
:= Name_Find
("gnat.adc");
141 Save_Style_Check
: constant Boolean := Opt
.Style_Check
;
142 -- Save style check mode so it can be restored later
144 Config_Pragmas
: List_Id
:= Empty_List
;
145 -- Gather configuration pragmas
147 Source_Config_File
: Source_File_Index
;
148 -- Source reference for -gnatec configuration file
153 -- We always analyze config files with style checks off, since we
154 -- don't want a miscellaneous gnat.adc that is around to discombobulate
155 -- intended -gnatg or -gnaty compilations. We also disconnect checking
156 -- for maximum line length.
158 Opt
.Style_Check
:= False;
159 Style_Check
:= False;
161 -- Capture current suppress options, which may get modified
163 Scope_Suppress
:= Opt
.Suppress_Options
;
165 -- First deal with gnat.adc file
167 if Opt
.Config_File
then
168 Source_gnat_adc
:= Load_Config_File
(Gnat_Adc
);
170 -- Case of gnat.adc file present
172 if Source_gnat_adc
> No_Source_File
then
174 -- Parse the gnat.adc file for configuration pragmas
176 Initialize_Scanner
(No_Unit
, Source_gnat_adc
);
177 Config_Pragmas
:= Par
(Configuration_Pragmas
=> True);
179 -- We add a compilation dependency for gnat.adc so that if it
180 -- changes, we force a recompilation.
182 Prepcomp
.Add_Dependency
(Source_gnat_adc
);
186 -- Now deal with specified config pragmas files if there are any
188 if Opt
.Config_File_Names
/= null then
190 -- Loop through config pragmas files
192 for Index
in Opt
.Config_File_Names
'Range loop
194 Len
: constant Natural := Config_File_Names
(Index
)'Length;
195 Str
: constant String (1 .. Len
) :=
196 Config_File_Names
(Index
).all;
198 Config_Name
: constant File_Name_Type
:= Name_Find
(Str
);
199 Temp_File
: constant Boolean :=
202 (Str
(Len
- 3 .. Len
) = ".TMP"
204 Str
(Len
- 3 .. Len
) = ".tmp");
205 -- Extension indicating a temporary config file?
208 -- Skip it if it's the default name, already loaded above.
209 -- Otherwise, we get confusing warning messages about seeing
210 -- the same thing twice.
212 if Config_Name
/= Gnat_Adc
213 and then Config_Name
/= Dot_Gnat_Adc
215 -- Load the file, error if we did not find it
217 Source_Config_File
:= Load_Config_File
(Config_Name
);
219 if Source_Config_File
<= No_Source_File
then
221 ("cannot find configuration pragmas file "
222 & Config_File_Names
(Index
).all);
224 -- If we did find the file, and it is not a temporary file,
225 -- then we add a compilation dependency for it so that if it
226 -- changes, we force a recompilation.
228 elsif not Temp_File
then
229 Prepcomp
.Add_Dependency
(Source_Config_File
);
232 -- Parse the config pragmas file, and accumulate results
234 Initialize_Scanner
(No_Unit
, Source_Config_File
);
236 (Config_Pragmas
, Par
(Configuration_Pragmas
=> True));
242 -- Now analyze all pragmas except those whose analysis must be
243 -- deferred till after the main unit is analyzed.
245 if Config_Pragmas
/= Error_List
246 and then Operating_Mode
/= Check_Syntax
248 Prag
:= First
(Config_Pragmas
);
249 while Present
(Prag
) loop
250 if not Delay_Config_Pragma_Analyze
(Prag
) then
251 Analyze_Pragma
(Prag
);
258 -- Restore style check, but if config file turned on checks, leave on
260 Opt
.Style_Check
:= Save_Style_Check
or Style_Check
;
262 -- Capture any modifications to suppress options from config pragmas
264 Opt
.Suppress_Options
:= Scope_Suppress
;
266 -- If a target dependency info file has been read through switch
267 -- -gnateT=, add it to the dependencies.
269 if Target_Dependent_Info_Read_Name
/= null then
271 Index
: Source_File_Index
;
274 Add_Str_To_Name_Buffer
(Target_Dependent_Info_Read_Name
.all);
275 Index
:= Load_Config_File
(Name_Enter
);
276 Prepcomp
.Add_Dependency
(Index
);
280 -- This is where we can capture the value of the compilation unit
281 -- specific restrictions that have been set by the config pragma
282 -- files (or from Targparm), for later restoration when processing
285 Save_Config_Cunit_Boolean_Restrictions
;
287 -- If there was a -gnatem switch, initialize the mappings of unit names
288 -- to file names and of file names to path names from the mapping file.
290 if Mapping_File_Name
/= null then
291 Fmap
.Initialize
(Mapping_File_Name
.all);
294 -- Adjust Optimize_Alignment mode from debug switches if necessary
296 if Debug_Flag_Dot_SS
then
297 Optimize_Alignment
:= 'S';
298 elsif Debug_Flag_Dot_TT
then
299 Optimize_Alignment
:= 'T';
302 -- We have now processed the command line switches, and the
303 -- configuration pragma files, so this is the point at which we want to
304 -- capture the values of the configuration switches (see Opt for further
307 Register_Config_Switches
;
309 -- Check for file which contains No_Body pragma
311 if Source_File_Is_No_Body
(Source_Index
(Main_Unit
)) then
312 Change_Main_Unit_To_Spec
;
315 -- Initialize the scanner. Note that we do this after the call to
316 -- Create_Standard, which uses the scanner in its processing of
317 -- floating-point bounds.
319 Initialize_Scanner
(Main_Unit
, Source_Index
(Main_Unit
));
321 -- Here we call the parser to parse the compilation unit (or units in
322 -- the check syntax mode, but in that case we won't go on to the
323 -- semantics in any case).
325 Discard_List
(Par
(Configuration_Pragmas
=> False));
326 Parsing_Main_Extended_Source
:= False;
328 -- The main unit is now loaded, and subunits of it can be loaded,
329 -- without reporting spurious loading circularities.
331 Set_Loading
(Main_Unit
, False);
333 -- Now that the main unit is installed, we can complete the analysis
334 -- of the pragmas in gnat.adc and the configuration file, that require
335 -- a context for their semantic processing.
337 if Config_Pragmas
/= Error_List
338 and then Operating_Mode
/= Check_Syntax
340 -- Do not attempt to process deferred configuration pragmas if the
341 -- main unit failed to load, to avoid cascaded inconsistencies that
342 -- can lead to a compiler crash.
344 and then Fatal_Error
(Main_Unit
) /= Error_Detected
346 -- Pragmas that require some semantic activity, such as
347 -- Interrupt_State, cannot be processed until the main unit is
348 -- installed, because they require a compilation unit on which to
349 -- attach with_clauses, etc. So analyze them now.
355 Prag
:= First
(Config_Pragmas
);
356 while Present
(Prag
) loop
358 -- Guard against the case where a configuration pragma may be
359 -- split into multiple pragmas and the original rewritten as a
362 if Nkind
(Prag
) = N_Pragma
363 and then Delay_Config_Pragma_Analyze
(Prag
)
365 Analyze_Pragma
(Prag
);
373 -- If we have restriction No_Exception_Propagation, and we did not have
374 -- an explicit switch turning off Warn_On_Non_Local_Exception, then turn
375 -- on this warning by default if we have encountered an exception
378 if Restriction_Check_Required
(No_Exception_Propagation
)
379 and then not No_Warn_On_Non_Local_Exception
380 and then Exception_Handler_Encountered
382 Warn_On_Non_Local_Exception
:= True;
385 -- Disable Initialize_Scalars for runtime files to avoid circular
388 if Initialize_Scalars
389 and then Fname
.Is_Predefined_File_Name
(File_Name
(Main_Source_File
))
391 Initialize_Scalars
:= False;
392 Init_Or_Norm_Scalars
:= Normalize_Scalars
;
395 -- Now on to the semantics. Skip if in syntax only mode
397 if Operating_Mode
/= Check_Syntax
then
399 -- Install the configuration pragmas in the tree
402 (Aux_Decls_Node
(Cunit
(Main_Unit
)), Config_Pragmas
);
404 -- Following steps are skipped if we had a fatal error during parsing
406 if Fatal_Error
(Main_Unit
) /= Error_Detected
then
408 -- Reset Operating_Mode to Check_Semantics for subunits. We cannot
409 -- actually generate code for subunits, so we suppress expansion.
410 -- This also corrects certain problems that occur if we try to
411 -- incorporate subunits at a lower level.
413 if Operating_Mode
= Generate_Code
414 and then Nkind
(Unit
(Cunit
(Main_Unit
))) = N_Subunit
416 Operating_Mode
:= Check_Semantics
;
419 -- Analyze (and possibly expand) main unit
421 Scope_Suppress
:= Suppress_Options
;
422 Semantics
(Cunit
(Main_Unit
));
424 -- Cleanup processing after completing main analysis
426 -- In GNATprove_Mode we do not perform most expansions but body
427 -- instantiation is needed.
430 (Operating_Mode
= Generate_Code
431 or else Operating_Mode
= Check_Semantics
);
433 if Operating_Mode
= Generate_Code
434 or else GNATprove_Mode
439 -- Analyze all inlined bodies, check access-before-elaboration
440 -- rules, and remove ignored Ghost code when generating code or
441 -- compiling for GNATprove.
443 if Operating_Mode
= Generate_Code
or else GNATprove_Mode
then
444 if Inline_Processing_Required
then
445 Analyze_Inlined_Bodies
;
448 -- Remove entities from program that do not have any execution
451 if Debug_Flag_UU
then
452 Collect_Garbage_Entities
;
455 if Legacy_Elaboration_Checks
then
459 -- Examine all top level scenarios collected during analysis
460 -- and resolution. Diagnose conditional ABEs, install run-time
461 -- checks to catch conditional ABEs, and guarantee the prior
462 -- elaboration of external units.
464 Check_Elaboration_Scenarios
;
466 -- Examine all top level scenarios collected during analysis and
467 -- resolution in order to diagnose conditional ABEs, even in the
468 -- presence of serious errors.
471 Check_Elaboration_Scenarios
;
474 -- At this stage we can unnest subprogram bodies if required
476 if Total_Errors_Detected
= 0 then
477 Exp_Unst
.Unnest_Subprograms
(Cunit
(Main_Unit
));
480 -- List library units if requested
486 -- Output waiting warning messages
488 Lib
.Xref
.Process_Deferred_References
;
489 Sem_Warn
.Output_Non_Modified_In_Out_Warnings
;
490 Sem_Warn
.Output_Unreferenced_Messages
;
491 Sem_Warn
.Check_Unused_Withs
;
492 Sem_Warn
.Output_Unused_Warnings_Off_Warnings
;
494 -- Remove any ignored Ghost code as it must not appear in the
495 -- executable. This action must be performed last because it
496 -- heavily alters the tree.
498 if Operating_Mode
= Generate_Code
or else GNATprove_Mode
then
499 Remove_Ignored_Ghost_Code
;
505 -- Qualify all entity names in inner packages, package bodies, etc
507 if not GNATprove_Mode
then
508 Exp_Dbug
.Qualify_All_Entity_Names
;
511 -- SCIL backend requirement. Check that SCIL nodes associated with
512 -- dispatching calls reference subprogram calls.
514 if Generate_SCIL
then
515 pragma Debug
(Sem_SCIL
.Check_SCIL_Nodes
(Cunit
(Main_Unit
)));
519 -- Verify the validity of the tree
521 if Debug_Flag_Underscore_VV
then
522 VAST
.Check_Tree
(Cunit
(Main_Unit
));
525 -- Dump the source now. Note that we do this as soon as the analysis
526 -- of the tree is complete, because it is not just a dump in the case
527 -- of -gnatD, where it rewrites all source locations in the tree.
531 -- Check again for configuration pragmas that appear in the context
532 -- of the main unit. These pragmas only affect the main unit, and the
533 -- corresponding flag is reset after each call to Semantics, but they
534 -- may affect the generated ali for the unit, and therefore the flag
535 -- must be set properly after compilation. Currently we only check for
536 -- Initialize_Scalars, but others should be checked: as well???
542 Item
:= First
(Context_Items
(Cunit
(Main_Unit
)));
543 while Present
(Item
) loop
544 if Nkind
(Item
) = N_Pragma
545 and then Pragma_Name
(Item
) = Name_Initialize_Scalars
547 Initialize_Scalars
:= True;
554 -- If a mapping file has been specified by a -gnatem switch, update
555 -- it if there has been some sources that were not in the mappings.
557 if Mapping_File_Name
/= null then
558 Fmap
.Update_Mapping_File
(Mapping_File_Name
.all);