PR fortran/64022
[official-gcc.git] / gcc / ada / frontend.adb
blobb3c85f1f8bce002fbfa17e86f71906995da2cfc7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F R O N T E N D --
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. 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with System.Strings; use System.Strings;
28 with Atree; use Atree;
29 with Checks;
30 with CStand;
31 with Debug; use Debug;
32 with Elists;
33 with Exp_Ch6;
34 with Exp_Dbug;
35 with Fmap;
36 with Fname.UF;
37 with Ghost; use Ghost;
38 with Inline; use Inline;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Live; use Live;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Opt; use Opt;
46 with Osint;
47 with Par;
48 with Prep;
49 with Prepcomp;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Snames; use Snames;
54 with Sprint;
55 with Scn; use Scn;
56 with Sem; use Sem;
57 with Sem_Aux;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_SCIL;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Warn; use Sem_Warn;
63 with Sinfo; use Sinfo;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with SCIL_LL; use SCIL_LL;
67 with Targparm; use Targparm;
68 with Tbuild; use Tbuild;
69 with Types; use Types;
71 procedure Frontend is
72 Config_Pragmas : List_Id;
73 -- Gather configuration pragmas
75 begin
76 -- Carry out package initializations. These are initializations which might
77 -- logically be performed at elaboration time, were it not for the fact
78 -- that we may be doing things more than once in the big loop over files.
79 -- Like elaboration, the order in which these calls are made is in some
80 -- cases important. For example, Lib cannot be initialized before Namet,
81 -- since it uses names table entries.
83 Rtsfind.Initialize;
84 Nlists.Initialize;
85 Elists.Initialize;
86 Lib.Load.Initialize;
87 Sem_Aux.Initialize;
88 Sem_Ch8.Initialize;
89 Sem_Prag.Initialize;
90 Fname.UF.Initialize;
91 Checks.Initialize;
92 Sem_Warn.Initialize;
93 Prep.Initialize;
94 Exp_Ch6.Initialize;
96 if Generate_SCIL then
97 SCIL_LL.Initialize;
98 end if;
100 -- Create package Standard
102 CStand.Create_Standard;
104 -- Check possible symbol definitions specified by -gnateD switches
106 Prepcomp.Process_Command_Line_Symbol_Definitions;
108 -- If -gnatep= was specified, parse the preprocessing data file
110 if Preprocessing_Data_File /= null then
111 Name_Len := Preprocessing_Data_File'Length;
112 Name_Buffer (1 .. Name_Len) := Preprocessing_Data_File.all;
113 Prepcomp.Parse_Preprocessing_Data_File (Name_Find);
115 -- Otherwise, check if there were preprocessing symbols on the command
116 -- line and set preprocessing if there are.
118 else
119 Prepcomp.Check_Symbols;
120 end if;
122 -- We set Parsing_Main_Extended_Source true here to cover processing of all
123 -- the configuration pragma files, as well as the main source unit itself.
125 Parsing_Main_Extended_Source := True;
127 -- Now that the preprocessing situation is established, we are able to
128 -- load the main source (this is no longer done by Lib.Load.Initialize).
130 Lib.Load.Load_Main_Source;
132 -- Return immediately if the main source could not be found
134 if Sinput.Main_Source_File = No_Source_File then
135 return;
136 end if;
138 -- Read and process configuration pragma files if present
140 declare
141 Save_Style_Check : constant Boolean := Opt.Style_Check;
142 -- Save style check mode so it can be restored later
144 Source_Config_File : Source_File_Index;
145 -- Source reference for -gnatec configuration file
147 Prag : Node_Id;
149 Temp_File : Boolean;
151 begin
152 -- We always analyze config files with style checks off, since we
153 -- don't want a miscellaneous gnat.adc that is around to discombobulate
154 -- intended -gnatg or -gnaty compilations. We also disconnect checking
155 -- for maximum line length.
157 Opt.Style_Check := False;
158 Style_Check := False;
160 -- Capture current suppress options, which may get modified
162 Scope_Suppress := Opt.Suppress_Options;
164 -- First deal with gnat.adc file
166 if Opt.Config_File then
167 Name_Buffer (1 .. 8) := "gnat.adc";
168 Name_Len := 8;
169 Source_gnat_adc := Load_Config_File (Name_Enter);
171 -- Case of gnat.adc file present
173 if Source_gnat_adc /= No_Source_File then
175 -- Parse the gnat.adc file for configuration pragmas
177 Initialize_Scanner (No_Unit, Source_gnat_adc);
178 Config_Pragmas := Par (Configuration_Pragmas => True);
180 -- We unconditionally add a compilation dependency for gnat.adc
181 -- so that if it changes, we force a recompilation. This is a
182 -- fairly recent (2014-03-28) change.
184 Prepcomp.Add_Dependency (Source_gnat_adc);
186 -- Case of no gnat.adc file present
188 else
189 Config_Pragmas := Empty_List;
190 end if;
192 else
193 Config_Pragmas := Empty_List;
194 end if;
196 -- Now deal with specified config pragmas files if there are any
198 if Opt.Config_File_Names /= null then
200 -- Loop through config pragmas files
202 for Index in Opt.Config_File_Names'Range loop
204 -- See if extension is .TMP/.tmp indicating a temporary config
205 -- file (which we ignore from the dependency point of view).
207 Name_Len := Config_File_Names (Index)'Length;
208 Name_Buffer (1 .. Name_Len) := Config_File_Names (Index).all;
209 Temp_File :=
210 Name_Len > 4
211 and then
212 (Name_Buffer (Name_Len - 3 .. Name_Len) = ".TMP"
213 or else
214 Name_Buffer (Name_Len - 3 .. Name_Len) = ".tmp");
216 -- Load the file, error if we did not find it
218 Source_Config_File := Load_Config_File (Name_Enter);
220 if Source_Config_File = No_Source_File then
221 Osint.Fail
222 ("cannot find configuration pragmas file "
223 & Config_File_Names (Index).all);
225 -- If we did find the file, and it is not a temporary file, then
226 -- we unconditionally add a compilation dependency for it so
227 -- that if it changes, we force a recompilation. This is a
228 -- fairly recent (2014-03-28) change.
230 elsif not Temp_File then
231 Prepcomp.Add_Dependency (Source_Config_File);
232 end if;
234 -- Parse the config pragmas file, and accumulate results
236 Initialize_Scanner (No_Unit, Source_Config_File);
237 Append_List_To
238 (Config_Pragmas, Par (Configuration_Pragmas => True));
239 end loop;
240 end if;
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
247 then
248 Prag := First (Config_Pragmas);
249 while Present (Prag) loop
250 if not Delay_Config_Pragma_Analyze (Prag) then
251 Analyze_Pragma (Prag);
252 end if;
254 Next (Prag);
255 end loop;
256 end if;
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;
265 end;
267 -- If a target dependency info file has been read through switch -gnateT=,
268 -- add it to the dependencies.
270 if Target_Dependent_Info_Read_Name /= null then
271 declare
272 Index : Source_File_Index;
273 begin
274 Name_Len := 0;
275 Add_Str_To_Name_Buffer (Target_Dependent_Info_Read_Name.all);
276 Index := Load_Config_File (Name_Enter);
277 Prepcomp.Add_Dependency (Index);
278 end;
279 end if;
281 -- This is where we can capture the value of the compilation unit specific
282 -- restrictions that have been set by the config pragma files (or from
283 -- Targparm), for later restoration when processing e.g. subunits.
285 Save_Config_Cunit_Boolean_Restrictions;
287 -- If there was a -gnatem switch, initialize the mappings of unit names to
288 -- 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);
292 end if;
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';
300 end if;
302 -- We have now processed the command line switches, and the configuration
303 -- pragma files, so this is the point at which we want to capture the
304 -- values of the configuration switches (see Opt for further details).
306 Opt.Register_Opt_Config_Switches;
308 -- Check for file which contains No_Body pragma
310 if Source_File_Is_No_Body (Source_Index (Main_Unit)) then
311 Change_Main_Unit_To_Spec;
312 end if;
314 -- Initialize the scanner. Note that we do this after the call to
315 -- Create_Standard, which uses the scanner in its processing of
316 -- floating-point bounds.
318 Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
320 -- Here we call the parser to parse the compilation unit (or units in
321 -- the check syntax mode, but in that case we won't go on to the
322 -- semantics in any case).
324 Discard_List (Par (Configuration_Pragmas => False));
325 Parsing_Main_Extended_Source := False;
327 -- The main unit is now loaded, and subunits of it can be loaded,
328 -- without reporting spurious loading circularities.
330 Set_Loading (Main_Unit, False);
332 -- Now that the main unit is installed, we can complete the analysis
333 -- of the pragmas in gnat.adc and the configuration file, that require
334 -- a context for their semantic processing.
336 if Config_Pragmas /= Error_List
337 and then Operating_Mode /= Check_Syntax
339 -- Do not attempt to process deferred configuration pragmas if the main
340 -- unit failed to load, to avoid cascaded inconsistencies that can lead
341 -- to a compiler crash.
343 and then Fatal_Error (Main_Unit) /= Error_Detected
344 then
345 -- Pragmas that require some semantic activity, such as Interrupt_State,
346 -- cannot be processed until the main unit is installed, because they
347 -- require a compilation unit on which to attach with_clauses, etc. So
348 -- analyze them now.
350 declare
351 Prag : Node_Id;
353 begin
354 Prag := First (Config_Pragmas);
355 while Present (Prag) loop
357 -- Guard against the case where a configuration pragma may be
358 -- split into multiple pragmas and the original rewritten as a
359 -- null statement.
361 if Nkind (Prag) = N_Pragma
362 and then Delay_Config_Pragma_Analyze (Prag)
363 then
364 Analyze_Pragma (Prag);
365 end if;
367 Next (Prag);
368 end loop;
369 end;
370 end if;
372 -- If we have restriction No_Exception_Propagation, and we did not have an
373 -- explicit switch turning off Warn_On_Non_Local_Exception, then turn on
374 -- this warning by default if we have encountered an exception handler.
376 if Restriction_Check_Required (No_Exception_Propagation)
377 and then not No_Warn_On_Non_Local_Exception
378 and then Exception_Handler_Encountered
379 then
380 Warn_On_Non_Local_Exception := True;
381 end if;
383 -- Now on to the semantics. Skip if in syntax only mode
385 if Operating_Mode /= Check_Syntax then
387 -- Install the configuration pragmas in the tree
389 Set_Config_Pragmas (Aux_Decls_Node (Cunit (Main_Unit)), Config_Pragmas);
391 -- Following steps are skipped if we had a fatal error during parsing
393 if Fatal_Error (Main_Unit) /= Error_Detected then
395 -- Reset Operating_Mode to Check_Semantics for subunits. We cannot
396 -- actually generate code for subunits, so we suppress expansion.
397 -- This also corrects certain problems that occur if we try to
398 -- incorporate subunits at a lower level.
400 if Operating_Mode = Generate_Code
401 and then Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
402 then
403 Operating_Mode := Check_Semantics;
404 end if;
406 -- Analyze (and possibly expand) main unit
408 Scope_Suppress := Suppress_Options;
409 Semantics (Cunit (Main_Unit));
411 -- Cleanup processing after completing main analysis
413 -- Comment needed for ASIS mode test and GNATprove mode test???
415 if Operating_Mode = Generate_Code
416 or else (Operating_Mode = Check_Semantics
417 and then (ASIS_Mode or GNATprove_Mode))
418 then
419 Instantiate_Bodies;
420 end if;
422 if Operating_Mode = Generate_Code then
423 if Inline_Processing_Required then
424 Analyze_Inlined_Bodies;
425 end if;
427 -- Remove entities from program that do not have any execution
428 -- time references.
430 if Debug_Flag_UU then
431 Collect_Garbage_Entities;
432 end if;
434 Check_Elab_Calls;
436 -- Remove any ignored Ghost code as it must not appear in the
437 -- executable.
439 Remove_Ignored_Ghost_Code;
440 end if;
442 -- At this stage we can unnest subprogram bodies if required
444 Exp_Ch6.Unnest_Subprograms;
446 -- List library units if requested
448 if List_Units then
449 Lib.List;
450 end if;
452 -- Output waiting warning messages
454 Lib.Xref.Process_Deferred_References;
455 Sem_Warn.Output_Non_Modified_In_Out_Warnings;
456 Sem_Warn.Output_Unreferenced_Messages;
457 Sem_Warn.Check_Unused_Withs;
458 Sem_Warn.Output_Unused_Warnings_Off_Warnings;
459 end if;
460 end if;
462 -- Qualify all entity names in inner packages, package bodies, etc.,
463 -- except when compiling for the VM back-ends, which depend on having
464 -- unqualified names in certain cases and handles the generation of
465 -- qualified names when needed.
467 if VM_Target = No_VM then
468 Exp_Dbug.Qualify_All_Entity_Names;
469 end if;
471 -- SCIL backend requirement. Check that SCIL nodes associated with
472 -- dispatching calls reference subprogram calls.
474 if Generate_SCIL then
475 pragma Debug (Sem_SCIL.Check_SCIL_Nodes (Cunit (Main_Unit)));
476 null;
477 end if;
479 -- Dump the source now. Note that we do this as soon as the analysis
480 -- of the tree is complete, because it is not just a dump in the case
481 -- of -gnatD, where it rewrites all source locations in the tree.
483 Sprint.Source_Dump;
485 -- Check again for configuration pragmas that appear in the context
486 -- of the main unit. These pragmas only affect the main unit, and the
487 -- corresponding flag is reset after each call to Semantics, but they
488 -- may affect the generated ali for the unit, and therefore the flag
489 -- must be set properly after compilation. Currently we only check for
490 -- Initialize_Scalars, but others should be checked: as well???
492 declare
493 Item : Node_Id;
495 begin
496 Item := First (Context_Items (Cunit (Main_Unit)));
497 while Present (Item) loop
498 if Nkind (Item) = N_Pragma
499 and then Pragma_Name (Item) = Name_Initialize_Scalars
500 then
501 Initialize_Scalars := True;
502 end if;
504 Next (Item);
505 end loop;
506 end;
508 -- If a mapping file has been specified by a -gnatem switch, update
509 -- it if there has been some sources that were not in the mappings.
511 if Mapping_File_Name /= null then
512 Fmap.Update_Mapping_File (Mapping_File_Name.all);
513 end if;
515 return;
516 end Frontend;