]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/frontend.adb
ada: Clean up scope depth and related code (tech debt)
[gcc.git] / gcc / ada / frontend.adb
CommitLineData
70482933
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- F R O N T E N D --
6-- --
7-- B o d y --
8-- --
cccef051 9-- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
70482933
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
70482933
RK
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 --
b5c84c3c
RD
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. --
70482933
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
70482933
RK
23-- --
24------------------------------------------------------------------------------
25
804f7040 26with System.Strings; use System.Strings;
fbf5a39b 27
104f58db 28with Atree; use Atree;
70482933
RK
29with Checks;
30with CStand;
104f58db 31with Debug; use Debug;
70482933 32with Elists;
f1668c3d 33with Exp_Ch6;
70482933 34with Exp_Dbug;
29a56f61 35with Exp_Unst;
6510f4c9 36with Fmap;
70482933 37with Fname.UF;
104f58db
BD
38with Ghost; use Ghost;
39with Inline; use Inline;
40with Lib; use Lib;
41with Lib.Load; use Lib.Load;
104f58db
BD
42with Live; use Live;
43with Namet; use Namet;
44with Nlists; use Nlists;
45with Opt; use Opt;
70482933 46with Osint;
70482933 47with Par;
4e0079cc 48with Prep;
fbf5a39b 49with Prepcomp;
104f58db
BD
50with Restrict; use Restrict;
51with Rident; use Rident;
851e9f19 52with Rtsfind;
104f58db 53with Snames; use Snames;
70482933 54with Sprint;
104f58db
BD
55with Scn; use Scn;
56with Sem; use Sem;
21d27997 57with Sem_Aux;
851e9f19 58with Sem_Ch8;
d06b3b1d 59with Sem_SCIL;
104f58db
BD
60with Sem_Elab; use Sem_Elab;
61with Sem_Prag; use Sem_Prag;
851e9f19 62with Sem_Warn;
104f58db
BD
63with Sinfo; use Sinfo;
64with Sinfo.Nodes; use Sinfo.Nodes;
65with Sinfo.Utils; use Sinfo.Utils;
66with Sinput; use Sinput;
67with Sinput.L; use Sinput.L;
851e9f19 68with SCIL_LL;
104f58db
BD
69with Tbuild; use Tbuild;
70with Types; use Types;
e60b6e23 71with VAST;
bc50ac71 72with Warnsw; use Warnsw;
70482933
RK
73
74procedure Frontend is
70482933 75begin
1f6821b4
AC
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
c3b36d48 80 -- cases important. For example, Lib cannot be initialized before Namet,
1f6821b4 81 -- since it uses names table entries.
70482933
RK
82
83 Rtsfind.Initialize;
70482933
RK
84 Nlists.Initialize;
85 Elists.Initialize;
86 Lib.Load.Initialize;
21d27997 87 Sem_Aux.Initialize;
70482933 88 Sem_Ch8.Initialize;
21d27997 89 Sem_Prag.Initialize;
70482933 90 Fname.UF.Initialize;
70482933 91 Checks.Initialize;
1b24ada5 92 Sem_Warn.Initialize;
4e0079cc 93 Prep.Initialize;
90e491a7 94 Sem_Elab.Initialize;
70482933 95
7665e4bd
AC
96 if Generate_SCIL then
97 SCIL_LL.Initialize;
98 end if;
99
70482933
RK
100 -- Create package Standard
101
102 CStand.Create_Standard;
103
fbf5a39b 104 -- Check possible symbol definitions specified by -gnateD switches
70482933 105
fbf5a39b 106 Prepcomp.Process_Command_Line_Symbol_Definitions;
70482933 107
fbf5a39b 108 -- If -gnatep= was specified, parse the preprocessing data file
70482933 109
fbf5a39b
AC
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);
70482933 114
fbf5a39b
AC
115 -- Otherwise, check if there were preprocessing symbols on the command
116 -- line and set preprocessing if there are.
70482933 117
fbf5a39b
AC
118 else
119 Prepcomp.Check_Symbols;
120 end if;
70482933 121
ea4ce0f7
VC
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.
124
125 Parsing_Main_Extended_Source := True;
126
fbf5a39b 127 -- Now that the preprocessing situation is established, we are able to
e14c931f 128 -- load the main source (this is no longer done by Lib.Load.Initialize).
70482933 129
fbf5a39b 130 Lib.Load.Load_Main_Source;
70482933 131
e1d9659d 132 -- Return immediately if the main source could not be found
804f7040 133
cd644ae2 134 if Sinput.Main_Source_File <= No_Source_File then
804f7040
VC
135 return;
136 end if;
137
fbf5a39b 138 -- Read and process configuration pragma files if present
70482933 139
fbf5a39b 140 declare
9caf55e3 141 Dot_Gnat_Adc : constant File_Name_Type := Name_Find ("./gnat.adc");
7ec25b2b 142 Gnat_Adc : constant File_Name_Type := Name_Find ("gnat.adc");
9caf55e3 143
fbf5a39b
AC
144 Save_Style_Check : constant Boolean := Opt.Style_Check;
145 -- Save style check mode so it can be restored later
70482933 146
7ec25b2b
AC
147 Config_Pragmas : List_Id := Empty_List;
148 -- Gather configuration pragmas
149
fbf5a39b
AC
150 Source_Config_File : Source_File_Index;
151 -- Source reference for -gnatec configuration file
70482933 152
fbf5a39b 153 Prag : Node_Id;
70482933 154
fbf5a39b 155 begin
9d2a2071
AC
156 -- We always analyze config files with style checks off, since we
157 -- don't want a miscellaneous gnat.adc that is around to discombobulate
158 -- intended -gnatg or -gnaty compilations. We also disconnect checking
159 -- for maximum line length.
70482933 160
fbf5a39b
AC
161 Opt.Style_Check := False;
162 Style_Check := False;
70482933 163
fbf5a39b 164 -- Capture current suppress options, which may get modified
70482933 165
fbf5a39b 166 Scope_Suppress := Opt.Suppress_Options;
70482933 167
fbf5a39b
AC
168 -- First deal with gnat.adc file
169
170 if Opt.Config_File then
9caf55e3 171 Source_gnat_adc := Load_Config_File (Gnat_Adc);
fbf5a39b 172
fc27e20e
RD
173 -- Case of gnat.adc file present
174
cd644ae2 175 if Source_gnat_adc > No_Source_File then
7f5e671b 176
fc27e20e
RD
177 -- Parse the gnat.adc file for configuration pragmas
178
fbf5a39b
AC
179 Initialize_Scanner (No_Unit, Source_gnat_adc);
180 Config_Pragmas := Par (Configuration_Pragmas => True);
fc27e20e 181
9caf55e3
BD
182 -- We add a compilation dependency for gnat.adc so that if it
183 -- changes, we force a recompilation.
fc27e20e 184
5ae24312 185 Prepcomp.Add_Dependency (Source_gnat_adc);
fbf5a39b 186 end if;
fbf5a39b 187 end if;
70482933 188
fbf5a39b 189 -- Now deal with specified config pragmas files if there are any
70482933 190
fbf5a39b 191 if Opt.Config_File_Names /= null then
fc27e20e
RD
192
193 -- Loop through config pragmas files
194
fbf5a39b 195 for Index in Opt.Config_File_Names'Range loop
9caf55e3
BD
196 declare
197 Len : constant Natural := Config_File_Names (Index)'Length;
198 Str : constant String (1 .. Len) :=
7ec25b2b
AC
199 Config_File_Names (Index).all;
200
9caf55e3 201 Config_Name : constant File_Name_Type := Name_Find (Str);
7ec25b2b
AC
202 Temp_File : constant Boolean :=
203 Len > 4
204 and then
205 (Str (Len - 3 .. Len) = ".TMP"
206 or else
207 Str (Len - 3 .. Len) = ".tmp");
9caf55e3 208 -- Extension indicating a temporary config file?
fccaf220 209
9caf55e3
BD
210 begin
211 -- Skip it if it's the default name, already loaded above.
7ec25b2b
AC
212 -- Otherwise, we get confusing warning messages about seeing
213 -- the same thing twice.
5ae24312 214
9caf55e3
BD
215 if Config_Name /= Gnat_Adc
216 and then Config_Name /= Dot_Gnat_Adc
217 then
218 -- Load the file, error if we did not find it
70482933 219
9caf55e3 220 Source_Config_File := Load_Config_File (Config_Name);
5ae24312 221
cd644ae2 222 if Source_Config_File <= No_Source_File then
9caf55e3
BD
223 Osint.Fail
224 ("cannot find configuration pragmas file "
225 & Config_File_Names (Index).all);
fc27e20e 226
9caf55e3
BD
227 -- If we did find the file, and it is not a temporary file,
228 -- then we add a compilation dependency for it so that if it
229 -- changes, we force a recompilation.
70482933 230
9caf55e3
BD
231 elsif not Temp_File then
232 Prepcomp.Add_Dependency (Source_Config_File);
233 end if;
fccaf220 234
9caf55e3
BD
235 -- Parse the config pragmas file, and accumulate results
236
237 Initialize_Scanner (No_Unit, Source_Config_File);
238 Append_List_To
239 (Config_Pragmas, Par (Configuration_Pragmas => True));
240 end if;
241 end;
fbf5a39b
AC
242 end loop;
243 end if;
70482933 244
fbf5a39b
AC
245 -- Now analyze all pragmas except those whose analysis must be
246 -- deferred till after the main unit is analyzed.
70482933 247
fbf5a39b
AC
248 if Config_Pragmas /= Error_List
249 and then Operating_Mode /= Check_Syntax
250 then
251 Prag := First (Config_Pragmas);
252 while Present (Prag) loop
253 if not Delay_Config_Pragma_Analyze (Prag) then
70482933 254 Analyze_Pragma (Prag);
fbf5a39b 255 end if;
70482933 256
fbf5a39b
AC
257 Next (Prag);
258 end loop;
259 end if;
70482933 260
a90bd866 261 -- Restore style check, but if config file turned on checks, leave on
70482933 262
fbf5a39b 263 Opt.Style_Check := Save_Style_Check or Style_Check;
70482933 264
fbf5a39b 265 -- Capture any modifications to suppress options from config pragmas
70482933 266
fbf5a39b 267 Opt.Suppress_Options := Scope_Suppress;
094cf333 268
9caf55e3
BD
269 -- If a target dependency info file has been read through switch
270 -- -gnateT=, add it to the dependencies.
271
272 if Target_Dependent_Info_Read_Name /= null then
273 declare
274 Index : Source_File_Index;
275 begin
276 Name_Len := 0;
277 Add_Str_To_Name_Buffer (Target_Dependent_Info_Read_Name.all);
278 Index := Load_Config_File (Name_Enter);
279 Prepcomp.Add_Dependency (Index);
280 end;
281 end if;
51fb9b73 282
9caf55e3
BD
283 -- This is where we can capture the value of the compilation unit
284 -- specific restrictions that have been set by the config pragma
285 -- files (or from Targparm), for later restoration when processing
286 -- e.g. subunits.
51fb9b73 287
9caf55e3 288 Save_Config_Cunit_Boolean_Restrictions;
6510f4c9 289
9caf55e3
BD
290 -- If there was a -gnatem switch, initialize the mappings of unit names
291 -- to file names and of file names to path names from the mapping file.
6510f4c9 292
9caf55e3
BD
293 if Mapping_File_Name /= null then
294 Fmap.Initialize (Mapping_File_Name.all);
295 end if;
1b24ada5 296
9caf55e3 297 -- Adjust Optimize_Alignment mode from debug switches if necessary
1b24ada5 298
9caf55e3
BD
299 if Debug_Flag_Dot_SS then
300 Optimize_Alignment := 'S';
301 elsif Debug_Flag_Dot_TT then
302 Optimize_Alignment := 'T';
303 end if;
70482933 304
9caf55e3
BD
305 -- We have now processed the command line switches, and the
306 -- configuration pragma files, so this is the point at which we want to
307 -- capture the values of the configuration switches (see Opt for further
308 -- details).
70482933 309
9cc97ad5 310 Register_Config_Switches;
804f7040 311
9caf55e3 312 -- Check for file which contains No_Body pragma
804f7040 313
9caf55e3
BD
314 if Source_File_Is_No_Body (Source_Index (Main_Unit)) then
315 Change_Main_Unit_To_Spec;
316 end if;
70482933 317
9caf55e3
BD
318 -- Initialize the scanner. Note that we do this after the call to
319 -- Create_Standard, which uses the scanner in its processing of
320 -- floating-point bounds.
70482933 321
9caf55e3 322 Initialize_Scanner (Main_Unit, Source_Index (Main_Unit));
70482933 323
9caf55e3
BD
324 -- Here we call the parser to parse the compilation unit (or units in
325 -- the check syntax mode, but in that case we won't go on to the
326 -- semantics in any case).
70482933 327
9caf55e3
BD
328 Discard_List (Par (Configuration_Pragmas => False));
329 Parsing_Main_Extended_Source := False;
70482933 330
9caf55e3
BD
331 -- The main unit is now loaded, and subunits of it can be loaded,
332 -- without reporting spurious loading circularities.
70482933 333
9caf55e3 334 Set_Loading (Main_Unit, False);
a39a553e 335
9caf55e3
BD
336 -- Now that the main unit is installed, we can complete the analysis
337 -- of the pragmas in gnat.adc and the configuration file, that require
338 -- a context for their semantic processing.
a39a553e 339
9caf55e3
BD
340 if Config_Pragmas /= Error_List
341 and then Operating_Mode /= Check_Syntax
70482933 342
9caf55e3
BD
343 -- Do not attempt to process deferred configuration pragmas if the
344 -- main unit failed to load, to avoid cascaded inconsistencies that
345 -- can lead to a compiler crash.
70482933 346
9caf55e3
BD
347 and then Fatal_Error (Main_Unit) /= Error_Detected
348 then
349 -- Pragmas that require some semantic activity, such as
350 -- Interrupt_State, cannot be processed until the main unit is
351 -- installed, because they require a compilation unit on which to
352 -- attach with_clauses, etc. So analyze them now.
353
354 declare
355 Prag : Node_Id;
356
357 begin
358 Prag := First (Config_Pragmas);
359 while Present (Prag) loop
360
361 -- Guard against the case where a configuration pragma may be
362 -- split into multiple pragmas and the original rewritten as a
363 -- null statement.
364
365 if Nkind (Prag) = N_Pragma
366 and then Delay_Config_Pragma_Analyze (Prag)
367 then
368 Analyze_Pragma (Prag);
369 end if;
370
371 Next (Prag);
372 end loop;
373 end;
374 end if;
de4ac038 375
9caf55e3
BD
376 -- If we have restriction No_Exception_Propagation, and we did not have
377 -- an explicit switch turning off Warn_On_Non_Local_Exception, then turn
378 -- on this warning by default if we have encountered an exception
379 -- handler.
de4ac038 380
9caf55e3
BD
381 if Restriction_Check_Required (No_Exception_Propagation)
382 and then not No_Warn_On_Non_Local_Exception
383 and then Exception_Handler_Encountered
384 then
385 Warn_On_Non_Local_Exception := True;
386 end if;
70482933 387
4211ccbb
AC
388 -- Disable Initialize_Scalars for runtime files to avoid circular
389 -- dependencies.
390
391 if Initialize_Scalars
392 and then Fname.Is_Predefined_File_Name (File_Name (Main_Source_File))
393 then
394 Initialize_Scalars := False;
395 Init_Or_Norm_Scalars := Normalize_Scalars;
396 end if;
397
9caf55e3 398 -- Now on to the semantics. Skip if in syntax only mode
70482933 399
9caf55e3 400 if Operating_Mode /= Check_Syntax then
1f6821b4 401
9caf55e3 402 -- Install the configuration pragmas in the tree
1f6821b4 403
9caf55e3
BD
404 Set_Config_Pragmas
405 (Aux_Decls_Node (Cunit (Main_Unit)), Config_Pragmas);
70482933 406
9caf55e3 407 -- Following steps are skipped if we had a fatal error during parsing
70482933 408
9caf55e3 409 if Fatal_Error (Main_Unit) /= Error_Detected then
fbf5a39b 410
9caf55e3
BD
411 -- Reset Operating_Mode to Check_Semantics for subunits. We cannot
412 -- actually generate code for subunits, so we suppress expansion.
413 -- This also corrects certain problems that occur if we try to
414 -- incorporate subunits at a lower level.
fbf5a39b 415
9caf55e3
BD
416 if Operating_Mode = Generate_Code
417 and then Nkind (Unit (Cunit (Main_Unit))) = N_Subunit
418 then
419 Operating_Mode := Check_Semantics;
420 end if;
fbf5a39b 421
9caf55e3 422 -- Analyze (and possibly expand) main unit
70482933 423
9caf55e3
BD
424 Scope_Suppress := Suppress_Options;
425 Semantics (Cunit (Main_Unit));
fbf5a39b 426
9caf55e3 427 -- Cleanup processing after completing main analysis
70482933 428
507fe403 429 pragma Assert (Operating_Mode in Check_Semantics | Generate_Code);
fbf5a39b 430
507fe403
EB
431 if Operating_Mode = Generate_Code or else GNATprove_Mode then
432
433 -- In GNATprove_Mode we do not perform most expansions but body
434 -- instantiation is needed.
fbf5a39b 435
9caf55e3 436 Instantiate_Bodies;
70482933 437
507fe403 438 -- Analyze inlined bodies if required
4a28b181 439
9caf55e3
BD
440 if Inline_Processing_Required then
441 Analyze_Inlined_Bodies;
442 end if;
e49de265 443
9caf55e3
BD
444 -- Remove entities from program that do not have any execution
445 -- time references.
70482933 446
9caf55e3
BD
447 if Debug_Flag_UU then
448 Collect_Garbage_Entities;
449 end if;
54740d7d 450
507fe403
EB
451 -- Check access-before-elaboration rules
452
967947ed
PMR
453 if Legacy_Elaboration_Checks then
454 Check_Elab_Calls;
455 end if;
456
90e491a7 457 -- Examine all top level scenarios collected during analysis
967947ed
PMR
458 -- and resolution. Diagnose conditional ABEs, install run-time
459 -- checks to catch conditional ABEs, and guarantee the prior
460 -- elaboration of external units.
90e491a7
PMR
461
462 Check_Elaboration_Scenarios;
70482933 463
967947ed
PMR
464 -- Examine all top level scenarios collected during analysis and
465 -- resolution in order to diagnose conditional ABEs, even in the
466 -- presence of serious errors.
90e491a7
PMR
467
468 else
469 Check_Elaboration_Scenarios;
fbf5a39b 470 end if;
70482933 471
9caf55e3 472 -- List library units if requested
f8dae9bb 473
9caf55e3
BD
474 if List_Units then
475 Lib.List;
476 end if;
f8dae9bb 477
9caf55e3 478 -- Output waiting warning messages
70482933 479
9caf55e3
BD
480 Sem_Warn.Output_Non_Modified_In_Out_Warnings;
481 Sem_Warn.Output_Unreferenced_Messages;
482 Sem_Warn.Check_Unused_Withs;
483 Sem_Warn.Output_Unused_Warnings_Off_Warnings;
7ffb58af
HK
484
485 -- Remove any ignored Ghost code as it must not appear in the
be6bb3fc 486 -- executable. This action must be performed very late because it
7ffb58af
HK
487 -- heavily alters the tree.
488
489 if Operating_Mode = Generate_Code or else GNATprove_Mode then
490 Remove_Ignored_Ghost_Code;
491 end if;
be6bb3fc
RK
492
493 -- At this stage we can unnest subprogram bodies if required
494
495 if Total_Errors_Detected = 0 then
496 Exp_Unst.Unnest_Subprograms (Cunit (Main_Unit));
497 end if;
498
fbf5a39b 499 end if;
fbf5a39b 500 end if;
9caf55e3 501 end;
70482933 502
e4d04166 503 -- Qualify all entity names in inner packages, package bodies, etc
70482933 504
d05586dc
YM
505 if not GNATprove_Mode then
506 Exp_Dbug.Qualify_All_Entity_Names;
507 end if;
70482933 508
327503f1
JM
509 -- SCIL backend requirement. Check that SCIL nodes associated with
510 -- dispatching calls reference subprogram calls.
511
512 if Generate_SCIL then
d06b3b1d 513 pragma Debug (Sem_SCIL.Check_SCIL_Nodes (Cunit (Main_Unit)));
e657b693 514 null;
327503f1
JM
515 end if;
516
e60b6e23
AC
517 -- Verify the validity of the tree
518
519 if Debug_Flag_Underscore_VV then
520 VAST.Check_Tree (Cunit (Main_Unit));
521 end if;
522
f1668c3d
JM
523 -- Validate all the subprogram calls; this work will be done by VAST; in
524 -- the meantime it is done to check extra formals and it can be disabled
525 -- using -gnatd_X (which also disables all the other assertions on extra
526 -- formals). It is invoked using pragma Debug to avoid adding any cost
527 -- when the compiler is built with assertions disabled.
528
05894d1c 529 if not Debug_Flag_Underscore_XX then
f1668c3d
JM
530 pragma Debug (Exp_Ch6.Validate_Subprogram_Calls (Cunit (Main_Unit)));
531 end if;
532
70482933
RK
533 -- Dump the source now. Note that we do this as soon as the analysis
534 -- of the tree is complete, because it is not just a dump in the case
535 -- of -gnatD, where it rewrites all source locations in the tree.
536
537 Sprint.Source_Dump;
07fc65c4 538
ed05b790
RD
539 -- Check again for configuration pragmas that appear in the context
540 -- of the main unit. These pragmas only affect the main unit, and the
82878151
AC
541 -- corresponding flag is reset after each call to Semantics, but they
542 -- may affect the generated ali for the unit, and therefore the flag
543 -- must be set properly after compilation. Currently we only check for
544 -- Initialize_Scalars, but others should be checked: as well???
545
546 declare
fb8e3581 547 Item : Node_Id;
82878151
AC
548
549 begin
550 Item := First (Context_Items (Cunit (Main_Unit)));
551 while Present (Item) loop
552 if Nkind (Item) = N_Pragma
6e759c2a 553 and then Pragma_Name (Item) = Name_Initialize_Scalars
82878151
AC
554 then
555 Initialize_Scalars := True;
556 end if;
43ccd04b 557
82878151
AC
558 Next (Item);
559 end loop;
560 end;
561
fbf5a39b 562 -- If a mapping file has been specified by a -gnatem switch, update
e14c931f 563 -- it if there has been some sources that were not in the mappings.
07fc65c4
GB
564
565 if Mapping_File_Name /= null then
566 Fmap.Update_Mapping_File (Mapping_File_Name.all);
567 end if;
70482933 568end Frontend;
This page took 5.700063 seconds and 5 git commands to generate.