* MAINTAINERS: Add a note that maintainership also includes web
[official-gcc.git] / gcc / ada / frontend.adb
blobbb28eae1192e057af7ec3deedf1ef57963d1d4e5
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-2017, 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 Exp_Unst;
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;
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;
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;
59 with Sem_SCIL;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Prag; use Sem_Prag;
62 with Sem_Warn;
63 with Sinfo; use Sinfo;
64 with Sinput; use Sinput;
65 with Sinput.L; use Sinput.L;
66 with SCIL_LL;
67 with Tbuild; use Tbuild;
68 with Types; use Types;
70 procedure Frontend is
71 begin
72 -- Carry out package initializations. These are initializations which might
73 -- logically be performed at elaboration time, were it not for the fact
74 -- that we may be doing things more than once in the big loop over files.
75 -- Like elaboration, the order in which these calls are made is in some
76 -- cases important. For example, Lib cannot be initialized before Namet,
77 -- since it uses names table entries.
79 Rtsfind.Initialize;
80 Nlists.Initialize;
81 Elists.Initialize;
82 Lib.Load.Initialize;
83 Sem_Aux.Initialize;
84 Sem_Ch8.Initialize;
85 Sem_Prag.Initialize;
86 Fname.UF.Initialize;
87 Checks.Initialize;
88 Sem_Warn.Initialize;
89 Prep.Initialize;
91 if Generate_SCIL then
92 SCIL_LL.Initialize;
93 end if;
95 -- Create package Standard
97 CStand.Create_Standard;
99 -- Check possible symbol definitions specified by -gnateD switches
101 Prepcomp.Process_Command_Line_Symbol_Definitions;
103 -- If -gnatep= was specified, parse the preprocessing data file
105 if Preprocessing_Data_File /= null then
106 Name_Len := Preprocessing_Data_File'Length;
107 Name_Buffer (1 .. Name_Len) := Preprocessing_Data_File.all;
108 Prepcomp.Parse_Preprocessing_Data_File (Name_Find);
110 -- Otherwise, check if there were preprocessing symbols on the command
111 -- line and set preprocessing if there are.
113 else
114 Prepcomp.Check_Symbols;
115 end if;
117 -- We set Parsing_Main_Extended_Source true here to cover processing of all
118 -- the configuration pragma files, as well as the main source unit itself.
120 Parsing_Main_Extended_Source := True;
122 -- Now that the preprocessing situation is established, we are able to
123 -- load the main source (this is no longer done by Lib.Load.Initialize).
125 Lib.Load.Load_Main_Source;
127 -- Return immediately if the main source could not be found
129 if Sinput.Main_Source_File <= No_Source_File then
130 return;
131 end if;
133 -- Read and process configuration pragma files if present
135 declare
136 Dot_Gnat_Adc : constant File_Name_Type := Name_Find ("./gnat.adc");
137 Gnat_Adc : constant File_Name_Type := Name_Find ("gnat.adc");
139 Save_Style_Check : constant Boolean := Opt.Style_Check;
140 -- Save style check mode so it can be restored later
142 Config_Pragmas : List_Id := Empty_List;
143 -- Gather configuration pragmas
145 Source_Config_File : Source_File_Index;
146 -- Source reference for -gnatec configuration file
148 Prag : Node_Id;
150 begin
151 -- We always analyze config files with style checks off, since we
152 -- don't want a miscellaneous gnat.adc that is around to discombobulate
153 -- intended -gnatg or -gnaty compilations. We also disconnect checking
154 -- for maximum line length.
156 Opt.Style_Check := False;
157 Style_Check := False;
159 -- Capture current suppress options, which may get modified
161 Scope_Suppress := Opt.Suppress_Options;
163 -- First deal with gnat.adc file
165 if Opt.Config_File then
166 Source_gnat_adc := Load_Config_File (Gnat_Adc);
168 -- Case of gnat.adc file present
170 if Source_gnat_adc > No_Source_File then
171 -- Parse the gnat.adc file for configuration pragmas
173 Initialize_Scanner (No_Unit, Source_gnat_adc);
174 Config_Pragmas := Par (Configuration_Pragmas => True);
176 -- We add a compilation dependency for gnat.adc so that if it
177 -- changes, we force a recompilation.
179 Prepcomp.Add_Dependency (Source_gnat_adc);
180 end if;
181 end if;
183 -- Now deal with specified config pragmas files if there are any
185 if Opt.Config_File_Names /= null then
187 -- Loop through config pragmas files
189 for Index in Opt.Config_File_Names'Range loop
190 declare
191 Len : constant Natural := Config_File_Names (Index)'Length;
192 Str : constant String (1 .. Len) :=
193 Config_File_Names (Index).all;
195 Config_Name : constant File_Name_Type := Name_Find (Str);
196 Temp_File : constant Boolean :=
197 Len > 4
198 and then
199 (Str (Len - 3 .. Len) = ".TMP"
200 or else
201 Str (Len - 3 .. Len) = ".tmp");
202 -- Extension indicating a temporary config file?
204 begin
205 -- Skip it if it's the default name, already loaded above.
206 -- Otherwise, we get confusing warning messages about seeing
207 -- the same thing twice.
209 if Config_Name /= Gnat_Adc
210 and then Config_Name /= Dot_Gnat_Adc
211 then
212 -- Load the file, error if we did not find it
214 Source_Config_File := Load_Config_File (Config_Name);
216 if Source_Config_File <= No_Source_File then
217 Osint.Fail
218 ("cannot find configuration pragmas file "
219 & Config_File_Names (Index).all);
221 -- If we did find the file, and it is not a temporary file,
222 -- then we add a compilation dependency for it so that if it
223 -- changes, we force a recompilation.
225 elsif not Temp_File then
226 Prepcomp.Add_Dependency (Source_Config_File);
227 end if;
229 -- Parse the config pragmas file, and accumulate results
231 Initialize_Scanner (No_Unit, Source_Config_File);
232 Append_List_To
233 (Config_Pragmas, Par (Configuration_Pragmas => True));
234 end if;
235 end;
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;
263 -- If a target dependency info file has been read through switch
264 -- -gnateT=, add it to the dependencies.
266 if Target_Dependent_Info_Read_Name /= null then
267 declare
268 Index : Source_File_Index;
269 begin
270 Name_Len := 0;
271 Add_Str_To_Name_Buffer (Target_Dependent_Info_Read_Name.all);
272 Index := Load_Config_File (Name_Enter);
273 Prepcomp.Add_Dependency (Index);
274 end;
275 end if;
277 -- This is where we can capture the value of the compilation unit
278 -- specific restrictions that have been set by the config pragma
279 -- files (or from Targparm), for later restoration when processing
280 -- e.g. subunits.
282 Save_Config_Cunit_Boolean_Restrictions;
284 -- If there was a -gnatem switch, initialize the mappings of unit names
285 -- to 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
300 -- configuration pragma files, so this is the point at which we want to
301 -- capture the values of the configuration switches (see Opt for further
302 -- details).
304 Opt.Register_Opt_Config_Switches;
306 -- Check for file which contains No_Body pragma
308 if Source_File_Is_No_Body (Source_Index (Main_Unit)) then
309 Change_Main_Unit_To_Spec;
310 end if;
312 -- Initialize the scanner. Note that we do this after the call to
313 -- Create_Standard, which uses the scanner in its processing of
314 -- floating-point bounds.
316 Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
318 -- Here we call the parser to parse the compilation unit (or units in
319 -- the check syntax mode, but in that case we won't go on to the
320 -- semantics in any case).
322 Discard_List (Par (Configuration_Pragmas => False));
323 Parsing_Main_Extended_Source := False;
325 -- The main unit is now loaded, and subunits of it can be loaded,
326 -- without reporting spurious loading circularities.
328 Set_Loading (Main_Unit, False);
330 -- Now that the main unit is installed, we can complete the analysis
331 -- of the pragmas in gnat.adc and the configuration file, that require
332 -- a context for their semantic processing.
334 if Config_Pragmas /= Error_List
335 and then Operating_Mode /= Check_Syntax
337 -- Do not attempt to process deferred configuration pragmas if the
338 -- main unit failed to load, to avoid cascaded inconsistencies that
339 -- can lead to a compiler crash.
341 and then Fatal_Error (Main_Unit) /= Error_Detected
342 then
343 -- Pragmas that require some semantic activity, such as
344 -- Interrupt_State, cannot be processed until the main unit is
345 -- installed, because they require a compilation unit on which to
346 -- attach with_clauses, etc. So analyze them now.
348 declare
349 Prag : Node_Id;
351 begin
352 Prag := First (Config_Pragmas);
353 while Present (Prag) loop
355 -- Guard against the case where a configuration pragma may be
356 -- split into multiple pragmas and the original rewritten as a
357 -- null statement.
359 if Nkind (Prag) = N_Pragma
360 and then Delay_Config_Pragma_Analyze (Prag)
361 then
362 Analyze_Pragma (Prag);
363 end if;
365 Next (Prag);
366 end loop;
367 end;
368 end if;
370 -- If we have restriction No_Exception_Propagation, and we did not have
371 -- an explicit switch turning off Warn_On_Non_Local_Exception, then turn
372 -- on this warning by default if we have encountered an exception
373 -- handler.
375 if Restriction_Check_Required (No_Exception_Propagation)
376 and then not No_Warn_On_Non_Local_Exception
377 and then Exception_Handler_Encountered
378 then
379 Warn_On_Non_Local_Exception := True;
380 end if;
382 -- Now on to the semantics. Skip if in syntax only mode
384 if Operating_Mode /= Check_Syntax then
386 -- Install the configuration pragmas in the tree
388 Set_Config_Pragmas
389 (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 pragma Assert
416 (Operating_Mode = Generate_Code
417 or else Operating_Mode = Check_Semantics);
419 if Operating_Mode = Generate_Code
420 or else (ASIS_Mode or GNATprove_Mode)
421 then
422 Instantiate_Bodies;
423 end if;
425 -- Analyze inlined bodies and check elaboration rules in GNATprove
426 -- mode as well as during compilation.
428 if Operating_Mode = Generate_Code or else GNATprove_Mode then
429 if Inline_Processing_Required then
430 Analyze_Inlined_Bodies;
431 end if;
433 -- Remove entities from program that do not have any execution
434 -- time references.
436 if Debug_Flag_UU then
437 Collect_Garbage_Entities;
438 end if;
440 Check_Elab_Calls;
442 -- Remove any ignored Ghost code as it must not appear in the
443 -- executable.
445 Remove_Ignored_Ghost_Code;
446 end if;
448 -- At this stage we can unnest subprogram bodies if required
450 Exp_Unst.Unnest_Subprograms (Cunit (Main_Unit));
452 -- List library units if requested
454 if List_Units then
455 Lib.List;
456 end if;
458 -- Output waiting warning messages
460 Lib.Xref.Process_Deferred_References;
461 Sem_Warn.Output_Non_Modified_In_Out_Warnings;
462 Sem_Warn.Output_Unreferenced_Messages;
463 Sem_Warn.Check_Unused_Withs;
464 Sem_Warn.Output_Unused_Warnings_Off_Warnings;
465 end if;
466 end if;
467 end;
469 -- Qualify all entity names in inner packages, package bodies, etc
471 Exp_Dbug.Qualify_All_Entity_Names;
473 -- SCIL backend requirement. Check that SCIL nodes associated with
474 -- dispatching calls reference subprogram calls.
476 if Generate_SCIL then
477 pragma Debug (Sem_SCIL.Check_SCIL_Nodes (Cunit (Main_Unit)));
478 null;
479 end if;
481 -- Dump the source now. Note that we do this as soon as the analysis
482 -- of the tree is complete, because it is not just a dump in the case
483 -- of -gnatD, where it rewrites all source locations in the tree.
485 Sprint.Source_Dump;
487 -- Check again for configuration pragmas that appear in the context
488 -- of the main unit. These pragmas only affect the main unit, and the
489 -- corresponding flag is reset after each call to Semantics, but they
490 -- may affect the generated ali for the unit, and therefore the flag
491 -- must be set properly after compilation. Currently we only check for
492 -- Initialize_Scalars, but others should be checked: as well???
494 declare
495 Item : Node_Id;
497 begin
498 Item := First (Context_Items (Cunit (Main_Unit)));
499 while Present (Item) loop
500 if Nkind (Item) = N_Pragma
501 and then Pragma_Name (Item) = Name_Initialize_Scalars
502 then
503 Initialize_Scalars := True;
504 end if;
506 Next (Item);
507 end loop;
508 end;
510 -- If a mapping file has been specified by a -gnatem switch, update
511 -- it if there has been some sources that were not in the mappings.
513 if Mapping_File_Name /= null then
514 Fmap.Update_Mapping_File (Mapping_File_Name.all);
515 end if;
517 return;
518 end Frontend;