PR c++/60417
[official-gcc.git] / gcc / ada / frontend.adb
blob5cea4dbba6a5671324794972e4706b94d0c09e95
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-2014, 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_Dbug;
34 with Fmap;
35 with Fname.UF;
36 with Inline; use Inline;
37 with Lib; use Lib;
38 with Lib.Load; use Lib.Load;
39 with Lib.Xref; use Lib.Xref;
40 with Live; use Live;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Opt; use Opt;
44 with Osint;
45 with Par;
46 with Prep;
47 with Prepcomp;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
51 with Snames; use Snames;
52 with Sprint;
53 with Scn; use Scn;
54 with Sem; use Sem;
55 with Sem_Aux;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_SCIL;
58 with Sem_Elab; use Sem_Elab;
59 with Sem_Prag; use Sem_Prag;
60 with Sem_Warn; use Sem_Warn;
61 with Sinfo; use Sinfo;
62 with Sinput; use Sinput;
63 with Sinput.L; use Sinput.L;
64 with SCIL_LL; use SCIL_LL;
65 with Targparm; use Targparm;
66 with Tbuild; use Tbuild;
67 with Types; use Types;
69 procedure Frontend is
70 Config_Pragmas : List_Id;
71 -- Gather configuration pragmas
73 begin
74 -- Carry out package initializations. These are initializations which might
75 -- logically be performed at elaboration time, were it not for the fact
76 -- that we may be doing things more than once in the big loop over files.
77 -- Like elaboration, the order in which these calls are made is in some
78 -- cases important. For example, Lib cannot be initialized before Namet,
79 -- since it uses names table entries.
81 Rtsfind.Initialize;
82 Nlists.Initialize;
83 Elists.Initialize;
84 Lib.Load.Initialize;
85 Sem_Aux.Initialize;
86 Sem_Ch8.Initialize;
87 Sem_Prag.Initialize;
88 Fname.UF.Initialize;
89 Checks.Initialize;
90 Sem_Warn.Initialize;
91 Prep.Initialize;
93 if Generate_SCIL then
94 SCIL_LL.Initialize;
95 end if;
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.
115 else
116 Prepcomp.Check_Symbols;
117 end if;
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
132 return;
133 end if;
135 -- Read and process configuration pragma files if present
137 declare
138 Save_Style_Check : constant Boolean := Opt.Style_Check;
139 -- Save style check mode so it can be restored later
141 Source_Config_File : Source_File_Index;
142 -- Source reference for -gnatec configuration file
144 Prag : Node_Id;
146 Temp_File : Boolean;
148 begin
149 -- We always analyze config files with style checks off, since we
150 -- don't want a miscellaneous gnat.adc that is around to discombobulate
151 -- intended -gnatg or -gnaty compilations. We also disconnect checking
152 -- for maximum line length.
154 Opt.Style_Check := False;
155 Style_Check := False;
157 -- Capture current suppress options, which may get modified
159 Scope_Suppress := Opt.Suppress_Options;
161 -- First deal with gnat.adc file
163 if Opt.Config_File then
164 Name_Buffer (1 .. 8) := "gnat.adc";
165 Name_Len := 8;
166 Source_gnat_adc := Load_Config_File (Name_Enter);
168 -- Case of gnat.adc file present
170 if Source_gnat_adc /= No_Source_File then
172 -- Parse the gnat.adc file for configuration pragmas
174 Initialize_Scanner (No_Unit, Source_gnat_adc);
175 Config_Pragmas := Par (Configuration_Pragmas => True);
177 -- We unconditionally add a compilation dependency for gnat.adc
178 -- so that if it changes, we force a recompilation. This is a
179 -- fairly recent (2014-03-28) change.
181 Prepcomp.Add_Dependency (Source_gnat_adc);
183 -- Case of no gnat.adc file present
185 else
186 Config_Pragmas := Empty_List;
187 end if;
189 else
190 Config_Pragmas := Empty_List;
191 end if;
193 -- Now deal with specified config pragmas files if there are any
195 if Opt.Config_File_Names /= null then
197 -- Loop through config pragmas files
199 for Index in Opt.Config_File_Names'Range loop
201 -- See if extension is .TMP/.tmp indicating a temporary config
202 -- file (which we ignore from the dependency point of view).
204 Name_Len := Config_File_Names (Index)'Length;
205 Name_Buffer (1 .. Name_Len) := Config_File_Names (Index).all;
206 Temp_File :=
207 Name_Len > 4
208 and then
209 (Name_Buffer (Name_Len - 3 .. Name_Len) = ".TMP"
210 or else
211 Name_Buffer (Name_Len - 3 .. Name_Len) = ".tmp");
213 -- Load the file, error if we did not find it
215 Source_Config_File := Load_Config_File (Name_Enter);
217 if Source_Config_File = No_Source_File then
218 Osint.Fail
219 ("cannot find configuration pragmas file "
220 & Config_File_Names (Index).all);
222 -- If we did find the file, and it is not a temporary file, then
223 -- we unconditionally add a compilation dependency for it so
224 -- that if it changes, we force a recompilation. This is a
225 -- fairly recent (2014-03-28) change.
227 elsif not Temp_File then
228 Prepcomp.Add_Dependency (Source_Config_File);
229 end if;
231 -- Parse the config pragmas file, and accumulate results
233 Initialize_Scanner (No_Unit, Source_Config_File);
234 Append_List_To
235 (Config_Pragmas, Par (Configuration_Pragmas => True));
236 end loop;
237 end if;
239 -- Now analyze all pragmas except those whose analysis must be
240 -- deferred till after the main unit is analyzed.
242 if Config_Pragmas /= Error_List
243 and then Operating_Mode /= Check_Syntax
244 then
245 Prag := First (Config_Pragmas);
246 while Present (Prag) loop
247 if not Delay_Config_Pragma_Analyze (Prag) then
248 Analyze_Pragma (Prag);
249 end if;
251 Next (Prag);
252 end loop;
253 end if;
255 -- Restore style check, but if config file turned on checks, leave on
257 Opt.Style_Check := Save_Style_Check or Style_Check;
259 -- Capture any modifications to suppress options from config pragmas
261 Opt.Suppress_Options := Scope_Suppress;
262 end;
264 -- If a target dependency info file has been read through switch -gnateT=,
265 -- add it to the dependencies.
267 if Target_Dependent_Info_Read_Name /= null then
268 declare
269 Index : Source_File_Index;
270 begin
271 Name_Len := 0;
272 Add_Str_To_Name_Buffer (Target_Dependent_Info_Read_Name.all);
273 Index := Load_Config_File (Name_Enter);
274 Prepcomp.Add_Dependency (Index);
275 end;
276 end if;
278 -- This is where we can capture the value of the compilation unit specific
279 -- restrictions that have been set by the config pragma files (or from
280 -- Targparm), for later restoration when processing e.g. subunits.
282 Save_Config_Cunit_Boolean_Restrictions;
284 -- If there was a -gnatem switch, initialize the mappings of unit names to
285 -- file names and of file names to path names from the mapping file.
287 if Mapping_File_Name /= null then
288 Fmap.Initialize (Mapping_File_Name.all);
289 end if;
291 -- Adjust Optimize_Alignment mode from debug switches if necessary
293 if Debug_Flag_Dot_SS then
294 Optimize_Alignment := 'S';
295 elsif Debug_Flag_Dot_TT then
296 Optimize_Alignment := 'T';
297 end if;
299 -- We have now processed the command line switches, and the configuration
300 -- pragma files, so this is the point at which we want to capture the
301 -- values of the configuration switches (see Opt for further details).
303 Opt.Register_Opt_Config_Switches;
305 -- Check for file which contains No_Body pragma
307 if Source_File_Is_No_Body (Source_Index (Main_Unit)) then
308 Change_Main_Unit_To_Spec;
309 end if;
311 -- Initialize the scanner. Note that we do this after the call to
312 -- Create_Standard, which uses the scanner in its processing of
313 -- floating-point bounds.
315 Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
317 -- Here we call the parser to parse the compilation unit (or units in
318 -- the check syntax mode, but in that case we won't go on to the
319 -- semantics in any case).
321 Discard_List (Par (Configuration_Pragmas => False));
322 Parsing_Main_Extended_Source := False;
324 -- The main unit is now loaded, and subunits of it can be loaded,
325 -- without reporting spurious loading circularities.
327 Set_Loading (Main_Unit, False);
329 -- Now that the main unit is installed, we can complete the analysis
330 -- of the pragmas in gnat.adc and the configuration file, that require
331 -- a context for their semantic processing.
333 if Config_Pragmas /= Error_List
334 and then Operating_Mode /= Check_Syntax
336 -- Do not attempt to process deferred configuration pragmas if the main
337 -- unit failed to load, to avoid cascaded inconsistencies that can lead
338 -- to a compiler crash.
340 and then not Fatal_Error (Main_Unit)
341 then
342 -- Pragmas that require some semantic activity, such as
343 -- Interrupt_State, cannot be processed until the main unit
344 -- is installed, because they require a compilation unit on
345 -- which to attach with_clauses, etc. So analyze them now.
347 declare
348 Prag : Node_Id;
350 begin
351 Prag := First (Config_Pragmas);
352 while Present (Prag) loop
353 if Delay_Config_Pragma_Analyze (Prag) then
354 Analyze_Pragma (Prag);
355 end if;
357 Next (Prag);
358 end loop;
359 end;
360 end if;
362 -- If we have restriction No_Exception_Propagation, and we did not have an
363 -- explicit switch turning off Warn_On_Non_Local_Exception, then turn on
364 -- this warning by default if we have encountered an exception handler.
366 if Restriction_Check_Required (No_Exception_Propagation)
367 and then not No_Warn_On_Non_Local_Exception
368 and then Exception_Handler_Encountered
369 then
370 Warn_On_Non_Local_Exception := True;
371 end if;
373 -- Now on to the semantics. Skip if in syntax only mode
375 if Operating_Mode /= Check_Syntax then
377 -- Install the configuration pragmas in the tree
379 Set_Config_Pragmas (Aux_Decls_Node (Cunit (Main_Unit)), Config_Pragmas);
381 -- Following steps are skipped if we had a fatal error during parsing
383 if not Fatal_Error (Main_Unit) then
385 -- Reset Operating_Mode to Check_Semantics for subunits. We cannot
386 -- actually generate code for subunits, so we suppress expansion.
387 -- This also corrects certain problems that occur if we try to
388 -- incorporate subunits at a lower level.
390 if Operating_Mode = Generate_Code
391 and then Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
392 then
393 Operating_Mode := Check_Semantics;
394 end if;
396 -- Analyze (and possibly expand) main unit
398 Scope_Suppress := Suppress_Options;
399 Semantics (Cunit (Main_Unit));
401 -- Cleanup processing after completing main analysis
403 -- Comment needed for ASIS mode test and GNATprove mode test???
405 if Operating_Mode = Generate_Code
406 or else (Operating_Mode = Check_Semantics
407 and then (ASIS_Mode or GNATprove_Mode))
408 then
409 Instantiate_Bodies;
410 end if;
412 if Operating_Mode = Generate_Code then
413 if Inline_Processing_Required then
414 Analyze_Inlined_Bodies;
415 end if;
417 -- Remove entities from program that do not have any
418 -- execution time references.
420 if Debug_Flag_UU then
421 Collect_Garbage_Entities;
422 end if;
424 Check_Elab_Calls;
425 end if;
427 -- List library units if requested
429 if List_Units then
430 Lib.List;
431 end if;
433 -- Output waiting warning messages
435 Lib.Xref.Process_Deferred_References;
436 Sem_Warn.Output_Non_Modified_In_Out_Warnings;
437 Sem_Warn.Output_Unreferenced_Messages;
438 Sem_Warn.Check_Unused_Withs;
439 Sem_Warn.Output_Unused_Warnings_Off_Warnings;
440 end if;
441 end if;
443 -- Qualify all entity names in inner packages, package bodies, etc.,
444 -- except when compiling for the VM back-ends, which depend on having
445 -- unqualified names in certain cases and handles the generation of
446 -- qualified names when needed.
448 if VM_Target = No_VM then
449 Exp_Dbug.Qualify_All_Entity_Names;
450 end if;
452 -- SCIL backend requirement. Check that SCIL nodes associated with
453 -- dispatching calls reference subprogram calls.
455 if Generate_SCIL then
456 pragma Debug (Sem_SCIL.Check_SCIL_Nodes (Cunit (Main_Unit)));
457 null;
458 end if;
460 -- Dump the source now. Note that we do this as soon as the analysis
461 -- of the tree is complete, because it is not just a dump in the case
462 -- of -gnatD, where it rewrites all source locations in the tree.
464 Sprint.Source_Dump;
466 -- Check again for configuration pragmas that appear in the context of
467 -- the main unit. These pragmas only affect the main unit, and the
468 -- corresponding flag is reset after each call to Semantics, but they
469 -- may affect the generated ali for the unit, and therefore the flag
470 -- must be set properly after compilation. Currently we only check for
471 -- Initialize_Scalars, but others should be checked: as well???
473 declare
474 Item : Node_Id;
476 begin
477 Item := First (Context_Items (Cunit (Main_Unit)));
478 while Present (Item) loop
479 if Nkind (Item) = N_Pragma
480 and then Pragma_Name (Item) = Name_Initialize_Scalars
481 then
482 Initialize_Scalars := True;
483 end if;
485 Next (Item);
486 end loop;
487 end;
489 -- If a mapping file has been specified by a -gnatem switch, update
490 -- it if there has been some sources that were not in the mappings.
492 if Mapping_File_Name /= null then
493 Fmap.Update_Mapping_File (Mapping_File_Name.all);
494 end if;
496 return;
497 end Frontend;